class Dummy(Model): _private = Int() computed = Int().tag(store=False) id = Int() enabled = Bool() string = Bool() list_of_int = List(int) list_of_str = List(str) list_of_any = List() list_of_tuple = List(Tuple()) list_of_tuple_of_float = List(Tuple(float)) tuple_of_any = Tuple() tuple_of_number = Tuple((float, int)) tuple_of_int_or_model = Tuple((int, Model)) tuple_of_forwarded = Tuple(ForwardTyped(lambda: NotYetDefined)) set_of_any = Tuple() set_of_number = Set(float) set_of_model = Set(AbstractModel) dict_of_any = Dict() dict_of_str_any = Dict(str) dict_of_str_int = Dict(str, int) typed_int = Typed(int) typed_dict = Typed(dict) instance_of_model = Instance(AbstractModel) forwarded_instance = ForwardInstance(lambda: NotYetDefined) coerced_int = Coerced(int) prop = Property(lambda self: True) tagged_prop = Property(lambda self: 0).tag(store=True)
class IOPlugin(HasPreferencesPlugin): """Plugin responsible for handling IO This plugin is in particular in charge of loading experimental data to be visualized. """ #: Set of extensions for which we have a registered loader. supported_extensions = Set(str) #: Preferred loader for a given extension. preferred_loader = Dict(str, str).tag(pref=True) #: Custom association between loaders and file extensions. custom_loader_extensions = Dict(str, list, default={ "csv": [".dat"] }).tag(pref=True) #: Collect all contributed Loader extensions. loaders = Typed(ExtensionsCollector) # FIXME make private def start(self) -> None: """Start the plugin life-cycle. This method is called by the framework at the appropriate time. It should never be called by user code. """ core = self.workbench.get_plugin("enaml.workbench.core") core.invoke_command("glaze.errors.enter_error_gathering") validator = make_extension_validator(Loader, ("get_cls", "get_config_view"), ("file_extensions", )) self.loaders = ExtensionsCollector( workbench=self.workbench, point=LOADER_POINT, ext_class=Loader, validate_ext=validator, ) self.loaders.start() # Ensure the list of supported extensions is up to date self._update_supported_extensions(None) self.observe("custom_loader_extensions", self._update_supported_extensions) self.loaders.observe("contributions", self._update_supported_extensions) core.invoke_command("glaze.errors.exit_error_gathering") def stop(self) -> None: """Stop the plugin life-cycle. This method is called by the framework at the appropriate time. It should never be called by user code. """ self.loaders.stop() del self.loaders def list_matching_loaders(self, filename) -> List[str]: """List loaders compatible with this file. The analysis is solely based on file extension. """ _, ext = os.path.splitext(filename) matching_loaders = [] for id, loader in self.loaders.contributions.items(): if ext in loader.file_extensions: matching_loaders.append(id) for id, exts in self.custom_loader_extensions.items(): if ext in exts: matching_loaders.append(id) return matching_loaders, self.preferred_loader.get(ext) def create_loader(self, id: str, path: str) -> BaseLoader: """Create a loader associated with a path Parameters ---------- id : str Id of the loader to create. path : str Path to the data file from which to load data. Returns ------- BaseLoader BaseLoader subclass that can be used to access the file content. Raises ------ KeyError: Raised if an unknown loader is requested. """ if id not in self.loaders.contributions: # FIXME raise KeyError() # Get the loader declaration decl = self.loaders.contributions[id] def mask_data( to_filter: Dataset, filter_base: Dataset, specifications: Mapping[str, MaskSpecification], ) -> Dataset: # FIXME should we be invoking a command here ? mask = self.workbench.get_plugin("oculy.transformers").create_mask( filter_base, specifications) return to_filter.where(mask) loader = decl.get_cls()(path=path, mask_data=mask_data, **self._loader_preferences.get(id, {})) return loader def create_loader_config(self, id, loader): """Create a loader config view.""" return self.loaders.contributions[id].get_config_view(loader) # --- Private API --------------------------------------------------------- def _update_supported_extensions(self, change): """Update the list of supported extensions.""" exts = set() for loader in self.loaders.contributions.values(): exts |= set(loader.file_extensions) for e in self.custom_loader_extensions.values(): exts |= set(e) self.supported_extensions = exts #: Store user preferences for loaders. _loader_preferences = Dict().tag(pref=True)
class SetAtom(Atom): untyped = Set() typed = Set(Int()) untyped_default = Set(default={1}) typed_default = Set(Int(), default={1})
(Coerced(int, coercer=lambda x: int(str(x), 2)), ['101'], [5], []), (Coerced( (int, float), coercer=lambda x: int(str(x), 2)), ['101'], [5], []), (Coerced(int, coercer=lambda x: []), [], [], ['']), (Tuple(), [(1, )], [(1, )], [[1]]), (Tuple(Int()), [(1, )], [(1, )], [(1.0, )]), (Tuple(int), [(1, )], [(1, )], [(1.0, )]), (List(), [[1]], [[1]], [(1, )]), (List(Int()), [[1]], [[1]], [[1.0]]), (List(float), [[1.0]], [[1.0]], [[1]]), (List((int, float)), [[1, 1.0]], [[1, 1.0]], [['']]), (ContainerList(), [[1]], [[1]], [(1, )]), (ContainerList(Int()), [[1]], [[1]], [[1.0]]), (ContainerList(float), [[1.0]], [[1.0]], [[1]]), (ContainerList((int, float)), [[1, 1.0]], [[1, 1.0]], [['']]), (Set(), [{1}], [{1}], [()]), (Set(Int()), [{1}], [{1}], [{''}]), (Set(item=Int()), [{1}], [{1}], [{''}]), (Set(int), [{1}], [{1}], [{''}]), (Dict(), [{ 1: 2 }], [{ 1: 2 }], [()]), (Dict(Int()), [{ 1: 2 }], [{ 1: 2 }], [{ '': 2 }]),
"post_getattr_mode", "post_setattr_mode", "post_validate_mode", ): assert getattr(cv, attr) == getattr(CloneTest.v, attr) assert cv.static_observers() == CloneTest.v.static_observers() @pytest.mark.parametrize( "untyped, typed", [ (List(), List(int)), (Tuple(), Tuple(int)), (Dict(), Dict(int, int)), (Set(), Set(int)), ], ) def test_cloning_containers_member(untyped, typed): """Check that cloning a list does clone the validation item is present.""" if not isinstance(untyped, Dict): assert untyped.clone().item is None typed.set_index(5) cl2 = typed.clone() assert cl2.index == typed.index validators = [typed.item] if hasattr(typed, "item") else typed.validate_mode[1] c_validators = [cl2.item] if hasattr(typed, "item") else cl2.validate_mode[1] for v, cv in zip(validators, c_validators):
class SetTest(Atom): no_default = Set() default = Set(default={"a"})
class SetTest(Atom): no_default = Set() default = Set(default={'a'})
CloneTest.v.set_post_validate_mode(PostValidate.ObjectMethod_NameOldNew, 'c') cv = CloneTest.v.clone() for attr in ('name', 'index', 'metadata', 'getattr_mode', 'setattr_mode', 'delattr_mode', 'default_value_mode', 'validate_mode', 'post_getattr_mode', 'post_setattr_mode', 'post_validate_mode'): assert getattr(cv, attr) == getattr(CloneTest.v, attr) assert cv.static_observers() == CloneTest.v.static_observers() @pytest.mark.parametrize('untyped, typed', [(List(), List(int)), (Tuple(), Tuple(int)), (Dict(), Dict(int, int)), (Set(), Set(int))]) def test_cloning_containers_member(untyped, typed): """Check that cloning a list does clone the validation item is present. """ if not isinstance(untyped, Dict): assert untyped.clone().item is None typed.set_index(5) cl2 = typed.clone() assert cl2.index == typed.index validators = ([typed.item] if hasattr(typed, 'item') else typed.validate_mode[1]) c_validators = ([cl2.item] if hasattr(typed, 'item') else cl2.validate_mode[1]) for v, cv in zip(validators, c_validators):
(Tuple(int), [(1, )], [(1, )], [(1.0, ), (None, )]), (Tuple(TSet[int]), [({1}, )], [({1}, )], [(1.0, ), (None, )]), (Tuple(Optional[int]), [(1, None)], [(1, None)], [("", )]), (List(), [[1]], [[1]], [(1, )]), (List(Int()), [[1]], [[1]], [[1.0]]), (List(float), [[1.0]], [[1.0]], [[1], [None]]), (List((int, float)), [[1, 1.0]], [[1, 1.0]], [[""]]), (List(TSet[int]), [[{1}]], [[{1}]], [[1], [None]]), (List(Optional[int]), [[1, None]], [[1, None]], [[""]]), (ContainerList(), [[1]], [[1]], [(1, )]), (ContainerList(Int()), [[1]], [[1]], [[1.0]]), (ContainerList(float), [[1.0]], [[1.0]], [[1], [None]]), (ContainerList((int, float)), [[1, 1.0]], [[1, 1.0]], [[""]]), (ContainerList(TSet[int]), [[{1}]], [[{1}]], [[1], [None]]), (ContainerList(Optional[int]), [[1, None]], [[1, None]], [[""]]), (Set(), [{1}], [{1}], [()]), (Set(Int()), [{1}], [{1}], [{""}]), (Set(item=Int()), [{1}], [{1}], [{""}]), (Set(int), [{1}], [{1}], [{""}, {None}]), (Set(Optional[int]), [{1, None}], [{1, None}], [[1]]), (Dict(), [{ 1: 2 }], [{ 1: 2 }], [()]), (Dict(Int()), [{ 1: 2 }], [{ 1: 2 }], [{ "": 2