Re: changing local namespace of a function

2005-02-06 Thread Kent Johnson
Bo Peng wrote: Kent Johnson wrote: You are still including the compile overhead in fun2. If you want to see how fast the compiled code is you should take the definition of myfun out of fun2: I assumed that most of the time will be spent on N times execution of myfunc. Doh! Right. Kent -- http://

Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Kent Johnson wrote: Bo Peng wrote: Exec is slow since compiling the string and calls to globals() use a lot of time. The last one is most elegant but __getattr__ and __setattr__ are costly. The 'evil hack' solution is good since accessing x and y takes no additional time. Previous comparison w

Re: changing local namespace of a function

2005-02-05 Thread Kent Johnson
Bo Peng wrote: Exec is slow since compiling the string and calls to globals() use a lot of time. The last one is most elegant but __getattr__ and __setattr__ are costly. The 'evil hack' solution is good since accessing x and y takes no additional time. Previous comparison was not completely fa

Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Exec is slow since compiling the string and calls to globals() use a lot of time. The last one is most elegant but __getattr__ and __setattr__ are costly. The 'evil hack' solution is good since accessing x and y takes no additional time. Previous comparison was not completely fair since I could

Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Kent Johnson wrote: You can part way there using keyword arguments. You just have to use dictionary syntax for changing values in the dictionary: >>> def f(d, x=None, y=None): ... d['z'] = x + y ... >>> a = {'x':1, 'y':2} >>> b = {'x':3, 'y':3} >>> >>> f(a, **a) >>> a {'y': 2, 'x': 1, '

Re: changing local namespace of a function

2005-02-05 Thread Michael Spencer
Alex Martelli wrote: Hmmm, you do realize that wrapdict uses a lot of indirection while my equivalent approach, just posted, is very direct, right? To reiterate the latter, and dress it up nicely too, it's class wrapwell(object): def __init__(self, somedict): self.__dict__ = somedict B

Re: changing local namespace of a function

2005-02-05 Thread Nick Coghlan
Something I forgot to mention. . . Bo Peng wrote: You know, I have a deep root in C/C++ so performance is the king and hacking is part of my daily life. Time to change now. :) The entire design of C++ is in many ways a regrettable monument to the idea that premature optimisation is evil - far too

Re: changing local namespace of a function

2005-02-05 Thread Nick Coghlan
Bo Peng wrote: I can not say enough thank you for this. Don't thank me, thank Guido. He created the property machinery - I just let you know it was there :) But yes, Python's OO is OO the way it should be - something that helps you get the job done quickly and cleanly, rather than making you jum

Re: changing local namespace of a function

2005-02-05 Thread Kent Johnson
Bo Peng wrote: Yes. I thought of using exec or eval. If there are a dozen statements, def fun(d): exec 'z = x + y' in globals(), d seems to be more readable than def fun(d): d['z'] = d['x'] + d['y'] But how severe will the performance penalty be? You can precompile the string using compile(), y

Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Nick Coghlan wrote: If you want to add more calculated properties to the data manipulator, simply define additional calculator methods, and define the attribute with make_prop. This has became really appealing You know, I have a deep root in C/C++ so performance is the king and hacking is pa

Re: changing local namespace of a function

2005-02-05 Thread Kent Johnson
Bo Peng wrote: Dear list, I have many dictionaries with the same set of keys and I would like to write a function to calculate something based on these values. For example, I have a = {'x':1, 'y':2} b = {'x':3, 'y':3} def fun(dict): dict['z'] = dict['x'] + dict['y'] fun(a) and fun(b) will set

Re: changing local namespace of a function

2005-02-05 Thread Nick Coghlan
Alex Martelli wrote: Bo Peng <[EMAIL PROTECTED]> wrote: ... Thank again for everyone's help. I have learned a lot from the posts, especially the wrapdict class. Hmmm, you do realize that wrapdict uses a lot of indirection while my equivalent approach, just posted, is very direct, right? To rei

Re: changing local namespace of a function

2005-02-05 Thread Alex Martelli
Bo Peng <[EMAIL PROTECTED]> wrote: > M.E.Farmer wrote: > > I really don't see your need. > > Maybe it is just my laziness. It is almost intolerable for me to write > lines and lines of code like > >d['z'] = func(d['x']+d['y']+d['whatever']['as']+d[a][0] ) > > It is ugly, unreadable and err

Re: changing local namespace of a function

2005-02-05 Thread Alex Martelli
Bo Peng <[EMAIL PROTECTED]> wrote: ... > Thank again for everyone's help. I have learned a lot from the posts, > especially the wrapdict class. Hmmm, you do realize that wrapdict uses a lot of indirection while my equivalent approach, just posted, is very direct, right? To reiterate the latter

Re: changing local namespace of a function

2005-02-05 Thread Nick Coghlan
Bo Peng wrote: I guess I will go with solution 3. It is evil but it is most close to my original intention. It leads to most readable code (except for the first line to do the magic and the last line to return result) and fastest performance. Thousands of programs use Python's class attribute ac

Re: changing local namespace of a function

2005-02-05 Thread Nick Coghlan
Bo Peng wrote: By the way, will 'with statement', like the one in pascal and many other languages, be a good addition to python? For example, with d do: z = x + y would be equivalent to d['z']=d['x']+d['y'] or d.z = d.x + d.y in some other cases. This would absolutely be the *best* solution t

Re: changing local namespace of a function

2005-02-04 Thread M.E.Farmer
>It is ugly, unreadable and error prone. If I have to use this code, I >would write > _z = func(_x + _y + _whatever['as'] + _a[0]) >and use a perl script to generate the real code. (See, I am not lazy :-) Ok laziness is an acceptable answer ;) This is starting to make sense , you've been reading

Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
Maybe it is just my laziness. It is almost intolerable for me to write lines and lines of code like d['z'] = func(d['x']+d['y']+d['whatever']['as']+d[a][0] ) By the way, will 'with statement', like the one in pascal and many other languages, be a good addition to python? For example, with d d

Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
M.E.Farmer wrote: I really don't see your need. Maybe it is just my laziness. It is almost intolerable for me to write lines and lines of code like d['z'] = func(d['x']+d['y']+d['whatever']['as']+d[a][0] ) It is ugly, unreadable and error prone. If I have to use this code, I would write _z

Re: changing local namespace of a function

2005-02-04 Thread M.E.Farmer
I quote myself : > Also try this stuff out in an interpreter session it is easy and fast > to get your own answers. Sorry I guess I should have added a nudge and a <.5 wink> at the end. Sometimes misinformation is just what we need to get our own answers! The absurdity of what you are doing led me

Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
Thank all for your suggestions. I have tried all methods and compared their performance. >>> import profile >>> a = {'x':1, 'y':2} >>> N = 10 >>> # solution one: use dictionary directly ... def fun1(d): ... for i in xrange(0,N): ... d['z'] = d['x'] + d['y'] ... >>> # solution two: use e

Re: changing local namespace of a function

2005-02-04 Thread Michael Spencer
Nick Coghlan wrote: Michael Spencer wrote: def fun(dict): # set dict as local namespace # locals() = dict? z = x + y As you no doubt have discovered from the docs and this group, that isn't doable with CPython. Not entirely impossible: Py> def f(d): ... exec "locals().update(d)" ... re

Re: changing local namespace of a function

2005-02-04 Thread Nick Coghlan
Bo Peng wrote: Jeff Shannon wrote: This sounds to me like you're trying to re-implement object orientation. I have no control over the big dictionaries. All I need to do is processing them in situ --- that is to say, go into each map and manipulate numbers. Parameter passing should be avoid whene

Re: changing local namespace of a function

2005-02-04 Thread Nick Coghlan
Michael Spencer wrote: def fun(dict): # set dict as local namespace # locals() = dict? z = x + y As you no doubt have discovered from the docs and this group, that isn't doable with CPython. Not entirely impossible: Py> def f(d): ... exec "locals().update(d)" ... return x + y ... Py> f(d

Re: changing local namespace of a function

2005-02-04 Thread Michael Spencer
Bo Peng wrote: Michael Spencer wrote: > There are hundreds of items in the dictionary (that will be needed in the calculation) so passing the whole dictionary is a lot better than passing individual items. ... def fun(d): exec 'z = x + y' in globals(), d seems to be more readable than def fun(

Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
Jeff Shannon wrote: This sounds to me like you're trying to re-implement object orientation. I have no control over the big dictionaries. All I need to do is processing them in situ --- that is to say, go into each map and manipulate numbers. Parameter passing should be avoid whenever possible s

Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
Michael Spencer wrote: As you no doubt have discovered from the docs and this group, that isn't doable with CPython. Too bad to know this. >>> a = {'x':1, 'y':2} >>> b = {'x':3, 'y':3} ... >>> def funa(x,y, **kw): ... del kw #Careful of unwanted names in locals with this approach ...

Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
M.E.Farmer wrote: def fun(d): ... __dict__ = d ... return __dict__ hth, Does not work? >>> a = { 'x':1, 'y':2} >>> b = { 'x':2, 'y':9} >>> def fun(d): ... __dict__ = d ... print locals() ... z = x + y >>> fun(a) {'__dict__': {'y': 2, 'x': 1}, 'd': {'y': 2, 'x': 1}} Traceback (most re

Re: changing local namespace of a function

2005-02-04 Thread Jeff Shannon
Bo Peng wrote: My function and dictionaries are a lot more complicated than these so I would like to set dict as the default namespace of fun. This sounds to me like you're trying to re-implement object orientation. Turn all of those functions into methods on a class, and instead of creating dic

Re: changing local namespace of a function

2005-02-04 Thread Michael Spencer
Bo Peng wrote: Dear list, I have many dictionaries with the same set of keys and I would like to write a function to calculate something based on these values. For example, I have a = {'x':1, 'y':2} b = {'x':3, 'y':3} def fun(dict): dict['z'] = dict['x'] + dict['y'] fun(a) and fun(b) will set

Re: changing local namespace of a function

2005-02-04 Thread M.E.Farmer
Hello Bo, Don't use dict it is a builtin ;) Also try this stuff out in an interpreter session it is easy and fast to get your own answers. >>> def fun(d): ... __dict__ = d ... return __dict__ hth, M.E.Farmer -- http://mail.python.org/mailman/listinfo/python-list

changing local namespace of a function

2005-02-04 Thread Bo Peng
Dear list, I have many dictionaries with the same set of keys and I would like to write a function to calculate something based on these values. For example, I have a = {'x':1, 'y':2} b = {'x':3, 'y':3} def fun(dict): dict['z'] = dict['x'] + dict['y'] fun(a) and fun(b) will set z in each dicti