Message177585
Here is a functional (and more effective) equivalent of attrgetter:
def attrgetter(attr, *attrs):
"""
Return a callable object that fetches the given attribute(s) from its operand.
After f=attrgetter('name'), the call f(r) returns r.name.
After g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).
After h=attrgetter('name.first', 'name.last'), the call h(r) returns
(r.name.first, r.name.last).
"""
if not attrs:
if not isinstance(attr, str):
raise TypeError('attribute name must be a string')
names = attr.split('.')
def func(obj):
for name in names:
obj = getattr(obj, name)
return obj
return func
else:
getters = tuple(map(attrgetter, (attr,) + attrs))
def func(obj):
return tuple(getter(obj) for getter in getters)
return func |
|
Date |
User |
Action |
Args |
2012-12-16 11:29:26 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, brett.cannon, zach.ware |
2012-12-16 11:29:26 | serhiy.storchaka | set | messageid: <1355657366.58.0.727671464335.issue16694@psf.upfronthosting.co.za> |
2012-12-16 11:29:26 | serhiy.storchaka | link | issue16694 messages |
2012-12-16 11:29:26 | serhiy.storchaka | create | |
|