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


Groups > comp.lang.python > #69756

Re: Keeping track of things with dictionaries

From Peter Otten <__peter__@web.de>
Subject Re: Keeping track of things with dictionaries
Date 2014-04-06 10:23 +0200
Organization None
References <534105ce$0$1365$4fafbaef@reader1.news.tin.it>
Newsgroups comp.lang.python
Message-ID <mailman.8943.1396772636.18130.python-list@python.org> (permalink)

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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