def test_namespace_flatten_loop_detection(): n1 = Namespace() n1.foo = n1 n1.bar = 'baz' n2 = Namespace() n2.buzz = n1 assert {'buzz__bar': 'baz'} == flatten(n2)
def test_dispatch_with_target(): @dispatch def quux(title): # something... return title @dispatch( b='X', quux=Namespace(call_target=quux), ) def bar(a, b, quux): return a + b + quux() def baz(a, b, c): # something... return a + b + c @dispatch(bar=Namespace(call_target=bar), bar__a='5', bar__quux__title='hi!', baz=Namespace(call_target=baz)) def foo(a, b, c, bar, baz): x = bar() y = baz() # do something with the inputs a, b, c... return a + b + c + x + y assert foo('1', '2', '3', bar__quux__title='7', baz__a='A', baz__b='B', baz__c='C') == '1235X7ABC'
def test_setdefaults_callable_backward_not_namespace(): actual = setdefaults_path( Namespace(foo__x=17), foo=EMPTY, ) expected = Namespace(foo__x=17) assert expected == actual
def test_deprecation_of_string_promotion2(): foo = Namespace(foo__bar=True) with pytest.deprecated_call() as d: foo = Namespace(foo, foo='foo') assert str( d.list[0].message ) == 'Deprecated promotion of written string value "foo" to dict(foo=True)' assert foo == Namespace(foo__foo=True, foo__bar=True)
def test_setdefatults_path_retain_empty(): actual = setdefaults_path(Namespace(a=Namespace()), a__b=Namespace()) expected = Namespace(a__b=Namespace()) assert expected == actual actual = setdefaults_path(Namespace(), attrs__class=Namespace()) expected = Namespace(attrs__class=Namespace()) assert expected == actual
def test_is_shortcut(): t = Namespace(x=1) assert not is_shortcut(t) s = Shortcut(x=1) assert isinstance(s, Namespace) assert is_shortcut(s)
def test_namespace_flatten_loop_detection(): n1 = Namespace() n1.foo = n1 n1.bar = 'baz' n2 = Namespace() n2.buzz = n1 assert {'buzz__bar': 'baz'} == flatten(n2)
def test_dispatch_store_arguments(): @dispatch( foo=1, bar=2, ) def foo(): pass assert foo.dispatch == Namespace(foo=1, bar=2)
def test_deprecated_string_value_promotion(): with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("default", category=DeprecationWarning) assert Namespace(foo__bar=True, foo__baz=False) == Namespace(dict(foo='bar'), dict(foo__baz=False)) assert 'Deprecated promotion of previous string value "bar" to dict(bar=True)' in str( w.pop()) warnings.resetwarnings() with warnings.catch_warnings(record=True) as w: warnings.filterwarnings("default", category=DeprecationWarning) assert Namespace(foo__bar=True, foo__baz=False) == Namespace(dict(foo__baz=False), dict(foo='bar')) assert 'Deprecated promotion of written string value "bar" to dict(bar=True)' in str( w.pop()) warnings.resetwarnings()
def test_no_call_target_overwrite(): def f(): pass def b(): pass x = setdefaults_path( dict(foo={}), foo=f, ) assert dict(foo=Namespace(call_target=f)) == x y = setdefaults_path( x, foo=b, ) assert dict(foo=Namespace(call_target=f)) == y
class Foo(RefinableObject): a = Refinable() b = Refinable() @dispatch( b='default_b', ) def __init__(self, **kwargs): self.non_refinable = 17 super(Foo, self).__init__(**kwargs) @staticmethod @dispatch(f=Namespace(call_target=f)) @refinable def c(f): """ c docstring """ return f() @staticmethod @shortcut @dispatch(call_target=f) def shortcut_to_f(call_target): return call_target()
def test_namespace_setitem_function_dict(): x = Namespace(f=f) x.setitem_path('f', dict(x=17)) assert dict(f=dict(call_target=f, x=17)) == x
def test_namespace_empty_initializer(): assert dict() == Namespace()
def test_flatten_identity_on_namespace_should_not_trigger_loop_detection(): foo = Namespace(show=True) assert dict(party1_labels__show=True, party2_labels__show=True) == flatten( Namespace(party1_labels=foo, party2_labels=foo))
def test_namespace_shortcut_overwrite_backward(): actual = Namespace(Namespace(x=Namespace(y__z=1, y__zz=2)), Namespace(x=Shortcut(a__b=3))) expected = Namespace(x__a__b=3, x__y__z=1, x__y__zz=2) assert expected == actual
def test_namespace_setitem_single_value(): x = Namespace() x.setitem_path('x', 17) assert dict(x=17) == x
def test_namespace_setitem_singe_value_overwrite(): x = Namespace(x=17) x.setitem_path('x', 42) assert dict(x=42) == x
def test_namespace_retain_empty(): assert Namespace(a=Namespace(b=Namespace())).a.b == Namespace()
def test_backward_compatible_empty_key(): assert Namespace(foo__='hej') == Namespace(foo=Namespace({'': 'hej'}))
def test_setdefaults_path_empty_marker_no_side_effect(): actual = setdefaults_path(Namespace(a__b=1, a__c=2), a=Namespace(d=3), a__e=4) expected = Namespace(a__b=1, a__c=2, a__d=3, a__e=4) assert expected == actual
def test_namespace_no_promote_overwrite_backwards(): x = Namespace(x__z=42) x.setitem_path('x', 17) assert Namespace(x=17) == x
def test_merge(a, b, expected, backward): if backward: a, b = b, a actual = Namespace(flatten(a), flatten(b)) assert expected == actual
def test_namespace_setitem_split_path(): x = Namespace() x.setitem_path('x__y', 17) assert dict(x=dict(y=17))
def test_namespace_setitem_split_path_overwrite(): x = Namespace(x__y=17) x.setitem_path('x__y', 42) assert dict(x=dict(y=42)) == x
def test_namespace_setitem_namespace_merge(): x = Namespace(x__y=17) x.setitem_path('x__z', 42) assert dict(x=dict(y=17, z=42)) == x
def test_namespace_setitem_function_backward(): x = Namespace(f__x=17) x.setitem_path('f', f) assert dict(f=dict(call_target=f, x=17)) == x
def test_namespace_setitem_promote_string_to_namespace(): x = Namespace(x='y') x.setitem_path('x__z', 17) assert dict(x=dict(y=True, z=17)) == x
request.user.is_admin = True request.user.is_staff = True return render_table_to_response(request, table=PersonTable()) # ----- class Column(tri_table.Column): @staticmethod def freetext(**kwargs): return Column.from_model( **setdefaults_path(kwargs, query__show=True, query__freetext=True)) @dispatch( app=Namespace(), ) def all_models(request, app, **kwargs): def data(): for app_name, models in apps.all_models.items(): for name, cls in models.items(): if app.get(app_name, {}).get(name, {}).get('show', True): yield Struct(app_name=app_name, model_name=name, model=cls) class ModelsTable(Table): app_name = Column(auto_rowspan=True) model_name = Column(cell__url=lambda row, **_: '/triadmin/%s/%s/' % (row.app_name, row.model_name)) class Meta: sortable = False
def test_namespace_setitem_function_non_dict(): x = Namespace(f=f) x.setitem_path('f', 17) assert dict(f=17) == x
self.performed_mutations = 0 self.mutate_index = mutate_index self.current_line = 1 self.pragma_no_mutate_lines = set() self.filename = filename self.exclude = exclude self.stack = [] self.dict_synonyms = (dict_synonyms or []) + ['dict'] def exclude_line(self): return self.current_line in self.pragma_no_mutate_lines or self.exclude( context=self) @dispatch( context=Namespace(), ) def mutate(source, mutate_index, context): """ :param source: source code :param mutate_index: the index of the mutation to be performed, if ALL mutates all available places :return: tuple: mutated source code, number of mutations performed """ result = parse(source) context = Context(mutate_index=mutate_index, **context) context.pragma_no_mutate_lines = { i + 1 for i, line in enumerate(source.split('\n')) if '# pragma: no mutate' in line } # lines are 1 based indexed mutate_list_of_nodes(result, context=context) result_source = dumps(result).replace(' not not ', ' ')
def test_namespace_no_promote_overwrite(): x = Namespace(x=17) x.setitem_path('x__z', 42) assert Namespace(x__z=42) == x