From f1df716a1ac376d04aaea4666b9ab2aa37214699 Mon Sep 17 00:00:00 2001 From: Dafydd Harries Date: Mon, 18 Mar 2013 02:21:13 -0400 Subject: license.py: allow for traversal and flattening --- license.py | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/license.py b/license.py index 853cdc0..e52d3f6 100644 --- a/license.py +++ b/license.py @@ -4,6 +4,9 @@ class License(object): self.name = name def __iter__(self): + yield self + + def flatten(self): yield self.name def __str__(self, depth=0): @@ -18,7 +21,11 @@ class Licenses(object): def __iter__(self): for x in self.xs: - for y in iter(x): + yield x + + def flatten(self): + for x in self.xs: + for y in x.flatten(): yield y def __str__(self, depth=0): @@ -42,18 +49,42 @@ class AnyLicense(Licenses): def parse_licenses(s): """ - >>> print parse_licenses("X") + >>> ls = parse_licenses("X") + >>> ls + + >>> print ls X - >>> print parse_licenses("X or Y or Z") + >>> list(ls) + [] + >>> list(ls.flatten()) + ['X'] + + >>> ls = parse_licenses("X or Y or Z") + >>> ls + , , ]> + >>> print ls X | Y | Z - >>> print parse_licenses("X and Y and Z") + >>> list(ls) + [, , ] + >>> list(ls.flatten()) + ['X', 'Y', 'Z'] + + >>> ls = parse_licenses("X and Y and Z") + >>> ls + , , ]> + >>> print ls X & Y & Z - >>> parse_licenses("X or Y and Z") - , , ]>]> + >>> list(ls) + [, , ] + >>> list(ls.flatten()) + ['X', 'Y', 'Z'] + >>> print parse_licenses("X or Y and Z") X | (Y & Z) + >>> print parse_licenses("X and Y or Z") (X & Y) | Z + >>> print parse_licenses("X, and Y or Z") X & (Y | Z) """ -- cgit v1.2.3