Skip to content

Commit

Permalink
Can now set categories with dict directly.
Browse files Browse the repository at this point in the history
Can also set all member categories with single dict from Bundle.
Likewise with a list, set, or tags for all member tags from Bundle.
  • Loading branch information
dotsdl committed Mar 22, 2016
1 parent 27880c0 commit b5dfee4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/bundles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,14 @@ And if we access a category with a key that isn't present among all members,
>>> b.categories['health']
[None, None, 'good', 'poor']

If we're interested in the values corresponding


Grouping by value
-----------------

Operating on members in parallel
================================

API Reference: Bundle
=====================
Expand Down
3 changes: 3 additions & 0 deletions docs/tags-categories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ API Reference: Tags
-------------------
See the :ref:`Tags_api` API reference for more details.


.. _Categories_guide:

Using categories
================
Categories are key-value pairs. They are particularly useful as switches for
Expand Down
24 changes: 24 additions & 0 deletions src/datreant/core/agglimbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ def __str__(self):
out = out + "'{}'\n".format(tags[i])
return out

@staticmethod
def _setter(self, val):
"""Used for constructing the property when attaching this Limb to a class.
"""
if isinstance(val, (Tags, list, set)):
val = list(val)
self.tags.clear()
self.tags.add(val)
else:
raise TypeError("Can only set with tags, a list, or set")

def __iter__(self):
return self.all.__iter__()

Expand Down Expand Up @@ -294,6 +306,18 @@ def __str__(self):
out = out + "'{}': '{}'\n".format(key, value)
return out

@staticmethod
def _setter(self, val):
"""Used for constructing the property when attaching this Limb to a class.
"""
if isinstance(val, (Categories, dict)):
val = dict(val)
self.categories.clear()
self.categories.add(val)
else:
raise TypeError("Can only set with categories or dict")

def __getitem__(self, keys):
"""Get values for a given key, list of keys, or set of keys.
Expand Down
5 changes: 5 additions & 0 deletions src/datreant/core/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,11 @@ def getter(self):
setattr(self, "_"+limb._name, limb(self))
return getattr(self, "_"+limb._name)

try:
setter = limb._setter
except AttributeError:
setter = None

# set the property
setattr(cls, limb._name,
property(getter, None, None, limb.__doc__))
Expand Down
17 changes: 15 additions & 2 deletions src/datreant/core/limbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ def _setter(self, val):
"""
if isinstance(val, (Tags, list, set)):
val = list(val)
self.tags.clear()
self.tags.add(list(val))
self.tags.add(val)
else:
raise TypeError("Can only set with a list or set")
raise TypeError("Can only set with tags, a list, or set")

def __getitem__(self, value):
with self._treant._read:
Expand Down Expand Up @@ -374,6 +375,18 @@ def __str__(self):
out = out + "'{}': '{}'\n".format(key, categories[key])
return out

@staticmethod
def _setter(self, val):
"""Used for constructing the property when attaching this Limb to a class.
"""
if isinstance(val, (Categories, dict)):
val = dict(val)
self.categories.clear()
self.categories.add(val)
else:
raise TypeError("Can only set with categories or dict")

def __getitem__(self, keys):
"""Get values for given `keys`.
Expand Down
15 changes: 15 additions & 0 deletions src/datreant/core/tests/test_treants.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,21 @@ def test_KeyError(self, treant):
with pytest.raises(KeyError):
treant.categories['hello?']

def test_get_categories(self, treant):
treant.categories['bark'] = 'dark'
treant.categories['leaves'] = 'many'
treant.categories['roots'] = 'shallow'

# get a single category
assert treant.categories['leaves'] == 'many'

# get multiple categories with list
assert treant.categories[['leaves', 'bark']] == ['many', 'dark']

# get multiple categories with set
assert treant.categories[{'leaves', 'bark'}] == {'leaves': 'many',
'bark': 'dark'}


class TestGroup(TestTreant):
"""Test Group-specific features.
Expand Down

0 comments on commit b5dfee4

Please sign in to comment.