def _dive(self, ref: str, path: Path, visited_refs: ty.Optional[VisitedRefs] = None, ref_resolver: ty.Optional[RefResolver] = None, registry: ty.Optional[Registry] = None, **context: ty.Any) -> ty.Any: visited_refs = visited_refs or set() assert ref_resolver is not None assert registry is not None try: with ref_resolver.resolving(ref) as target: target_path = Path(ref_resolver.resolution_scope) if target_path in visited_refs: raise RecursiveReferenceError() result = registry.get(target_path) if result is Bad: raise SchemaError( Error(path, self.messages['bad_reference'])) elif result is not None: return result proxy = ObjectProxy(Dummy) registry[target_path] = proxy visited_refs.add(target_path) try: result = self._deeper(target, target_path, visited_refs=visited_refs, ref_resolver=ref_resolver, registry=registry, **context) except SchemaError as e: registry[target_path] = Bad raise SchemaError( Error(path, self.messages['bad_reference']), *e.errors) except RecursiveReferenceError: registry[target_path] = Bad raise SchemaError( Error(path, self.messages['recursive_reference'])) else: proxy.__wrapped__ = weakref.proxy(result) registry[target_path] = result finally: visited_refs.discard(target_path) return result except RefResolutionError: raise SchemaError( Error(path, self.messages['unresolvable_reference']))
def test_result_should_be_lazy(): MockArticle = ObjectProxy(Article) MockArticle.get = MagicMock(return_value=Article.get(1)) relationship = RelationshipResult('id', 1, MockArticle) # test before db call (lazy) assert relationship.id == 1 MockArticle.get.assert_not_called() # test db call only once assert relationship.headline == "Hi!" assert relationship.reporter.id == 1 assert relationship.headline == "Hi!" MockArticle.get.assert_called_once_with(1)
def test_onetoone_should_handle_not_being_lazy(): MockArticle = ObjectProxy(Article) MockArticle.get = MagicMock(return_value=Article.get(1)) relationship = OneToOne(MockArticle, lazy=False) article = relationship.deserialize(1) MockArticle.get.assert_called_once_with(1) # Access fields that would normally trigger laxy loading assert article.id == 1 assert article.headline == "Hi!" assert article.reporter.id == 1 assert article.headline == "Hi!" # make sure our call count is still 1 MockArticle.get.assert_called_once_with(1)
def _entry(self, value: ty.Any, path: Path, visited_refs: ty.Optional[VisitedRefs] = None, registry: ty.Optional[Registry] = None, **context: ty.Any) -> ty.Any: ref = self._get_ref(value) if ref is not None: return self._dive(ref, path, visited_refs=visited_refs, registry=registry, **context) else: assert registry is not None result = registry.get(path) if result is Bad: pass elif result is not None: return result proxy = ObjectProxy(Dummy) registry[path] = proxy try: result = self.subtype.convert(value, path, registry=registry, **context) except SchemaError: registry[path] = Bad raise else: proxy.__wrapped__ = weakref.proxy(result) registry[path] = result return result
def test_onetomany_should_handle_not_being_lazy(): MockArticle = ObjectProxy(Article) MockArticle.get = MagicMock() MockArticle.batch_get = MagicMock(return_value=Article.batch_get([1, 3])) relationship = OneToMany(MockArticle, lazy=False) articles = list(relationship.deserialize([1, 3])) MockArticle.batch_get.assert_called_once() MockArticle.get.assert_not_called() # Access fields that would normally trigger laxy loading # order is not guaranteed in batch_get if articles[0].id == 1: assert articles[0].headline == "Hi!" assert articles[1].id == 3 assert articles[1].headline == "My Article" else: assert articles[0].id == 3 assert articles[0].headline == "My Article" assert articles[1].id == 1 assert articles[1].headline == "Hi!" # make sure our call count is still 1 MockArticle.batch_get.assert_called_once() MockArticle.get.assert_not_called()
def __init__(self, config_parser, exclude=[], include=[]): """Wrap existing ConfigParser so that it excludes entries for unwanted species. Note: only one of `exclude` and `include` arguments can be specified. :param config_parser: ConfigParser instance to be wrapped. :param exclude: Collection of species labels, entries for species included in `exclude` will be removed from the lists returned by properties such as `pair`, `eam_density` and `eam_embed`. :param include: Species labels that should be returned by `pair`, `eam_density` and `eam_embed` functions.""" ObjectProxy.__init__(self, config_parser) if exclude and include: raise ValueError( "Both exclude and include arguments specified. Only one can be used at one time." ) if exclude: self._species_list = exclude self._exclude_flag = True else: self._species_list = include self._exclude_flag = False
def __init__(self, wrapped): ObjectProxy.__init__(self, wrapped)
def __repr__(self): if None in self.__tree__: return self.__wrapped__.__repr__() return ObjectProxy.__repr__(self)
def __dir__(self): return [l for l in list(self.__tree__) + ObjectProxy.__dir__(self) if l is not None]
def __init__(self, tree, is_flat=False, get_nesting=None): ObjectProxy.__init__(self, tree.get(None)) self._self_tree = tree self._self_tree_is_flat = is_flat self._self_tree_get_nesting = get_nesting
def __init__(self, wrapped, trace=None): ObjectProxy.__init__(self, wrapped) self._self_trace = trace
}, 'astropy': { 'handlers': ['default'], 'level': 'WARNING', 'propagate': False }, }, 'root': { 'handlers': ['default'], 'level': 'DEBUG', 'propagate': False }, }, } dict_config = ObjectProxy(None) """ A proxy to the `~logging.config.dictConfigClass` instance, which is made available after `init_log` is called. """ def init_log(preset='default', level='INFO', filepath=None, colored=True, **kwargs): """Initialize logging with some sensible presets. Parameters ----------
def __getattr__(self, name): if name.startswith('_pam_'): name = name.replace('_pam_', '_self_', 1) return getattr(self, name) else: return ObjectProxy.__getattr__(self, name)
def __delattr__(self, name): if name.startswith('_pam_'): name = name.replace('_pam_', '_self_', 1) delattr(self, name) else: ObjectProxy.__delattr__(self, name)
def __init__(self, tree, is_flat=False, get_nesting=None): ObjectProxy.__init__(self, tree.get(None)) self._self_tree = tree self._self_tree_is_flat = is_flat self._self_tree_get_nesting = get_nesting
def __dir__(self): return [l for l in list(self.__tree__) + ObjectProxy.__dir__(self) if l is not None]
def __repr__(self): if None in self.__tree__: return self.__wrapped__.__repr__() return ObjectProxy.__repr__(self)
def generate_nested_column(txnctx, column_name, path, schema, mode): """Generate instance ready structures for read-only checkouts Parameters ---------- txnctx : ColumnTxn transaction context object used to access commit ref info on disk column_name : str name of the column that the reader constructors are being generated for path : Path path to the repository on disk schema : ColumnDefinitionTypes schema definition of the column. mode: str read-only or write-enabled mode. one of ['a', 'r']. Returns ------- :class:`~.nested.NestedSampleReader` Top level column accessor classes fully initialized for requested state. Initailized structures defining and initializing access to the subsample data on disk. """ specs, bes = _nested_load_sample_keys_and_specs(column_name, txnctx) if not all([BACKEND_IS_LOCAL_MAP[be] for be in bes]): _warn_remote(column_name) if mode == 'a': bes.add(schema.backend) fhand = open_file_handles(backends=bes, path=path, mode=mode, schema=schema) samples = {} schema_proxy = proxy(schema) fhand['enter_count'] = 0 if mode == 'r': for samp, subspecs in specs.items(): samples[samp] = FlatSubsampleReader(columnname=column_name, samplen=samp, be_handles=fhand, specs=subspecs, mode='r') res = NestedSampleReader(columnname=column_name, samples=samples, backend_handles=fhand, repo_path=path, mode='r', schema=schema) elif mode == 'a': fhand = ObjectProxy(fhand) fhand_proxy = proxy(fhand) for samp, subspecs in specs.items(): samples[samp] = FlatSubsampleWriter(schema=schema_proxy, aset_ctx=proxy(txnctx), repo_path=path, columnname=column_name, samplen=samp, be_handles=fhand_proxy, specs=subspecs, mode='a') res = NestedSampleWriter(aset_ctx=txnctx, columnname=column_name, samples=samples, backend_handles=fhand, schema=schema, repo_path=path, mode='a') else: raise ValueError(f'mode {mode} is not valid.') return res
def __init__(self, wrapped): ObjectProxy.__init__(self, wrapped) AbstractObservable.__init__(self)