PageRenderTime 67ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/operator/app_operator.py

https://bitbucket.org/alex_gaynor/pypy-postgresql/
Python | 115 lines | 84 code | 23 blank | 8 comment | 22 complexity | 5dc0826cde0eb02849eb987d3ea36d29 MD5 | raw file
  1. '''NOT_RPYTHON: because of attrgetter and itemgetter
  2. Operator interface.
  3. This module exports a set of operators as functions. E.g. operator.add(x,y) is
  4. equivalent to x+y.
  5. '''
  6. def countOf(a,b):
  7. 'countOf(a, b) -- Return the number of times b occurs in a.'
  8. count = 0
  9. for x in a:
  10. if x == b:
  11. count += 1
  12. return count
  13. def delslice(obj, start, end):
  14. 'delslice(a, b, c) -- Same as del a[b:c].'
  15. if not isinstance(start, int) or not isinstance(end, int):
  16. raise TypeError("an integer is expected")
  17. del obj[start:end]
  18. __delslice__ = delslice
  19. def getslice(a, start, end):
  20. 'getslice(a, b, c) -- Same as a[b:c].'
  21. if not isinstance(start, int) or not isinstance(end, int):
  22. raise TypeError("an integer is expected")
  23. return a[start:end]
  24. __getslice__ = getslice
  25. def indexOf(a, b):
  26. 'indexOf(a, b) -- Return the first index of b in a.'
  27. index = 0
  28. for x in a:
  29. if x == b:
  30. return index
  31. index += 1
  32. raise ValueError, 'sequence.index(x): x not in sequence'
  33. # XXX the following is approximative
  34. def isMappingType(obj,):
  35. 'isMappingType(a) -- Return True if a has a mapping type, False otherwise.'
  36. # XXX this is fragile and approximative anyway
  37. return hasattr(obj, '__getitem__') and hasattr(obj, 'keys')
  38. def isNumberType(obj,):
  39. 'isNumberType(a) -- Return True if a has a numeric type, False otherwise.'
  40. return hasattr(obj, '__int__') or hasattr(obj, '__float__')
  41. def isSequenceType(obj,):
  42. 'isSequenceType(a) -- Return True if a has a sequence type, False otherwise.'
  43. return hasattr(obj, '__getitem__') and not hasattr(obj, 'keys')
  44. def repeat(obj, num):
  45. 'repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.'
  46. if not isinstance(num, (int, long)):
  47. raise TypeError, 'an integer is required'
  48. if not isSequenceType(obj):
  49. raise TypeError, "non-sequence object can't be repeated"
  50. return obj * num
  51. __repeat__ = repeat
  52. def setslice(a, b, c, d):
  53. 'setslice(a, b, c, d) -- Same as a[b:c] = d.'
  54. a[b:c] = d
  55. __setslice__ = setslice
  56. class attrgetter(object):
  57. def __init__(self, attr, *attrs):
  58. self.attrs = (attr,) + attrs
  59. def _resolve_attr(self, obj, attr):
  60. last = 0
  61. while True:
  62. try:
  63. dot = attr.find(".", last)
  64. except AttributeError:
  65. raise TypeError
  66. if dot > 0:
  67. obj = getattr(obj, attr[last:dot])
  68. last = dot + 1
  69. else:
  70. return getattr(obj, attr[last:])
  71. def __call__(self, obj):
  72. if len(self.attrs) == 1:
  73. return self._resolve_attr(obj, self.attrs[0])
  74. return tuple(self._resolve_attr(obj, attr) for attr in self.attrs)
  75. class itemgetter(object):
  76. def __init__(self, item, *args):
  77. self.items = args
  78. self.item = item
  79. def __call__(self, obj):
  80. result = obj[self.item]
  81. if self.items:
  82. list = [result] + [obj[item] for item in self.items]
  83. return tuple(list)
  84. return result
  85. class methodcaller(object):
  86. def __init__(self, method_name, *args, **kwargs):
  87. self.method_name = method_name
  88. self.args = args
  89. self.kwargs = kwargs
  90. def __call__(self, obj):
  91. return getattr(obj, self.method_name)(*self.args, **self.kwargs)