Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYON: add plugin support #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

drewrisinger
Copy link

@drewrisinger drewrisinger commented Sep 15, 2021

Overview

This PR adds plugin support for PYON, allowing PYON to be extended with more complex data types
at the user's discretion.

Use Cases:

  • Adding complex data types that would otherwise need to be reduced to tuples/dicts, for storage in e.g. ARTIQ results or sent across networks. This is my primary use case, I'm particularly keen to use DataClasses for readability.
  • Adding shortcut values for readability, e.g. a simple plugin could decode "pi" -> np.pi.

Example Plugin:

I included an example plugin in test_pyon_plugin.py. The short is that you need to add
a new encode & decode function for each type that you want to add. They look like roughly:

@sipyco.hookimpl
def sipyco_pyon_encode(value, pretty=False, indent_level=0) -> str:
    if isinstance(value, CustomType):
        return pyon_encoding(value)

@sipyco.hookimpl
def sipyco_pyon_decoders() -> "Sequence[Tuple[str, Any]]":
    return [("CustomType", CustomType)]

Notes

This is not the cleanest implementation. I was aiming for minimal diff with the existing PYON infrastructure,
hence leaving in _Encoder class.
If there is a reason to extend other sipyco functionality, the pluggy infrastructure could handle that equally well.

I included an example & test for this, and all existing PYON tests continue to pass.

Forces recursive calls of pyon.encode(), which is compatible with the plugin architecture.

Previously, values similar to {'entry': CustomType()} would not encode

because _Encoder didn't recognze CustomType
Comment on lines +35 to +38
pm = pluggy.PluginManager("sipyco")
pm.add_hookspecs(sipyco.hookspecs)
pm.register(pyon)
pm.load_setuptools_entrypoints("sipyco")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably change this to pm = plugin.get_plugin_manager(), then just add the register() at the end?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant