Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'assignment': 0.07; 'matches': 0.07; 'modified': 0.07; 'tool,': 0.07; 'get(self,': 0.09; 'key.': 0.09; 'keys,': 0.09; 'lookup': 0.09; 'override': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '*a,': 0.16; '1234': 0.16; 'count,': 0.16; 'diagnostic': 0.16; 'dict': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'increment': 0.16; 'item):': 0.16; 'iteration': 0.16; 'keys.': 0.16; 'lookups': 0.16; 'marker': 0.16; 'presume': 0.16; 'raised.': 0.16; 'repr': 0.16; 'retrieval,': 0.16; 'retrieving': 0.16; 'returned,': 0.16; 'self[k]': 0.16; 'stats': 0.16; 'subclass': 0.16; 'subject:class': 0.16; 'subject:failed': 0.16; 'subject:key': 0.16; 'tweak': 0.16; 'exception': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'retrieval': 0.19; 'seems': 0.21; '>>>': 0.22; 'handles': 0.22; 'cc:addr:python.org': 0.22; 'subject:like': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'returned': 0.30; 'start,': 0.30; 'then.': 0.30; 'message-id:@mail.gmail.com': 0.30; 'keyerror:': 0.31; 'keys': 0.31; 'subject:that': 0.31; 'class': 0.32; 'probably': 0.32; 'regular': 0.32; 'actual': 0.34; 'sense': 0.34; 'skip:_ 10': 0.34; "i'd": 0.34; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'should': 0.36; 'pm,': 0.38; 'track': 0.38; 'that,': 0.38; 'anything': 0.39; 'either': 0.39; 'tracking': 0.61; 'success': 0.61; 'simple': 0.61; "you're": 0.61; "you'll": 0.62; 'places': 0.64; 'submission': 0.64; 'total': 0.65; '30,': 0.65; 'default': 0.69; 'counts': 0.83; "'test'": 0.84; 'dict,': 0.84; 'dict.': 0.84; 'members:': 0.84; 'notably,': 0.84; 'items,': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Jk9HlCRWcgpy2cVviHGST2/q7a5V/LvUvIuumz6gkug=; b=pqLpLEGLVOWnwS1zbi4+CWAUgymW1A7KhUwMJFmn/P9GSOZjmAIBu5hcYvzOPVnOkD 8uXj5JVLm0tgxEhGCLWzXzHdfz4zrEqri8if81V6xSjnJUsIk3iYyRDCSUsSfLFCKmTT uWJMYb24vpum2uahNYSlNwrXcRpNLJSBMO5gf2VaGmyGQ9VdVnkD8mvWRCdZV1wPsZxr vnwD3914GGfmYPIFDGxg9CspRYzFjgRwyVFtD0OY55lv3xPwNmLM1HqPNPOcQR3xVyWf tST/EC8avv/9Yly+huslKsSoAQMJODI7zAV4TazOYf8ld6ArLROkyJDIPsBUXFjUDNO5 ZLqQ== MIME-Version: 1.0 X-Received: by 10.58.77.9 with SMTP id o9mr740087vew.69.1404138536563; Mon, 30 Jun 2014 07:28:56 -0700 (PDT) In-Reply-To: <1404135829.10485.136092537.2F0486D2@webmail.messagingengine.com> References: <1404135829.10485.136092537.2F0486D2@webmail.messagingengine.com> Date: Tue, 1 Jul 2014 00:28:56 +1000 Subject: Re: Creating a dict-like class that counts successful and failed key matches From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404138539 news.xs4all.nl 2970 [2001:888:2000:d::a6]:43930 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73749 On Mon, Jun 30, 2014 at 11:43 PM, wrote: > As a diagnostic tool, I would like to create a dict-like class that counts > successful and failed key matches by key. By failed I mean in the sense that > a default value was returned vs. an exception raised. By count, I mean by > tracking counts for individual keys vs. just total success/failure counts. > The class needs to support setting values, retrieving values, and retrieving > keys, items, and key/item pairs. Basically anything that a regular dict, I'd > like my modified class to do as well. Sounds like you want to subclass dict, then. Something like this: class StatsDict(dict): def __init__(self, *a, **ka): super().__init__(*a, **ka) self.success = defaultdict(int) self.fail = defaultdict(int) def __getitem__(self, item): try: ret = super().__getitem__(item) self.success[item] += 1 return ret except KeyError: self.fail[item] += 1 raise On initialization, set up some places for keeping track of stats. On item retrieval (I presume you're not also looking for stats on item assignment - for that, you'd want to also override __setitem__), increment either the success marker or the fail marker for that key, based exactly on what you say: was something returned, or was an exception raised. To get the stats, just look at the success and fail members: >>> d = StatsDict() >>> d["foo"]=1234 >>> d["foo"] 1234 >>> d["spam"] (chomp) KeyError: 'spam' >>> d["foo"] 1234 >>> d["foo"] 1234 >>> d["test"] (chomp) KeyError: 'test' >>> len(d.success) # Unique successful keys 1 >>> len(d.fail) # Unique failed keys 2 >>> sum(d.success.values()) # Total successful lookups 3 >>> sum(d.fail.values()) # Total unsuccessful lookups 2 You can also interrogate the actual defaultdicts, eg to find the hottest N keys. For everything other than simple key retrieval, this should function identically to a regular dict. Its repr will be a dict's repr, its iteration will be its keys, all its other methods will be available and won't be affected by this change. Notably, the .get() method isn't logged; if you use that and want to get stats for it, you'll have to reimplement it - something like this: def get(self, k, d=None): try: return self[k] except KeyError: return d The lookup self[k] handles the statisticking, but if you let this go through to the dict implementation of get(), it seems to ignore __getitem__. This probably isn't exactly what you want, but it's a start, at least, and something to tweak into submission :) ChrisA