Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80278

Real-world use of Counter

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'affected': 0.07; 'attribute': 0.07; 'incompatible': 0.07; '[1,': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'handful': 0.09; 'message- id:@stoneleaf.us': 0.09; 'operand': 0.09; 'way:': 0.09; 'worse': 0.09; '~ethan~': 0.09; 'python': 0.11; 'behave': 0.16; 'collections': 0.16; 'counter()': 0.16; 'debugging,': 0.16; 'in- place': 0.16; 'instead:': 0.16; 'iterable': 0.16; 'maintainer': 0.16; 'operators.': 0.16; 'typeerror:': 0.16; "{'a':": 0.16; 'all.': 0.16; 'fix': 0.17; 'module': 0.19; '(the': 0.22; 'import': 0.22; '(in': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'specifically': 0.29; "i'm": 0.30; 'code': 0.31; 'minor': 0.31; 'raised': 0.31; 'anyone': 0.31; 'quite': 0.32; 'anybody': 0.35; 'in:': 0.36; 'up!': 0.36; 'so,': 0.37; 'to:addr:python-list': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'most': 0.60; 'received:173': 0.61; 'background:': 0.68; 'real- world': 0.68; 'bitten': 0.84; 'type(s)': 0.84
Date Wed, 05 Nov 2014 08:26:52 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1
MIME-Version 1.0
To Python <python-list@python.org>
Subject Real-world use of Counter
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15504.1415204817.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415204817 news.xs4all.nl 2884 [2001:888:2000:d::a6]:39769
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80278

Show key headers only | View raw


I'm looking for real-world uses of collections.Counter, specifically to see if anyone has been surprised by, or had to 
spend extra-time debugging, issues with the in-place operators.

Background:

Most Python data types will cause a TypeError to be raised if unusable types are passed in:

--> {'a': 0}.update(5)
TypeError: 'int' object is not iterable

--> [1, 2, 3].extend(3.14)
TypeError: 'float' object is not iterable

--> from collections import Counter
--> Counter() + [1, 2, 3]
TypeError: unsupported operand type(s) for +: 'Counter' and 'list'

Most Counter in-place methods also behave this way:

--> c /= [1, 2, 3]
TypeError: unsupported operand type(s) for /=: 'Counter' and 'list'

However, in the case of a handful of Counter in-place methods (+=, -=, &=, and |=), this is what happens instead:

--> c = Counter()
--> c += [1, 2, 3]
AttributeError: 'list' object has no attribute 'items'

Even worse (in my opinion) is the case of an empty Counter `and`ed with an incompatible type:

--> c &= [1, 2, 3]
-->

No error is raised at all.

In order to avoid unnecessary code churn (the fix itself is quite simple), the maintainer of the collections module 
wants to know if anybody has actually been affected by these inconsistencies, and if so, whether it was a minor 
inconvenience, or a compelling use-case.

So, if this has bitten you, now is the time to speak up!  :)

--
~Ethan~

Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Real-world use of Counter Ethan Furman <ethan@stoneleaf.us> - 2014-11-05 08:26 -0800
  Re: Real-world use of Counter Rustom Mody <rustompmody@gmail.com> - 2014-11-05 22:00 -0800
    Re: Real-world use of Counter Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-06 08:51 -0700
    Re: Real-world use of Counter Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-06 22:05 -0700

csiph-web