def test_fmap(): t = Tree(1, [ Tree(2, [ Tree(3) , Tree(4) ]) , Tree(5, [ Tree(6, [ Tree(7) ]) ]) ]) expected = Tree(2, [ Tree(4, [ Tree(6) , Tree(8) ]) , Tree(10, [ Tree(12, [ Tree(14) ]) ]) ]) assert_equals(expected, t.fmap(lambda x: x*2))
def test_build_tree(): def children_for(x): return range(0, x/2) expected = Tree(8, [ Tree(0) , Tree(1) , Tree(2, [ Tree(0) ]) , Tree(3, [ Tree(0) ]) ]) assert_equals(expected, Tree.build_tree(8, children_for))
def _build_manifest(cls, path, f, ignored_directories=None): if not path: raise ValueError("path must be a non-empty string") if not f: raise ValueError("transform function must be specified") path = os.path.abspath(path) if not os.path.isdir(path): raise ValueError("{} does not name a directory".format(path)) if not ignored_directories: ignored_directories = [] def child_directories(dirname): names = [os.path.join(dirname, name) for name in os.listdir(dirname) if not name.startswith(".")] return [name for name in names if os.path.isdir(name) and not name in ignored_directories] directory_tree = Tree.build_tree(path, child_directories) manifest_tree = directory_tree.fmap(f) return { entry.path: entry for entry in itertools.chain.from_iterable(manifest_tree) }
def test_fmap_no_children(): t = Tree(1) assert_equals(Tree(2), t.fmap(lambda x: x*2))