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