Package logilab-common-0 ::
Package 36 ::
Package 1
|
|
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
30 from logilab.common.deprecation import moved
31
32 get_cycles = moved('logilab.common.graph', 'get_cycles')
33 cached = moved('logilab.common.decorators', 'cached')
34 ProgressBar = moved('logilab.common.shellutils', 'ProgressBar')
35 Execute = moved('logilab.common.shellutils', 'Execute')
36 acquire_lock = moved('logilab.common.shellutils', 'acquire_lock')
37 release_lock = moved('logilab.common.shellutils', 'release_lock')
38 deprecated_function = moved('logilab.common.deprecation', 'deprecated_function')
39 class_renamed = moved('logilab.common.deprecation', 'class_renamed')
40
42 """Return the intersection of list1 and list2."""
43 warn('this function is deprecated, use a set instead', DeprecationWarning,
44 stacklevel=2)
45 intersect_dict, result = {}, []
46 for item in list1:
47 intersect_dict[item] = 1
48 for item in list2:
49 if item in intersect_dict:
50 result.append(item)
51 return result
52
54 """Return elements of list1 not in list2."""
55 warn('this function is deprecated, use a set instead', DeprecationWarning,
56 stacklevel=2)
57 tmp, result = {}, []
58 for i in list2:
59 tmp[i] = 1
60 for i in list1:
61 if i not in tmp:
62 result.append(i)
63 return result
64
66 """Return list1 union list2."""
67 warn('this function is deprecated, use a set instead', DeprecationWarning,
68 stacklevel=2)
69 tmp = {}
70 for i in list1:
71 tmp[i] = 1
72 for i in list2:
73 tmp[i] = 1
74 return tmp.keys()
75
76
78 """A dictionary for which keys are also accessible as attributes."""
80 try:
81 return self[attr]
82 except KeyError:
83 raise AttributeError(attr)
84
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 -def flatten(iterable, tr_func=None, results=None):
115 """Flatten a list of list with any level.
116
117 If tr_func is not None, it should be a one argument function that'll be called
118 on each final element.
119
120 :rtype: list
121
122 >>> flatten([1, [2, 3]])
123 [1, 2, 3]
124 """
125 if results is None:
126 results = []
127 for val in iterable:
128 if isinstance(val, (list, tuple)):
129 flatten(val, tr_func, results)
130 elif tr_func is None:
131 results.append(val)
132 else:
133 results.append(tr_func(val))
134 return results
135
136
137
138
139 -def make_domains(lists):
140 """
141 Given a list of lists, return a list of domain for each list to produce all
142 combinations of possibles values.
143
144 :rtype: list
145
146 Example:
147
148 >>> make_domains(['a', 'b'], ['c','d', 'e'])
149 [['a', 'b', 'a', 'b', 'a', 'b'], ['c', 'c', 'd', 'd', 'e', 'e']]
150 """
151 domains = []
152 for iterable in lists:
153 new_domain = iterable[:]
154 for i in range(len(domains)):
155 domains[i] = domains[i]*len(iterable)
156 if domains:
157 missing = (len(domains[0]) - len(iterable)) / len(iterable)
158 i = 0
159 for j in range(len(iterable)):
160 value = iterable[j]
161 for dummy in range(missing):
162 new_domain.insert(i, value)
163 i += 1
164 i += 1
165 domains.append(new_domain)
166 return domains
167