Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #69756
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'interpreter': 0.05; 'none:': 0.07; 'stops': 0.07; 'function,': 0.09; 'logic': 0.09; 'obj': 0.09; 'pep': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'adjusted': 0.16; 'assigns': 0.16; 'collections': 0.16; 'defaultdict': 0.16; 'exist.': 0.16; 'fits': 0.16; 'objects.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subclass': 0.16; 'subject:dictionaries': 0.16; 'supplied': 0.16; 'task.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'basically': 0.19; 'meant': 0.20; 'example': 0.22; 'import': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'precise': 0.24; 'query': 0.26; '(for': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'record': 0.27; 'point': 0.28; 'function': 0.29; 'returned': 0.30; "i'm": 0.30; 'code': 0.31; 'keys': 0.31; 'object.': 0.31; 'class': 0.32; 'becomes': 0.33; 'skip:b 30': 0.33; 'skip:_ 10': 0.34; 'there,': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'there': 0.35; 'doing': 0.36; 'method': 0.36; 'example,': 0.37; 'needed': 0.38; 'to:addr:python-list': 0.38; 'track': 0.38; 'does': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'new': 0.61; 'name': 0.63; 'sum': 0.64; 'note:': 0.66; 'side': 0.67; 'frequently': 0.68; 'sales': 0.69; 'brand': 0.72; 'construction': 0.72; 'incoming': 0.72 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Keeping track of things with dictionaries |
| Date | Sun, 06 Apr 2014 10:23:38 +0200 |
| Organization | None |
| References | <534105ce$0$1365$4fafbaef@reader1.news.tin.it> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bdbe5f.dip0.t-ipconnect.de |
| User-Agent | KNode/4.11.5 |
| 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.8943.1396772636.18130.python-list@python.org> (permalink) |
| Lines | 62 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1396772636 news.xs4all.nl 2881 [2001:888:2000:d::a6]:43644 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:69756 |
Show key headers only | View raw
Giuliano Bertoletti wrote:
> I frequently use this pattern to keep track of incoming data (for
> example, to sum up sales of a specific brand):
>
> =====================================
>
> # read a brand record from a db
> ...
>
> # keep track of brands seen
> obj = brands_seen.get(brandname)
> if obj is None:
> obj = Brand()
> brands_seen[brandname] = obj
>
> obj.AddData(...) # this might for example keep track of sales
>
> =====================================
>
> as you might guess, brands_seen is a dictionary whose keys are
> brandnames and whose values are brand objects.
>
> Now the point is: is there a cleverer way to do this?
>
> Basically what I'm doing is query the dictionary twice if the object
> does not exist.
>
> What I would like to understand is if there's some language built-in
> logic to:
>
> - supply a function which is meant to return a new object
> - have the interpreter to locate the point in the dictionary where the
> key is to be
> - if the key is already there, it returns the value/object associated
> and stops
> - if the key is not there, it calls the supplied function, assigns the
> returned value to the dictionary and return the object.
Cudos, you give a precise discription of your problem in both english and
code.
There is a data structure in the stdlib that fits your task. With a
collections.defaultdict your code becomes
from collections import defaultdict
brands_seen = defaultdict(Brand)
brands_seen[brandname].add_data(...) # Method name adjusted to PEP 8
Side note: If you needed the key in the construction of the value you would
have to subclass
class BrandsSeen(dict):
def __missing__(self, brandname):
result = self[brandname] = Brand(brandname)
return result
brands_seen = BrandsSeen()
brands_seen[brandname].add_data(...)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Keeping track of things with dictionaries Giuliano Bertoletti <gbe32241@libero.it> - 2014-04-06 09:44 +0200
Re: Keeping track of things with dictionaries Peter Otten <__peter__@web.de> - 2014-04-06 10:23 +0200
Re: Keeping track of things with dictionaries Josh English <Joshua.R.English@gmail.com> - 2014-04-07 21:02 -0700
Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-08 14:08 +1000
Re: Keeping track of things with dictionaries Josh English <Joshua.R.English@gmail.com> - 2014-04-07 23:22 -0700
Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 09:14 +0200
Re: Keeping track of things with dictionaries Steven D'Aprano <steve@pearwood.info> - 2014-04-08 07:47 +0000
Re: Keeping track of things with dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-08 01:53 -0600
Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-08 18:00 +1000
Re: Keeping track of things with dictionaries Peter Otten <__peter__@web.de> - 2014-04-08 10:21 +0200
Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 10:26 +0200
Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 10:31 +0200
Re: Keeping track of things with dictionaries alex23 <wuwei23@gmail.com> - 2014-04-09 12:34 +1000
Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-08 18:35 +1000
Re: Keeping track of things with dictionaries Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-09 12:43 +1200
Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-09 12:33 +1000
Re: Keeping track of things with dictionaries alex23 <wuwei23@gmail.com> - 2014-04-09 12:45 +1000
Re: Keeping track of things with dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-08 21:19 -0600
Re: Keeping track of things with dictionaries Gene Heskett <gheskett@wdtv.com> - 2014-04-08 23:31 -0400
Re: Keeping track of things with dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-08 21:37 -0600
Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 11:28 +0200
Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-08 19:34 +1000
Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 11:41 +0200
Re: Keeping track of things with dictionaries Gene Heskett <gheskett@wdtv.com> - 2014-04-09 05:51 -0400
csiph-web