| 1 | '''NOT_RPYTHON: because of attrgetter and itemgetter |
| 2 | Operator interface. |
| 3 | |
| 4 | This module exports a set of operators as functions. E.g. operator.add(x,y) is |
| 5 | equivalent to x+y. |
| 6 | ''' |
| 7 | |
| 8 | |
| 9 | def countOf(a,b): |
| 10 | 'countOf(a, b) -- Return the number of times b occurs in a.' |
| 11 | count = 0 |
| 12 | for x in a: |
| 13 | if x is b or x == b: |
| 14 | count += 1 |
| 15 | return count |
| 16 | |
| 17 | def _resolve_attr_chain(chain, obj, idx=0): |
| 18 | obj = getattr(obj, chain[idx]) |
| 19 | if idx + 1 == len(chain): |
| 20 | return obj |
| 21 | else: |
| 22 | return _resolve_attr_chain(chain, obj, idx + 1) |
| 23 | |
| 24 | class attrgetter(object): |
| 25 | def __init__(self, attr, *attrs): |
| 26 | if ( |
| 27 | not isinstance(attr, str) or |
| 28 | not all(isinstance(a, str) for a in attrs) |
| 29 | ): |
| 30 | raise TypeError("attribute name must be a string, not %r" % |
| 31 | type(attr).__name__) |
| 32 | elif attrs: |
| 33 | self._multi_attrs = [ |
| 34 | a.split(".") for a in [attr] + list(attrs) |
| 35 | ] |
| 36 | self._call = self._multi_attrgetter |
| 37 | elif "." not in attr: |
| 38 | self._simple_attr = attr |
| 39 | self._call = self._simple_attrgetter |
| 40 | else: |
| 41 | self._single_attr = attr.split(".") |
| 42 | self._call = self._single_attrgetter |
| 43 | |
| 44 | def __call__(self, obj): |
| 45 | return self._call(obj) |
| 46 | |
| 47 | def _simple_attrgetter(self, obj): |
| 48 | return getattr(obj, self._simple_attr) |
| 49 | |
| 50 | def _single_attrgetter(self, obj): |
| 51 | return _resolve_attr_chain(self._single_attr, obj) |
| 52 | |
| 53 | def _multi_attrgetter(self, obj): |
| 54 | return tuple([ |
| 55 | _resolve_attr_chain(attrs, obj) |
| 56 | for attrs in self._multi_attrs |
| 57 | ]) |
| 58 | |
| 59 | def __reduce__(self): |
| 60 | try: |
| 61 | attrs = (self._simple_attr,) |
| 62 | except AttributeError: |
| 63 | try: |
| 64 | attrs = ('.'.join(self._single_attr),) |
| 65 | except AttributeError: |
| 66 | attrs = tuple('.'.join(a) for a in self._multi_attrs) |
| 67 | return (type(self), attrs) |
| 68 | |
| 69 | def __repr__(self): |
| 70 | try: |
| 71 | a = repr(self._simple_attr) |
| 72 | except AttributeError: |
| 73 | try: |
| 74 | a = repr('.'.join(self._single_attr)) |
| 75 | except AttributeError: |
| 76 | lst = self._multi_attrs |
| 77 | a = ', '.join([repr('.'.join(a1)) for a1 in lst]) |
| 78 | return 'operator.attrgetter(%s)' % (a,) |
| 79 | |
| 80 | |
| 81 | class itemgetter(object): |
| 82 | def __init__(self, item, *items): |
| 83 | self._single = not bool(items) |
| 84 | if self._single: |
| 85 | self._idx = item |
| 86 | else: |
| 87 | self._idx = [item] + list(items) |
| 88 | |
| 89 | def __call__(self, obj): |
| 90 | if self._single: |
| 91 | return obj[self._idx] |
| 92 | else: |
| 93 | return tuple([obj[i] for i in self._idx]) |
| 94 | |
| 95 | def __repr__(self): |
| 96 | if self._single: |
| 97 | a = repr(self._idx) |
| 98 | else: |
| 99 | a = ', '.join([repr(i) for i in self._idx]) |
| 100 | return 'operator.itemgetter(%s)' % (a,) |
| 101 | |
| 102 | |
| 103 | class methodcaller(object): |
| 104 | def __init__(*args, **kwargs): |
| 105 | if len(args) < 2: |
| 106 | raise TypeError("methodcaller() called with not enough arguments") |
| 107 | self, method_name = args[:2] |
| 108 | if not isinstance(method_name, str): |
| 109 | raise TypeError("method name must be a string") |
| 110 | self._method_name = method_name |
| 111 | self._args = args[2:] |
| 112 | self._kwargs = kwargs |
| 113 | |
| 114 | def __call__(self, obj): |
| 115 | return getattr(obj, self._method_name)(*self._args, **self._kwargs) |
| 116 | |
| 117 | def __repr__(self): |
| 118 | args = [repr(self._method_name)] |
| 119 | for a in self._args: |
| 120 | args.append(repr(a)) |
| 121 | for key, value in self._kwargs.items(): |
| 122 | args.append('%s=%r' % (key, value)) |
| 123 | return 'operator.methodcaller(%s)' % (', '.join(args),) |