mirror of https://github.com/python/cpython
Merged revisions 84242-84244 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r84242 | benjamin.peterson | 2010-08-21 15:01:28 -0500 (Sat, 21 Aug 2010) | 1 line indent properly ........ r84243 | benjamin.peterson | 2010-08-21 15:03:15 -0500 (Sat, 21 Aug 2010) | 1 line fix more indentation ........ r84244 | benjamin.peterson | 2010-08-21 15:08:36 -0500 (Sat, 21 Aug 2010) | 1 line provide sample implementations for attrgetter and methodcaller ........
This commit is contained in:
parent
3ebaed60b4
commit
058981b2ec
|
@ -496,7 +496,23 @@ expect a function argument.
|
||||||
attribute is requested, returns a tuple of attributes. After,
|
attribute is requested, returns a tuple of attributes. After,
|
||||||
``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After,
|
``f = attrgetter('name')``, the call ``f(b)`` returns ``b.name``. After,
|
||||||
``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name,
|
``f = attrgetter('name', 'date')``, the call ``f(b)`` returns ``(b.name,
|
||||||
b.date)``.
|
b.date)``. Equivalent to::
|
||||||
|
|
||||||
|
def attrgetter(*items):
|
||||||
|
if len(items) == 1:
|
||||||
|
attr = items[0]
|
||||||
|
def g(obj):
|
||||||
|
return resolve_attr(obj, attr)
|
||||||
|
else:
|
||||||
|
def g(obj):
|
||||||
|
return tuple(resolve_att(obj, attr) for attr in items)
|
||||||
|
return g
|
||||||
|
|
||||||
|
def resolve_attr(obj, attr):
|
||||||
|
for name in attr.split("."):
|
||||||
|
obj = getattr(obj, name)
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
The attribute names can also contain dots; after ``f = attrgetter('date.month')``,
|
The attribute names can also contain dots; after ``f = attrgetter('date.month')``,
|
||||||
the call ``f(b)`` returns ``b.date.month``.
|
the call ``f(b)`` returns ``b.date.month``.
|
||||||
|
@ -516,15 +532,15 @@ expect a function argument.
|
||||||
operand's :meth:`__getitem__` method. If multiple items are specified,
|
operand's :meth:`__getitem__` method. If multiple items are specified,
|
||||||
returns a tuple of lookup values. Equivalent to::
|
returns a tuple of lookup values. Equivalent to::
|
||||||
|
|
||||||
def itemgetter(*items):
|
def itemgetter(*items):
|
||||||
if len(items) == 1:
|
if len(items) == 1:
|
||||||
item = items[0]
|
item = items[0]
|
||||||
def g(obj):
|
def g(obj):
|
||||||
return obj[item]
|
return obj[item]
|
||||||
else:
|
else:
|
||||||
def g(obj):
|
def g(obj):
|
||||||
return tuple(obj[item] for item in items)
|
return tuple(obj[item] for item in items)
|
||||||
return g
|
return g
|
||||||
|
|
||||||
The items can be any type accepted by the operand's :meth:`__getitem__`
|
The items can be any type accepted by the operand's :meth:`__getitem__`
|
||||||
method. Dictionaries accept any hashable value. Lists, tuples, and
|
method. Dictionaries accept any hashable value. Lists, tuples, and
|
||||||
|
@ -545,12 +561,12 @@ expect a function argument.
|
||||||
Example of using :func:`itemgetter` to retrieve specific fields from a
|
Example of using :func:`itemgetter` to retrieve specific fields from a
|
||||||
tuple record:
|
tuple record:
|
||||||
|
|
||||||
>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
|
>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
|
||||||
>>> getcount = itemgetter(1)
|
>>> getcount = itemgetter(1)
|
||||||
>>> map(getcount, inventory)
|
>>> map(getcount, inventory)
|
||||||
[3, 2, 5, 1]
|
[3, 2, 5, 1]
|
||||||
>>> sorted(inventory, key=getcount)
|
>>> sorted(inventory, key=getcount)
|
||||||
[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
|
[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]
|
||||||
|
|
||||||
|
|
||||||
.. function:: methodcaller(name[, args...])
|
.. function:: methodcaller(name[, args...])
|
||||||
|
@ -559,7 +575,12 @@ expect a function argument.
|
||||||
additional arguments and/or keyword arguments are given, they will be given
|
additional arguments and/or keyword arguments are given, they will be given
|
||||||
to the method as well. After ``f = methodcaller('name')``, the call ``f(b)``
|
to the method as well. After ``f = methodcaller('name')``, the call ``f(b)``
|
||||||
returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the
|
returns ``b.name()``. After ``f = methodcaller('name', 'foo', bar=1)``, the
|
||||||
call ``f(b)`` returns ``b.name('foo', bar=1)``.
|
call ``f(b)`` returns ``b.name('foo', bar=1)``. Equivalent to::
|
||||||
|
|
||||||
|
def methodcaller(name, *args, **kwargs):
|
||||||
|
def caller(obj):
|
||||||
|
return getattr(obj, name)(*args, **kwargs)
|
||||||
|
return caller
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue