Package logilab-common-0 :: Package 39 :: Package 0
[frames] | no frames]

Source Code for Package logilab-common-0.39.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   
30 -class attrdict(dict):
31 """A dictionary for which keys are also accessible as attributes."""
32 - def __getattr__(self, attr):
33 try: 34 return self[attr] 35 except KeyError: 36 raise AttributeError(attr)
37
38 -class nullobject(object):
39 - def __nonzero__(self):
40 return False
41 42 # flatten ----- 43 # XXX move in a specific module and use yield instead 44 # do not mix flatten and translate 45 # 46 # def iterable(obj): 47 # try: iter(obj) 48 # except: return False 49 # return True 50 # 51 # def is_string_like(obj): 52 # try: obj +'' 53 # except (TypeError, ValueError): return False 54 # return True 55 # 56 #def is_scalar(obj): 57 # return is_string_like(obj) or not iterable(obj) 58 # 59 #def flatten(seq): 60 # for item in seq: 61 # if is_scalar(item): 62 # yield item 63 # else: 64 # for subitem in flatten(item): 65 # yield subitem 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 # XXX is function below still used ? 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