Skip to content

Commit

Permalink
Merge pull request #13 from dshen109/master
Browse files Browse the repository at this point in the history
Add `.items()` to Namespaces
  • Loading branch information
shakefu committed Jul 5, 2018
2 parents 6e881df + 24e3025 commit 853b7ea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pytool/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def __bool__(self):
return bool(self.__dict__)

def iteritems(self, base_name=None):
""" Return iterator which returns ``(key, value)`` tuples.
""" Return generator which returns ``(key, value)`` tuples.
:param str base_name: Base namespace (optional)
Expand All @@ -495,6 +495,16 @@ def iteritems(self, base_name=None):
else:
yield name, value

def items(self, base_name=None):
""" Return generator which returns ``(key, value)`` tuples.
Analagous to dict.items() behavior in Python3
:param str base_name: Base namespace (optional)
"""
return self.iteritems(base_name)

def as_dict(self, base_name=None):
""" Return the current namespace as a dictionary.
Expand Down
21 changes: 20 additions & 1 deletion test/test_lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ def test_namespace_list_key_access_traversal():
eq_(ns['foo.0'], 'you')
eq_(ns['foo.1'], 'blue')


ns.nested = []
ns2 = pytool.lang.Namespace()
ns2.foo.bar = 'you'
Expand Down Expand Up @@ -623,3 +622,23 @@ def test_namespace_traversal_str_key_list_raises_typeerror():
ns.foo = [1, 2]

ns['foo.1e9']


def test_namespace_items():
ns = pytool.lang.Namespace()
ns.foo = "bar"
ns.fooby = "foobar"

ns_items = {k: v for k, v in ns.items()}

eq_(ns_items, {"foo": "bar", "fooby": "foobar"})


def test_namespace_items_nested():
ns = pytool.lang.Namespace()
ns.foo.bar = "foobar"
ns.fooby = "foobaz"

ns_items = {k: v for k, v in ns.items()}

eq_(ns_items, {"foo.bar": "foobar", "fooby": "foobaz"})

0 comments on commit 853b7ea

Please sign in to comment.