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://
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
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
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
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, '
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
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
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
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
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
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
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
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
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
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
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
>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
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
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
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
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
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
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
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
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(
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
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
...
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
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
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
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
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
32 matches
Mail list logo