Statistics
| Branch: | Tag: | Revision:

root / lib / compat.py @ 6396164f

History | View | Annotate | Download (3.1 kB)

1
#
2
#
3

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

    
21

    
22
"""Module containing backported language/library functionality.
23

24
"""
25

    
26
import itertools
27

    
28
try:
29
  import functools
30
except ImportError:
31
  functools = None
32

    
33
try:
34
  import roman
35
except ImportError:
36
  roman = None
37

    
38

    
39
def all(seq, pred=bool): # pylint: disable-msg=W0622
40
  """Returns True if pred(x) is True for every element in the iterable.
41

42
  Please note that this function provides a C{pred} parameter which isn't
43
  available in the version included in Python 2.5 and above.
44

45
  """
46
  for _ in itertools.ifilterfalse(pred, seq):
47
    return False
48
  return True
49

    
50

    
51
def any(seq, pred=bool): # pylint: disable-msg=W0622
52
  """Returns True if pred(x) is True for at least one element in the iterable.
53

54
  Please note that this function provides a C{pred} parameter which isn't
55
  available in the version included in Python 2.5 and above.
56

57
  """
58
  for _ in itertools.ifilter(pred, seq):
59
    return True
60
  return False
61

    
62

    
63
def partition(seq, pred=bool): # pylint: disable-msg=W0622
64
  """Partition a list in two, based on the given predicate.
65

66
  """
67
  return (list(itertools.ifilter(pred, seq)),
68
          list(itertools.ifilterfalse(pred, seq)))
69

    
70

    
71
# Even though we're using Python's built-in "partial" function if available,
72
# this one is always defined for testing.
73
def _partial(func, *args, **keywords): # pylint: disable-msg=W0622
74
  """Decorator with partial application of arguments and keywords.
75

76
  This function was copied from Python's documentation.
77

78
  """
79
  def newfunc(*fargs, **fkeywords):
80
    newkeywords = keywords.copy()
81
    newkeywords.update(fkeywords)
82
    return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142
83

    
84
  newfunc.func = func
85
  newfunc.args = args
86
  newfunc.keywords = keywords
87
  return newfunc
88

    
89

    
90
def TryToRoman(val, convert=True):
91
  """Try to convert a value to roman numerals
92

93
  If the roman module could be loaded convert the given value to a roman
94
  numeral. Gracefully fail back to leaving the value untouched.
95

96
  @type val: integer
97
  @param val: value to convert
98
  @type convert: boolean
99
  @param convert: if False, don't try conversion at all
100
  @rtype: string or typeof(val)
101
  @return: roman numeral for val, or val if conversion didn't succeed
102

103
  """
104
  if roman is not None and convert:
105
    try:
106
      return roman.toRoman(val)
107
    except roman.RomanError:
108
      return val
109
  else:
110
    return val
111

    
112

    
113
if functools is None:
114
  partial = _partial
115
else:
116
  partial = functools.partial