Re: magical expanding hash

2006-01-18 Thread braver
Giovanni Bajo wrote, > dict.setdefault, as I already explained to you. I wonder about numerics too. Say we have a = None somewhere. I want to increment it, so I'd say a += 8. Now if this is a parsing app, the increment may happen everywhere -- so I'd write a function to do it if I worry about

Re: magical expanding hash

2006-01-18 Thread Giovanni Bajo
braver wrote: > Also, what's the shortest python idiom for get_or_set in expression? dict.setdefault, as I already explained to you. Again, I'd like to point out that what you're doing is *not* the correct Pythonic way of doing things. In Python, there is simply no implicit sub-dicts creation, n

Re: magical expanding hash

2006-01-18 Thread Steven D'Aprano
On Tue, 17 Jan 2006 18:00:00 -0700, Steven Bethard wrote: > Steve Holden wrote: >> Steven Bethard wrote: >>> Agreed. I really hope that Python 3.0 applies Raymond Hettinger's >>> suggestion "Improved default value logic for Dictionaries" from >>> http://wiki.python.org/moin/Python3%2e0Sugge

Re: magical expanding hash

2006-01-18 Thread Steve Holden
Steven Bethard wrote: > Steve Holden wrote: > >>Steven Bethard wrote: >> >>>Agreed. I really hope that Python 3.0 applies Raymond Hettinger's >>>suggestion "Improved default value logic for Dictionaries" from >>> http://wiki.python.org/moin/Python3%2e0Suggestions >>> >>>This would allow you

Re: magical expanding hash

2006-01-17 Thread James Stroud
braver wrote: > Thanks, James! This is really helpful. > > : It would take a lot of coding to make that << work right. Better is > the pythonic > : > : m[key] = [value] > : > : Its really only one more keystroke than > : > : m[key] << value > > But it's only for the first element, right? I'd ha

Re: magical expanding hash

2006-01-17 Thread Jean-Paul Calderone
On Tue, 17 Jan 2006 16:47:15 -0800, James Stroud <[EMAIL PROTECTED]> wrote: >braver wrote: >> Well, I know some python, but since there are powerful and magical >> features in it, I just wonder whether there're some which address this >> issue better than others. >> > >In python, += is short, of co

Re: magical expanding hash

2006-01-17 Thread braver
Thanks, James! This is really helpful. : It would take a lot of coding to make that << work right. Better is the pythonic : : m[key] = [value] : : Its really only one more keystroke than : : m[key] << value But it's only for the first element, right? I'd have to say meh[key1]...[keyN].append(el

Re: magical expanding hash

2006-01-17 Thread Steven Bethard
Steve Holden wrote: > Steven Bethard wrote: >> Agreed. I really hope that Python 3.0 applies Raymond Hettinger's >> suggestion "Improved default value logic for Dictionaries" from >> http://wiki.python.org/moin/Python3%2e0Suggestions >> >> This would allow you to make the setdefault() call o

Re: magical expanding hash

2006-01-17 Thread James Stroud
braver wrote: > Well, I know some python, but since there are powerful and magical > features in it, I just wonder whether there're some which address this > issue better than others. > In python, += is short, of course, for a = a + 1 But if we haven't already assigned a, how does the interpret

Re: magical expanding hash

2006-01-17 Thread braver
Well, I know some python, but since there are powerful and magical features in it, I just wonder whether there're some which address this issue better than others. -- http://mail.python.org/mailman/listinfo/python-list

Re: magical expanding hash

2006-01-17 Thread Paul Rubin
"braver" <[EMAIL PROTECTED]> writes: > Can assigning to hash without intermediate levels, possibly adding to a > numeric leaf or adding an element to a leaf array, be python code? > > h['a']['b']['c'] += 42 > > If it can, I'd like to have a class which supports it. Yes, it's simple enough to wri

Re: magical expanding hash

2006-01-17 Thread braver
Can assigning to hash without intermediate levels, possibly adding to a numeric leaf or adding an element to a leaf array, be python code? h['a']['b']['c'] += 42 If it can, I'd like to have a class which supports it. Is keeping a list at the leaf of a hash python code? h['a']['b']['c'].push(7)

Re: magical expanding hash

2006-01-17 Thread Fredrik Lundh
"braver" wrote: > The point of this exercise is to compare how either ruby or python can > implement perl's default behavior when dealing with hashes. Since > these are bread and butter of scripting, having a MEH class handy can > enable fast semantically equivalent translation. This can be > be

Re: magical expanding hash

2006-01-17 Thread braver
The point of this exercise is to compare how either ruby or python can implement perl's default behavior when dealing with hashes. Since these are bread and butter of scripting, having a MEH class handy can enable fast semantically equivalent translation. This can be beneficial for demonstrating

Re: magical expanding hash

2006-01-17 Thread Fredrik Lundh
"braver" wrote > Exactly, << as in C++/ruby streams. But notice the extra checks needed > to see whether we want a new leaf which is an array or a number, or we > create an intermediate hash level. Would the checks look the same in > python? we? trust me, the number of people who think it's a

Re: magical expanding hash

2006-01-17 Thread Paul Rubin
"braver" <[EMAIL PROTECTED]> writes: > Exactly, << as in C++/ruby streams. But notice the extra checks needed > to see whether we want a new leaf which is an array or a number, or we > create an intermediate hash level. Would the checks look the same in > python? You could check what is being sh

Re: magical expanding hash

2006-01-17 Thread Fredrik Lundh
"braver" wrote > Exactly, << as in C++/ruby streams. But notice the extra checks needed > to see whether we want a new leaf which is an array or a number, or we > create an intermediate hash level. Would the checks look the same in > python? we? trust me, the number of people who think it's a

Re: magical expanding hash

2006-01-17 Thread braver
Exactly, << as in C++/ruby streams. But notice the extra checks needed to see whether we want a new leaf which is an array or a number, or we create an intermediate hash level. Would the checks look the same in python? -- http://mail.python.org/mailman/listinfo/python-list

Re: magical expanding hash

2006-01-17 Thread Paul Rubin
"braver" <[EMAIL PROTECTED]> writes: > Actually, the behavior is important to translate perl into ruby. Can > it be implemented in python looking similarly? It's kind of bizarre in Python to use << as a mutation operator, but I guess you could do it. Sort of like 'cout << "hello world"' in C++.

Re: magical expanding hash

2006-01-17 Thread Steve Holden
Steven Bethard wrote: > Paul Rubin wrote: > >>Hmm, >> >> x[a][b][c][d] = e# x is a "magic" dict >> >>becomes >> >> x.setdefault(a,{}).setdefault(b,{}).setdefault(c,{})[d] = e >> >>if I understand correctly. Ugh. > > > Agreed. I really hope that Python 3.0 applies Raymond Hettinger's >

Re: magical expanding hash

2006-01-17 Thread braver
Actually, the behavior is important to translate perl into ruby. Can it be implemented in python looking similarly? -- http://mail.python.org/mailman/listinfo/python-list

Re: magical expanding hash

2006-01-17 Thread Paul Rubin
"braver" <[EMAIL PROTECTED]> writes: > Nice. What about pushing to leaves which are arrays, or incrementing > leaves which are numbers? If the array leaf didn't exist, or a number > wasn't set yet, << must create an empty array and push the element from > the RHS into it, and += must init the lea

Re: magical expanding hash

2006-01-17 Thread braver
Nice. What about pushing to leaves which are arrays, or incrementing leaves which are numbers? If the array leaf didn't exist, or a number wasn't set yet, << must create an empty array and push the element from the RHS into it, and += must init the leaf to 0 and add the RHS to it. Here's the corr

Re: magical expanding hash

2006-01-17 Thread Steven Bethard
Paul Rubin wrote: > Hmm, > >x[a][b][c][d] = e# x is a "magic" dict > > becomes > >x.setdefault(a,{}).setdefault(b,{}).setdefault(c,{})[d] = e > > if I understand correctly. Ugh. Agreed. I really hope that Python 3.0 applies Raymond Hettinger's suggestion "Improved default value

Re: magical expanding hash

2006-01-17 Thread Paul Rubin
"Diez B. Roggisch" <[EMAIL PROTECTED]> writes: > > BTW: remember that setdefault() is written "setdefault()" but it's read > > "getorset()". > > I can only second that. The misleading name has - well, mislead me :) Hmm, x[a][b][c][d] = e# x is a "magic" dict becomes x.setdefault(a,{}

Re: magical expanding hash

2006-01-17 Thread Diez B. Roggisch
> > BTW: remember that setdefault() is written "setdefault()" but it's read > "getorset()". I can only second that. The misleading name has - well, mislead me :) Regards, Diez -- http://mail.python.org/mailman/listinfo/python-list

Re: magical expanding hash

2006-01-17 Thread Giovanni Bajo
James Stroud wrote: >> I need a magical expanding hash with the following properties: >> >> * it creates all intermediate keys >> >> meh['foo']['bar] = 1 >> >> -- works even if meh['foo'] didn't exist before >> >

Re: magical expanding hash

2006-01-16 Thread Paul Rubin
"braver" <[EMAIL PROTECTED]> writes: > I need a magical expanding hash with the following properties: ... > I have such a class in ruby. Can python do that? Python's built-in dict objects don't do that but you could write such a class pretty straightforwardly. --

Re: magical expanding hash

2006-01-16 Thread James Stroud
braver wrote: > I need a magical expanding hash with the following properties: > > * it creates all intermediate keys > > meh['foo']['bar] = 1 > > -- works even if meh['foo'] didn't exist before > > * allows pushing new elements to leav

magical expanding hash

2006-01-16 Thread braver
I need a magical expanding hash with the following properties: * it creates all intermediate keys meh['foo']['bar] = 1 -- works even if meh['foo'] didn't exist before * allows pushing new elements to leaves which are arrays meh['foo']['list]