Skip to content

Commit

Permalink
Merge pull request #133 from datreant/feature-parents
Browse files Browse the repository at this point in the history
Added parents property to View.
  • Loading branch information
kain88-de committed Dec 6, 2017
2 parents 8d1dc4a + 73c89d0 commit 165b335
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Enhancements
(Issue #107)
* Added Bundle.get method, allowing for convenient filtering on tags
and categories (#144)
* Added ``parents`` property to ``View`` (#133)


Fixes
Expand Down
10 changes: 10 additions & 0 deletions src/datreant/core/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ def draw(self, depth=None, hidden=False):
if isinstance(member, Tree):
member.draw(depth=depth, hidden=hidden)

def parents(self):
"""Return a View of the parent directories for each member.
Because a View functions as an ordered set, and some members of this
collection may share a parent, the View of parents may contain fewer
elements than this collection.
"""
return View([member.parent for member in self])

@property
def loc(self):
"""Get a View giving Tree/Leaf at `path` relative to each Tree in
Expand Down
13 changes: 13 additions & 0 deletions src/datreant/core/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ def return_nothing(treant):
class CollectionsTests(object):
"""Mixin tests for collections"""

def test_parents(self, collection, tmpdir):
with tmpdir.as_cwd():
t1 = dtr.Treant('free-associate/lark')
t2 = dtr.Treant('free-associate/hark')
t3 = dtr.Treant('characters/linus')

col = collection(t1, t2, t3)

assert len(col.parents()) == 2
assert 'free-associate' in col.parents().names
assert 'linus' not in col.parents().names
assert 'characters' in col.parents().names

class TestGetitem(object):
@pytest.mark.parametrize('slx', (
[1, 2],
Expand Down

0 comments on commit 165b335

Please sign in to comment.