Package logilab :: Package common
[frames] | no frames]

Source Code for Package logilab.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  # set this to False if you've mx DateTime installed but you don't want your db 
 30  # adapter to use it (should be set before you got a connection) 
 31  USE_MX_DATETIME = True 
 32   
 33   
34 -class attrdict(dict):
35 """A dictionary for which keys are also accessible as attributes."""
36 - def __getattr__(self, attr):
37 try: 38 return self[attr] 39 except KeyError: 40 raise AttributeError(attr)
41
42 -class dictattr(dict):
43 - def __init__(self, proxy):
44 self.__proxy = proxy
45
46 - def __getitem__(self, attr):
47 try: 48 return getattr(self.__proxy, attr) 49 except AttributeError: 50 raise KeyError(attr)
51
52 -class nullobject(object):
53 - def __nonzero__(self):
54 return False
55 56 # flatten ----- 57 # XXX move in a specific module and use yield instead 58 # do not mix flatten and translate 59 # 60 # def iterable(obj): 61 # try: iter(obj) 62 # except: return False 63 # return True 64 # 65 # def is_string_like(obj): 66 # try: obj +'' 67 # except (TypeError, ValueError): return False 68 # return True 69 # 70 #def is_scalar(obj): 71 # return is_string_like(obj) or not iterable(obj) 72 # 73 #def flatten(seq): 74 # for item in seq: 75 # if is_scalar(item): 76 # yield item 77 # else: 78 # for subitem in flatten(item): 79 # yield subitem 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 # XXX is function below still used ? 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