-
Notifications
You must be signed in to change notification settings - Fork 0
Itemtuplegetter #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
97c60a3
3007f63
3c6fb44
eca1652
4733a38
036af09
44341e2
f855899
aa71a35
2e04c84
c2b01af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,10 +14,10 @@ | |
| 'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand', | ||
| 'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul', | ||
| 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift', | ||
| 'is_', 'is_none', 'is_not', 'is_not_none', 'isub', 'itemgetter', 'itruediv', | ||
| 'ixor', 'le', 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod', | ||
| 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift', | ||
| 'setitem', 'sub', 'truediv', 'truth', 'xor'] | ||
| 'is_', 'is_none', 'is_not', 'is_not_none', 'isub', 'itemgetter', | ||
| 'itemtuplegetter', 'itruediv', 'ixor', 'le', 'length_hint', 'lshift', 'lt', | ||
| 'matmul', 'methodcaller', 'mod', 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', | ||
| 'pow', 'rshift', 'setitem', 'sub', 'truediv', 'truth', 'xor'] | ||
|
|
||
| from builtins import abs as _abs | ||
|
|
||
|
|
@@ -307,6 +307,70 @@ def __repr__(self): | |
| def __reduce__(self): | ||
| return self.__class__, self._items | ||
|
|
||
| class itemtuplegetter: | ||
| """ | ||
| Return a callable object that fetches the given items from its operand in a tuple. | ||
|
|
||
| If defaults is given, when called on an object where i-th `items` is not present, | ||
| the corresponding defaults is returned instead. If the defaults iterable is | ||
| shorter than subscripts iterable, the remaining subscripts have no defaults. | ||
| If the defaults iterable is longer than subscripts iterable, extra defaults are | ||
| ignored. | ||
|
|
||
| The returned callable has two read-only properties: | ||
| operator.itemtulegetter.items: a tuple containing items to fetch | ||
| operator.itemtulegetter.defaults: a tuple containing provided defaults | ||
|
|
||
| For example, | ||
| After f = itemtuplegetter([0, 2], defaults=(-1, -2)), f([1, 2]) evaluates to (1, -2). | ||
| After g = itemtuplegetter([0, 2], defaults=(-1)), f([1, 2]) resutls in an IndexError. | ||
| After h = itemtuplegetter([0], defaults=(-1, -2)), f([1, 2]) evaluates to (1,). | ||
| After i = itemtuplegetter([1, 0], defaults=(-1, -2)), f([1, 2]) evaluates to (2, 1). | ||
| """ | ||
| __slots__ = ('_items', '_defaults') | ||
|
|
||
| def __init__(self, items, /, defaults = None): | ||
|
||
| self._items = tuple(items) | ||
|
||
| self._defaults = () if defaults is None else tuple(defaults) | ||
|
||
|
|
||
| @property | ||
| def items(self): | ||
| return self._items | ||
|
|
||
| @property | ||
| def defaults(self): | ||
| return self._defaults | ||
|
|
||
| def __call__(self, obj, /): | ||
| if not (defaults := self._defaults): | ||
| return tuple(obj[item] for item in self._items) | ||
| else: | ||
| result = [] | ||
| append = result.append | ||
| items = iter(self._items) | ||
| for default in defaults: | ||
| try: | ||
| item = next(items) | ||
| except StopIteration: | ||
| return tuple(result) | ||
| try: | ||
| append(obj[item]) | ||
| except (IndexError, KeyError): | ||
| append(default) | ||
|
|
||
| for item in items: | ||
| append(obj[item]) | ||
| return tuple(result) | ||
|
|
||
| def __repr__(self): | ||
| return '%s.%s(%s, defaults=%s)' % (self.__class__.__module__, | ||
| self.__class__.__name__, | ||
| repr(self._items), | ||
| repr(self._defaults)) | ||
|
|
||
| def __reduce__(self): | ||
| return self.__class__, (self._items, self._defaults) | ||
|
|
||
| class methodcaller: | ||
| """ | ||
| Return a callable object that calls the given method on its operand. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
itemtuPle
& one below