Package logilab ::
Package common
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Logilab common library (aka Logilab's extension to the standard library).
19
20 :type STD_BLACKLIST: tuple
21 :var STD_BLACKLIST: directories ignored by default by the functions in
22 this package which have to recurse into directories
23
24 :type IGNORED_EXTENSIONS: tuple
25 :var IGNORED_EXTENSIONS: file extensions that may usually be ignored
26 """
27 __docformat__ = "restructuredtext en"
28 from logilab.common.__pkginfo__ import version as __version__
29
30 STD_BLACKLIST = ('CVS', '.svn', '.hg', 'debian', 'dist', 'build')
31
32 IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~')
33
34
35
36 USE_MX_DATETIME = True
37
38
40 """A dictionary for which keys are also accessible as attributes."""
42 try:
43 return self[attr]
44 except KeyError:
45 raise AttributeError(attr)
46
50
52 try:
53 return getattr(self.__proxy, attr)
54 except AttributeError:
55 raise KeyError(attr)
56
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 -def flatten(iterable, tr_func=None, results=None):
89 """Flatten a list of list with any level.
90
91 If tr_func is not None, it should be a one argument function that'll be called
92 on each final element.
93
94 :rtype: list
95
96 >>> flatten([1, [2, 3]])
97 [1, 2, 3]
98 """
99 if results is None:
100 results = []
101 for val in iterable:
102 if isinstance(val, (list, tuple)):
103 flatten(val, tr_func, results)
104 elif tr_func is None:
105 results.append(val)
106 else:
107 results.append(tr_func(val))
108 return results
109
110
111
112
113 -def make_domains(lists):
114 """
115 Given a list of lists, return a list of domain for each list to produce all
116 combinations of possibles values.
117
118 :rtype: list
119
120 Example:
121
122 >>> make_domains(['a', 'b'], ['c','d', 'e'])
123 [['a', 'b', 'a', 'b', 'a', 'b'], ['c', 'c', 'd', 'd', 'e', 'e']]
124 """
125 domains = []
126 for iterable in lists:
127 new_domain = iterable[:]
128 for i in range(len(domains)):
129 domains[i] = domains[i]*len(iterable)
130 if domains:
131 missing = (len(domains[0]) - len(iterable)) / len(iterable)
132 i = 0
133 for j in range(len(iterable)):
134 value = iterable[j]
135 for dummy in range(missing):
136 new_domain.insert(i, value)
137 i += 1
138 i += 1
139 domains.append(new_domain)
140 return domains
141