def sorts_right(objects): assert {y.expected_position for y in values(objects) } == set(range(len(objects))), "Borken test" sort_after(objects) assert [x.expected_position for x in values(objects) ] == list(range(len(objects))), keys(objects)
def test_sort_after_points_to_nothing_plural(): with pytest.raises(KeyError) as e: sort_after(dict( quux=Struct(), foo=Struct(after='does-not-exist2'), quux6=Struct(after='does-not-exist'), )) assert e.value.args[0] == """\
def collect_members(container, *, name: str, items_dict: Dict = None, items: Dict[str, Any] = None, cls: Type, unknown_types_fall_through=False): forbidden_names = FORBIDDEN_NAMES & (set(keys(items_dict or {})) | set(keys(items or {}))) if forbidden_names: raise ForbiddenNamesException(f'The names {", ".join(sorted(forbidden_names))} are reserved by iommi, please pick other names') assert name != 'items' unbound_items = Struct() _unapplied_config = {} if items_dict is not None: for key, x in items_of(items_dict): x._name = key unbound_items[key] = x if items is not None: for key, item in items_of(items): if isinstance(item, Traversable): # noinspection PyProtectedMember assert not item._is_bound item._name = key unbound_items[key] = item elif isinstance(item, dict): if key in unbound_items: _unapplied_config[key] = item else: item = setdefaults_path( Namespace(), item, call_target__cls=cls, _name=key, ) unbound_items[key] = item() else: assert unknown_types_fall_through or item is None, f'I got {type(item)} when creating a {cls.__name__}.{key}, but I was expecting Traversable or dict' unbound_items[key] = item for k, v in items_of(Namespace(_unapplied_config)): unbound_items[k] = unbound_items[k].reinvoke(v) # noinspection PyProtectedMember assert unbound_items[k]._name is not None to_delete = { k for k, v in items_of(unbound_items) if v is None } for k in to_delete: del unbound_items[k] sort_after(unbound_items) set_declared_member(container, name, unbound_items) setattr(container, name, NotBoundYet(container, name))
def test_sort_after_points_to_nothing_plural(): with pytest.raises(KeyError) as e: sort_after( dict( quux=Struct(), foo=Struct(after='does-not-exist2'), quux6=Struct(after='does-not-exist'), )) assert (e.value.args[0] == """\ Tried to order after does-not-exist, does-not-exist2 but those keys do not exist. Available names: foo quux quux6""")
def collect_members( container, *, name: str, items_dict: Dict = None, items: Dict[str, Any] = None, cls: Type, unknown_types_fall_through=False, ): """ This function is used to collect and merge data from the constructor argument, the declared members, and other config into one data structure. `bind_members` is then used at bind time to recursively bind the nested parts. Example: .. code:: python class ArtistTable(Table): instrument = Column() # <- declared member MyTable( columns__name=Column(), # <- constructor argument columns__instrument__after='name', # <- inserted config for a declared member ) In this example the resulting table will have two columns `instrument` and `name`, with `instrument` after name even though it was declared before. """ forbidden_names = FORBIDDEN_NAMES & (set(keys(items_dict or {})) | set(keys(items or {}))) if forbidden_names: raise ForbiddenNamesException( f'The names {", ".join(sorted(forbidden_names))} are reserved by iommi, please pick other names' ) assert name != 'items' unbound_items = Struct() _unapplied_config = {} if items_dict is not None: for key, x in items_of(items_dict): x._name = key unbound_items[key] = x if items is not None: for key, item in items_of(items): if isinstance(item, Traversable): # noinspection PyProtectedMember assert not item._is_bound item._name = key unbound_items[key] = item elif isinstance(item, dict): if key in unbound_items: _unapplied_config[key] = item else: item = setdefaults_path( Namespace(), item, call_target__cls=cls, _name=key, ) unbound_items[key] = item() else: assert ( unknown_types_fall_through or item is None ), f'I got {type(item)} when creating a {cls.__name__}.{key}, but I was expecting Traversable or dict' unbound_items[key] = item for k, v in items_of(Namespace(_unapplied_config)): unbound_items[k] = reinvoke(unbound_items[k], v) # noinspection PyProtectedMember assert unbound_items[k]._name is not None to_delete = {k for k, v in items_of(unbound_items) if v is None} for k in to_delete: del unbound_items[k] sort_after(unbound_items) set_declared_member(container, name, unbound_items) setattr(container, name, NotBoundYet(container, name))