
Hi all, this seems like the right forum to ask -- is there a reason why Python doesn't have a 'bag' builtin type, e.g. b = bag(foo=bar, baz=bif) assert b.foo == bar assert b.bz == bif ? It's easy to write and there's a great discussion over at the cookbook: http://code.activestate.com/recipes/259174-bag-collection-class/ Inquiring minds want to know! thanks, --titus -- C. Titus Brown, ctb@msu.edu

On Thu, Apr 15, 2010 at 12:02 AM, C. Titus Brown <ctb@msu.edu> wrote:
this seems like the right forum to ask -- is there a reason why Python doesn't have a 'bag' builtin type, e.g.
Python 2.7 and 3.1 have a Counter type, that is similar to (but not identical to) that recipe: http://docs.python.org/py3k/library/collections.html#collections.Counter -- Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>

On Thu, Apr 15, 2010 at 12:08:26AM -0500, Daniel Stutzbach wrote:
On Thu, Apr 15, 2010 at 12:02 AM, C. Titus Brown <ctb@msu.edu> wrote:
this seems like the right forum to ask -- is there a reason why Python doesn't have a 'bag' builtin type, e.g.
Python 2.7 and 3.1 have a Counter type, that is similar to (but not identical to) that recipe:
http://docs.python.org/py3k/library/collections.html#collections.Counter
Huh, seems like a different use case from mine -- I just would like to be able to refer to dictionary keys as attributes. So I guess the cookbook recipe distracted you from my real interest, which is the short notation: b = bag(foo=bar, bif=baz) assert b.foo == bar assert b.bif == baz Still curious :) thanks, --t -- C. Titus Brown, ctb@msu.edu

On Wed, Apr 14, 2010 at 10:12 PM, C. Titus Brown <ctb@msu.edu> wrote:
On Thu, Apr 15, 2010 at 12:08:26AM -0500, Daniel Stutzbach wrote:
On Thu, Apr 15, 2010 at 12:02 AM, C. Titus Brown <ctb@msu.edu> wrote:
this seems like the right forum to ask -- is there a reason why Python doesn't have a 'bag' builtin type, e.g.
Python 2.7 and 3.1 have a Counter type, that is similar to (but not identical to) that recipe:
http://docs.python.org/py3k/library/collections.html#collections.Counter
Huh, seems like a different use case from mine -- I just would like to be able to refer to dictionary keys as attributes. So I guess the cookbook recipe distracted you from my real interest, which is the short notation:
b = bag(foo=bar, bif=baz)
assert b.foo == bar assert b.bif == baz
Still curious :)
Also, that's not a bag at all. Seems you're looking for something similar to namedtuple: http://docs.python.org/dev/library/collections.html#collections.namedtuple For the record, a bag is an unordered collection that permits duplicate elements. Sort of like an unordered, unindexable list, or a set that allows duplicate elements. It's got nothing whatsoever to do with being able to access elements using attribute syntax. Cheers, Chris -- http://blog.rebertia.com

On Wed, Apr 14, 2010 at 10:20:51PM -0700, Chris Rebert wrote:
On Wed, Apr 14, 2010 at 10:12 PM, C. Titus Brown <ctb@msu.edu> wrote:
On Thu, Apr 15, 2010 at 12:08:26AM -0500, Daniel Stutzbach wrote:
On Thu, Apr 15, 2010 at 12:02 AM, C. Titus Brown <ctb@msu.edu> wrote:
this seems like the right forum to ask -- is there a reason why Python doesn't have a 'bag' builtin type, e.g.
Python 2.7 and 3.1 have a Counter type, that is similar to (but not identical to) that recipe:
http://docs.python.org/py3k/library/collections.html#collections.Counter
Huh, seems like a different use case from mine -- I just would like to be able to refer to dictionary keys as attributes. ??So I guess the cookbook recipe distracted you from my real interest, which is the short notation:
?? b = bag(foo=bar, bif=baz)
?? assert b.foo == bar ?? assert b.bif == baz
Still curious :)
Also, that's not a bag at all. Seems you're looking for something similar to namedtuple: http://docs.python.org/dev/library/collections.html#collections.namedtuple
For the record, a bag is an unordered collection that permits duplicate elements. Sort of like an unordered, unindexable list, or a set that allows duplicate elements. It's got nothing whatsoever to do with being able to access elements using attribute syntax.
Thanks! --titus -- C. Titus Brown, ctb@msu.edu

The example you give is a dict aside from syntax. What makes it bag-like? Do you expect that I can write b = bag(foo=bar, foo=car) But I'd hardly expect b.foo == bar or b.foo == car as you suggest. --- Bruce http://www.vroospeak.com On Wed, Apr 14, 2010 at 10:12 PM, C. Titus Brown <ctb@msu.edu> wrote:
On Thu, Apr 15, 2010 at 12:08:26AM -0500, Daniel Stutzbach wrote:
On Thu, Apr 15, 2010 at 12:02 AM, C. Titus Brown <ctb@msu.edu> wrote:
this seems like the right forum to ask -- is there a reason why Python doesn't have a 'bag' builtin type, e.g.
Python 2.7 and 3.1 have a Counter type, that is similar to (but not identical to) that recipe:
http://docs.python.org/py3k/library/collections.html#collections.Counter
Huh, seems like a different use case from mine -- I just would like to be able to refer to dictionary keys as attributes. So I guess the cookbook recipe distracted you from my real interest, which is the short notation:
b = bag(foo=bar, bif=baz)
assert b.foo == bar assert b.bif == baz
Still curious :)
thanks, --t -- C. Titus Brown, ctb@msu.edu _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

On Wed, Apr 14, 2010 at 10:22:13PM -0700, Bruce Leban wrote:
The example you give is a dict aside from syntax. What makes it bag-like? Do you expect that I can write
b = bag(foo=bar, foo=car)
But I'd hardly expect b.foo == bar or b.foo == car as you suggest.
I guess I don't understand what a bag is -- I'd just heard this structure referred to as such. "dict aside from syntax" is the point, yes! thanks, --titus

On Wed, 14 Apr 2010 22:30:38 -0700 "C. Titus Brown" <ctb@msu.edu> wrote:
I guess I don't understand what a bag is -- I'd just heard this structure referred to as such.
"dict aside from syntax" is the point, yes!
Then what you need is an interface transforming pseudo-keys into attr names. (I do that often, esp. for config objects read from text files in which the user only specifies differences from default -- then I can access config elements using object syntax -- seems to be similar to your expectations). Just use setattr / getattr to build a generic class for such needs. It makes a flexible type for data "units" (composite objects); flexible in that the attr names are data themselves. Denis ________________________________ vit esse estrany ☣ spir.wikidot.com

On Wed, Apr 14, 2010 at 10:12:38PM -0700, C. Titus Brown wrote:
I just would like to be able to refer to dictionary keys as attributes. So I guess the cookbook recipe distracted you from my real interest, which is the short notation:
b = bag(foo=bar, bif=baz)
assert b.foo == bar assert b.bif == baz
See class DictRecord at http://ppa.cvs.sourceforge.net/viewvc/*checkout*/ppa/QPS/qps/qUtils.py License: Python. Oleg. -- Oleg Broytman http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.

On Wed, Apr 14, 2010 at 10:02 PM, C. Titus Brown <ctb@msu.edu> wrote:
Hi all,
this seems like the right forum to ask -- is there a reason why Python doesn't have a 'bag' builtin type, e.g.
b = bag(foo=bar, baz=bif)
assert b.foo == bar assert b.bz == bif
?
It's easy to write and there's a great discussion over at the cookbook:
http://code.activestate.com/recipes/259174-bag-collection-class/
Inquiring minds want to know!
There was some discussion: http://mail.python.org/pipermail/python-ideas/2009-July/005219.html http://www.mail-archive.com/python-list@python.org/msg229512.html ...the outcome of which was the Counter class: http://docs.python.org/dev/py3k/library/collections.html#collections.Counter It's /somewhat/ like a bag, except elements can have negative multiplicity and the default iterator yields each element exactly once, regardless of its multiplicity (yeah, I know, doesn't quite make sense). There have been attempts to also add a proper bag/multiset type, but the use cases for bags are relatively few and collections.Counter is usually close enough that it can be used in such cases, although not as elegantly. Cheers, Chris -- http://blog.rebertia.com

On Apr 15, 3:02 pm, "C. Titus Brown" <c...@msu.edu> wrote:
b = bag(foo=bar, baz=bif)
assert b.foo == bar assert b.bz == bif
I've always known this as a 'bunch' after Alex Martelli's recipe[1]. It's handy, but at 3 lines of code not an urgent candidate (IMO) for stdlib: class Bunch: def __init__(self, **kwds): self.__dict__.update(kwds) [1]: http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-...

On Wed, Apr 14, 2010 at 11:02:54PM -0700, alex23 wrote:
On Apr 15, 3:02?pm, "C. Titus Brown" <c...@msu.edu> wrote:
? ?b = bag(foo=bar, baz=bif)
? ?assert b.foo == bar ? ?assert b.bz == bif
I've always known this as a 'bunch' after Alex Martelli's recipe[1]. It's handy, but at 3 lines of code not an urgent candidate (IMO) for stdlib:
class Bunch: def __init__(self, **kwds): self.__dict__.update(kwds)
[1]: http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-...
Well, yes -- but 'sets' weren't exactly rocket science either, and I find them really useful, too :). --titus -- C. Titus Brown, ctb@msu.edu

On 15 April 2010 08:02, alex23 <wuwei23@gmail.com> wrote:
On Apr 15, 3:02 pm, "C. Titus Brown" <c...@msu.edu> wrote:
b = bag(foo=bar, baz=bif)
assert b.foo == bar assert b.bz == bif
I've always known this as a 'bunch' after Alex Martelli's recipe[1]. It's handy, but at 3 lines of code not an urgent candidate (IMO) for stdlib:
class Bunch: def __init__(self, **kwds): self.__dict__.update(kwds)
Yeah, we've used this class a tonne - especially for testing where you just need a quick object with a bunch of attributes. I think we added a repr as well. It would certainly be a nice harmless addition to the collections module. Michael
[1]: http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-... _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

On Thu, Apr 15, 2010 at 1:43 PM, Michael Foord <fuzzyman@gmail.com> wrote:
On 15 April 2010 08:02, alex23 <wuwei23@gmail.com> wrote:
On Apr 15, 3:02 pm, "C. Titus Brown" <c...@msu.edu> wrote:
b = bag(foo=bar, baz=bif)
assert b.foo == bar assert b.bz == bif
I've always known this as a 'bunch' after Alex Martelli's recipe[1]. It's handy, but at 3 lines of code not an urgent candidate (IMO) for stdlib:
class Bunch: def __init__(self, **kwds): self.__dict__.update(kwds)
Yeah, we've used this class a tonne - especially for testing where you just need a quick object with a bunch of attributes. I think we added a repr as well. It would certainly be a nice harmless addition to the collections module.
Michael
In the past I've found useful a hybrid of the 'bunch' and the namedtuple[1]. George [1] http://code.activestate.com/recipes/576555-records/

C. Titus Brown wrote:
Hi all,
this seems like the right forum to ask -- is there a reason why Python doesn't have a 'bag' builtin type, e.g.
b = bag(foo=bar, baz=bif)
assert b.foo == bar assert b.bz == bif
?
That's what's normally called a "namespace" in Python-land and can easily be had via a regular instance: class Namespace: def __init__(self, **kws): self.__dict__.update(kws) b = Namespace(foo=bar, baz=bif) assert b.foo == bar assert b.bz == bif For a more elaborate example of a namespace class, see mx.Misc.Namespace from our eGenix mx Base package: http://www.egenix.com/products/python/mxBase/ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Apr 15 2010)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/

On 15 Apr 2010, at 09:31 , M.-A. Lemburg wrote:
C. Titus Brown wrote:
Hi all,
this seems like the right forum to ask -- is there a reason why Python doesn't have a 'bag' builtin type, e.g.
b = bag(foo=bar, baz=bif)
assert b.foo == bar assert b.bz == bif
?
That's what's normally called a "namespace" in Python-land and can easily be had via a regular instance:
class Namespace: def __init__(self, **kws): self.__dict__.update(kws)
b = Namespace(foo=bar, baz=bif)
An other way is to inherit from dict and just forward __getattr__ and __setattr__ to __getitem__ and __setitem__ (with exception conversion maybe), that way both accesses are possible, and you get all the dicty goodness for free (dict merging with both arg and **kwargs constructor arguments, iterations, string representation, `dict.update`, …) class NS(dict): def __getattr__(self, key): return self.__getitem__(key) def __setattr__(self, key, value): return self.__setitem__(key, value) def __delattr__(self, key): return self.__delitem__(key) (note: that one probably breaks `hasattr` and `getattr` given KeyError isn't converted to AttributeError, but that should not take many lines)
participants (11)
-
alex23
-
Bruce Leban
-
C. Titus Brown
-
Chris Rebert
-
Daniel Stutzbach
-
George Sakkis
-
M.-A. Lemburg
-
Masklinn
-
Michael Foord
-
Oleg Broytman
-
spir ☣