dsc / bunch

A Bunch is a Python dictionary that provides attribute-style access (a la JavaScript objects).
http://github.com/dsc/bunch
MIT License
482 stars 177 forks source link

Bunch of bunch from dict of dict #14

Closed s-celles closed 10 years ago

s-celles commented 10 years ago

Hello,

it will be nice if Bunch could recursively build a bunch of bunch from dict of dict.

Example:

from bunch import Bunch

old_bunch = Bunch({"a": 0, "b": {"b1": 1, "b2": 2}})

returns

Bunch(a=0, b={"b1": 1, "b2": 2})

it will be nice if it could returns

new_bunch = Bunch(a=0, b=Bunch(b1=1, b2=2))

Because accessing b2 sub key can be done using:

old_bunch.b["b2"]

recursively contructed Bunch could give access to b2 sub key using:

new_bunch.b.b2

Kind regards

dsc commented 10 years ago

That's what bunchify is for.

>>> from bunch import bunchify
>>> bb = bunchify({"a": 0, "b": {"b1": 1, "b2": 2}})
>>> print bb.toYAML()
a: 0
b:
    b1: 1
    b2: 2
dsc commented 10 years ago

(I guess I really ought to generate the docs from the source, as I see the source of your confusion. It's not mentioned anywhere!)

https://github.com/dsc/bunch/blob/master/bunch/__init__.py#L224