Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Pavlos Parissis Newsgroups: comp.lang.python Subject: Re: subclassing collections.Counter Date: Tue, 15 Dec 2015 18:43:05 +0100 Lines: 84 Message-ID: References: <567036A5.6050205@gmail.com> <56703DE8.7010609@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="N0QOECxw3vGgQjmhTCp5K2dkofSoMdOoJ" X-Trace: news.uni-berlin.de FO0Whygwi5920b4peDEMSQqiYj1C0SfVdIGYozG5RDFA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'indicated': 0.07; 'method,': 0.07; 'override': 0.07; 'subject:skip:c 10': 0.07; 'any.': 0.09; 'dict': 0.09; 'highlight': 0.09; 'methods,': 0.09; 'metrics': 0.09; 'subclasses': 0.09; 'itself.': 0.11; 'pushed': 0.13; '9:20': 0.16; 'correctly,': 0.16; 'filename:fname piece:signature': 0.16; 'keys.': 0.16; 'magic': 0.16; 'received:192.168.0.103': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'url:py': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '>>>': 0.20; '2015': 0.20; 'saying': 0.22; 'class,': 0.22; 'doc': 0.22; 'maintains': 0.22; 'cheers,': 0.22; 'am,': 0.23; 'leave': 0.23; 'dec': 0.23; "haven't": 0.24; 'header :In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'change,': 0.27; 'function': 0.28; 'this.': 0.28; 'values': 0.28; 'container': 0.29; 'extending': 0.29; 'far,': 0.29; 'thus,': 0.29; '15,': 0.30; 'extend': 0.31; 'probably': 0.31; "can't": 0.32; 'class': 0.33; 'problem': 0.33; 'source': 0.33; 'url:python': 0.33; 'items.': 0.33; 'right?': 0.33; 'message- id:@gmail.com': 0.34; 'tue,': 0.34; 'received:google.com': 0.35; 'ones': 0.35; 'could': 0.35; 'returning': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'needed': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'method': 0.37; 'thanks': 0.37; 'suggestion': 0.37; 'thought': 0.37; 'hi,': 0.38; 'received:192': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'url:3': 0.60; 'your': 0.60; 'behavior': 0.61; "you'll": 0.61; 'real': 0.62; 'times': 0.63; 'act': 0.67; 'sum': 0.69; 'dict,': 0.84; 'url:cpython': 0.84; 'average.': 0.91; 'items,': 0.91; 'average': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; bh=4qx9IY5JUyGNTB0efyGaffTnYd/lxlqBeB1D1usCUL4=; b=j10DIs72nmC4LSNoJJeFu5f4n7aPD+4MscZ4fx8zwZNCotu54qfUdIGPZ2tduOvl+1 vnd/8sV4eO0YMNE02r0uzekfzTyT1wZZIJLBeKlVceS7X0T9SHa+1+bViPvpqoEV4gwF Y50JlBi5rLJaBdRhbMr/9ImAQ4TtCtWVqPul0AYaCT1Egx4oDgPsbuTdF1VlsHwqq0Cc 4xJdgECcRTTgcW2hSJmcSSN5U/EvHjyGO4K+4wOGM8F/OvSEFOxMULt2FgFc4ntxJj/T xhsfKOU/48J2I5D7AOYasKTMCHQ0KzUnzQOJ1QwP4qHyWYJobyPOvNOSVFrgEn5XeGKs +KYg== X-Received: by 10.194.79.201 with SMTP id l9mr51270338wjx.151.1450201389133; Tue, 15 Dec 2015 09:43:09 -0800 (PST) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100476 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --N0QOECxw3vGgQjmhTCp5K2dkofSoMdOoJ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 15/12/2015 06:22 =CE=BC=CE=BC, Ian Kelly wrote: > On Tue, Dec 15, 2015 at 9:20 AM, Pavlos Parissis > wrote: >> On 15/12/2015 05:11 =CE=BC=CE=BC, Ian Kelly wrote: >>> On Tue, Dec 15, 2015 at 8:49 AM, Pavlos Parissis >>> wrote: >>>> Hi, >>>> >>>> I need to store values for metrics and return the average for some >>>> and the sum for the rest. Thus, I thought I could extend >>>> collections.Counter class by returning averages for some keys. >>> >>> Leave Counter out of it, as this is not what it's designed for. Write= >>> a custom Metrics class, with each attribute being a pseudo-collection= >>> that maintains a sum or average. >>> >> >> But then I will have to override a lot of magic methods, right? >> What is the real problem of extending Counter in the way I did? >=20 > Only the ones that you have use for. So far, you haven't indicated > that you need any. All you said about your use case was "I need to > store values for metrics." >=20 I needed the update() to do the addition rather the override. > If you want your metrics container to act like a dict, then my > suggestion would be to just use a dict, with pseudo-collections for > the values as above. >=20 If I understood you correctly, you are saying store all metrics in a dict and have a counter key as well to store the times metrics are pushed in, and then have a function to do the math. Am I right? >> Calling items() over the object doesn't call __getitem__ and I can't >> find in the doc which method I need to change, any ideas? >=20 > You can find the source at > https://hg.python.org/cpython/file/3.5/Lib/collections/__init__.py. > Counter subclasses dict but doesn't override items, so if you want to > change the behavior of items then you'll probably have to override > items. >=20 I thought that items() calls a magic method, but it is actually a method itself. Thanks for highlight this. Cheers, Pavlos --N0QOECxw3vGgQjmhTCp5K2dkofSoMdOoJ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJWcFEpAAoJEIP8ktofcXa5WuIQAIkcZHq6MkXW7TH9ARIA+Ze7 VksriwF5bCAYoiR9dPvwCmCqTKqDArdyyFc48vev6RF1+UXd/dCX+upmemT/3s8W K4tDrrNNAHL2tzmQ6h5RN0sm1vrX/aE0++yPna0xvU3u+G149+dt2kZWBm1hlJot UFLkofuR2nkMV8MKrbZv+ipfqGscWXrSEk4YN1vcII0+PnMJ6A1o9MBDRcTNZTsE xdzduw4YzsKiJlnLUa0htf5TgwbSWPDNg1aqh/zzbzFPmtogjVICaB50x1rMI/I+ cguGaq9EHDYEcx5gESkiYy+hJ69tH1NkEDoFXG+bL3SeP9qwS62ykgtXOXlFqTn8 Ly7RZOLjzfTJqNi7oAz0oLvt1fY4+AFAdlRwlWpSbsPYpSba13FSNBTO0/364lxV l8JbvF4zmw/R+/NhELJAb5x1SP1if+DpEmmMDrQiitWnx5Nd+wskX8pY/7bVgNFV rY1cXcleE7MRk6LKWZOfTZJi3kfy+UYxBW6cx2dUAuqFo7ToPEJMbN1/xJR7wXbp guP9++/OOTYHWI4UrRDs1M6VY6tjQ+3s5dFqj0FSJcSrzbtl6dfuCPfkI2DY/9nw 1EGdZApKM8/RWF0898pOtB7IZRc4JLTPgrRff1BfuDJB0ok/d/uEvS+fF3wR8y6X w+6Dg4PkLAgPQ44ZFNV3 =nRS2 -----END PGP SIGNATURE----- --N0QOECxw3vGgQjmhTCp5K2dkofSoMdOoJ--