Friday, December 18, 2009

comp.lang.python - 24 new messages in 11 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* Line indexing in Python - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/35abd4b45d347cc2?hl=en
* Design question about pretree classifier - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/24875d55431e0caf?hl=en
* imports in __init__.py - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/e90896c5b607904a?hl=en
* iterators and views of lists - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/22d674ee0510cd97?hl=en
* Raw string substitution problem - 4 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/61fdda4299b6a7b4?hl=en
* pylint 0.19 / astng 0.19.2 - 5 messages, 4 authors
http://groups.google.com/group/comp.lang.python/t/924ed1545e99e5c1?hl=en
* share dictionary between processes - 2 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/3be143506dce9c64?hl=en
* Dangerous behavior of list(generator) - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/ae70dfa12677c1d5?hl=en
* Help with invoking standard mail app in windows - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/3aff8de87dc63018?hl=en
* Programming intro book ch1 and ch2 (Windows/Python 3) - Request For Comments
- 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/b9f6bcfdb74181e1?hl=en
* Another MySQL Problem - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/34673c5abf2eb179?hl=en

==============================================================================
TOPIC: Line indexing in Python
http://groups.google.com/group/comp.lang.python/t/35abd4b45d347cc2?hl=en
==============================================================================

== 1 of 4 ==
Date: Fri, Dec 18 2009 8:35 am
From: seafoid

Thanks for that Richard and Steve!

Below is my full code so far:

for line in file:
if line.startswith("1"):
a.write(line)
elif line.endswith("0"):
lists_a = line.strip().split()
print lists_a
elif line.startswith("2"):
lists_b = line.strip().split()
print list_a

Essentially, I want to read in a file and depending on location of 0, 1, 2,
write to another file or create lists.

The above passes without error warning but does nothing (semantic error?).

Any Suggestions?

Thanks in advance,
Seafoid.
--
View this message in context: http://old.nabble.com/Line-indexing-in-Python-tp26845253p26846049.html
Sent from the Python - python-list mailing list archive at Nabble.com.

== 2 of 4 ==
Date: Fri, Dec 18 2009 8:39 am
From: seafoid

Thanks for that Richard and Steve!

Below is my full code so far:

for line in file:
if line.startswith("1"):
a.write(line)
elif line.endswith("0"):
lists_a = line.strip().split()
print lists_a
elif line.startswith("2"):
lists_b = line.strip().split()
print list_b

Essentially, I want to read in a file and depending on location of 0, 1, 2,
write to another file or create lists.

The above passes without error warning but does nothing (semantic error?).

Any Suggestions?

Thanks in advance,
Seafoid.
--
View this message in context: http://old.nabble.com/Line-indexing-in-Python-tp26845253p26846049.html
Sent from the Python - python-list mailing list archive at Nabble.com.

== 3 of 4 ==
Date: Fri, Dec 18 2009 8:54 am
From: Lie Ryan


On 12/19/2009 3:27 AM, seafoid wrote:
>
> Thanks for that Richard and Steve.
>
> I have another question.

What's the question?

> fname = raw_input('Please enter the name of the file: ')
>
> # create file objects
>
> blah = open(fname, 'r')
> a = open('rubbish', 'w')
>
> for line in blah:
> if line.startswith("0"):
> a.write(line)
> elif line.endswith("0"):
> lists_a = line.strip().split()
> print lists_a

The following block is a dead code; the block will never be executed
since if line.startswith("0") is true, the control will fall to the
a.write(line) block and this block is skipped.
> elif line.startswith("0"):
> lists_b = line.strip().split()
> print lists_b
>
> Essentially, I wish to take input from a file and based on the location of
> zero, assign lines to lists.
>
> Any suggestions?
>
> Seafoid.
>

== 4 of 4 ==
Date: Fri, Dec 18 2009 9:33 am
From: seafoid

Thanks for that Lie.

I had to have a think about what you meant when you referred to control
going to a.write(line).

Have you any suggestions how I may render this code undead or should I scrap
it and create something new?

My confusion and ineptitude is perhaps explained by my being a biologist :-(

Thanks,
Seafoid.
--
View this message in context: http://old.nabble.com/Line-indexing-in-Python-tp26845253p26846854.html
Sent from the Python - python-list mailing list archive at Nabble.com.


==============================================================================
TOPIC: Design question about pretree classifier
http://groups.google.com/group/comp.lang.python/t/24875d55431e0caf?hl=en
==============================================================================

== 1 of 2 ==
Date: Fri, Dec 18 2009 8:38 am
From: Julian


Hello,

I've got a design problem for a classifier. To make it short: it maps
strings on strings.

Some strings have exactly one classification, some none and some more
than one.

There's a method classify(self, word) wich classifies a word. For the
first case there's no problem:

- one classification: return the value (it's a string)

But:

- none classification: return an exception or None? I think None is
better, hence its not an exception that there is no classification but
a defined state. What do you think?
- many classifications: what to do? retun a sequence of strings? raise
an exception and implement another method wich returns than the
classifications? what should I do here?

thanks for your answers!


== 2 of 2 ==
Date: Fri, Dec 18 2009 9:59 am
From: Steve Holden


Julian wrote:
> Hello,
>
> I've got a design problem for a classifier. To make it short: it maps
> strings on strings.
>
> Some strings have exactly one classification, some none and some more
> than one.
>
> There's a method classify(self, word) wich classifies a word. For the
> first case there's no problem:
>
> - one classification: return the value (it's a string)
>
> But:
>
> - none classification: return an exception or None? I think None is
> better, hence its not an exception that there is no classification but
> a defined state. What do you think?
> - many classifications: what to do? retun a sequence of strings? raise
> an exception and implement another method wich returns than the
> classifications? what should I do here?
>
> thanks for your answers!

Always return a list or tuple. For no classifications it should be
empty, for one classification it should have one element, ... , for N
classifications it should have N elements.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/


==============================================================================
TOPIC: imports in __init__.py
http://groups.google.com/group/comp.lang.python/t/e90896c5b607904a?hl=en
==============================================================================

== 1 of 2 ==
Date: Fri, Dec 18 2009 8:42 am
From: Phil


I wrote my last message late last night. When I said "I am unable to
import a module from the package without an import error.", I did mean
the 'modulename' module.

However, I just set up a Debian VM with Python 2.5.2 and what I was
trying to do works. So it is either something that changed with Python
3.1.1, or a problem with Windows.


== 2 of 2 ==
Date: Fri, Dec 18 2009 9:40 am
From: Peter Otten <__peter__@web.de>


Phil wrote:

> I wrote my last message late last night. When I said "I am unable to
> import a module from the package without an import error.", I did mean
> the 'modulename' module.
>
> However, I just set up a Debian VM with Python 2.5.2 and what I was
> trying to do works. So it is either something that changed with Python
> 3.1.1, or a problem with Windows.

In Python 3.x absolute import is on by default. Change

from application import *

to

from .application import *

to indicate that the application module is located within the current
package.

Peter


==============================================================================
TOPIC: iterators and views of lists
http://groups.google.com/group/comp.lang.python/t/22d674ee0510cd97?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Dec 18 2009 9:03 am
From: Anh Hai Trinh


On Dec 18, 3:07 am, Brendan Miller <catph...@catphive.net> wrote:

> Well, it doesn't really need to be any slower than a normal list. You
> only need to use index and do extra additions because it's in python.
> However, if listagent were written in C, you would just have a pointer
> into the contents of the original list, and the length, which is all
> that list itself has.

You're right, I was thinking in Python instead of C. So the
translations in that case is really straight forward, using just
pointer arithmetic.

----aht

==============================================================================
TOPIC: Raw string substitution problem
http://groups.google.com/group/comp.lang.python/t/61fdda4299b6a7b4?hl=en
==============================================================================

== 1 of 4 ==
Date: Fri, Dec 18 2009 9:09 am
From: sion@viridian.paintbox (Sion Arrowsmith)


Gregory Ewing <greg.ewing@canterbury.ac.nz> wrote:
>MRAB wrote:
>> Regular expressions and replacement strings have their own escaping
>> mechanism, which also uses backslashes.
>This seems like a misfeature to me. It makes sense for
>a regular expression to give special meanings to backslash
>sequences, because it's a sublanguage with its own syntax.
>But I can't see any earthly reason to do that with the
>*replacement* string, which is just data.

>>> re.sub('a(.)c', r'\1', "123abcdefg")
'123bdefg'

Still think the replacement string is "just data"?

--
\S

under construction

== 2 of 4 ==
Date: Fri, Dec 18 2009 9:17 am
From: MRAB


Gregory Ewing wrote:
> MRAB wrote:
>
>> Regular expressions and replacement strings have their own escaping
>> mechanism, which also uses backslashes.
>
> This seems like a misfeature to me. It makes sense for a regular
> expression to give special meanings to backslash sequences, because
> it's a sublanguage with its own syntax. But I can't see any earthly
> reason to do that with the *replacement* string, which is just data.
>
> It looks like a feature that's been blindly copied over from Perl
> without thinking about whether it makes sense in Python.
>
In simple cases you might be replacing with the same string every time,
but other cases you might want the replacement to contain substrings
captured by the regex.

For example, swapping pairs of words:

>>> re.sub(r'(\w+) (\w+)', r'\2 \1', r'first second third fourth')
'second first fourth third'


Python also allows you to provide a function that returns the
replacement string, but that seems a bit long-winded for those cases
when a simple replacement template would suffice.


== 3 of 4 ==
Date: Fri, Dec 18 2009 9:58 am
From: Alan G Isaac


On 12/17/2009 7:59 PM, Rhodri James wrote:
> "re.compile('a\\nc')" passes a sequence of four characters to
> re.compile: 'a', '\', 'n' and 'c'. re.compile() then does it's own
> interpretation: 'a' passes through as is, '\' flags an escape which
> combined with 'n' produces the newline character (0x0a), and 'c' passes
> through as is.


I got that from MRAB's posts. (Thanks.)
What I'm not getting is why the replacement string
gets this particular interpretation. What is the payoff?
(Contrast e.g. Vim's substitution syntax.)

Thanks,
Alan

== 4 of 4 ==
Date: Fri, Dec 18 2009 9:59 am
From: Alan G Isaac


On 12/18/2009 12:17 PM, MRAB wrote:
> In simple cases you might be replacing with the same string every time,
> but other cases you might want the replacement to contain substrings
> captured by the regex.


Of course that "conversion" is needed in the replacement.
But e.g. Vim substitutions handle this fine without the
odd (to non perlers) handling of backslashes in replacement.

Alan Isaac


==============================================================================
TOPIC: pylint 0.19 / astng 0.19.2
http://groups.google.com/group/comp.lang.python/t/924ed1545e99e5c1?hl=en
==============================================================================

== 1 of 5 ==
Date: Fri, Dec 18 2009 9:24 am
From: Jean-Michel Pichavant


Sylvain Thénault wrote:
> Hi,
>
> I'm very pleased to announce the release of pylint 0.19 / astng 0.19.2 release!
>
> More information / download on http://www.logilab.org/project/pylint/0.19.0.
>
> This is a "community" release, including the work we've done during the pylint
> bug day [1] and patches mostly from James Lingard and Vincent Ferotin.
>
> Many thanks to James Lingard which provided two long waited features:
>
> * check of function call arguments
> * check string interpolation consistency
>
>
> So, happy christmas, enjoy!
>
> [1] http://www.logilab.org/blogentry/19260
>
I have troubles after updating pylint:

easy_install pylint -U --install-dir
/opt/tools/python/python2.3/site-packages

> pylint

File
"/opt/tools/python/python2.3/site-packages/logilab_astng-0.19.2-py2.5.egg/logilab/astng/infutils.py",
line 28, in <module>
from logilab.astng._nodes import Proxy_, List, Tuple, Function, If,
TryExcept
ImportError: No module named _nodes

There is no _nodes.py file within the egg. Has anyone experienced the
same issue ?

Jean-Michel

== 2 of 5 ==
Date: Fri, Dec 18 2009 9:32 am
From: Jerry Hill


On Fri, Dec 18, 2009 at 12:24 PM, Jean-Michel Pichavant >  File
> "/opt/tools/python/python2.3/site-packages/logilab_astng-0.19.2-py2.5.egg/logilab/astng/infutils.py",
> line 28, in <module>
>   from logilab.astng._nodes import Proxy_, List, Tuple, Function, If,
> TryExcept
> ImportError: No module named _nodes

You're installing the wrong version of logilab. That's the python 2.5
version, judging by the name of the egg. I'm not sure how
easy_install decides which version of python to install for, but it's
definitely picking the wrong one here.

--
Jerry


== 3 of 5 ==
Date: Fri, Dec 18 2009 9:49 am
From: Jean-Michel Pichavant


Jerry Hill wrote:
> On Fri, Dec 18, 2009 at 12:24 PM, Jean-Michel Pichavant > File
>
>> "/opt/tools/python/python2.3/site-packages/logilab_astng-0.19.2-py2.5.egg/logilab/astng/infutils.py",
>> line 28, in <module>
>> from logilab.astng._nodes import Proxy_, List, Tuple, Function, If,
>> TryExcept
>> ImportError: No module named _nodes
>>
>
> You're installing the wrong version of logilab. That's the python 2.5
> version, judging by the name of the egg. I'm not sure how
> easy_install decides which version of python to install for, but it's
> definitely picking the wrong one here.
>
>
well, according this mail title, astng 0.19.2 is the correct one.

I should have mentioned /opt/tools/python/python2.3/ is a network drive
where we install 3rd party modules. It is still named python2.3 but this
is in fact totally irrelevant (we're the kind of lazy guys).
Python 2.5 is the one installed on my system so easy_install guess is right.

Or maybe you suggested that pylint is no more compatible with python2.5 ?

JM


== 4 of 5 ==
Date: Fri, Dec 18 2009 9:55 am
From: Sylvain Thénault


On 18 décembre 18:24, Jean-Michel Pichavant wrote:
> Sylvain Thénault wrote:
> >Hi,
> >
> >I'm very pleased to announce the release of pylint 0.19 / astng 0.19.2 release!
> >
> >More information / download on http://www.logilab.org/project/pylint/0.19.0.
> >
> >This is a "community" release, including the work we've done during the pylint
> >bug day [1] and patches mostly from James Lingard and Vincent Ferotin.
> >
> >Many thanks to James Lingard which provided two long waited features:
> >
> >* check of function call arguments
> >* check string interpolation consistency
> >
> >
> >So, happy christmas, enjoy!
> >
> >[1] http://www.logilab.org/blogentry/19260
> I have troubles after updating pylint:
>
> easy_install pylint -U --install-dir
> /opt/tools/python/python2.3/site-packages
>
> > pylint
>
> File "/opt/tools/python/python2.3/site-packages/logilab_astng-0.19.2-py2.5.egg/logilab/astng/infutils.py",
> line 28, in <module>
> from logilab.astng._nodes import Proxy_, List, Tuple, Function,
> If, TryExcept
> ImportError: No module named _nodes
>
> There is no _nodes.py file within the egg. Has anyone experienced
> the same issue ?

yep someone else on the list have the same pb. I think I've identified it:

python setup.py register sdist upload

*doesn't* regenerate MANIFEST if one is found... So packages uploaded to pypi
had some missing files. Should be fixed now, sorry for this pb.
--
Sylvain Thénault LOGILAB, Paris (France)
Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations
Développement logiciel sur mesure: http://www.logilab.fr/services
CubicWeb, the semantic web framework: http://www.cubicweb.org

== 5 of 5 ==
Date: Fri, Dec 18 2009 10:04 am
From: David Robinow


On Fri, Dec 18, 2009 at 12:49 PM, Jean-Michel Pichavant
<jeanmichel@sequans.com> wrote:
> Jerry Hill wrote:
>>
>> On Fri, Dec 18, 2009 at 12:24 PM, Jean-Michel Pichavant >  File
>>> "/opt/tools/python/python2.3/site-packages/logilab_astng-0.19.2-py2.5.egg/logilab/astng/infutils.py",
>>> line 28, in <module>
>>>  from logilab.astng._nodes import Proxy_, List, Tuple, Function, If,
>>> TryExcept
>>> ImportError: No module named _nodes
>>>

It works for me (but I'm running cygwin)
You should have a file
/opt/tools/python/python2.3/site-packages/logilab_astng-0.19.2-py2.5.egg/logilab/astng/_nodes.py

Does it exist? If not, reinstall logilab_astng-0.19.2

==============================================================================
TOPIC: share dictionary between processes
http://groups.google.com/group/comp.lang.python/t/3be143506dce9c64?hl=en
==============================================================================

== 1 of 2 ==
Date: Fri, Dec 18 2009 9:27 am
From: Steve Holden


blumenkraft wrote:
> Hi,
>
> I want to share dictionary between two distinct processes.
>
>
> Something like this:
>
> first.py
> import magic_share_module
>
> def create_dictionary():
> return {"a": 1}
>
> magic_share_module.share("shared_dictionary",
> creator.create_dictionary)
> while True:
> pass
>
>
> second.py
> import magic_share_module
> d = magic_share_module.get_shared("shared_dictionary")
> print d["a"]
>
> And then run in command line:
> python first.py &
> sleep 1
> python second.py
>
> I have looked at POSH, but it requires master process that will fork
> childs. I want read-only sharing between completely unrelated
> processes.
> Is it possible?

Take a look at pyro, though it may be overkill for your needs.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

== 2 of 2 ==
Date: Fri, Dec 18 2009 9:27 am
From: Steve Holden


blumenkraft wrote:
> Hi,
>
> I want to share dictionary between two distinct processes.
>
>
> Something like this:
>
> first.py
> import magic_share_module
>
> def create_dictionary():
> return {"a": 1}
>
> magic_share_module.share("shared_dictionary",
> creator.create_dictionary)
> while True:
> pass
>
>
> second.py
> import magic_share_module
> d = magic_share_module.get_shared("shared_dictionary")
> print d["a"]
>
> And then run in command line:
> python first.py &
> sleep 1
> python second.py
>
> I have looked at POSH, but it requires master process that will fork
> childs. I want read-only sharing between completely unrelated
> processes.
> Is it possible?

Take a look at pyro, though it may be overkill for your needs.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

==============================================================================
TOPIC: Dangerous behavior of list(generator)
http://groups.google.com/group/comp.lang.python/t/ae70dfa12677c1d5?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Dec 18 2009 9:58 am
From: Albert van der Horst


In article <mailman.1818.1260694428.2873.python-list@python.org>,
Gabriel Genellina <gagsl-py2@yahoo.com.ar> wrote:
<SNIP>
>
>Despite a promise in PEP 289, generator expressions semantics isn't
>explained in detail in the language reference. I can't tell if the
>difference is intentional, accidental, undocumented behavior, an
>implementation accident, a bug, or what...

Philosophically speaking ...
An important feature that is not documented is a severe defect.
(important maps to severe).
Before it is documented, there can be no discrepancy between
specification and implementation so other defects are formally
not present in relation to this situation.

>--
>Gabriel Genellina
>

Groetjes Albert.


--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst


==============================================================================
TOPIC: Help with invoking standard mail app in windows
http://groups.google.com/group/comp.lang.python/t/3aff8de87dc63018?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Dec 18 2009 9:56 am
From: Astan Chee


Hi,
I don't know if my last mail made it or not but here it is again.
I'm trying to launch standard mail app in windows and after looking
around most look like this:

import urllib, webbrowser, win32api
def mailto_url(to=None,subject=None,body=None,cc=None):
"""
encodes the content as a mailto link as described on
http://www.faqs.org/rfcs/rfc2368.html
"""
url = "mailto: " + urllib.quote(to.strip(),"@,")
sep = "?"
if cc:
url+= sep + "cc=" + urllib.quote(cc,"@,")
sep = "&"
if subject:
url+= sep + "subject=" + urllib.quote(subject,"")
sep = "&"
if body:
# Also note that line breaks in the body of a message MUST be
# encoded with "%0D%0A". (RFC 2368)
body="\r\n".join(body.splitlines())
url+= sep + "body=" + urllib.quote(body,"")
sep = "&"
return url

url = mailto_url(txtTo,txtSubject,body,txtCC)
# win32api.ShellExecute(0,'open',url,None,None,0)
webbrowser.open(url,new=1)
# os.startfile(url)

all of these are having "WindowsError : [Error 5] Access is denied"
errors. I'm using windows xp and python 2.5. I have outlook 2007
installed as a default mail client. Clicking on any mailto links in html
brings up the normal write mail from the mail client. Any ideas why this
is happening or how do I debug what access is being denied?
Thanks for any help
Astan


==============================================================================
TOPIC: Programming intro book ch1 and ch2 (Windows/Python 3) - Request For
Comments
http://groups.google.com/group/comp.lang.python/t/b9f6bcfdb74181e1?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Dec 18 2009 10:00 am
From: "Alf P. Steinbach"


I finally finished (draft), I believe!, chapter 2...

Chapter 1 gets the reader up & running, i.e. it's "Hello, world!", basic tool
usage, without discussing anything about programming really. One reaction to
this chapter, based on the two example programs in it, was that it wasn't
gradual and slow enough; hey, those examples are far too advanced, and
unexplained! But that reader misunderstood: the progression is actually *slower*
than one might expect. This chapter is only about tool usage. I.e., it's about
getting those programs running, for the reader who can't rely on teachers or
fellow students or, as Kernighan & Ritchie put it, "your local guru" (IIRC).

Chapter 2 is about Basic Concepts (of programming). It's the usual: variables,
basic types and arrays, loops, decision, routines, classes, events, although not
presented in that order. I make heavy use of complete, concrete examples, many
of them graphical, and everything is in support of what's actually needed for
such concrete examples. The intent is to enable the reader to experiment and try
things out -- since the only way to really learn is by doing! As best I could
I've labored to apply this minimalism also to the Python language, using only a
"minimal" subset (to the degree of not even introducing boolean ops :-) ).

Chapter 3 will, by my current plan, delve into the Python language and such
things as how integers and floating point works on the inside, and that includes
those in chapter 2 not even mentioned boolean operations. One important issue,
introducing exceptions, and in support of that, type hierarchies. After chapter
3 I have only much vaguer notions about what to introduce in what order, but a
main issue, assuming that I go on with this writing, will be to apply and teach
methodology all the way, integrated into the examples and text.

A table of contents + chapters 1 and 2 is available in PDF format at Google Docs, at

<url: http://tinyurl.com/programmingbookP3>

Comments are very welcome!

Re comments: there are two deviations from current Python practice in chapter 2.
First, that I use spaces inside argument parentheses, which makes the code more
readable when one gets used/trained to it because it gives the eye more direct
information (with some training visual structure can be processed unconsciously
& effortlessly, but purely logical structure has to be processed analytically).
The second deviation is that since most names are constants, I do not follow PEP
8's recommendation to use uppercase names of constants. In fact almost no Python
code does, but then it seems that people are not aware of how many of their
names are constants and think that they're uppercasing constants when in fact
they're not. E.g. routine arguments and in particular routine names are usually
constants, absolutely not meant to be modified, but it would be silly to UC...

So both these two deviations from Python practice are /intentional/, since this
is a book about programming, not about Python the language & how to conform to
idiosyncratic language-specific conventions.

But, if there are other deviations from Python practice I'd be very glad to hear
of it! I'm very hopeful that any such convention deviations can be fixed. :-)


Cheers,

- Alf

==============================================================================
TOPIC: Another MySQL Problem
http://groups.google.com/group/comp.lang.python/t/34673c5abf2eb179?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Dec 18 2009 10:17 am
From: John Nagle


MRAB wrote:
> Victor Subervi wrote:
>> Hi;
>>
>> mysql> truncate tem126072414516;
>> Query OK, 0 rows affected (0.00 sec)
>>
>> Then I run a script:
>>
>> if whatDo == 'insert':
>> try:
>> sql = 'insert into %s (ProdID, Quantity) values ("%s", "%s");' %
>> (tmpTable, prodid, quantity)
>> print sql
>> cursor.execute(sql)

Don't put values into an SQL statement using the "%" operator. It doesn't
do SQL escapes and allows SQL injection attacks.

Try something more like this (assuming that tmpTable does NOT come
from external input, which would be very risky).

cursor = db.cursor() ## create cursor
sql = 'insert into ' + tmpTable + ' (ProdID, Quantity) values (%s,%s);'
values = (prodid, quantity) ## values to insert
print sql
cursor.execute(sql, values) ## let SQL do the substitution
db.commit() ## commit transaction


> 1. The table names look different.
> 2. Did you commit the changes?

That, too.

John Nagle


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate