Statistics
| Branch: | Tag: | Revision:

root / lib / compat.py @ 6915fe26

History | View | Annotate | Download (3.8 kB)

1 cea881e5 Michael Hanselmann
#
2 cea881e5 Michael Hanselmann
#
3 cea881e5 Michael Hanselmann
4 eb62069e Iustin Pop
# Copyright (C) 2010, 2011 Google Inc.
5 cea881e5 Michael Hanselmann
#
6 cea881e5 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 cea881e5 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 cea881e5 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 cea881e5 Michael Hanselmann
# (at your option) any later version.
10 cea881e5 Michael Hanselmann
#
11 cea881e5 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 cea881e5 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 cea881e5 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 cea881e5 Michael Hanselmann
# General Public License for more details.
15 cea881e5 Michael Hanselmann
#
16 cea881e5 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 cea881e5 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 cea881e5 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 cea881e5 Michael Hanselmann
# 02110-1301, USA.
20 cea881e5 Michael Hanselmann
21 cea881e5 Michael Hanselmann
22 cea881e5 Michael Hanselmann
"""Module containing backported language/library functionality.
23 cea881e5 Michael Hanselmann

24 cea881e5 Michael Hanselmann
"""
25 cea881e5 Michael Hanselmann
26 cea881e5 Michael Hanselmann
import itertools
27 eb62069e Iustin Pop
import operator
28 cea881e5 Michael Hanselmann
29 cea881e5 Michael Hanselmann
try:
30 b459a848 Andrea Spadaccini
  # pylint: disable=F0401
31 cea881e5 Michael Hanselmann
  import functools
32 cea881e5 Michael Hanselmann
except ImportError:
33 cea881e5 Michael Hanselmann
  functools = None
34 cea881e5 Michael Hanselmann
35 6396164f Guido Trotter
try:
36 b459a848 Andrea Spadaccini
  # pylint: disable=F0401
37 6396164f Guido Trotter
  import roman
38 6396164f Guido Trotter
except ImportError:
39 6396164f Guido Trotter
  roman = None
40 6396164f Guido Trotter
41 cea881e5 Michael Hanselmann
42 716a32cb Guido Trotter
# compat.md5_hash and compat.sha1_hash can be called to generate and md5 and a
43 716a32cb Guido Trotter
# sha1 hashing modules, under python 2.4, 2.5 and 2.6, even though some changes
44 716a32cb Guido Trotter
# went on. compat.sha1 is python-version specific and is used for python
45 716a32cb Guido Trotter
# modules (hmac, for example) which have changed their behavior as well from
46 716a32cb Guido Trotter
# one version to the other.
47 716a32cb Guido Trotter
try:
48 b459a848 Andrea Spadaccini
  # pylint: disable=F0401
49 716a32cb Guido Trotter
  # Yes, we're not using the imports in this module.
50 b459a848 Andrea Spadaccini
  # pylint: disable=W0611
51 716a32cb Guido Trotter
  from hashlib import md5 as md5_hash
52 716a32cb Guido Trotter
  from hashlib import sha1 as sha1_hash
53 716a32cb Guido Trotter
  # this additional version is needed for compatibility with the hmac module
54 716a32cb Guido Trotter
  sha1 = sha1_hash
55 716a32cb Guido Trotter
except ImportError:
56 716a32cb Guido Trotter
  from md5 import new as md5_hash
57 716a32cb Guido Trotter
  import sha
58 716a32cb Guido Trotter
  sha1 = sha
59 716a32cb Guido Trotter
  sha1_hash = sha.new
60 716a32cb Guido Trotter
61 716a32cb Guido Trotter
62 403f5172 Guido Trotter
def _all(seq):
63 403f5172 Guido Trotter
  """Returns True if all elements in the iterable are True.
64 cea881e5 Michael Hanselmann

65 cea881e5 Michael Hanselmann
  """
66 403f5172 Guido Trotter
  for _ in itertools.ifilterfalse(bool, seq):
67 cea881e5 Michael Hanselmann
    return False
68 cea881e5 Michael Hanselmann
  return True
69 cea881e5 Michael Hanselmann
70 e687ec01 Michael Hanselmann
71 403f5172 Guido Trotter
def _any(seq):
72 403f5172 Guido Trotter
  """Returns True if any element of the iterable are True.
73 cea881e5 Michael Hanselmann

74 cea881e5 Michael Hanselmann
  """
75 403f5172 Guido Trotter
  for _ in itertools.ifilter(bool, seq):
76 cea881e5 Michael Hanselmann
    return True
77 cea881e5 Michael Hanselmann
  return False
78 cea881e5 Michael Hanselmann
79 e687ec01 Michael Hanselmann
80 403f5172 Guido Trotter
try:
81 b459a848 Andrea Spadaccini
  # pylint: disable=E0601
82 b459a848 Andrea Spadaccini
  # pylint: disable=W0622
83 23e0ef8c Guido Trotter
  all = all
84 403f5172 Guido Trotter
except NameError:
85 403f5172 Guido Trotter
  all = _all
86 403f5172 Guido Trotter
87 403f5172 Guido Trotter
try:
88 b459a848 Andrea Spadaccini
  # pylint: disable=E0601
89 b459a848 Andrea Spadaccini
  # pylint: disable=W0622
90 23e0ef8c Guido Trotter
  any = any
91 403f5172 Guido Trotter
except NameError:
92 403f5172 Guido Trotter
  any = _any
93 cea881e5 Michael Hanselmann
94 e687ec01 Michael Hanselmann
95 b459a848 Andrea Spadaccini
def partition(seq, pred=bool): # pylint: disable=W0622
96 cea881e5 Michael Hanselmann
  """Partition a list in two, based on the given predicate.
97 cea881e5 Michael Hanselmann

98 cea881e5 Michael Hanselmann
  """
99 cea881e5 Michael Hanselmann
  return (list(itertools.ifilter(pred, seq)),
100 cea881e5 Michael Hanselmann
          list(itertools.ifilterfalse(pred, seq)))
101 cea881e5 Michael Hanselmann
102 cea881e5 Michael Hanselmann
103 cea881e5 Michael Hanselmann
# Even though we're using Python's built-in "partial" function if available,
104 cea881e5 Michael Hanselmann
# this one is always defined for testing.
105 b459a848 Andrea Spadaccini
def _partial(func, *args, **keywords): # pylint: disable=W0622
106 cea881e5 Michael Hanselmann
  """Decorator with partial application of arguments and keywords.
107 cea881e5 Michael Hanselmann

108 cea881e5 Michael Hanselmann
  This function was copied from Python's documentation.
109 cea881e5 Michael Hanselmann

110 cea881e5 Michael Hanselmann
  """
111 cea881e5 Michael Hanselmann
  def newfunc(*fargs, **fkeywords):
112 cea881e5 Michael Hanselmann
    newkeywords = keywords.copy()
113 cea881e5 Michael Hanselmann
    newkeywords.update(fkeywords)
114 b459a848 Andrea Spadaccini
    return func(*(args + fargs), **newkeywords) # pylint: disable=W0142
115 cea881e5 Michael Hanselmann
116 cea881e5 Michael Hanselmann
  newfunc.func = func
117 cea881e5 Michael Hanselmann
  newfunc.args = args
118 cea881e5 Michael Hanselmann
  newfunc.keywords = keywords
119 cea881e5 Michael Hanselmann
  return newfunc
120 cea881e5 Michael Hanselmann
121 cea881e5 Michael Hanselmann
122 403f5172 Guido Trotter
if functools is None:
123 403f5172 Guido Trotter
  partial = _partial
124 403f5172 Guido Trotter
else:
125 403f5172 Guido Trotter
  partial = functools.partial
126 403f5172 Guido Trotter
127 403f5172 Guido Trotter
128 6396164f Guido Trotter
def TryToRoman(val, convert=True):
129 6396164f Guido Trotter
  """Try to convert a value to roman numerals
130 6396164f Guido Trotter

131 6396164f Guido Trotter
  If the roman module could be loaded convert the given value to a roman
132 6396164f Guido Trotter
  numeral. Gracefully fail back to leaving the value untouched.
133 6396164f Guido Trotter

134 6396164f Guido Trotter
  @type val: integer
135 6396164f Guido Trotter
  @param val: value to convert
136 6396164f Guido Trotter
  @type convert: boolean
137 6396164f Guido Trotter
  @param convert: if False, don't try conversion at all
138 6396164f Guido Trotter
  @rtype: string or typeof(val)
139 6396164f Guido Trotter
  @return: roman numeral for val, or val if conversion didn't succeed
140 6396164f Guido Trotter

141 6396164f Guido Trotter
  """
142 6396164f Guido Trotter
  if roman is not None and convert:
143 6396164f Guido Trotter
    try:
144 6396164f Guido Trotter
      return roman.toRoman(val)
145 6396164f Guido Trotter
    except roman.RomanError:
146 6396164f Guido Trotter
      return val
147 6396164f Guido Trotter
  else:
148 6396164f Guido Trotter
    return val
149 6396164f Guido Trotter
150 eb62069e Iustin Pop
#: returns the first element of a list-like value
151 eb62069e Iustin Pop
fst = operator.itemgetter(0)
152 eb62069e Iustin Pop
153 eb62069e Iustin Pop
#: returns the second element of a list-like value
154 eb62069e Iustin Pop
snd = operator.itemgetter(1)