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

Source Code for Package logilab.common

  1  # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
  2  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
  3  # 
  4  # This file is part of logilab-common. 
  5  # 
  6  # logilab-common is free software: you can redistribute it and/or modify it under 
  7  # the terms of the GNU Lesser General Public License as published by the Free 
  8  # Software Foundation, either version 2.1 of the License, or (at your option) any 
  9  # later version. 
 10  # 
 11  # logilab-common is distributed in the hope that it will be useful, but WITHOUT 
 12  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 13  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more 
 14  # details. 
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public License along 
 17  # with logilab-common.  If not, see <http://www.gnu.org/licenses/>. 
 18  """Logilab common library (aka Logilab's extension to the standard library). 
 19   
 20  :type STD_BLACKLIST: tuple 
 21  :var STD_BLACKLIST: directories ignored by default by the functions in 
 22    this package which have to recurse into directories 
 23   
 24  :type IGNORED_EXTENSIONS: tuple 
 25  :var IGNORED_EXTENSIONS: file extensions that may usually be ignored 
 26  """ 
 27  __docformat__ = "restructuredtext en" 
 28  from logilab.common.__pkginfo__ import version as __version__ 
 29   
 30  STD_BLACKLIST = ('CVS', '.svn', '.hg', 'debian', 'dist', 'build') 
 31   
 32  IGNORED_EXTENSIONS = ('.pyc', '.pyo', '.elc', '~') 
 33   
 34  # set this to False if you've mx DateTime installed but you don't want your db 
 35  # adapter to use it (should be set before you got a connection) 
 36  USE_MX_DATETIME = True 
 37   
 38   
39 -class attrdict(dict):
40 """A dictionary for which keys are also accessible as attributes."""
41 - def __getattr__(self, attr):
42 try: 43 return self[attr] 44 except KeyError: 45 raise AttributeError(attr)
46
47 -class dictattr(dict):
48 - def __init__(self, proxy):
49 self.__proxy = proxy
50
51 - def __getitem__(self, attr):
52 try: 53 return getattr(self.__proxy, attr) 54 except AttributeError: 55 raise KeyError(attr)
56
57 -class nullobject(object):
58 - def __repr__(self):
59 return '<nullobject>'
60 - def __nonzero__(self):
61 return False
62 63 # flatten ----- 64 # XXX move in a specific module and use yield instead 65 # do not mix flatten and translate 66 # 67 # def iterable(obj): 68 # try: iter(obj) 69 # except: return False 70 # return True 71 # 72 # def is_string_like(obj): 73 # try: obj +'' 74 # except (TypeError, ValueError): return False 75 # return True 76 # 77 #def is_scalar(obj): 78 # return is_string_like(obj) or not iterable(obj) 79 # 80 #def flatten(seq): 81 # for item in seq: 82 # if is_scalar(item): 83 # yield item 84 # else: 85 # for subitem in flatten(item): 86 # yield subitem 87
88 -def flatten(iterable, tr_func=None, results=None):
89 """Flatten a list of list with any level. 90 91 If tr_func is not None, it should be a one argument function that'll be called 92 on each final element. 93 94 :rtype: list 95 96 >>> flatten([1, [2, 3]]) 97 [1, 2, 3] 98 """ 99 if results is None: 100 results = [] 101 for val in iterable: 102 if isinstance(val, (list, tuple)): 103 flatten(val, tr_func, results) 104 elif tr_func is None: 105 results.append(val) 106 else: 107 results.append(tr_func(val)) 108 return results
109 110 111 # XXX is function below still used ? 112
113 -def make_domains(lists):
114 """ 115 Given a list of lists, return a list of domain for each list to produce all 116 combinations of possibles values. 117 118 :rtype: list 119 120 Example: 121 122 >>> make_domains(['a', 'b'], ['c','d', 'e']) 123 [['a', 'b', 'a', 'b', 'a', 'b'], ['c', 'c', 'd', 'd', 'e', 'e']] 124 """ 125 domains = [] 126 for iterable in lists: 127 new_domain = iterable[:] 128 for i in range(len(domains)): 129 domains[i] = domains[i]*len(iterable) 130 if domains: 131 missing = (len(domains[0]) - len(iterable)) / len(iterable) 132 i = 0 133 for j in range(len(iterable)): 134 value = iterable[j] 135 for dummy in range(missing): 136 new_domain.insert(i, value) 137 i += 1 138 i += 1 139 domains.append(new_domain) 140 return domains
141