#20989: tuple() and dict() accept iterators
---------------------------------+--------------------
     Reporter:  jeroen.pulles@…  |      Owner:  nobody
         Type:  Uncategorized    |     Status:  new
    Component:  Uncategorized    |    Version:  master
     Severity:  Normal           |   Keywords:
 Triage Stage:  Unreviewed       |  Has patch:  0
Easy pickings:  0                |      UI/UX:  0
---------------------------------+--------------------
 As I understand it, dict() and tuple() accept iterators, e.g.

 {{{
 l = [1, 2, 3]
 tuple(v for v in l)
 }}}

 There are various calls to tuple() that place the iterator in a list
 comprehension before feeding the list to tuple(). For example in
 django/db/models/sql/query.py:

 {{{
 aliases = tuple([change_map.get(a, a) for a in aliases])
 }}}

 can be written as:

 {{{
 aliases = tuple(change_map.get(a, a) for a in aliases)
 }}}

 saving two characters, an extra iteration and list creation. The same goes
 for dict(), where dict([(v, v) for v in l]) can be rewritten as dict((v,
 v) for v in l).


 ----


 A shell command like this can replace the calls. When I tried it, I got
 lots of errors in the unit tests, but my impression was that that didn't
 have anything to do with the edit...

 {{{
 find django/ -name '*.py' -print0 \
     | xargs -n 200 -0 grep -l 'tuple([[][^]]\+[]])' \
     | while read F
 do
     cp "$F" "$F.orig"
     sed -e 's/tuple([[]\(.*\)[]])/tuple(\1)/' "$F.orig" > "$F"
 done
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/20989>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/070.de3e5693c7fb97c2fd5d89b07fd7e416%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to