Package logilab-common-0 ::
Package 39 ::
Package 0
|
|
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-2008 `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
31 """A dictionary for which keys are also accessible as attributes."""
33 try:
34 return self[attr]
35 except KeyError:
36 raise AttributeError(attr)
37
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 -def flatten(iterable, tr_func=None, results=None):
68 """Flatten a list of list with any level.
69
70 If tr_func is not None, it should be a one argument function that'll be called
71 on each final element.
72
73 :rtype: list
74
75 >>> flatten([1, [2, 3]])
76 [1, 2, 3]
77 """
78 if results is None:
79 results = []
80 for val in iterable:
81 if isinstance(val, (list, tuple)):
82 flatten(val, tr_func, results)
83 elif tr_func is None:
84 results.append(val)
85 else:
86 results.append(tr_func(val))
87 return results
88
89
90
91
92 -def make_domains(lists):
93 """
94 Given a list of lists, return a list of domain for each list to produce all
95 combinations of possibles values.
96
97 :rtype: list
98
99 Example:
100
101 >>> make_domains(['a', 'b'], ['c','d', 'e'])
102 [['a', 'b', 'a', 'b', 'a', 'b'], ['c', 'c', 'd', 'd', 'e', 'e']]
103 """
104 domains = []
105 for iterable in lists:
106 new_domain = iterable[:]
107 for i in range(len(domains)):
108 domains[i] = domains[i]*len(iterable)
109 if domains:
110 missing = (len(domains[0]) - len(iterable)) / len(iterable)
111 i = 0
112 for j in range(len(iterable)):
113 value = iterable[j]
114 for dummy in range(missing):
115 new_domain.insert(i, value)
116 i += 1
117 i += 1
118 domains.append(new_domain)
119 return domains
120