blob: 0924e8c2489060e7ef098bead81248bf0510e102 [file] [log] [blame]
Daniel Dunbar378530c2009-01-05 19:53:30 +00001def any_true(list, predicate):
2 for i in list:
3 if predicate(i):
4 return True
5 return False
6
7def any_false(list, predicate):
8 return any_true(list, lambda x: not predicate(x))
9
10def all_true(list, predicate):
11 return not any_false(list, predicate)
12
13def all_false(list, predicate):
14 return not any_true(list, predicate)
15
16def prependLines(prependStr, str):
17 return ('\n'+prependStr).join(str.splitlines())
18
19def pprint(object, useRepr=True):
20 def recur(ob):
21 return pprint(ob, useRepr)
22 def wrapString(prefix, string, suffix):
23 return '%s%s%s' % (prefix,
24 prependLines(' ' * len(prefix),
25 string),
26 suffix)
27 def pprintArgs(name, args):
28 return wrapString(name + '(', ',\n'.join(map(recur,args)), ')')
29
30 if isinstance(object, tuple):
31 return wrapString('(', ',\n'.join(map(recur,object)),
32 [')',',)'][len(object) == 1])
33 elif isinstance(object, list):
34 return wrapString('[', ',\n'.join(map(recur,object)), ']')
35 elif isinstance(object, set):
36 return pprintArgs('set', list(object))
37 elif isinstance(object, dict):
38 elts = []
39 for k,v in object.items():
40 kr = recur(k)
41 vr = recur(v)
42 elts.append('%s : %s' % (kr,
43 prependLines(' ' * (3 + len(kr.splitlines()[-1])),
44 vr)))
45 return wrapString('{', ',\n'.join(elts), '}')
46 else:
47 if useRepr:
48 return repr(object)
49 return str(object)
50
51def prefixAndPPrint(prefix, object, useRepr=True):
52 return prefix + prependLines(' '*len(prefix), pprint(object, useRepr))