Skip to content

Commit

Permalink
Allow searching descendants.
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveMarshall committed Jan 21, 2009
1 parent 9876c3f commit 049b5a9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions atom.py
Expand Up @@ -233,6 +233,24 @@ def get_all_descendants(self):

return descendants

def get_children_of_type(self, type):
children = []
if self.is_container():
[children.append(child) for child in self if child.type == type]

return children

def get_descendants_of_type(self, type):
descendants = []
if self.is_container():
for child in self:
if child.type == type:
descendants.append(child)
descendants += child.get_descendants_of_type(type)

return descendants


# File-like behaviours

def next(self):
Expand Down
17 changes: 17 additions & 0 deletions atomtest.py
Expand Up @@ -386,6 +386,23 @@ def testGetAllDescendants(self):
self.assertEqual(loaded_atom[0][1], descendants[2])
self.assertEqual(loaded_atom[1], descendants[3])

def testGetChildrenOfType(self):
loaded_atom = atom.Atom(self.atom_stream)
children_of_type = loaded_atom.get_children_of_type(self.child_1_type)

self.assertEqual(1, len(children_of_type))
self.assertEqual(loaded_atom[0], children_of_type[0])

def testGetDescendantsOfType(self):
loaded_atom = atom.Atom(self.atom_stream)
descendants_of_type = loaded_atom.get_descendants_of_type(self.child_1_1_type)

self.assertEqual(3, len(descendants_of_type))
self.assertEqual(loaded_atom[0][0], descendants_of_type[0])
self.assertEqual(loaded_atom[0][1], descendants_of_type[1])
self.assertEqual(loaded_atom[1], descendants_of_type[2])



class LoadedContainerAtomChildManipulation(unittest.TestCase):
type = 'moov'
Expand Down

0 comments on commit 049b5a9

Please sign in to comment.