Package logilab-common-0 :: Package 36 :: Package 1
[frames] | no frames]

Source Code for Package logilab-common-0.36.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   
41 -def intersection(list1, list2):
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
53 -def difference(list1, list2):
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
65 -def union(list1, list2):
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
77 -class attrdict(dict):
78 """A dictionary for which keys are also accessible as attributes."""
79 - def __getattr__(self, attr):
80 try: 81 return self[attr] 82 except KeyError: 83 raise AttributeError(attr)
84
85 -class nullobject(object):
86 - def __nonzero__(self):
87 return False
88 89 # flatten ----- 90 # XXX move in a specific module and use yield instead 91 # do not mix flatten and translate 92 # 93 # def iterable(obj): 94 # try: iter(obj) 95 # except: return False 96 # return True 97 # 98 # def is_string_like(obj): 99 # try: obj +'' 100 # except (TypeError, ValueError): return False 101 # return True 102 # 103 #def is_scalar(obj): 104 # return is_string_like(obj) or not iterable(obj) 105 # 106 #def flatten(seq): 107 # for item in seq: 108 # if is_scalar(item): 109 # yield item 110 # else: 111 # for subitem in flatten(item): 112 # yield subitem 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 # XXX is function below still used ? 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