Re: Text widget updates only after calling method exits (threading issue?)

2007-12-12 Thread Marc 'BlackJack' Rintsch
On Tue, 11 Dec 2007 17:58:37 -0800, mariox19 wrote:

> If I am supposed to send messages to Tkinter objects only from the
> main thread, how can I get the letters to appear 1 per second?

Take a look at the `after()` method on widgets.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


xmlrpclib.binary to a file doubt

2007-12-12 Thread Jose Ignacio Gisbert
Hi all,

 

I have a problem in my application which uses xml-rpc methods. From one hand
I can send a file to a server doing 

 

f=open(Myfile,'rb')

g=f.read()

name=(f.name).split("/")

name=name[len(name)-1]

calltosendmethod(xmlrpclib.Binary(g),name)

 

And it works fine. But on the other hand I have to get a document from the
server, which method returns document content in xmlrpclib.binary format,
and I am not able to store it on a local file. If somebody knows how to,
please tell me something, anything will helps. 

 

Thanks in advance, 

___
José Ignacio Gisbert Sanus

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is anyone happy with csv module?

2007-12-12 Thread Marc 'BlackJack' Rintsch
On Tue, 11 Dec 2007 20:08:21 -0300, Gabriel Genellina wrote:

>  data = [row for row in csv.reader(..)]

A bit shorter::

   data = list(csv.reader(..))

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread George Sakkis
On Dec 12, 2:18 am, Kay Schluehr <[EMAIL PROTECTED]> wrote:
> On Dec 12, 7:34 am, sturlamolden <[EMAIL PROTECTED]> wrote:

> > I am not sure why a new type annotation syntax was needed Python 3:
>
> Because people care about a feature when there is @syntax.

Good point; the inverse is not true though: time and time again people
cared about some syntax for properties without any change so far. The
result is a handful of different ways to spell out properties; python
2.6 will add yet another variation (http://mail.python.org/pipermail/
python-dev/2007-October/075057.html).

George
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Text widget updates only after calling method exits (threading issue?)

2007-12-12 Thread Eric Brunel
On Wed, 12 Dec 2007 02:58:37 +0100, mariox19 <[EMAIL PROTECTED]> wrote:

> Are Tkinter widgets running on their own thread?

No. And usually, GUI toolkits and threads don't mix well...

> If I try to make a simple application that will print the letters A to
> Z to a Tkinter Text widget, and I space the printing of each letter by
> 1 second, it seems no text will appear in the Text widget until the
> method exits.
>
> Take a look at this snippet:
>
> # Assume I have created no threads other than the one that comes
> with Main
>
> def printToTkinterTextWidget(text):
> """
> Prints A-Z to the Text widget, 1 letter per second.
> """
> # Problem: no text appears in the widget until 26 seconds has
> elapsed
> for aNumber in range(65, 91):
> self.textWidget.insert(END, text)
> time.sleep(1)

time.sleep will not give back the control to the Tkinter mainloop, so your  
text widget won't be refreshed on screen. Try:
self.textWidget.update_idletasks()
before the sleep.

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread Kay Schluehr
On Dec 12, 9:04 am, George Sakkis <[EMAIL PROTECTED]> wrote:
> On Dec 12, 2:18 am, Kay Schluehr <[EMAIL PROTECTED]> wrote:
>
> > On Dec 12, 7:34 am, sturlamolden <[EMAIL PROTECTED]> wrote:
> > > I am not sure why a new type annotation syntax was needed Python 3:
>
> > Because people care about a feature when there is @syntax.
>
> Good point; the inverse is not true though: time and time again people
> cared about some syntax for properties without any change so far. The
> result is a handful of different ways to spell out properties; python
> 2.6 will add yet another variation (http://mail.python.org/pipermail/
> python-dev/2007-October/075057.html).
>
> George

Yes, I'm aware. Curiously, whenever property syntax is discussed the
discussion loses track and is dragged away by needless side
discussions. Just look at Stephen Bethards withdrawn PEP 359 [1] in
which he finally muses about replacing the class statement by the make
statement. So the PEP ended in "abstract nonsense" instead of
clarifying the point.

[1] http://www.python.org/dev/peps/pep-0359/

I vaguely remember a discussion a few years ago, where someone made
the quite reasonable suggestion of introducing some kind of
thunk_statement:

class A(object):
foo = property:
def fget(self):
return self._foo
def fset(self, value):
self._foo = value

which was translated as follows:

class A(object):
def thunk():
def fget(self):
return self._foo
def fset(self, value):
self._foo = value
return vars()
foo = propery(**thunk())
del thunk

Now people started to consider using the compound statement within
expressions as well because the equal sign is used within method
signatures and call syntax. This lead to a general discussion about
the expr/statement distinction in Python, about multiline lambdas and
functional style programming. These association graphs are almost
predictable.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: UliPad 3.8.1 released!

2007-12-12 Thread Gary Herron
limodou wrote:
> Bug fix verion.
>
>1. Remove profile invoke(big mistake)
>2. Fix svn plugin checkout bug
>
> Download:
> http://ulipad.googlecode.com/files/ulipad.3.8.1.zip
> http://ulipad.googlecode.com/files/ulipad.3.8.1.exe
>   

Can you please take the time, when making such an announcement, to tell
us *what* it is you are releasing.  Just a sentence or two and a URL
would be only common courtesy.

Thanks,
Gary Herron


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help needed with python unicode cgi-bin script

2007-12-12 Thread Duncan Booth
"weheh" <[EMAIL PROTECTED]> wrote:

> John and Martin,
> 
> Thanks for your help. However, I have identified the culprit to be
> with Apache and the command:
> AddDefaultCharset utf-8
> which forces my browser to utf-8 encoding.
> 
> It looks like your suggestions to change charset were incorrect. My
> example works equally well with charset=utf8 as it does with
> charset=windows-1252. 
> 
> Incidentally, next time, if you really want to be helpful, might I
> suggest you leave out the mocking. I could care less, myself, but
> someone else might have gotten their feelings hurt. And in the end, it
> doesn't make you look good.
> 
> Thanks again. Cheers. 
> 
> 
FWIW, the code you posted only ever attempted to set the character set 
encoding using an html meta tag which is the wrong place to set it. The 
encoding specified in the HTTP headers always takes precedence. This is why 
the default charset setting in Apache was the one which applied.

What you should have been doing was setting the encoding in the content-
type header. i.e. in this line of your code:

   print u"""Content-Type: text/html

You should have changed it to read:

   Content-Type: text/html; charset=windows-1252

but because you didn't Apache was quietly changing it to read:

   Content-Type: text/html; charset=utf-8


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: new style class

2007-12-12 Thread Bruce Coram
Steven

Regrettably I have to reply to your post because it misses the point of 
my initial post completely.  I suggested that Eric Raymond's advice 
provided cover for people who were rude, hostile or arrogant.  There are 
two obvious responses:  his advice does not provide such cover or it 
does but it does not matter.  It make no assertions about any particular 
person or group of persons.  It merely suggests that people who were 
disposed to rudeness could point to his article as supporting their 
approach.  It was a plea that we conduct ourselves in a civil manner and 
treat other people with respect.

I might be justified in assuming that you have spent some time working 
with politicians because you impute arguments to me that I do not make.  
At no point do I suggest that pointing somebody at Eric Raymond's advice 
is rude, hostile or arrogant, and your interpretation of my words to 
arrive at this is perverse.  It may be that English is not your first 
language in which case such a slip could be excused.  If not, it is 
evidence either of slipshod thinking or wilfull manipulation and 
obfuscation. If it is the latter then the post does not sit well in a 
forum that strives for accuracy.

Had I written:
The best response to those who *we assume* can not be bothered  to do 
the necessary work is either no reply or a simple "You would be  well 
advised to do some research before asking your question."

you would be justified in claiming that my advice was not to give any 
advice. I did not include the words 'we assume' and therefore my advice 
either to stop replying or to give a polite sign off was based on there 
being evidence that a person seeking advice could not be bothered i.e. 
there had been sufficient contact to allow that conclusion to be drawn 
in a reasonable manner.  English is a language that permits great 
precision in conveying meaning.  However, it is necessary on occasions 
to do some work and thinking in order to extract the writer's idea.

You also seem to have overlooked that I state twice that Eric Raymond's 
advice is good or very good.

I have no desire to to indulge in online verbal brawling but please take 
more care in drafting a reply, particularly in a situation where your 
interpretation of my post might lead others who post to believe that I 
thought them rude, hostile or arrogant.

Bruce Coram



Steven D'Aprano wrote:
> On Sat, 08 Dec 2007 23:14:44 +, Bruce Coram wrote:
>
>   
>>> http://www.catb.org/~esr/faqs/smart-questions.html
>>>   
>>>   
>> Eric Raymond's advice on how to ask questions the smart way would seem
>> to provide an excuse for people with ego control problems to indulge
>> themselves at the expense of others.  While it is undoubtedly true that
>> there are people who post who should spend more time reading and
>> researching the problem, that is no excuse for replies that  are rude,
>> hostile or exhibit similar displays of ill-tempered arrogance.
>> 
>
> Pointing somebody at Eric Raymond's advice is neither rude, hostile or 
> arrogant. It may be brusque. It may fail to sugar-coat the message 
> sufficiently, and hurt some recipient's feelings, but that's their 
> problem, not that of the sender.
>
>
>   
>> Eric
>> Raymond should perhaps re-read his advice and re-draft it to avoid
>> providing cover for those 'experts' who are either rude or ignorant - or
>> both.
>> 
>
> Why don't you do so yourself? He solicits suggestions and revisions.
>
> Or ask for permission to fork the document and come up with your own. 
> (You have to ask first, because as far as I can see the document is not 
> released with an open licence.)
>
>
>   
>> If an 'expert' has time to indulge his/her ego is such an
>> intemperate manner then he/she probably doesn't have enough to do, or
>> enjoys being rude.  
>> 
>
> Dare I suggest that perhaps YOU should read smart-questions? In 
> particular, the bits where Raymond writes about RTFM:
>
> "You shouldn't be offended by this; by hacker standards, your respondent 
> is showing you a rough kind of respect simply by not ignoring you. You 
> should instead be thankful for this grandmotherly kindness."
>
> Pointing somebody at smart-questions is a rather more polite form of RTFM.
>
>
>   
>> The best response to those who can not be bothered
>> to do the necessary work is either no reply 
>> 
>
> Ignoring people's request for help to punish them for poor behaviour is 
> not only rude but it is counter-productive. Not only do you not solve 
> their immediate problem, but you keep them in a state of ignorance as to 
> why they are being shunned -- thus guaranteeing that they will invariably 
> transgress again.
>
>
>   
>> or a simple "You would be
>> well advised to do some research before asking your question."
>> 
>
> Again leaving them no better off and still likely to transgress in the 
> future. How much is "some"? What sort of research? Asking on Usenet is 
> research isn't it? Why should I be ex

Re: Is anyone happy with csv module?

2007-12-12 Thread massimo s.
Thanks to everyone in this thread. As always on this newsgroup, I
learned very much.

I'm also quite embarrassed of my ignorance. Only excuse I have is that
I learned programming and Python by myself, with no formal (or
informal) education in programming. So, I am often clumsy.

On Dec 12, 1:29 am, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:
> > I'm just trying to use the CSV module
> > and I mostly can get it working. I just think its interface is much
> > less than perfect. I'd like something I can, say, give a whole
> > dictionary in input and obtain a CSV file in output, with each key of
> > the dictionary being a column in the CSV file. Or a row, if I prefer.
> > Something like:
>
> > dict={'First':[1,2,3,4],'Second':[10,20,30,40],'Third':
> > [100,200,300,400]}
>
> 
> you're shadowing the builtin 'dict' type here, which is usalluy a bad idea
> 

Yes, this I know, I just overlooked it when improvising the example.

> > f=open('test.csv','w')
> > try:
> > csv_write_dict(f,dict,keys='columns',delimiter=',')
> > finally:
> > f.close()
>
> > and obtaining:
> > First,Second,Third
> > 1,10,100
> > 2,20,200
> > 3,30,300
> > 4,40,400
>
> Doing the needed transformation (from a column:rows dict to the required
> format) is close to trivial. So you could actually implement it
> yourself, monkeypatch the relevant csv class, and submit a patch to the
> maintainer of the module.
>
> FWIW, I never had data structured that way to pass to the csv module -
> to be true, I think I never had a case where tabular data were
> structured by columns.

FWIW, I never had data structured by row. At most, I had data
structured by *both* row and column.
Vive la différence. :)

> > Doing the same thing with the current csv module is much more
> > cumbersome: see this example 
> > fromhttp://www.oreillynet.com/onlamp/blog/2007/08/pymotw_csv.html
>
> > f = open(sys.argv[1], 'wt')
> > try:
> > fieldnames = ('Title 1', 'Title 2', 'Title 3')
> > writer = csv.DictWriter(f, fieldnames=fieldnames)
> > headers = {}
> > for n in fieldnames:
> > headers[n] = n
> > writer.writerow(headers)
>
> # same as the 4 lines above
> writer.writerow(dict((item, item) for item in fieldnames))
>
> > for i in range(10):
> > writer.writerow({ 'Title 1':i+1,
> >   'Title 2':chr(ord('a') + i),
> >   'Title 3':'08/%02d/07' % (i+1),
> >   })
>
> This one looks so totally unrealistic to me - I mean, wrt/ to real-life
> use cases - that I won't even propose a rewrite.

I can frankly think of a lot of cases where this kind of pattern makes
a lot of sense, but in that case it was just for the example purpose.

> > finally:
> > f.close()
>
> A bit of a WTF, indeed. But most of the problem is with this example
> code, not with the csv module (apologies to whoever wrote this snippet).

Thank you. Let me say it was the *best* tutorial I found online -much
better than official docs, IMHO. Maybe it is the reason I felt dizzy
when trying to use csv.

> FWIW, here's a function what you want, at least for your first use case:
>
> def csv_write_cols(writer, data):
>  keys = data.keys()
>  writer.writerow(dict(zip(keys,keys)))
>  for row in zip(*data.values()):
>  writer.writerow(dict(zip(keys, row)))

Thanks!

> Now you do what you want, but as far as I'm concerned, I wouldn't start
> a total rewrite of an otherwise working (and non-trivial) module just
> for a trivial four (4) lines function.

I fully agree. I would like to add a bit of other trivial functions,
but this is a *clear* example of csv writer usage, which I did not
find.

> Also, have you considered that your columns may as well be rows, ie:
>
> First,  1,   2,   3,   4
> Second, 10,  20,  30,  40
> Third,  100, 200, 300, 400

Doesn't play well with my data for a number of reasons. For example,
columns VS rows limits on spreadsheets.

> > Another unrelated quirk I've found is that iterating the rows read by
> > a csv reader object seems to erase the rows themselves; I have to copy
> > them in another list to use them.
>
> It's not a "quirk", Sir, it's a feature !-)
>
> The csv reader object - like file objects and a couple others - are
> iterators. In this case, it means the csv reader is smart enough to not
> read the whole file into memory - which is not necessarily what you
> want, specially for huge files - but iterating over lines as long as you
> ask for them.
>
> Note that if you need the whole thing in memory, "copying" the rows in a
> list is a no-brainer:
>rows = list(reader)

I know. I just thought odd it was undocumented. But it's self-evident
now that I missed how iterators work.
I'll look into the issue.

> > Probably it's me not being a professional programmer,
>
> 
> Not sure the professional status is key here - I mean, it just mean
> you're getting paid for it, but says nothing about your competences.
> 

In the meaning that I have no formal tra

Re: ANN: UliPad 3.8.1 released!

2007-12-12 Thread limodou
Please visit the site:

http://code.google.com/p/ulipad

I'm sorry forgot that.

-- 
I like python!
UliPad <>: http://code.google.com/p/ulipad/
meide <>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "do" as a keyword

2007-12-12 Thread Brian Blais

On Dec 11, 2007, at Dec 11:11:11 PM, Terry Reedy wrote:



"Steven D'Aprano" <[EMAIL PROTECTED]> wrote in  
message

news:[EMAIL PROTECTED]
||
| But loops that run at least once is a basic element of algorithms.
| Perhaps not as common as the zero or more times of the while  
loop, but

| still fundamental. It is a shame it has to be faked using:
|
| while True: # force the first iteration to always run
|process
|if condition: break
|
| Ugly and misleading.

I disagree.  Nothing is being faked.  The generic loop is

while True:
pre_process
if condition: break
post_process



I find that when teaching beginning programmers, they usually think  
in "until" terms, and not "while" terms.


do:
Forward()
until Touched()

and I have to explain to them that Python doesn't have "until", and  
that the logic for while is exactly the opposite:


while not Touched():
Forward()

they find the "while" logic to be unintuitive, and I often find  
myself feeling the same way: crafting it with the until logic, and  
then reversing it.  Perhaps I should do as above, and do:


while True:
Forward()
if Touched(): break

but somehow that feels wrong to me, like bypassing the point of the  
while: all that power to check for conditions, and you just use it to  
check True, and then use a break inside.  It's readable, I guess, but  
not a programming construct I am immediately drawn to.



Brian Blais


--
Brian Blais
[EMAIL PROTECTED]
http://web.bryant.edu/~bblais



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is a "real" C-Python possible?

2007-12-12 Thread Carl Friedrich Bolz
sturlamolden wrote:
> On 11 Des, 20:25, John Nagle <[EMAIL PROTECTED]> wrote:
> 
>> Shed Skin effort. Its author writes "Am I the only one seeing the potential
>> of an implicitly statically typed Python-like-language that runs at
>> practically the same speed as C++?"
> 
> Don't forget about Pyrex and PyPy's RPython.
> 
> By the way, we don't need a hotspot JIT compiler. Lisp has a compiler
> invoked by the programmer. We could include optional static typing in
> Python, and have an optional static optimizing native compiler for
> selected portions of code. That would be easier to implement in the
> short run, with  JIT-capabilities added later. Pyrex, ShedSkin or
> RPython are all good starting points.

I just want to stress that adding type hints _won't_ make programs 
faster if you use a good specializing JIT compiler. Psyco in particular 
would not benefit from type hints at all (even if you changed Psyco take 
them into account) and would give you exactly the same speed as without 
them.

Cheers,

Carl Friedrich Bolz
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread Nicola Larosa (tekNico)
sturlamolden wrote:
> def fibo(n):
> while 1:
> try:
> return fibo.seq[n]
> except AttributeError:
> fibo.seq = [0, 1, 1]
> except IndexError:
> fibo.seq.append( fibo.seq[-2] + fibo.seq[-1] )

I really like this formulation. However, its memory consumption is
proportional to the input number. On a system with one gigabyte of
RAM, it computes the Fibonacci number of 10 in about four seconds.
However, trying to compute 20, the machine swaps madly, and the
Python interpreter DOSes the Linux kernel solid, making the system
unresponsive. :-|

If all that cache is not reused, building it may be avoided by
appending the following two lines to the above function:

fibo.seq.pop(0)
n -= 1

With this addition, the above system manages to compute the Fibonacci
number of 100 (one million) in about 190 seconds. :-)


> This is 6 orders of magnitude faster than Congiano's benchmark. That
> is a speed up by a factor of a million.

That's really besides the point. Nice OT, anyway. ;-)

--
Nicola Larosa - http://www.tekNico.net/

AtomPub sits in a very strange place, as it has the potential to
disrupt half a dozen or more industry sectors, such as, Enterprise
Content Management, Blogging, Digital/Desktop Publishing and
Archiving, Mobile Web, EAI/WS-* messaging, Social Networks, Online
Productivity tools.
 -- Bill de hÓra, July 2007
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread George Sakkis
On Dec 12, 4:09 am, Kay Schluehr <[EMAIL PROTECTED]> wrote:

> Curiously, whenever property syntax is discussed the
> discussion loses track and is dragged away by needless side
> discussions. Just look at Stephen Bethards withdrawn PEP 359 [1] in
> which he finally muses about replacing the class statement by the make
> statement. So the PEP ended in "abstract nonsense" instead of
> clarifying the point.
>
> [1]http://www.python.org/dev/peps/pep-0359/

Ah, the 'make' statement.. I liked (and still do) that PEP, I think it
would have an impact comparable to the decorator syntax sugar, if not
more. Alas, it was too much ahead of its time.. who knows, it might
revive on some 3.x version.

George
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Counter-spam: Change the subject

2007-12-12 Thread Paul Boddie
On Dec 12, 5:17 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote:
>
> As far as I know, that is unusual behavior.  In Outlook Express and, I
> believe, other readers I have used, the original subject is the one
> displayed.  And if I have already downloaded the original title and marked
> the post as read, any reponse is just spam that prolongs the junk.  So
> please do not do this.

It would be better if Google actually blocked the spammers that people
probably complain about all the time (or used to, given that it got
tiresome to report spam with the same apparent origin, with the same
subject, and see nothing get done about it). Alternatively, a killfile
would be a useful addition to Google Groups.

Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "do" as a keyword

2007-12-12 Thread Antoon Pardon
On 2007-12-12, Terry Reedy <[EMAIL PROTECTED]> wrote:
>
> "Steven D'Aprano" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
>||
>| But loops that run at least once is a basic element of algorithms.
>| Perhaps not as common as the zero or more times of the while loop, but
>| still fundamental. It is a shame it has to be faked using:
>|
>| while True: # force the first iteration to always run
>|process
>|if condition: break
>|
>| Ugly and misleading.
>
> I disagree.  Nothing is being faked.  The generic loop is
>
> while True:
> pre_process
> if condition: break
> post_process
>
> If there is no pre_process, abbreviate the first two lines as 'while 
> condition:'.  If there is no post_process, some would like another 
> abbreviation.  Understanable.  But the use cases seem relatively few.  And 
> anyway, a competant programmer must understand the generic loop and a 
> fraction form, which I believe is at least as common as the no post_process 
> case.

And this generic loop is faked. There is no notion in the language that
somehow connects this if statement to be a breaking condition for a
loop. If it would be a real syntatic construct in the language, it would
probably look more like:

do:
  pre_process
until condition:
  post_process

Personnaly I would have preferred to have this one generic loop
construct with no abbreviations instead of having the while abbreviation
that is to be combined with a break statement to fake the generic loop.

-- 
Antoon Pardon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Class Best Practice

2007-12-12 Thread MarkE
On 4 Dec, 23:18, Rod Person <[EMAIL PROTECTED]> wrote:
> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
>
> I've been doing python programming for about 2 years as a hobby and now
> I'm finally able to use it at work in an enterprise environment. Since
> I will be creating the base classes and libraries I wondering which why
> is consider best when creating python classes:
>
> 1:
> class Foo(object):
>   member1=''
>   member2=0
>
>   def __init__(self,member1='',member2=0):
> self.member1 = member1
> self.member2 = member2
>
> 2:
> class Foo(object):
> def  __init(self,member1='',member2=0):
> self.member1 = member1
> self.member2 = member2
>

Don't forget to call the base class __init__ function
-- 
http://mail.python.org/mailman/listinfo/python-list


2008 computer new lang-python

2007-12-12 Thread ashik
thia is new ode adbbfy hsadhj
http://www.freewebs.com/thuiss/
http://indianfriendfinder.com/go/g906725-pmem
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why does producer delay halt shell pipe?

2007-12-12 Thread dwhall
Thanks, N, it works like a charm.

!!Dean

On Dec 11, 12:49 pm, Nanjundi <[EMAIL PROTECTED]> wrote:
> turn off python buffering & it should work.
> export PYTHONUNBUFFERED=t

-- 
http://mail.python.org/mailman/listinfo/python-list


psycopg

2007-12-12 Thread sujitha mary
hi all,
while executing this cur.execute('insert into seq(id,sequence)
values(3,'+content+')')
i'm getting an error  psycopg2.ProgrammingError: syntax error at or near
"prophage"
LINE 1: insert into seq(id,sequence) values(3,Tum2 prophage complete...
-- 
http://mail.python.org/mailman/listinfo/python-list

Re:

2007-12-12 Thread Lee Capps

On Dec 11, 2007, at 5:14 PM, katie smith wrote:

> "[16, 16, 2, 16, 2, 16, 8, 16]"

Regular expressions might be a good way to handle this.

import re

s = '[16, 16, 2, 16, 2, 16, 8, 16]'
get_numbers = re.compile('\d\d*').findall

numbers = [int(x) for x in get_numbers(s)]

See:

http://docs.python.org/lib/module-re.html

---
Lee Capps
Technology Specialist
CTE Resource Center
[EMAIL PROTECTED]



-- 
http://mail.python.org/mailman/listinfo/python-list


gnome/nautilus extensions ?

2007-12-12 Thread manatlan
Hello

I'd like to create a new "nautilus extension" in python. I'd like to
make a "nautilus side panel" ... is anybody has an example ?
or just tell me where to find more info (google was not my friend on
this search)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are Python deques linked lists?

2007-12-12 Thread Neil Cerutti
On 2007-12-10, Hrvoje Niksic <[EMAIL PROTECTED]> wrote:
> Neil Cerutti <[EMAIL PROTECTED]> writes:
>
>> Anyhow, implementing linked lists in Python is not challenging, but
>> they don't work well with Python iterators, which aren't suitable
>> for a linked list's purposes--so you have to give up the happy-joy
>> for loop syntax in favor of Python's frowny-sad while loops.
>
> With the help of generators, it is trivial to turn a frowny loop into
> a happy one:
>
> class Node:
> def __init__(self):
> self.next = None
> # attach data to the node as needed, and use "next" to refer
> # to the next node.
>
> def __iter__(self):
> node = self
> while 1:
> yield node
> node = node.next
> if node is None:
> break
>
> def linkify(it):
> """Turn a Python iterable into a linked list."""
> head = prev = None
> for elem in it:
> node = Node()
> node.data = elem
> if prev is None:
> head = node
> else:
> prev.next = node
> prev = node
> return head
>
> # iterate over a linked list using 'for':
>
 linkedlist = linkify([1, 2, 3])
 for n in linkedlist:
> ...   print n.data
> ...
> 1
> 2
> 3
>
> Correctly handling empty lists is left as an excercise for the
> reader.

It was providing the ability to mutate the list using an iterator
as a node reference that I alleged rendered linked lists a bit
awkward in Python syntax. The discussion yielded a couple of
solutions, though.

-- 
Neil Cerutti
And now the sequence of events in no particular order. --Dan Rather
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: psycopg

2007-12-12 Thread Calvin Spealman
Don't do that, for a number of reasons. String concatenation is  
really never a good idea and formatting your own query strings is  
exactly what leads to things like sql injection. Let the db library  
handle it for you:


cur.execute('insert into seq(id,sequence) values(3, %s)', (content,))

Notice that, although we're using the %s placeholder, we are _not_  
using the % operator to format the string. This is because the db  
module will do any proper preparation of the value for you before  
inserting into the string.


On Dec 12, 2007, at 8:31 AM, sujitha mary wrote:


hi all,
while executing this cur.execute('insert into seq(id,sequence)  
values(3,'+content+')')
i'm getting an error  psycopg2.ProgrammingError : syntax error at  
or near "prophage"
LINE 1: insert into seq(id,sequence) values(3,Tum2 prophage  
complete...


--
http://mail.python.org/mailman/listinfo/python-list


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Loading an exe icon into a pixmap/bitmap

2007-12-12 Thread kyosohma
On Dec 12, 12:09 am, [EMAIL PROTECTED] wrote:
> I've been searching for a way to load an icon from an executable into
> something that I can eventually display either through pygame or
> pygtk. I've tried the stuff found 
> athttp://groups.google.com/group/comp.lang.python/browse_thread/thread/...
> but the tests just output all black (not surprising, as that guy
> couldn't get it to work either). I've mucked around with taht code and
> the ExtractIcon stuff from pywin32, but still am stuck. I've seen some
> reference to a wxpython ways to do it, but I'm trying to not have to
> convert over to that. Any help is appreciated. Thanks

wxPython uses an included tool called img2py. The source looks pretty
straight-forward. You may be able to modify it or re-create it using
your GUI toolkit of choice.

If you install wxPython, you'll find it (approx.) here:

C:\Python24\Lib\site-packages\wx-2.8-msw-unicode\wx\tools

Mike
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: GetPath to variable

2007-12-12 Thread kyosohma
On Dec 11, 5:42 am, "Connolly" <[EMAIL PROTECTED]> wrote:
> Hey there, new Python user here.
>
> Currently, I'm working on a project in Python for my college work, I chose
> to use Python since I heard of its great advantages, now for designing GUI's
> I'm using the wxPython package.
> What I'm trying to do at the moment is the following;
> A file directory browser is created using self.dialong.ShowModal, when the
> user chooses the folder they want to use for this program, then
> self.dialong.GetPath is created, after this it (would) goes into a variable
> and is stored in a config file I'm creating using the ConfigObj module.
> My problem here is GetPath does not want to be stored in some kind of
> variable, is there a specific method to put this string into a variable to
> store in the config file.
>
> Thanks,
>
> - Connolly

Gabriel is correct. It needs the () at the end. Download the wxPython
demo as it shows the basic usage of most of the widgets, including the
FileDialog.

Mike
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread Neil Cerutti
On 2007-12-11, massimo s. <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm struggling to use the python in-built csv module, and I
> must say I'm less than satisfied. Apart from being rather
> poorly documented, I find it especially cumbersome to use, and
> also rather limited. What I dislike more is that it seems
> working by *rows* instead than by *columns*.

It is very *thoroughly* documented, which is a style that won't
suit every purpose.

> So I have some questions:
> 1) Is there a good tutorial, example collection etc. on the csv
> module that I'm missing?

Just skip to 9.1.5 Examples, and you'll be on your way.

> 2) Is there an alternative csv read/write module?

There are other ways to tackle the data, for example, using an
csv ODBC apaptor. That may or may not seem like an easy solution
to you. It certainly doesn't suit me.

> 3) In case anyone else is as unhappy as me, and no tutorial
> etc. enlighten us, and no alternative is present, anyone is
> interested in an alternative csv module? I'd like to write one
> if it is the case.

I was intimidated by it at first, implemented my own reader
(mostly as a fun parsing exercise), used that for a while, and
then threw it out.

I advise you to spend time staring at the examples, and use the
simplest example the suits your needs. Also search this archives
of this group for examples.

-- 
Neil Cerutti
The pastor will preach his farewell message, after which the choir will sing,
"Break Forth Into Joy." --Church Bulletin Blooper
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread Neil Cerutti
On 2007-12-12, John Machin <[EMAIL PROTECTED]> wrote:
>> It's clear that I am thinking to completely different usages
>> for CSV than what most people in this thread. I use csv to
>> export and import numerical data columns to and from
>> spreadsheets.
>
> For that purpose, CSV files are the utter pox and then some.
> Consider using xlrd and xlwt (nee pyexcelerator) to read (resp.
> write) XLS files directly.

I can vouch for that advice. I was exporting .xls files to csv
text files for over a year before I tried the xlrd solution--the
whole process is less cumbersome now, though it was bewildering
at first working with Excel in Python. Actually, surprises still
crop up now and then, mostly to do with cell types. The advantage
of working with csv was that everything was a string.

-- 
Neil Cerutti
The world is more like it is now than it ever has been before. --Dwight
Eisenhower
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Edu-sig] "do" as a keyword

2007-12-12 Thread kirby urner
> I find that when teaching beginning programmers, they usually think in
> "until" terms, and not "while" terms.
>

If really beginning, an overview of this whole idea of control structures
makes sense, such as this wikipedia article:
http://en.wikipedia.org/wiki/Control_flow

Then explain how Python is very minimalist in its approach, unlike
some languages, which try to provide all kinds of control structure
semantics, including multiple case loops (do... case... case...)
which Python famously does not natively have either.

> they find the "while" logic to be unintuitive, and I often find myself
> feeling the same way: crafting it with the until logic, and then reversing
> it.

I wouldn't make "intuitive" the guiding light in all cases, as it's
often just code for "conditioned reflex" or "what we're used to."
Usually beginners outgrow their initial discomfort, like when
learning to drive stick instead of automatic or whatever.

Kirby
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem parsing lines in a file

2007-12-12 Thread Kees Bakker
barronmo wrote:

> I'm having difficulty getting the following code to work.  All I want
> to do is remove the '0:00:00' from the end of each line.  Here is part
> of the original file:
> 
> 3,3,"Dyspepsia NOS",9/12/2003 0:00:00
>...
> 20,3,"Bubonic plague",11/11/2003 0:00:00
> 
> output = open('my/path/ProblemListFixed.txt', 'w')
> for line in open('my/path/ProblemList.txt', 'r'):
>  newline = line.rstrip('0:00:00')
>  output.write(newline)
> output.close()
> 
> 
> This result is a copy of "ProblemList" without any changes made.  What
> am I doing wrong?  Thanks for any help.

You should feel lucky that it didn't work :-) If you would have used
newline = line.rstrip('0:00:00\n')
you would not have found the problem, nor the solution.
--
Kees

-- 
http://mail.python.org/mailman/listinfo/python-list


How to get milliseconds when substructing datetime objects?

2007-12-12 Thread Dmitri O.Kondratiev
Please help to find simple solutiion for measuring times of operations with
millisecond precision.
For this I am trying to use datetime() objects:

import time
import datetime

def dreamTime(secs):
t1 = datetime.datetime.now()
time.sleep(secs)
t2 = datetime.datetime.now()
dt = t2 - t1
print "Start time: "+str(t1)
print "Stop  time: "+str(t2)
print "Dream time sec: "+str(dt)
"""
# The following code results in errors like:
# AttributeError: 'datetime.timedelta' object has no attribute 'second'
"""
dts = dt.second
dtmicro = dt.microsecond
dtmilli = dts * 1000 + dtmicro / float(1000)
dts2 = dtmilli / float(1000)
print "Dream Millies: "+str(dtmilli)
print "Dream Seconds, again: "+str(dts2)

-

Thanks!

-- 
Dmitri O. Kondratiev
[EMAIL PROTECTED]
http://www.geocities.com/dkondr
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: psycopg

2007-12-12 Thread J. Clifford Dyer
On Wed, Dec 12, 2007 at 09:08:44AM -0500, Calvin Spealman wrote regarding Re: 
psycopg:
> 
>Don't do that, for a number of reasons. String concatenation is really
>never a good idea and formatting your own query strings is exactly what
>leads to things like sql injection. Let the db library handle it for
>you:
> 

If you don't know what a SQL injection is, and you don't feel like googling for 
it, this should give you a good idea of why this matters:

http://xkcd.com/327/

>cur.execute('insert into seq(id,sequence) values(3, %s)', (content,))
> 
>Notice that, although we're using the %s placeholder, we are _not_
>using the % operator to format the string. This is because the db
>module will do any proper preparation of the value for you before
>inserting into the string.
> 
>On Dec 12, 2007, at 8:31 AM, sujitha mary wrote:
> 
>  hi all,
>  while executing this cur.execute('insert into seq(id,sequence)
>  values(3,'+content+')')
>  i'm getting an error  psycopg2.ProgrammingError : syntax error at or
>  near "prophage"
>  LINE 1: insert into seq(id,sequence) values(3,Tum2 prophage
>  complete...
> 
>--
> 
>[1]http://mail.python.org/mailman/listinfo/python-list
> 
> References
> 
>1. http://mail.python.org/mailman/listinfo/python-list

> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread sturlamolden
On 12 Des, 12:56, George Sakkis <[EMAIL PROTECTED]> wrote:

> Ah, the 'make' statement.. I liked (and still do) that PEP, I think it
> would have an impact comparable to the decorator syntax sugar, if not
> more.

I think it is one step closer to Lisp. I believe that it would be
worth considering adding defmacro statement. Any syntax, including if,
else, for, while, class, lambda, try, except, etc.  would be
implemented with defmacros. We would only need a minimalistic syntax,
that would bootstrap a full Python syntax on startup. And as for
speed, we all know how Lisp compares to Python.







-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zipfile content reading via an iterator?

2007-12-12 Thread Tim Chase
Gabriel Genellina wrote:
>> I'm dealing with several large items that have been zipped up to
>> get quite impressive compression.  However, uncompressed, they're
>> large enough to thrash my memory to swap and in general do bad
>> performance-related things.  I'm trying to figure out how to
>> produce a file-like iterator out of the contents of such an item.
> 
> The Time Machine in action again - that's already done, but in SVN. You  
> want the new ZipFile.open(filename) method, which returns a file-like  
> object.

Thanks!  I'll give the 2.6 version a try.

As a side question, is there any catalog of Time Machine items 
(instances where folks have asked for a feature only to have the 
response be "it's already implemented in the development 
version")?  I've seen the Time Machine response several times on 
c.l.p and it would be interesting to thumb through an archive of 
the developers prescience.

-tkc

(sorry if this comes through twice...experienced some mailer 
problems)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread massimo s.
On Dec 12, 2:58 pm, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> On 2007-12-11, massimo s. <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > I'm struggling to use the python in-built csv module, and I
> > must say I'm less than satisfied. Apart from being rather
> > poorly documented, I find it especially cumbersome to use, and
> > also rather limited. What I dislike more is that it seems
> > working by *rows* instead than by *columns*.
>
> It is very *thoroughly* documented, which is a style that won't
> suit every purpose.
> > So I have some questions:
> > 1) Is there a good tutorial, example collection etc. on the csv
> > module that I'm missing?
>
> Just skip to 9.1.5 Examples, and you'll be on your way.

If by "thoroughly" you mean "it actually describes technically what it
is and does but not how to really do things", yes, it is thoroughly
documented.
The examples section is a joke. It gives good examples for the
simplest usage cases (good), then it almost immediately digs into
details like the Unicode stuff, leaving aside the rest. DictWriter and
DictReader are absent from the examples. And also the Sniffer.

And, as a sidenote, why putting something useful like the unicode
decoder-encoders in the example section instead of inserting them
directly in the library?

I don't want to be mean with the author of csv and its docs. I now
understand there are excellent reasons for csv to be done the way it
is, and it's only my fault if I didn't see that before. I also know
first hand that documenting code is hard and boring stuff. Kudos to
anyone doing that, but in the Example section there is surely room for
improvement. It's probably OK for people doing things row-by-row and
that already know perfectly their way in and out all that, but if this
thread teaches us something, is that the same thing can be used for
vastly different purposes.

I will try to submit a patch to the documentation based on examples
coming from here and what I will learn by digging into csv.

> > 3) In case anyone else is as unhappy as me, and no tutorial
> > etc. enlighten us, and no alternative is present, anyone is
> > interested in an alternative csv module? I'd like to write one
> > if it is the case.
>
> I was intimidated by it at first, implemented my own reader
> (mostly as a fun parsing exercise), used that for a while, and
> then threw it out.
>
> I advise you to spend time staring at the examples, and use the
> simplest example the suits your needs. Also search this archives
> of this group for examples.

OK, thanks!

As for people advicing xlrd/xlrwt: thanks for the useful tip, I didn't
know about it and looks cool, but in this case no way I'm throwing
another dependency to the poor users of my software. Csv module was
good because was built-in.

m.
-- 
http://mail.python.org/mailman/listinfo/python-list


__init__ method for containers

2007-12-12 Thread Neil Cerutti
List and deque disagree on what __init__ does. Which one is
right?

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import deque
>>> x = deque([0, 1])
>>> x.__init__([2, 3])
>>> x
deque([0, 1, 2, 3])
>>> y = list([0, 1])
>>> y.__init__([2, 3])
>>> y
[2, 3]

test_deque.py even contains a test verifying its __init__
behavior, so perhaps deque has a good reason to differ from the
behavior of list.

Moreover, both methods use the same doc string, i.e.:

  __init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

When implementing a list-like container extension type, is there
any reason to choose anything other than list-like behavior,
i.e., if you call __init__, you'll initialize the container?
deque's behavior doesn't make sense to me.

-- 
Neil Cerutti
One of the causes of the American Revolution was the English put tacks in
their tea. --History Exam Blooper
-- 
http://mail.python.org/mailman/listinfo/python-list


Stringified list back to list of ints

2007-12-12 Thread Paul McGuire
On Dec 12, 7:25 am, Lee Capps <[EMAIL PROTECTED]> wrote:
> Regular expressions might be a good way to handle this.
>
> import re
>
> s = '[16, 16, 2, 16, 2, 16, 8, 16]'
> get_numbers = re.compile('\d\d*').findall
>
> numbers = [int(x) for x in get_numbers(s)]
>

Isn't '\d\d*' the same as '\d+' ?

And why would you invoke re's when str.split(',') (after stripping
leading and trailing []'s) does the job so well?

numbers = map(int, s.strip('[]').split(','))

Or if map is not to your liking:

numbers = [int(x) for x in s.strip('[]').split(',')]

-- Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Solve a graphical problem about different logos with a script

2007-12-12 Thread jimred73
Hi

I have to bring alot of diffrent company logos into a harmony with
each other.

Therefore I'm looking for a way to measure the relation between white
and colour so that i'm able to scale or shrink   the logo that at
the end, all logos have the same quotient and therefore have the same
optical weight.

The problem is: A squared Logo with a dark background looks heavier
then a wide one with just some black text. Therefore the squared one
has to be resized that it will match with the other.

Is something like this possible with a few lines of python and
imagemagick?

Regards

JIM
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ method for containers

2007-12-12 Thread Calvin Spealman
I agree that the behavior should be more consistant, but you also  
should not be calling __init__ more than once on any given instance  
and that in and of itself should probably constitute undefined behavior.

On Dec 12, 2007, at 3:22 PM, Neil Cerutti wrote:

> List and deque disagree on what __init__ does. Which one is
> right?
>
> Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit  
> (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
 from collections import deque
 x = deque([0, 1])
 x.__init__([2, 3])
 x
> deque([0, 1, 2, 3])
 y = list([0, 1])
 y.__init__([2, 3])
 y
> [2, 3]
>
> test_deque.py even contains a test verifying its __init__
> behavior, so perhaps deque has a good reason to differ from the
> behavior of list.
>
> Moreover, both methods use the same doc string, i.e.:
>
>   __init__(...)
> x.__init__(...) initializes x; see x.__class__.__doc__ for  
> signature
>
> When implementing a list-like container extension type, is there
> any reason to choose anything other than list-like behavior,
> i.e., if you call __init__, you'll initialize the container?
> deque's behavior doesn't make sense to me.
>
> -- 
> Neil Cerutti
> One of the causes of the American Revolution was the English put  
> tacks in
> their tea. --History Exam Blooper
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread Marco Mariani
John Machin wrote:

> For that purpose, CSV files are the utter pox and then some. Consider
> using xlrd and xlwt (nee pyexcelerator) to read (resp. write) XLS
> files directly.

xlwt is unreleased (though quite stable, they say) at the moment, so the 
links are:

easy_install xlrd
svn co https://secure.simplistix.co.uk/svn/xlwt/trunk

-- 
http://mail.python.org/mailman/listinfo/python-list


pygtk theme colors ?

2007-12-12 Thread manatlan
I understand nothing ...
I'm trying to get the color of a normal background window

and when I change my themes (i switch between a light and a dark
theme)
i obtain always the same output below :

style = gtk.Button().get_style()
l=[gtk.STATE_NORMAL,gtk.STATE_ACTIVE,gtk.STATE_PRELIGHT,gtk.STATE_SELECTED,gtk.STATE_INSENSITIVE]
for i in l:
print "- base",i,style.base[i].to_string()
for i in l:
print "- text",i,style.text[i].to_string()
for i in l:
print "- fg",i,style.fg[i].to_string()
for i in l:
print "- bg",i,style.bg[i].to_string()

what's the trouble ?!?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread Marco Mariani
massimo s. wrote:

> As for people advicing xlrd/xlrwt: thanks for the useful tip, I didn't
> know about it and looks cool, but in this case no way I'm throwing
> another dependency to the poor users of my software. Csv module was
> good because was built-in.

The trouble with sending CSV files to Excel (or OpenOffice, or Gnumeric, 
or whatever) is that there is no way to specify the data types.

Unless you're using a predefined worksheet (and refreshing its data from 
the CSV file) the spreadsheet program _won't_ get the data types right. 
Strings will become numbers (and stripped of precious leading zeroes), 
dates may (or may not) become floats or something entirely different. 
Fixed point decimals might grow eyes and bite you.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zipfile content reading via an iterator?

2007-12-12 Thread Gabriel Genellina
En Wed, 12 Dec 2007 12:03:12 -0300, Tim Chase  
<[EMAIL PROTECTED]> escribió:

> As a side question, is there any catalog of Time Machine items
> (instances where folks have asked for a feature only to have the
> response be "it's already implemented in the development
> version")?  I've seen the Time Machine response several times on
> c.l.p and it would be interesting to thumb through an archive of
> the developers prescience.

Not that I know of. Quoting a message from Duncan Booth "First Python  
reference to a time machine I can find is Paul Prescod in April 1998  
proposing its construction, although it was obviously fully operational by  
September 1998."

http://mail.python.org/pipermail/python-list/2003-July/214868.html

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "do" as a keyword

2007-12-12 Thread Chris Mellon
On Dec 11, 2007 2:19 PM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Tue, 11 Dec 2007 15:06:31 +, Neil Cerutti wrote:
>
> > When I use languages that supply do-while or do-until looping constructs
> > I rarely need them.
> ...
> > However, did you have an specific need for a do-while construct? Perhaps
> > we could show you the alternatives.
>
> "Need" is a strong word. After all, all looping constructs could be done
> with a conditional jump, so arguably we don't "need" while or for either.
>
> But loops that run at least once is a basic element of algorithms.
> Perhaps not as common as the zero or more times of the while loop, but
> still fundamental. It is a shame it has to be faked using:
>

I agree that it's fundamental, but I'd like to mention that I've
written many thousands of lines of Python code, from throwaway code
for demonstration to enterprisey servers and all sorts of things in
between and I've *never* written a "1 or more times" loop except when
I was demonstrating that exact thing. One thing that Python has
definitely changed my thinking about is that I tend to formulate both
problems and solutions in terms of iteration over sequence rather than
as traditional conditional based looping. If I need a "1 or more" loop
I formulate the problem as a sequence of 1 or more elements.

This isn't something I go out of my way to do, simply the way I think
about problems in Python.

I just did a scan of some of the code I've been working on for the use
of while. This is a few thousand LOC (and one project, an SVN
renderer, doesn't have any use if it at all) and I've got exactly 3
instances.

I've got a while True: as the mainloop of a thread, which technically
qualifies as a "1 or more" loop but doesn't count because the exit
condition is complicated and couldn't be easily written with do..until
(you'd either break the same way or you'd have to set a flag instead,
which is ugly).

I've got the use of while as a counter in some code that unpacks bits
from an integer (interop with C structures). It's a 0 or more loop.

And I've got an iterator for a linked list, which is close but I don't
want to yield anything if the head is None, so it's in a plain while:
style:

def linkedListIter(head, nextName="next"):
current = head
while current:
yield current
current = getattr(current, nextName)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Stringified list back to list of ints

2007-12-12 Thread Calvin Spealman
I still hold my vote that if you need to reverse the  
"stringification" of a list, you shouldn't have stringified the list  
and lost hold of the original list in the first place. That is the  
solution above all others.

On Dec 12, 2007, at 10:26 AM, Paul McGuire wrote:

> On Dec 12, 7:25 am, Lee Capps <[EMAIL PROTECTED]> wrote:
>> Regular expressions might be a good way to handle this.
>>
>> import re
>>
>> s = '[16, 16, 2, 16, 2, 16, 8, 16]'
>> get_numbers = re.compile('\d\d*').findall
>>
>> numbers = [int(x) for x in get_numbers(s)]
>>
>
> Isn't '\d\d*' the same as '\d+' ?
>
> And why would you invoke re's when str.split(',') (after stripping
> leading and trailing []'s) does the job so well?
>
> numbers = map(int, s.strip('[]').split(','))
>
> Or if map is not to your liking:
>
> numbers = [int(x) for x in s.strip('[]').split(',')]
>
> -- Paul
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


RE: xmlrpclib.binary to a file doubt

2007-12-12 Thread Jose Ignacio Gisbert
I have resolved my problem!, I think it is easy for everyone, but I unknown
“data” binary object attribute. So, code for store a file that is passed as
a binary object would be:

file = calltoreceivemethod()

placetostore=open(filename,'wb')

data=file.data

placetostore.write(data)

 

Regards,

Naxo

 

  _  

De: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] En
nombre de Jose Ignacio Gisbert
Enviado el: miércoles, 12 de diciembre de 2007 9:12
Para: python-list@python.org
Asunto: xmlrpclib.binary to a file doubt

 

Hi all,

 

I have a problem in my application which uses xml-rpc methods. From one hand
I can send a file to a server doing 

 

f=open(Myfile,'rb')

g=f.read()

name=(f.name).split("/")

name=name[len(name)-1]

calltosendmethod(xmlrpclib.Binary(g),name)

 

And it works fine. But on the other hand I have to get a document from the
server, which method returns document content in xmlrpclib.binary format,
and I am not able to store it on a local file. If somebody knows how to,
please tell me something, anything will helps. 

 

Thanks in advance, 

___
José Ignacio Gisbert Sanus

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is a "real" C-Python possible?

2007-12-12 Thread Chris Mellon
On Dec 12, 2007 8:36 AM, sturlamolden <[EMAIL PROTECTED]> wrote:
> On 12 Des, 12:56, George Sakkis <[EMAIL PROTECTED]> wrote:
>
> > Ah, the 'make' statement.. I liked (and still do) that PEP, I think it
> > would have an impact comparable to the decorator syntax sugar, if not
> > more.
>
> I think it is one step closer to Lisp. I believe that it would be
> worth considering adding defmacro statement. Any syntax, including if,
> else, for, while, class, lambda, try, except, etc.  would be
> implemented with defmacros. We would only need a minimalistic syntax,
> that would bootstrap a full Python syntax on startup. And as for
> speed, we all know how Lisp compares to Python.
>

You say that as if "one step closer to Lisp" is a worthwhile goal.

Python has not become what it is, and achieved the success it has,
because a bunch of people really wanted to use Lisp but didn't think
other people could handle it.

The goal of these sorts of discussions should be to make Python a
better Python. But what happens far too often (especially with
Lispers, but not just them by any means) is that people want to make
Python into a clone or "better" version of whatever other language
they like.

If you're the sort of person who views lisp as the goal that other
languages should aspire to, and I know many of those people exist and
even frequent this list, then you should probably spend your time and
energy on making Lisp a better Lisp and addressing whatever weaknesses
in Lisp have you using Python instead. Trying to fix Lisp (or
whatever) by transforming Python into it isn't going to make you any
happier, and it's just going to derail any discussion of making Python
a better *Python*.
-- 
http://mail.python.org/mailman/listinfo/python-list


problem pickling a function

2007-12-12 Thread Emin.shopper Martinian.shopper
Dear Experts,

I love the pickle module, but I occasionally have problems pickling a
function. For example, if I create an instance g of class f and assign
g.xto a function, then I cannot pickle g (example code below). I know
that I
can pickle f separately if I want to, and I understand why I get the
pickling error.

But is there a way to assign functions to instances of a class without
preventing pickleability? It doesn't seem unreasonable to me to want to
assign functions to instances of a class (after all functions are first
class objects, so why shouldn't I be able to pass them around?) Is there a
better way or is this just a limitation of pickle?

Example code illustrating problem is below:

>>> class f: pass
>>> g = f()
>>> g.x = ','.join
>>> import pickle; pickle.dumps(g)
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\lib\pickle.py", line 1366, in dumps
Pickler(file, protocol).dump(obj)
  File "C:\Python25\lib\pickle.py", line 224, in dump
self.save(obj)
  File "C:\Python25\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
  File "C:\Python25\lib\pickle.py", line 725, in save_inst
save(stuff)
  File "C:\Python25\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
  File "C:\Python25\lib\pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
  File "C:\Python25\lib\pickle.py", line 663, in _batch_setitems
save(v)
  File "C:\Python25\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
  File "C:\Python25\lib\pickle.py", line 748, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle : it's not found as __main__.join
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: __init__ method for containers

2007-12-12 Thread Neil Cerutti
On 2007-12-12, Calvin Spealman <[EMAIL PROTECTED]> wrote:
> I agree that the behavior should be more consistant, but you
> also  should not be calling __init__ more than once on any
> given instance  and that in and of itself should probably
> constitute undefined behavior.

That seems wise to me, too, but the the explicit __init__ test in
test_deque seems to argue otherwise. Maybe the test in error.

Moreover, the behavior of deque.__init__ may actually contradict
its doc string.

>>> help(deque.__init__)
Help on wrapper_descriptor:

__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

-- 
Neil Cerutti
A song fest was hell at the Methodist church Wednesday. --Church Bulletin
Blooper
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Edu-sig] "do" as a keyword

2007-12-12 Thread michel paul
> they find the "while" logic to be unintuitive

I've found that a good way to explain 'while' is to consider it as an 'if'
statement that repeats.  Kids grasp simple conditionals fairly easily.  I
would sometimes hear them talk about 'if loops' when they were actually
trying to discuss conditional statements, and I would say to them hey look,
there's no such THING as an 'if loop'!  But then it occurred to me - well,
that's what a while statement is!  So, let's just take a simple conditional,
change the 'if' to 'while', and there you have a loop!  Only problem, it
will keep repeating forever, so now we have to consider ways to get it to
stop.

Speaking from my own learning experiences, 'while True' initially seemed
awkward, probably for the same reason 'if True' would - why bother?  But at
this point I actually do like the 'while True' structure.

- Michel Paul

On Dec 12, 2007 3:01 AM, Brian Blais <[EMAIL PROTECTED]> wrote:

> On Dec 11, 2007, at Dec 11:11:11 PM, Terry Reedy wrote:
>
>
> "Steven D'Aprano" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> ||
> | But loops that run at least once is a basic element of algorithms.
> | Perhaps not as common as the zero or more times of the while loop, but
> | still fundamental. It is a shame it has to be faked using:
> |
> | while True: # force the first iteration to always run
> |process
> |if condition: break
> |
> | Ugly and misleading.
>
> I disagree.  Nothing is being faked.  The generic loop is
>
> while True:
> pre_process
> if condition: break
> post_process
>
>
> I find that when teaching beginning programmers, they usually think in
> "until" terms, and not "while" terms.
>
> do:
> Forward()
> until Touched()
>
> and I have to explain to them that Python doesn't have "until", and that
> the logic for while is exactly the opposite:
>
> while not Touched():
> Forward()
>
> they find the "while" logic to be unintuitive, and I often find myself
> feeling the same way: crafting it with the until logic, and then reversing
> it.  Perhaps I should do as above, and do:
>
> while True:
> Forward()
> if Touched(): break
>
> but somehow that feels wrong to me, like bypassing the point of the while:
> all that power to check for conditions, and you just use it to check True,
> and then use a break inside.  It's readable, I guess, but not a programming
> construct I am immediately drawn to.
>
>
> Brian Blais
>
>
> --
> Brian Blais
> [EMAIL PROTECTED]
> http://web.bryant.edu/~bblais 
>
>
>
>
> ___
> Edu-sig mailing list
> [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/edu-sig
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is anyone happy with csv module?

2007-12-12 Thread Neil Cerutti
On 2007-12-12, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> John Machin <[EMAIL PROTECTED]> wrote:
>> For that purpose, CSV files are the utter pox and then some.
>> Consider using xlrd and xlwt (nee pyexcelerator) to read
>> (resp. write) XLS files directly.
>
> FWIW, CSV is a much more generic format for spreadsheets than
> XLS. For example, I deal almost exclusively in CSV files for
> simialr situations as the OP because I also work with software
> that can't (or in some cases "can't easily") deal with XLS
> files.  CSV files can be read in by basically anything.

When I have a choice, I use simple tab-delimited text files.  The
usually irrelevent limitation is the inability to embed tabs or
newlines in fields. The relevant advantage is the simplicity.

-- 
Neil Cerutti
The recording I listened to had Alfred Brendel doing the dirty work of
performing this sonata (Liszt B minor) --Music Lit Essay
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TCP reset caused by socket.py

2007-12-12 Thread Object01
On Dec 11, 6:17 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Tue, 11 Dec 2007 10:51:13 -0300, Object01 <[EMAIL PROTECTED]> escribi�:
>
> > I've been working with the source code for Trac (http://
> > trac.edgewall.org/) lately and have run across a bizarre problem.  It
> > seems that all POST requests to Trac's standalone server (tracd) have
> > a random chance of causing the server to issue a TCP RST packet that
> > resets the connection.
>
> > Running Trac 10.3.1 on Win2K3 using Python 2.4, watching traffic with
> > Wireshark 0.99.5.
>
> > I've been stepping through the code using Winpdb 1.3.2 and have
> > isolated the problem to the socket.py that's included in Python 2.4.
> > Line 157, in _socketobject.close():
>
> >   self.send = self.recv = self.sendto = self.recvfrom =
> > self._sock._dummy
>
> > is what's causing the TCP RST to be issued.  If I set a breakpoint on
> > that line and step over it on a POST request, there's about an 80%
> > chance the server will issue a TCP RST.  When debugging, the entire
> > response makes it onto the wire before TCP RST is issued.  If I'm -
> > not- debugging, it's anybody's guess as to how much of the response
> > makes it out. The interruption, when it occurs, always seems to be
> > between calls to _fileobject.write().  This indicates a timing issue:
> > perhaps buffered data isn't being waited on properly prior to
> > the .close() method doing its work.
>
> I think the trigger is the line just above that, where the "real" socket
> is deleted. A RST when you close a socket is OK. From your description it
> appears that the close method should not have been called at that time;
> I'd look at the stack and see why is it being called when it shouldn't.
>
> I don't believe Python itself randomly calls close() so it looks like a
> Trac problem, or in the webserver used. Is there any timer used? Is the
> server multithreaded?
>
> --
> Gabriel Genellina

The server is multithreaded, handling each request on its own thread.
But is a RST really a part of a valid close operation?  It was my
understanding that the RST is used to indicate a problem with the
connection, usually a half-open connection.  I never see RSTs unless
this error occurs.

Looking at the stack, it appears the socket is being closed at the
right time.  All data has been sent and the request handler is in its
tear-down phase, expecting no more data from the client (who never
sends any).  I don't see different stack traces between error and no-
error requests.

And on line 156, how is the real socket being deleted?  It (the _sock
member) appears to be set to a dummy class that has no real
functionality.

--
Jeff S.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is a "real" C-Python possible?

2007-12-12 Thread J. Clifford Dyer
On Wed, Dec 12, 2007 at 06:36:49AM -0800, sturlamolden wrote regarding Re: Is a 
"real" C-Python possible?:
> 
> On 12 Des, 12:56, George Sakkis <[EMAIL PROTECTED]> wrote:
> 
> > Ah, the 'make' statement.. I liked (and still do) that PEP, I think it
> > would have an impact comparable to the decorator syntax sugar, if not
> > more.
> 
> I think it is one step closer to Lisp. I believe that it would be
> worth considering adding defmacro statement. Any syntax, including if,
> else, for, while, class, lambda, try, except, etc.  would be
> implemented with defmacros. We would only need a minimalistic syntax,
> that would bootstrap a full Python syntax on startup. And as for
> speed, we all know how Lisp compares to Python.
> 

Programmable syntax is a very powerful concept.  However, python is designed 
not only to be powerful, but simple, and this change would drastically reduce 
the simplicity of Python.  It would cease to be a good beginner's language.  If 
you want a language with different syntax than python, python has wonderful 
parsing libraries.  Use those instead.

My 2?.

Cheers,
Cliff

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: problem pickling a function

2007-12-12 Thread Calvin Spealman
On Dec 12, 2007, at 11:01 AM, Emin.shopper Martinian.shopper wrote:

> Dear Experts,
>
> I love the pickle module, but I occasionally have problems pickling  
> a function. For example, if I create an instance g of class f and  
> assign g.x to a function, then I cannot pickle g (example code  
> below). I know that I can pickle f separately if I want to, and I  
> understand why I get the pickling error.

This all has to do with how pickles are able to recreate the objects  
in question. Pickle can only handle things, basically, with global  
names. This pretty much means it can handle regular classes and  
module functions, but not the methods of those classes. Why? Why  
would it have to? If it can handle the class it just expects that  
name to be the same class when it brings it back up, it doesn't  
actually store the class or function itself, just the name.

> But is there a way to assign functions to instances of a class  
> without preventing pickleability? It doesn't seem unreasonable to  
> me to want to assign functions to instances of a class (after all  
> functions are first class objects, so why shouldn't I be able to  
> pass them around?) Is there a better way or is this just a  
> limitation of pickle?

Presumably you could do something with __getstate__ and __setstate__  
methods, but this is many cases of "Are you really sure you want to  
do that?"

-- 
http://mail.python.org/mailman/listinfo/python-list


Newbie NameError problem

2007-12-12 Thread MartinRinehart
I don't understand what I don't understand in the following:
--
# reader.py - testing char-by-char marching methods

f = open('sample_decaf.d', 'r')
text = f.readlines()
f.close()

# this is C-style, 15 lines, in Python:
end_line = len(text)
line_ptr = 0

while line_ptr < end_line:

input = text[line_ptr]
output = ''
char_ptr = 0
end_char = len(input)

while char_ptr < end_char:
output += input[char_ptr]
char_ptr += 1

print output,
line_ptr += 1

# this is Python, 7 lines:

for line in text:
output = ''

for char in line:
output += char

print output,

# but I need locations, so this is impure, 11-line, Python:

line_ptr = 0
for line in text:
output = ''
char_ptr = 0

for char in line:
output += char
char_ptr += 1

print output,
line_ptr += 1

# with a Loc object, 10 lines (not counting the Loc class):

loc = Loc(0,0) # Name error: name 'Loc' is not defined
for line in text:
output = ''

for char in line:
output += char
loc.nextChar()

print output + Loc.repr(),
loc.nextLine()

class Loc:
line = 0
char = 0

def __init__(self, l, c):
line = l
char = c

def nextChar(self):
char += 1

def nextLine(self):
line += 1
char = 0

def repr(self):
return 'Loc: line='+str(line)+', char='+str(char)

# end of class Loc

# end of reader.py
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ method for containers

2007-12-12 Thread Raymond Hettinger
On Dec 12, 7:22 am, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> List and deque disagree on what __init__ does. Which one is
> right?

File a bug report and assign to me.


Raymond
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is Python really a scripting language?

2007-12-12 Thread oj
On Dec 12, 4:34 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote:
> "Ron Provost" <[EMAIL PROTECTED]> wrote in message
>
> news:[EMAIL PROTECTED]
> But here's my problem, most of my coworkers, when they see my apps and
> learn that they are written in Python ask questions like, "Why would you
> write that in a scripting language?"  Whenever I hear a comment like that I
> can feel myself boiling inside.
> ===
>
> I don't blame you.  Python is an full-fledged algorithm/programming
> language that was designed to *also* be used a scripting language.

It depends on your definition of scripting language, I guess.

Python it byte-compiled and run in an interpreter. Much like how Java
is run, only the compilation of python scripts is usually hidden from
the user.

You could argue that python is no more of a scripting language then
Java.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ method for containers

2007-12-12 Thread Calvin Spealman
On Dec 12, 2007, at 4:05 PM, Neil Cerutti wrote:

> On 2007-12-12, Calvin Spealman <[EMAIL PROTECTED]> wrote:
>> I agree that the behavior should be more consistant, but you
>> also  should not be calling __init__ more than once on any
>> given instance  and that in and of itself should probably
>> constitute undefined behavior.
>
> That seems wise to me, too, but the the explicit __init__ test in
> test_deque seems to argue otherwise. Maybe the test in error.
>
> Moreover, the behavior of deque.__init__ may actually contradict
> its doc string.

It documents that deque.__init__ initializes it, as all __init__  
methods do. All init methods are also assumed to _only_ be called at  
the start of the life of the object and never more than once, so  
breaking that breaks assumption and thus the documentation isn't  
wrong because you are trying to apply it to a state that shouldn't  
exist. This like saying saying claims of cigarettes causing cancer  
are false if you shoot someone before they get the cancer. Well, you  
aren't supposed to shoot people, so that doesn't count.

 help(deque.__init__)
> Help on wrapper_descriptor:
>
> __init__(...)
> x.__init__(...) initializes x; see x.__class__.__doc__ for  
> signature
>
> -- 
> Neil Cerutti
> A song fest was hell at the Methodist church Wednesday. --Church  
> Bulletin
> Blooper
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie NameError problem

2007-12-12 Thread Calvin Spealman
On Dec 12, 2007, at 11:26 AM, [EMAIL PROTECTED] wrote:

> I don't understand what I don't understand in the following:

You also don't understand how to ask for help properly. Your example  
is too large, for one. You want a "minimal working example" (http:// 
ironfroggy-code.blogspot.com/2007/02/minimal-working-examples-how-to- 
why-and.html) where working means it works to demonstrate the problem  
you are having. You need to actually show us what is breaking, which  
means the traceback that gives you the NameError.

As for the problem itself, I am not going to look through all that  
code or run it myself. That is why you want minimal examples and to  
include the errors for us to read. But, if you don't know, a  
NameError means you tried to use a variable before assigning to it,  
so look for that. It could also be as simple as misspelling something.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread J. Clifford Dyer
On Wed, Dec 12, 2007 at 10:08:38AM -0600, [EMAIL PROTECTED] wrote regarding Re: 
Is anyone happy with csv module?:
> 
> FWIW, CSV is a much more generic format for spreadsheets than XLS.
> For example, I deal almost exclusively in CSV files for simialr situations
> as the OP because I also work with software that can't (or in some
> cases "can't easily") deal with XLS files.  CSV files can be read in
> by basically anything.

Compatibility-wise, yes, CSV is much more generic.  But spreadsheet != ledger 
paper.  Spreadsheets incorporate many functions (as simple as summation!) that 
CSV cannot handle.  Thus functionality-wise CSV is infact a very specific 
subset of spreadsheets, and is not generic in the slightest.

But the software you are dealing with probably doesn't actually need 
spreadsheets.  It just needs digital ledgers.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pygtk theme colors ?

2007-12-12 Thread manatlan
I've found ...
In fact, you'll need to "realize" the window, and you should obtain
the real gtk theme style  (if you didn't realize the window, you
obtain the default gtk theme style)

(I post it here, so i could find it in the future again ;-)

w = gtk.Window()
w.realize()
style=w.get_style()

l=[gtk.STATE_NORMAL,gtk.STATE_ACTIVE,gtk.STATE_PRELIGHT,gtk.STATE_SELECTED,gtk.STATE_INSENSITIVE]
for i in l:
print "- base",i,style.base[i].to_string()
for i in l:
print "- text",i,style.text[i].to_string()
for i in l:
print "- fg",i,style.fg[i].to_string()
for i in l:
print "- bg",i,style.bg[i].to_string()


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem pickling a function

2007-12-12 Thread Emin.shopper Martinian.shopper
On Dec 12, 2007 11:48 AM, Calvin Spealman <[EMAIL PROTECTED]>
wrote:

> On Dec 12, 2007, at 11:01 AM, Emin.shopper Martinian.shopper wrote:
>
> > But is there a way to assign functions to instances of a class
> > without preventing pickleability? It doesn't seem unreasonable to
> > me to want to assign functions to instances of a class (after all
> > functions are first class objects, so why shouldn't I be able to
> > pass them around?) Is there a better way or is this just a
> > limitation of pickle?
>
> Presumably you could do something with __getstate__ and __setstate__
> methods, but this is many cases of "Are you really sure you want to
> do that?"
>
>
Why is it unreasonable to want to pass functions as arguments to classes? If
functions are first class arguments, that seems perfectly reasonable to me.

I guess the best work around is to put the desired function into a
staticmethod as shown below:

>>> class f: pass
>>> g = f()
>>> class j:
@staticmethod
def join(*args,**kw):
 return ','.join(*args,**kw)
>>> g.x=j
>>> import pickle; pickle.dumps(g)
'c__main__\nj\np0\n.

The above provides a way to pickle functions but it seems like a bit of a
hack...
-- 
http://mail.python.org/mailman/listinfo/python-list

DBApi Question with MySQL

2007-12-12 Thread Hans Müller
Hi,

I'd like to modify some tables in a database in one transaction.
This approach doesn't work:

import MySQLdb

con = MySQLdb.connect("servernam", user = "username", passwd = "verysecret, db 
= "test", use_unicode = True, charset = "utf8")

cursor = con.cursor()

con.begin()

cursor.execute("delete from table")

do-some-stuff and wait

cursor.execute("insert into table value(%s, %s)", (1, 2))

con.commit()


When I look into the databse while the script is running, all rows from table 
are gone.
The expected behavior would be to see the new lines only when the script is 
finished.
The deletion should be (since inside a transaction) invisible up to the 
commit().

Has someone an idea how to use transactions correctly ?

What I need is this

start transaction

delete a lot of date in some tables (about 2 million rows)

insert a lot of new date in these tables (also about 2 million lines)

commit all changes, so all changes become visible here and only here.


Thanks a lot,

Greetings
Hans
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread Shane Geiger
Neil Cerutti wrote:
> On 2007-12-12, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>   
>> John Machin <[EMAIL PROTECTED]> wrote:
>> 
>>> For that purpose, CSV files are the utter pox and then some.
>>> Consider using xlrd and xlwt (nee pyexcelerator) to read
>>> (resp. write) XLS files directly.
>>>   
>> FWIW, CSV is a much more generic format for spreadsheets than
>> XLS. For example, I deal almost exclusively in CSV files for
>> simialr situations as the OP because I also work with software
>> that can't (or in some cases "can't easily") deal with XLS
>> files.  CSV files can be read in by basically anything.
>> 
>
> When I have a choice, I use simple tab-delimited text files.  The
> usually irrelevent limitation is the inability to embed tabs or
> newlines in fields. The relevant advantage is the simplicity.
>   

That is very unnecessary.  You can have your tabs and not eat them, too:



#!/usr/bin/python
"""
EXAMPLE USAGE OF PYTHON'S CSV.DICTREADER FOR PEOPLE NEW TO PYTHON AND/OR
CSV.DICTREADER

Python - Batteries Included(tm)

This file will demonstrate that when you use the python CSV module, you
don't have to remove the newline characters, as between "acorp_ Ac" and
"orp Foundation" and other parts of the data below.

It also demonstrates python's csv.DictReader, which allows you to read a
CSV record into a dictionary.

This will also demonstrate the use of lists ([]s) and dicts ({}s).

If this doesn't whet your appetite for getting ahold of a powertool
instead of sed for managing CSV data, I don't know what will.

"""

  FIRST: CREATE A TEMPORARY CSV FILE FOR DEMONSTRATION PURPOSES
mycsvdata = """
"Category","0","acorp_ Ac
orp Foundation","","","Acorp Co","(480) 905-1906","877-462-5267 toll
free","800-367-2228","800-367-2228","[EMAIL PROTECTED]
g","7895 East Drive","Scottsdale","AZ","85260-6916","","","","","","Pres
Fred & Linda ","0","0","1","3","4","1"

"Category","0","acorp_ Bob and Margaret Schwartz","","","","317-321-6030
her","317-352-0844","","","","321 North Butler Ave.","In
dianapolis","IN","46219","","","","","","Refrigeration
man","0","1","2","3","4","0"

"Category","0","acorp_ Elschlager,
Bob","","","","","702-248-4556","","","[EMAIL PROTECTED]","7950 W.
Flamingo Rd. #2032","Las Vega
s","NV","89117","","","","","","guy I met","0","1","2","3","4","1"

"""

##  NOTE:  IF YOU HAVE A RECORD SEPARATOR WITHIN QUOTES, IT WILL NOT BE
TREATED LIKE A RECORD SEPARATOR!
##   Beef|"P|otatos"|Dinner Roll|Ice Cream


import os, sys
def writefile(filename, filedata, perms=750):
f = open(filename, "w")
f.write(filedata)
os.system("chmod "+str(perms)+" "+filename)
f.close()

file2write = 'mycsvdata.txt'
writefile(file2write,mycsvdata)

# Check that the file exists
if not os.path.exists(file2write):
print "ERROR: unable to write file:", file2write," Exiting now!"
sys.exit()

#   ...so everything down to this point merely creates the
# temporary CSV file for the code to test (below).



  SECOND:  READ IN THE CSV FILE TO CREATE A LIST OF PYTHON
DICTIONARIES, WHERE EACH
#  DICTIONARY CONTAINS THE DATA FROM ONE ROW.  THE KEYS OF THE
DICTIONARY WILL BE THE FIELD NAMES
#  AND THE VALUES OF THE DICTIONARY WILL BE THE VALUES CONTAINED WITHIN
THE CSV FILE'S ROW.

import csv

### NOTE: Modify this list to match the fields of the CSV file.
header_flds =
['cat','num','name','blank1','blank2','company','phone1','phone2', \
  
'phone3','phone4','email','addr1','city','state','zip','blank3', \
  
'blank4','blank5','blank6','blank7','title','misc1','misc2','misc3', \
   'mics4','misc5','misc6']

file2open = 'mycsvdata.txt'

reader = csv.DictReader(open(file2open), [], delimiter=",")
data = []
while True:
try:
# Read next "header" line (if there isn't one then exit the loop)
reader.fieldnames = header_flds
rdr = reader.next()
data.append(rdr)
except StopIteration: break


def splitjoin(x):
""" This removes any nasty \n that might exist in a field
(of course, if you want that in the field, don't use this)
"""
return ''.join((x).split('\n'))


  THIRD: ITERATE OVER THE LIST OF DICTS (IN WHICH EACH DICT IS A
ROW/RECORD FROM THE CSV FILE)

# example of accessing all the dictionaries once they are in the list
'data':
import string
for rec in data:   # for each CVS record
itmz = rec.items()  # get the items from the dictionary
print "- = " * 20
for key,val in itmz:
print key.upper()+":  \t\t",splitjoin(val)
# Note: splitjoin() allows a record to contain fields
with newline characters






-- 
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED]  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie NameError problem

2007-12-12 Thread cokofreedom
On Dec 12, 5:51 pm, Paul Rudin <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] writes:
> > I don't understand what I don't understand in the following:
>
> I haven't tried to understand what your code is doing - but the
> NameError arises because you try to use Loc before its definition. Put
> the definition first and the error should go away.

Class Loc must be placed ABOVE the code that uses it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread sturlamolden
On 12 Des, 17:44, "J. Clifford Dyer" <[EMAIL PROTECTED]> wrote:

> Programmable syntax is a very powerful concept.

You don't have to use the programmable syntax just because it's there.
But I do realize it would be a misfeature if it is abused.

Two points:

* Programmable syntax would make it easier to write an efficient
native compiler. The compiler would only need to know about the small
subset of language used for bootstrapping (i.e. any high-level OOP
constructs could emerge from defmacros).

* Numerical extensions like NumPy create a temporary array when
expressions like '(a+b)*(c+d)' is evaluated. This is because the
overloaded operators do not see the whole expression. Programmable
syntax is a remedy for this.



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread sturlamolden
On 12 Des, 17:00, "Chris Mellon" <[EMAIL PROTECTED]> wrote:

> Python has not become what it is, and achieved the success it has,
> because a bunch of people really wanted to use Lisp but didn't think
> other people could handle it.
>
> The goal of these sorts of discussions should be to make Python a
> better Python.

I do not want to use Lisp. The syntax is awkward and strange, and does
not fit in my brain. I cannot read Lisp code and get a mental image of
what it does. Readability is what sets Python apart.

But I do think programmable syntax can make Python a better Python,
just as it made Lisp a better Lisp.





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help needed with python unicode cgi-bin script

2007-12-12 Thread weheh
Hi Duncan, thanks for the reply.
>>
> FWIW, the code you posted only ever attempted to set the character set
> encoding using an html meta tag which is the wrong place to set it. The
> encoding specified in the HTTP headers always takes precedence. This is 
> why
> the default charset setting in Apache was the one which applied.
>
> What you should have been doing was setting the encoding in the content-
> type header. i.e. in this line of your code:
>
>   print u"""Content-Type: text/html
>
> You should have changed it to read:
>
>   Content-Type: text/html; charset=windows-1252
>
> but because you didn't Apache was quietly changing it to read:
>
>   Content-Type: text/html; charset=utf-8
>
Will this work under the following situation? Let's say the user is filling 
out a text field on a form on my website. The user has their browser 
encoding set to utf8. My website has charset=windows-1252 as you indicate 
above. Will I run into a conflict somewhere? 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie NameError problem

2007-12-12 Thread Paul Rudin
[EMAIL PROTECTED] writes:

> I don't understand what I don't understand in the following:

I haven't tried to understand what your code is doing - but the
NameError arises because you try to use Loc before its definition. Put
the definition first and the error should go away.
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ method for containers

2007-12-12 Thread Raymond Hettinger
On Dec 12, 8:41 am, Calvin Spealman <[EMAIL PROTECTED]>
wrote:
> It documents that deque.__init__ initializes it, as all __init__
> methods do. All init methods are also assumed to _only_ be called at
> the start of the life of the object and never more than once, so
> breaking that breaks assumption and thus the documentation isn't
> wrong because you are trying to apply it to a state that shouldn't
> exist.

That's overstating the case somewhat.  The init methods are
*typically* called only once but they are not *assumed* to be called
only once. They are in-fact just like any other method except that
their first invocation is automatic.  The clear and re-initialize
behavior of __init__ for lists is evidence.  If list.__init__ was
assumed to be called only once, there would be no need for the step
that clears-out previous values.

Also, it is not obvious what the "right" behavior is.  While
list.__init__ clears previous values, dict.__init__ does not.

In the case of deque.__init__, the lack of clearing behavior is a bug
because the API aspires to mimic lists as much as possible.


Raymond
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie NameError problem

2007-12-12 Thread Sion Arrowsmith
 <[EMAIL PROTECTED]> wrote:
>I don't understand what I don't understand in the following:
> [ ... ]

You've already got an answer as to what's causing your name error.
But that's not your only problem. It looks like you need an
introduction to enumerate():

for line_ptr, text in enumerate(file('sample_decaf.d')):
for char_ptr, char in enumerate(text):
# whatever you want to do with your chacter-by-character
# processing with line and charater positions

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Improvements to the Python core (was: Is a "real" C-Python possible?)

2007-12-12 Thread Christian Heimes
Kay Schluehr wrote:
 > Given that the Python core team has been mostly silent about JIT
> compilation and Armin Rigos work in particular which started 5 years
> ago ( Psyco will not be promoted towards Python 3.0 and there is no
> indication that anyone but Armin would maintain Psyco ) I wonder about
> this sudden insight. But maybe you can tell us more when you work on
> such stuff for CPython yourself? Given your status in the core Python
> team this would have a chance of not being just a loosely coupled
> independent project as almost all the interesting stuff that happens
> around Python these days.

I don't see an indication that anybody but the creator of Psyco does
understand the code base. *g*

Guido has stated his opinion about optimizations more than once. My own
opinion as core developer (which is quite similar to Guido's opinion) is:

We are happy and glad for every improvement regarding speed, memory
usage or features if and only if:

 * The implementation must be clean, well designed, well documented well
written and platform independent / supported on all platforms. Python
runs on machines from mobile phones to large main frames.

 * The improvement must NOT hinder or slow down future development at
all cost. If it's so complicated that it might slow down future
development than it's a no go. It's more important to us to have a clean
and understandable code base than to add hundreds of small improvements
which makes debugging a nightmare.

 * You are willing to support and fix the improvement for X years where
X is between 4 and INF years.

 * The modification must not slow down Python for common uses like a
single threaded, single CPU bound program or small script. This rules
out all existing attempts to remove the GIL from Python since they have
slowed down Python to 50% or less. However Guido said a few months ago
that he would endorse a SMP branch of Python aimed to multi core and
multi threaded apps.

 * The code and all its dependencies must be compatible with Python license.

 * The code must be written following the C89 standards.

By the way core development is open for everybody. Any patch is
appreciated, starting from fixing a typo in the docs over bug reports,
bug fixes to new features.

Read the PEPs! http://www.python.org/dev/peps/
Subscribe to the mailing lists (I suggest gmane.org)!
Get involved!

Christian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: DBApi Question with MySQL

2007-12-12 Thread Jeff McNeil
Which storage engine are you using?  My assumption is that you're using
standard MyISAM tables, which will not support what you're trying to do.  If
you run the code below against MySQL tables created using InnoDB, it should
work as expected.
See http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-transactions.html.

-Jeff


On 12/12/07, Hans Müller <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I'd like to modify some tables in a database in one transaction.
> This approach doesn't work:
>
> import MySQLdb
>
> con = MySQLdb.connect("servernam", user = "username", passwd =
> "verysecret, db = "test", use_unicode = True, charset = "utf8")
>
> cursor = con.cursor()
>
> con.begin()
>
> cursor.execute("delete from table")
>
> do-some-stuff and wait
>
> cursor.execute("insert into table value(%s, %s)", (1, 2))
>
> con.commit()
>
>
> When I look into the databse while the script is running, all rows from
> table are gone.
> The expected behavior would be to see the new lines only when the script
> is finished.
> The deletion should be (since inside a transaction) invisible up to the
> commit().
>
> Has someone an idea how to use transactions correctly ?
>
> What I need is this
>
> start transaction
>
> delete a lot of date in some tables (about 2 million rows)
>
> insert a lot of new date in these tables (also about 2 million lines)
>
> commit all changes, so all changes become visible here and only here.
>
>
> Thanks a lot,
>
> Greetings
> Hans
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Newbie NameError problem

2007-12-12 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> I don't understand what I don't understand in the following:

You already have the answer (hint: a Python module is sequentially 
executed when loaded by the interpreter)

Just a couple side notes:

  > # but I need locations, so this is impure, 11-line, Python:
> 
> line_ptr = 0
> for line in text:
> output = ''
> char_ptr = 0
> 
> for char in line:
> output += char
> char_ptr += 1
> 
> print output,
> line_ptr += 1


Use enumerate and a for loop.

for line_ptr, line in enumerate(text):
   for char_ptr, char in enumerate(line):
  print "char %s of line %s is %s" % (char_ptr, line_ptr, char)

> # with a Loc object, 10 lines (not counting the Loc class):
> 
> loc = Loc(0,0) # Name error: name 'Loc' is not defined

Indeed. It is not yet defined at this time. Remember that in Python, 
almost everything happens at runtime.


(snip)

I think you're going to have a hard time if you don't read about 
Python's object model...

> class Loc:

Better to use new-style classes unless you have compelling reasons to 
stick with the very outdated 'classic' model.

class Loc(object):

> line = 0
> char = 0

This define two class attributes - that is, attributes that are members 
of the Loc class object (*not* Loc instances), and so are shared by all 
instances. This is probably not what you want.

> def __init__(self, l, c):
> line = l
> char = c

This defines two local variables line and char, bind them to resp. l and 
c, then returns - discarding locals of course.

The use of 'self' is *not* optional in Python. Within the function, it's 
a reference to the current instance. If you want to define instance 
attributes, you have to set these attributes on the current instance.

def __init__(self, line, char):
   self.line = line
   self.char = char

> def nextChar(self):
> char += 1

NameError here. You want self.char

> def nextLine(self):
> line += 1

NameError here. You want self.line

> char = 0

Local variable, discarded when exiting the function. You want self.char

> def repr(self):
> return 'Loc: line='+str(line)+', char='+str(char)

NameError * 2, etc...

Also, Python has string formatting:

   def __repr__(self):
   return "" % (self.line, self.char)

HTH
-- 
http://mail.python.org/mailman/listinfo/python-list


classmethod inheritance

2007-12-12 Thread Yoav Goldberg
A lot of my code supplement/replace the constructor with class factory
methods.

So, instead of:
>  a= A(...)

I would write:
> a = A.from_file(...)
> a = A.create(...)
etc.

This is implemented as follows:

class A:
   def __init__(self, ...): pass

   @classmethod
   def from_file(cls, ):
inst = cls()

return inst

Derived classes of A can of course use these factories to create instances
of the derived class as well.

The problem arise when the derived class needs a different implementation of
the factory.
If it was a constructor (or any method for that matter), this would be
achieved as:

class B(A):
   def __init__(self, ...):
   A.__init__(self, ...)
  

However, this can not be done with the factory:

class B(A):
@classmethod
def from_file(cls, ...)
inst = A.from_file(...)

return inst

will result with the type of the created object being A and not B.

This can be overcome in two ways. The first (neat hack) is due to a
colleague of mine, using static methods and default arguments:

class A:
   @staticmethod
   def from_file(cls=None, ...)
   if not cls: cls = A
   inst = cls()
   ...

class B(A):
@staticmethod
   def from_file(cls=None, ...)
   if not cls: cls = B
   inst = A.from_file(cls, ...)


The second is by delegating the work of the factory to an instance method:

class A:
   @classmethod
   def from_file(cls,...)
   inst = cls()
   inst._from_file(...)
   return inst

and then overriding the instance method and never touching the factory
method.


Now, my questions are as follow:

1/ Am I missing a third, more "standard" way of achieving inheritance of
class methods?
2/ If not, I think future python's should have a syntax for this -- the
first method I proposed is a neat hack, and the second is a workaround, but
I wouldn't want the first method to be the official way of doing things, and
I suspect there are some use cases that can not be implemented as nicely
with the second method.


Yoav
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is a "real" C-Python possible?

2007-12-12 Thread Christian Heimes
Kay Schluehr wrote:
> class A(object):
> foo = property:
> def fget(self):
> return self._foo
> def fset(self, value):
> self._foo = value
> 
> which was translated as follows:
> 
> class A(object):
> def thunk():
> def fget(self):
> return self._foo
> def fset(self, value):
> self._foo = value
> return vars()
> foo = propery(**thunk())
> del thunk

Python 2.6 and 3.0 have a more Pythonic way for the problem:

class A(object):
@property
def foo(self):
return self._foo

@foo.setter
def foo(self, value)
self._foo = value

@foo.deletter
def foo(self)
del self._foo

class B(A):
# one can even overwrite the getter in a subclass
@foo.getter
def foo(self):
return self._foo * 2

Christian

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rewriting Recipe/410687 without map and lambda

2007-12-12 Thread sofeng
On Dec 11, 8:29 pm, "Terry Reedy" <[EMAIL PROTECTED]> wrote:
> "sofeng" <[EMAIL PROTECTED]> wrote in message
>
> news:[EMAIL PROTECTED]
> |I would like to use the following recipe to transpose a list of lists
> | with different 
> lengths.http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410687
> |
> | Here is an example of what I would like to do:
> |
> | Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
> | [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
> | Type "help", "copyright", "credits" or "license" for more information.
> | >>> a = [[1,2,3],[4,5,6,7],[8,9]]
> | >>> print map(lambda *row: list(row), *a)
> | [[1, 4, 8], [2, 5, 9], [3, 6, None], [None, 7, None]]
> | >>>
> |
> | However, in the Python 3000 FAQ (http://www.artima.com/weblogs/
> | viewpost.jsp?thread=211200), Guido says not to use map with lambda
> | because a list comprehension is clearer and faster.
> |
> | How can I rewrite the above recipe using a list comprehension instead?
>
> Here is the sort of thing Guido is talking about:
>
> >>> a = [[1,2,3],[4,5,6,7],[8,9]]
> >>> [list(r) for r in zip(*a)]
>
> [[1, 4, 8], [2, 5, 9]]
>
> Except in this case, zip does not pad, while map does, so the list comp
> form has to be
>
> >>> [list(r) for r in map(None, *a)]
>
> [[1, 4, 8], [2, 5, 9], [3, 6, None], [None, 7, None]]
>
> but at this point, it is about as easy to put the lambda in place of None.
>
> But when one maps one sequence or equal-length sequences, Guido's point
> applies.
>
> tjr

Thank you very much.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread George Sakkis
On Dec 12, 4:09 am, Kay Schluehr <[EMAIL PROTECTED]> wrote:

> I vaguely remember a discussion a few years ago, where someone made
> the quite reasonable suggestion of introducing some kind of
> thunk_statement:
>
> class A(object):
> foo = property:
> def fget(self):
> return self._foo
> def fset(self, value):
> self._foo = value

That's almost identical to a recipe I had written once upon a time,
without requiring a syntax change: 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698

George
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: DBApi Question with MySQL

2007-12-12 Thread Paul McNett
Hans Müller wrote:

> Hi,
> 
> I'd like to modify some tables in a database in one transaction.
> This approach doesn't work:
> 
> import MySQLdb
> 
> con = MySQLdb.connect("servernam", user = "username", passwd = "verysecret, 
> db = "test", use_unicode = True, charset = "utf8")
> 
> cursor = con.cursor()
> 
> con.begin()
> 
> cursor.execute("delete from table")
> 
> do-some-stuff and wait
> 
> cursor.execute("insert into table value(%s, %s)", (1, 2))
> 
> con.commit()
> 
> 
> When I look into the databse while the script is running, all rows from table 
> are gone.
> The expected behavior would be to see the new lines only when the script is 
> finished.
> The deletion should be (since inside a transaction) invisible up to the 
> commit().
> 
> Has someone an idea how to use transactions correctly ?
> 
> What I need is this
> 
> start transaction
> 
> delete a lot of date in some tables (about 2 million rows)
> 
> insert a lot of new date in these tables (also about 2 million lines)
> 
> commit all changes, so all changes become visible here and only here.
> 
> 
> Thanks a lot,
> 
> Greetings
> Hans


Quick questions before going any further:

1) What's the table type in MySQL: ISAM, INNO, or ? As you probably know 
MyISAM doesn't support transactions.

2) Is MySQL set to AutoCommit? Issue:
cursor.execute("select @@autocomit")
print cursor.fetchall()

If so try sending:
cursor.execute("set autocommit=0")

...and then doing the code you posted.




Paul

-- 
http://paulmcnett.com
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Help needed with python unicode cgi-bin script

2007-12-12 Thread Duncan Booth
"weheh" <[EMAIL PROTECTED]> wrote:

> Hi Duncan, thanks for the reply.
>>>
>> FWIW, the code you posted only ever attempted to set the character
>> set encoding using an html meta tag which is the wrong place to set
>> it. The encoding specified in the HTTP headers always takes
>> precedence. This is why
>> the default charset setting in Apache was the one which applied.
>>
>> What you should have been doing was setting the encoding in the
>> content- type header. i.e. in this line of your code:
>>
>>   print u"""Content-Type: text/html
>>
>> You should have changed it to read:
>>
>>   Content-Type: text/html; charset=windows-1252
>>
>> but because you didn't Apache was quietly changing it to read:
>>
>>   Content-Type: text/html; charset=utf-8
>>
> Will this work under the following situation? Let's say the user is
> filling out a text field on a form on my website. The user has their
> browser encoding set to utf8. My website has charset=windows-1252 as
> you indicate above. Will I run into a conflict somewhere? 
> 
If you are sending the user a form you should be specifying the acceptable 
character sets with the accept-charset attribute on the form tag. Basically 
the rule should be to pick an appropriate character set and stick to it. 
don't depend on defaults, be explicit.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread Neil Cerutti
On 2007-12-12, Shane Geiger <[EMAIL PROTECTED]> wrote:
> Neil Cerutti wrote:
>> On 2007-12-12, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>>   
>>> John Machin <[EMAIL PROTECTED]> wrote:
>>> 
 For that purpose, CSV files are the utter pox and then some.
 Consider using xlrd and xlwt (nee pyexcelerator) to read
 (resp. write) XLS files directly.
   
>>> FWIW, CSV is a much more generic format for spreadsheets than
>>> XLS. For example, I deal almost exclusively in CSV files for
>>> simialr situations as the OP because I also work with software
>>> that can't (or in some cases "can't easily") deal with XLS
>>> files.  CSV files can be read in by basically anything.
>>> 
>>
>> When I have a choice, I use simple tab-delimited text files.  The
>> usually irrelevent limitation is the inability to embed tabs or
>> newlines in fields. The relevant advantage is the simplicity.
>>   
>
> That is very unnecessary.  You can have your tabs and not eat them, too:
>
>
>
> #!/usr/bin/python
> """
> EXAMPLE USAGE OF PYTHON'S CSV.DICTREADER FOR PEOPLE NEW TO
> PYTHON AND/OR CSV.DICTREADER

I gladly use the csv module to generate valid csv data for others
or for myself. But I'm no longer comfortable using just anyone's
csv export feature. A commercial product I use every day creates
invalid csv files. How many more products have tried to "roll
their own" and botched it horribly? I wish more apps embedded
Python. ;-)

Thanks for posting the example code.

-- 
Neil Cerutti
You've got to take the sour with the bitter. --Samuel Goldwyn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __init__ method for containers

2007-12-12 Thread Neil Cerutti
On 2007-12-12, Raymond Hettinger <[EMAIL PROTECTED]> wrote:
> On Dec 12, 7:22 am, Neil Cerutti <[EMAIL PROTECTED]> wrote:
>> List and deque disagree on what __init__ does. Which one is
>> right?
>
> File a bug report and assign to me.

Will do. Registration in progress.

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie NameError problem

2007-12-12 Thread Paul Rudin
[EMAIL PROTECTED] writes:

> On Dec 12, 5:51 pm, Paul Rudin <[EMAIL PROTECTED]> wrote:
>> [EMAIL PROTECTED] writes:
>> > I don't understand what I don't understand in the following:
>>
>> I haven't tried to understand what your code is doing - but the
>> NameError arises because you try to use Loc before its definition. Put
>> the definition first and the error should go away.
>
> Class Loc must be placed ABOVE the code that uses it.

Isn't that what I said?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Improvements to the Python core

2007-12-12 Thread Paul Rudin
Christian Heimes <[EMAIL PROTECTED]> writes:

>
> We are happy and glad for every improvement regarding speed, memory
> usage or features if and only if: ...

> ... platform independent / supported on all platforms. Python runs
> on machines from mobile phones to large main frames.

JOOI - there are things in the standard library that are not supported
on all platforms. Why would that be a basis for excluding some
psyco-like package?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TCP reset caused by socket.py

2007-12-12 Thread Bjoern Schliessmann
Gabriel Genellina wrote:
> A RST when you close a socket is OK. 

Says who? MS? ;)

Regards,


Björn

-- 
BOFH excuse #328:

Fiber optics caused gas main leak

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mimicking a file in memory

2007-12-12 Thread [EMAIL PROTECTED]
On Nov 20, 4:37 pm, Jarek Zgoda <[EMAIL PROTECTED]> wrote:

> Try with StringIO/cStringIO, these modules are supposed to give you
> in-memoryobjects compatible with file object interface.

I found this solution not working.
I had similar problem: I wanted to write some string into the in-
memory file, then transfer it via ftp to some file and forget in-
memory  content.

from ftplib import FTP
ftp = FTP('ftp.server.org')
ftp.login('ID','pswd')
import StringIO
filename = 'some_file.txt'
command = 'STOR ' + filename
outfile = StringIO.StringIO()
outfile.write(some_string + '\n')
ftp.storlines(command, outfile)
ftp.quit()
outfile.close()

The file shows up on the FTP server, but with ZERO length. I think the
problem is that ftp.storelines attempts to use outfile's read()
function, which is not present in StringIO objects (they use
getvalue() instead). Quite an annoying inconsistency.
Any thoughts, please?

--
Marcin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TCP reset caused by socket.py

2007-12-12 Thread Bjoern Schliessmann
Object01 wrote:
> The server is multithreaded, handling each request on its own
> thread.

Ugh.

> But is a RST really a part of a valid close operation?  

Depends on the state of the parties :) The proper way to close
non-defunct connections is using FIN segments.

> It was my understanding that the RST is used to indicate a problem
> with the connection, usually a half-open connection.  I never see
> RSTs unless this error occurs.

RFC793:

| Reset Generation
|
| As a general rule, reset (RST) must be sent whenever a segment
| arrives which apparently is not intended for the current
| connection.  A reset must not be sent if it is not clear that this
| is the case. 

Regards,


Björn

-- 
BOFH excuse #347:

The rubber band broke

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread George Sakkis
On Dec 12, 1:12 pm, Christian Heimes <[EMAIL PROTECTED]> wrote:

> Kay Schluehr wrote:
> > class A(object):
> > foo = property:
> > def fget(self):
> > return self._foo
> > def fset(self, value):
> > self._foo = value
>
> > which was translated as follows:
>
> > class A(object):
> > def thunk():
> > def fget(self):
> > return self._foo
> > def fset(self, value):
> > self._foo = value
> > return vars()
> > foo = propery(**thunk())
> > del thunk
>
> Python 2.6 and 3.0 have a more Pythonic way for the problem:
>
> class A(object):
> @property
> def foo(self):
> return self._foo
>
> @foo.setter
> def foo(self, value)
> self._foo = value
>
> @foo.deletter
> def foo(self)
> del self._foo
>
> class B(A):
> # one can even overwrite the getter in a subclass
> @foo.getter
> def foo(self):
> return self._foo * 2
>
> Christian

This is by definition Pythonic since it was conceived by the BDFL.It
is also certainly an improvement over the current common practice of
polluting the class namespace with private getters and setters. Still
it's a kludge compared to languages with builtin support for
properties.

George
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread Kay Schluehr
On Dec 12, 3:36 pm, sturlamolden <[EMAIL PROTECTED]> wrote:
> On 12 Des, 12:56, George Sakkis <[EMAIL PROTECTED]> wrote:
>
> > Ah, the 'make' statement.. I liked (and still do) that PEP, I think it
> > would have an impact comparable to the decorator syntax sugar, if not
> > more.
>
> I think it is one step closer to Lisp. I believe that it would be
> worth considering adding defmacro statement. Any syntax, including if,
> else, for, while, class, lambda, try, except, etc.  would be
> implemented with defmacros. We would only need a minimalistic syntax,
> that would bootstrap a full Python syntax on startup. And as for
> speed, we all know how Lisp compares to Python.

It would be great if Python could be speeded up to SBCL Lisp by just
transforming one parse tree into another one. But since Python is
compiled to bytecodes I dare to say that surface syntax is not the key
factor ;)

Kay
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tuples !?!?

2007-12-12 Thread Bjoern Schliessmann
Bruno Desthuilliers wrote:
> [EMAIL PROTECTED] a écrit :

>> The problem is '2' != 2
> 
> It would indeed be a problem if this expression eval'd to True.
> That's the case in some, hem, 'languages', and believe me it's
> *not* the RightThing.

What kind of "hem" language is this? :)

>>> '2' != 2
True
>>> 

Regards,


Björn

-- 
BOFH excuse #430:

Mouse has out-of-cheese-error

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mimicking a file in memory

2007-12-12 Thread Neil Cerutti
On 2007-12-12, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I found this solution not working.
> outfile = StringIO.StringIO()
> outfile.write(some_string + '\n')

You need to rewind the file with outfile.seek(0) before
proceeding, or storlines will encounter an immediate EOF when it
attempts to read data.

> ftp.storlines(command, outfile)
 outfile.close()

-- 
Neil Cerutti
Henry VII found walking difficult because he had an abbess on his knee.
--History Exam Blooper
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TCP reset caused by socket.py

2007-12-12 Thread Object01
On Dec 12, 12:45 pm, Bjoern Schliessmann  wrote:
> Object01 wrote:
> > The server is multithreaded, handling each request on its own
> > thread.
>
> Ugh.
>
> > But is a RST really a part of a valid close operation?
>
> Depends on the state of the parties :) The proper way to close
> non-defunct connections is using FIN segments.
>
> > It was my understanding that the RST is used to indicate a problem
> > with the connection, usually a half-open connection.  I never see
> > RSTs unless this error occurs.
>
> RFC793:
>
> | Reset Generation
> |
> | As a general rule, reset (RST) must be sent whenever a segment
> | arrives which apparently is not intended for the current
> | connection.  A reset must not be sent if it is not clear that this
> | is the case.
>
> Regards,
>
> Björn
>
> --
> BOFH excuse #347:
>
> The rubber band broke

Is there something I can look for in the packet traffic that would
indicate one party is misbehaving?  The sequence numbers seem ok.
*shrug*  I'd expect to see data sent from server to client and then
see a sequence of packets that close the connection gracefully.
Instead I see the server send data to the client and then a RST,
nothing more.

The crux of this strangeness seems to be Python sending a RST without
any prompting from the client.   It's just send to client, send to
client, send to client, reset.  OH!  Maybe the client isn't
acknowledging properly or the server is incorrectly expecting
acknowledgment and resets when it doesn't arrive?

Rrr...

--
Jeff S.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TCP reset caused by socket.py

2007-12-12 Thread Jeff McNeil
Do you have any non-standard network hardware along the route?  Perhaps a
transparent proxy like a load balancer or a firewall of sorts? I've seen
this type of thing happen before with load balancer gear.   In my situation,
I had a load balancer that kept a state table. If the load balancer didn't
see client-side traffic within a 5 second period, it would terminate the
connection by sending some out of sequence data that generated the RST.
We spent half a day tracking down (what we thought was) a bug in one of our
Apache modules and it turned out to simply be a configurable timeout value.




On 12/12/07, Object01 <[EMAIL PROTECTED]> wrote:
>
> On Dec 12, 12:45 pm, Bjoern Schliessmann  [EMAIL PROTECTED]> wrote:
> > Object01 wrote:
> > > The server is multithreaded, handling each request on its own
> > > thread.
> >
> > Ugh.
> >
> > > But is a RST really a part of a valid close operation?
> >
> > Depends on the state of the parties :) The proper way to close
> > non-defunct connections is using FIN segments.
> >
> > > It was my understanding that the RST is used to indicate a problem
> > > with the connection, usually a half-open connection.  I never see
> > > RSTs unless this error occurs.
> >
> > RFC793:
> >
> > | Reset Generation
> > |
> > | As a general rule, reset (RST) must be sent whenever a segment
> > | arrives which apparently is not intended for the current
> > | connection.  A reset must not be sent if it is not clear that this
> > | is the case.
> >
> > Regards,
> >
> > Björn
> >
> > --
> > BOFH excuse #347:
> >
> > The rubber band broke
>
> Is there something I can look for in the packet traffic that would
> indicate one party is misbehaving?  The sequence numbers seem ok.
> *shrug*  I'd expect to see data sent from server to client and then
> see a sequence of packets that close the connection gracefully.
> Instead I see the server send data to the client and then a RST,
> nothing more.
>
> The crux of this strangeness seems to be Python sending a RST without
> any prompting from the client.   It's just send to client, send to
> client, send to client, reset.  OH!  Maybe the client isn't
> acknowledging properly or the server is incorrectly expecting
> acknowledgment and resets when it doesn't arrive?
>
> Rrr...
>
> --
> Jeff S.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: mimicking a file in memory

2007-12-12 Thread Peter Otten
[EMAIL PROTECTED] wrote:

> On Nov 20, 4:37 pm, Jarek Zgoda <[EMAIL PROTECTED]> wrote:
> 
>> Try with StringIO/cStringIO, these modules are supposed to give you
>> in-memoryobjects compatible with file object interface.
> 
> I found this solution not working.
> I had similar problem: I wanted to write some string into the in-
> memory file, then transfer it via ftp to some file and forget in-
> memory  content.
> 
> from ftplib import FTP
> ftp = FTP('ftp.server.org')
> ftp.login('ID','pswd')
> import StringIO
> filename = 'some_file.txt'
> command = 'STOR ' + filename
> outfile = StringIO.StringIO()
> outfile.write(some_string + '\n')

Try it again with 

outfile.seek(0) 

before the storlines() call.

> ftp.storlines(command, outfile)
> ftp.quit()
> outfile.close()
> 
> The file shows up on the FTP server, but with ZERO length. 

I believe you would see the same problem if outfile were a real file.

> I think the
> problem is that ftp.storelines attempts to use outfile's read()
> function, which is not present in StringIO objects (they use
> getvalue() instead). Quite an annoying inconsistency.

>>> "read" in dir(StringIO.StringIO())
True

A quick look into the source code can put an end to the rest of that
speculation:

def storlines(self, cmd, fp):
'''Store a file in line mode.'''
self.voidcmd('TYPE A')
conn = self.transfercmd(cmd)
while 1:
buf = fp.readline()
if not buf: break
if buf[-2:] != CRLF:
if buf[-1] in CRLF: buf = buf[:-1]
buf = buf + CRLF
conn.sendall(buf)
conn.close()
return self.voidresp()

storlines() will happily accept every object fp featuring a readline()
method.

> Any thoughts, please?

Keep the library docs under your pillow and the library source on your
screen :)

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is a "real" C-Python possible?

2007-12-12 Thread Chris Mellon
On Dec 12, 2007 12:53 PM, George Sakkis <[EMAIL PROTECTED]> wrote:
> On Dec 12, 1:12 pm, Christian Heimes <[EMAIL PROTECTED]> wrote:
>
> > Kay Schluehr wrote:
> > > class A(object):
> > > foo = property:
> > > def fget(self):
> > > return self._foo
> > > def fset(self, value):
> > > self._foo = value
> >
> > > which was translated as follows:
> >
> > > class A(object):
> > > def thunk():
> > > def fget(self):
> > > return self._foo
> > > def fset(self, value):
> > > self._foo = value
> > > return vars()
> > > foo = propery(**thunk())
> > > del thunk
> >
> > Python 2.6 and 3.0 have a more Pythonic way for the problem:
> >
> > class A(object):
> > @property
> > def foo(self):
> > return self._foo
> >
> > @foo.setter
> > def foo(self, value)
> > self._foo = value
> >
> > @foo.deletter
> > def foo(self)
> > del self._foo
> >
> > class B(A):
> > # one can even overwrite the getter in a subclass
> > @foo.getter
> > def foo(self):
> > return self._foo * 2
> >
> > Christian
>
> This is by definition Pythonic since it was conceived by the BDFL.It
> is also certainly an improvement over the current common practice of
> polluting the class namespace with private getters and setters. Still
> it's a kludge compared to languages with builtin support for
> properties.


How exactly is this a kludge? This is almost identical syntax (but
with less indentation) to a C# property declaration. The only thing
that's simpler is auto-generation of trivial accessors via a
decoration, but those are useless in Python so only the case of
getters and setters that actually do something needs to be addressed.

If the only thing that's not a "kludge" is direct syntax support for a
feature, you've set a pretty high and uselessly arbitrary bar.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tuples !?!?

2007-12-12 Thread Bruno Desthuilliers
Bjoern Schliessmann a écrit :
> Bruno Desthuilliers wrote:
> 
>>[EMAIL PROTECTED] a écrit :
> 
> 
>>>The problem is '2' != 2
>>
>>It would indeed be a problem if this expression eval'd to True.
>>That's the case in some, hem, 'languages', and believe me it's
>>*not* the RightThing.
> 
> 
> What kind of "hem" language is this? :)


> 
'2' != 2
> 
> True

hem... Sorry, I of course meant that it would be a problem if '2' == 2 
eval'd to true  - or '2' != 2 eval'd to false.  My bad...

Thanks for the correction anyway.

Ah, and, yes, the 'hem' language I was thinking about has a three 
letters recursive acronym for name and is widely (and mostly) used for 
web applications. Should I spell the name, or did you guess ?-)
-- 
http://mail.python.org/mailman/listinfo/python-list


After running for four days, Python program stalled

2007-12-12 Thread John Nagle
Had a Python program stall, using no time, after running OK for four days.
Python 2.4, Windows.  Here's the location in Python where it's stalled.
Any idea what it's waiting for?

John Nagle

77FA1428   mov ecx,dword ptr [ebp-10h]
77FA142B   mov dword ptr fs:[0],ecx
77FA1432   pop edi
77FA1433   pop esi
77FA1434   pop ebx
77FA1435   leave
77FA1436   ret 10h
77FA1439   cmp dword ptr [ebp-24h],0
77FA143D   je  77FA1447
77FA143F   pushdword ptr [ebp-24h]
77FA1442   call77F8B5DF
77FA1447   ret
77FA1448   ret 4
77FA144B   int 3# STALLED HERE
77FA144C   ret
77FA144D   int 3
77FA144E   ret
77FA144F   mov eax,dword ptr [esp+4]
77FA1453   int 3
77FA1454   ret 4
77FA1457   mov eax,dword ptr [ebp-14h]
77FA145A   mov eax,dword ptr [eax]
77FA145C   mov eax,dword ptr [eax]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Stringified list back to list of ints

2007-12-12 Thread bearophileHUGS
Another solution, possibly safer:

>>> from cStringIO import StringIO
>>> import csv
>>> s = "[16, 16, 2, 16, 2, 16, 8, 16]"
>>> sf = StringIO(s.strip()[1:-1])
>>> list(csv.reader(sf))
[['16', ' 16', ' 2', ' 16', ' 2', ' 16', ' 8', ' 16']]

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is anyone happy with csv module?

2007-12-12 Thread J. Clifford Dyer
On Wed, Dec 12, 2007 at 11:02:04AM -0600, [EMAIL PROTECTED] wrote regarding Re: 
Is anyone happy with csv module?:
> 
> J. Clifford Dyer <[EMAIL PROTECTED]> wrote:
> > But the software you are dealing with probably doesn't actually 
> > need spreadsheets.  It just needs digital ledgers.
> 
> I saw someone else in this thread note that they considered CSV to
> be a serialization method and not a file format, which I thought was a
> brilliant way of looking at things.
> 
> OTOH, from a practical perspective, most of the people I end up interacting
> with say "spreadsheet" to mean exactly what you're describing as a
> "ledger paper" ... sure they might want to do operations on those cells
> in the data, but it isn't key what actual format the data is presented
> to them in (XLS, CSV, tab delimited, etc).  Oddly enough though, they
> almost always say "excel spreadsheet" even though that's not what they
> need (rather, its just a side effect of them not realizing there actually
> *is* other software that handles these things)

I know, I know.  The last place I worked, we had timesheets that were excel 
spreadsheets, but you actually had to total up your own hours.  I added to my 
job description the role of teaching people how to make the spreadsheet do the 
work for you.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >