Mailing List Archive

[issue4124] Patch for adding "default" to itemgetter and attrgetter
Raymond Hettinger <rhettinger@users.sourceforge.net> added the comment:

Will take a look at it next week.

----------
assignee: -> rhettinger
nosy: +rhettinger
type: -> feature request

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue4124>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue4124] Patch for adding "default" to itemgetter and attrgetter [ In reply to ]
Raymond Hettinger <rhettinger@users.sourceforge.net> added the comment:

Am curious about your use cases. ISTM that in every case I've every
used either function, I've always known that the attribute or item is
going to be there. For instance, the original motivating use cases were
to support the key= argument to
min/max/sorted/nlargest/nsmallest/groupby and to support iterator
algebra with itertools:

def powerset(iterable):
'''Iterate over all subsets.

>>> list(powerset('abc'))
[set([]), set(['a']), set(['b']), set(['a', 'b'])]

'''
# Only 1 initial call to PyObject_Hash().
# No trips around the eval-loop.
# Memory friendly.
seq = map(set, list(iterable)[::-1])
selector_stream = product([False, True], repeat=len(seq))
newsets, ns1 = tee(starmap(set, repeat(())))
components = imap(compress, repeat(seq), selector_stream)
sets_and_components = imap(chain, izip(newsets), components)
results = starmap(set.update, sets_and_components)
return imap(itemgetter(0), izip(ns1, results))

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue4124>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue4124] Patch for adding "default" to itemgetter and attrgetter [ In reply to ]
Miki Tebeka <miki.tebeka@gmail.com> added the comment:

Hmmm, too much time has passed and my 1bit memory has overflowed since :)

IIRC it has to do with the fact that not all nodes in BeautifulSoup has a
"name" attribute. I wanted to count how may "tr" there were, so
len(filter(lambda n: n == "tr", map(attrgetter("name"), soup)))
was what I wanted to do, but I got AttributeError.

The natural way to me would be
len(filter(lambda n: n == "tr", map(attrgetter("name", ""), soup)))
but instead I had to do
len(filter(lambda n: n == "tr", map(lambda n: gettattr(n, "name", ""),
soup)))

Another thing I can think of is for usage in count/max ... when some of the
objects don't have the attribute you're looking for and it's by design
(bad one
maybe).

You'd like to be able to do:
max(map(itemgetter("time", 0)), articles)

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue4124>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue4124] Patch for adding "default" to itemgetter and attrgetter [ In reply to ]
Raymond Hettinger <rhettinger@users.sourceforge.net> added the comment:

That makes sense. You've found two object models that have optional
attributes and have had some need to extract them with a default.

My remaining concern is about adding complexity for functionality that
is not often needed. It wouldn't be an issue if itemgetter() and
attrgetter() already had a simple signature, but they already allow
multiple arguments and IMO that doesn't mesh well with providing defaults.

FWIW, looking back at your use cases, it feels like the functional tools
have come together awkwardly. It may be slower, but the following seems
easier to read, easier to write, and clearer about its intention:

sum('tr' == getattr(node, 'name', '') for node in soup)
max(getattr(art, 'time', 0) for art in articles)

In general, listcomps and genexps read better than equivalents using
lambda or a stack of builtin operators. And, lambda is dog slow. So,
the following may be slower than the above code:

len(filter(lambda n: n == "tr", map(attrgetter("name", ""), soup)))

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue4124>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue4124] Patch for adding "default" to itemgetter and attrgetter [ In reply to ]
Miki Tebeka <miki.tebeka@gmail.com> added the comment:

Can't we find a faster dog for lambda :)

Anyway, I agree that we need to see more demand for that before going on.
Let's keep this ticket open and see if someone else comes along.

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue4124>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue4124] Patch for adding "default" to itemgetter and attrgetter [ In reply to ]
Changes by Raymond Hettinger <rhettinger@users.sourceforge.net>:


----------
priority: -> low

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue4124>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com