473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Replacement for locals() ???

I use locals() for conveniently "filling out" SQL-Statements:

[snip]

def updData(id, surename):
sql="""update fisch set surename=%(surename)s where id=%(id)s"""
cs.execute(sql, locals())
[/snip]

that works great and is well within the definition of the python
db-api and is supported by pyPgSQL ... everything seems fine.

UNTIL

I read within psysco-FAQ and Jim Hugoninis IronPython paper: a lot of
words about "locals() being very difficult or impossible to optimize"

AND UNTIL

so ... is there any convenient replacement? Of course I could do

{"id":id, "surename":surename} , but that would be very very
in-elegant.
Harald
Jul 18 '05 #1
6 1486

Harald> I read within psysco-FAQ and Jim Hugoninis IronPython paper: a
Harald> lot of words about "locals() being very difficult or impossible
Harald> to optimize"

Harald> AND UNTIL

Harald> so ... is there any convenient replacement? Of course I could do

Harald> {"id":id, "surename":surename} , but that would be very very
Harald> in-elegant.

That's the only solution I've found so far. Makes for rather ugly code. If
you have to choose, what do you want, fast or pretty? <wink>

Skip
Jul 18 '05 #2
Skip Montanaro wrote:
Harald> I read within psysco-FAQ and Jim Hugoninis IronPython paper: a
Harald> lot of words about "locals() being very difficult or impossible
Harald> to optimize"

Harald> AND UNTIL

Harald> so ... is there any convenient replacement? Of course I could do

Harald> {"id":id, "surename":surename} , but that would be very very
Harald> in-elegant.

That's the only solution I've found so far. Makes for rather ugly code. If
you have to choose, what do you want, fast or pretty? <wink>


You can have both: fast and pretty.
That's why I got interested in Bytecodehacks:
I can write locals(), but transform that into
the explicit from by introspection, and replace
the function before I use it.
(Haven't written an un-local()iser yet, but it
seems straight forward).

ciao - chris

--
Christian Tismer :^) <mailto:ti****@stackless.com>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/

Jul 18 '05 #3

"Harald Massa" <cp*********@spamgourmet.com> wrote in message
news:Xn********************************@195.20.224 .116...
So two of the greatest PythonBrains have problems in optimizing Python
because of locals(), the most common usage of Python in my practice is
peripheral; so just by way of precaution I am looking for a replacement.


I suspect you are very prematurely optimizing. Since you are filling out
SQL statements, I would expect that your bottlenecks are and will be the
execution of the statements, and not the construction of the statements.

tjr

Jul 18 '05 #4
>> So two of the greatest PythonBrains have problems in optimizing
Python because of locals(), the most common usage of Python in my
practice is peripheral; so just by way of precaution I am looking for
a replacement.


Terry> I suspect you are very prematurely optimizing. Since you are
Terry> filling out SQL statements, I would expect that your bottlenecks
Terry> are and will be the execution of the statements, and not the
Terry> construction of the statements.

Perhaps he is, however I think the presence of locals() in a function
prevents one from using Psyco on that program if the function is executed.
It may even (I don't recall off the top of my head and don't have immediate
access to Intel hdwe to try it on) crash the program.

Skip
Jul 18 '05 #5
Terry,

So two of the greatest PythonBrains have problems in optimizing
Python because of locals(), the most common usage of Python in my
practice is peripheral; so just by way of precaution I am looking for
a
r eplacement.


I suspect you are very prematurely optimizing. Since you are filling
out SQL statements, I would expect that your bottlenecks are and will
be the execution of the statements, and not the construction of the
statements.


you are partially right. I do not have ANY performance problems in that
Python code. Most of the time the program is either waiting for a
COM-Server, a database-Server or user-typing.

As Skip stated: using locals() prevents the EASY use of psyco.
(import psyco; psyco full
Forcing me to manually bind psyco to functions ... etc.

also in standard lib "encodings" locals() is used.

I know there may be some exotic usages of locals(), but most use is
really straightforward string substitution & parameterparsing; and
thatfor I am asking: "How can I make it easy to psyco / ironpython /
PyPy???/ parrot to optimize my code" ...

Best wishes,

Harald

Jul 18 '05 #6
Harald Massa wrote:
Terry,
So two of the greatest PythonBrains have problems in optimizing
Python because of locals(), the most common usage of Python in my
practice is peripheral; so just by way of precaution I am looking for
a
r eplacement.


I suspect you are very prematurely optimizing. Since you are filling
out SQL statements, I would expect that your bottlenecks are and will
be the execution of the statements, and not the construction of the
statements.

you are partially right. I do not have ANY performance problems in that
Python code. Most of the time the program is either waiting for a
COM-Server, a database-Server or user-typing.

As Skip stated: using locals() prevents the EASY use of psyco.
(import psyco; psyco full


Ok, I see. Note that the EASY use is almost always much slower
than a partial, controlled usage of Psyco, for non-trivial
applications.

The bad thing about locals() is that it simply goes wrong
with Psyco. I think it would be nicer if psyco would
simply refuse optimization in this case and leave things intact.

There are some constructs which just disable Psyco: If the
code contains certain opcodes, Psyco will not touch the whole
function. Examples are:

- generators: an existing yield disables Psyco for the func
- usage of nested scopes.

On the scopes case: If you have a construct like this:

def outer():
something = 42
def inner():
print something
inner()

Then both outer and inner will contain opcodes which disable
Psyco from touching things at all.
So if you can manage to inject such a construct, your locals
will be fine with Psyco.full().

Anyway, I think we should hint Armin (the other one) to
change the locals behavior to disable Psyco.

ciao - chris

--
Christian Tismer :^) <mailto:ti****@stackless.com>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Giles Brown | last post by:
I do not understand why the following code produces NameError: name 'FirstClass' is not defined when both a global and local dict are passed into exec, but not when only a global dict is passed...
15
by: Paul Paterson | last post by:
I am trying to find a way to mimic by-reference argument passing for immutables in Python. I need to do this because I am writing an automated VB to Python converter. Here's an example of the VB...
2
by: tedsuzman | last post by:
----- def f(): ret = 2 exec "ret += 10" return ret print f() ----- The above prints '12', as expected. However,
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
1
by: Paolo Pantaleo | last post by:
Hi this exaple: def lcl(): n=1 x=locals() x=100 print "n in lcl() is:" +str(n) #This will say Name error
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
2
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I want to give a user the possibility of "restarting" an interactive session, by removing all the objects defined by her since the beginning. The way I make this possible is by having a "function"...
6
by: John [H2O] | last post by:
I would like to write a function to write variables to a file and modify a few 'counters'. This is to replace multiple instances of identical code in a module I am writing. This is my approach:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.