Re: Problem with -3 switch

2009-01-12 Thread Steve Holden
Carl Banks wrote:
> On Jan 9, 6:11 pm, John Machin  wrote:
>> On Jan 10, 6:58 am, Carl Banks  wrote:
[...]
>> Steve & Cliff are talking about the rather small subset of Python that
>> is not only valid syntax in both 2.x and 3.x but also has the same
>> meaning in 2.x and 3.x.
> 
> That would be a crippled language, yes.  But I do not believe that's
> what Steve and Cliff are referring to.  Steve wrote of "running your
> code through 2to3", and that was what Cliff followed up to, so I
> believe they are both referring to writing valid code in 2.6 which is
> able to be translated through 2to3, and then generating 3.0 code using
> 2to3.  That is not a crippled language at all, just a PITA sometimes.
> 
Correct. The recommended way of maintaining a dual-version code base is
to paraphrase your 2.6 code in such a way that the 2to3 converter will
produce correct 3.0 code that required no further attention. If you
don't do this you are making a rod for your own back.
[...]

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

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


Re: Is negative seek() from EOF of a GzipFile supported on Python 2.5.2 ?

2009-01-12 Thread Steve Holden
Gabriel Genellina wrote:
> En Sun, 11 Jan 2009 08:12:27 -0200, Barak, Ron 
> escribió:
> 
>> I googled a bit, and found http://bugs.python.org/issue1355023.
>> It seems that this patch implemented fuller seek() for GzipFile around
>> November 2006 (including whence==2).
>> Do I misunderstand and this patch was not actually implemented,
>> namely, is seek(-n,2) not implemented in Python 2.5.2 ?
>>
>> The source of gzip.py on my system seems to suggest that negative
>> seeks are supported:
> 
> The source comments are misleading: "negative seeks" means seeking
> backwards (towards the beginning of the file), not offset<0.
> The second argument to seek (whence) was added in 2.6, and only accepts 0
> or 1; whence==2 isn't supported.
> 
Specifically in the case of a zip file that means the whole entry would
need to be expanded to allow such a seek to take place from the end of
the file; hence there are good pragmatic reasons for the restriction.

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

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


Re: Object help

2009-01-12 Thread Steve Holden
James Mills wrote:
> On Mon, Jan 12, 2009 at 9:49 AM, killsto  wrote:
>> Thanks. That makes sense. It helps a lot. Although, you spelled color
>> wrong :P.
> 
> color
> colour
> 
> They are both correct depending on what
> country you come from :)
> 
They are also both incorrect, depending which country you come from :P

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

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


Re: Problem with -3 switch

2009-01-12 Thread Carl Banks
On Jan 12, 12:32 am, John Machin  wrote:
> On Jan 12, 12:23 pm, Carl Banks  wrote:
>
>
>
> > On Jan 9, 6:11 pm, John Machin  wrote:
>
> > > On Jan 10, 6:58 am, Carl Banks  wrote:
>
> > > > On Jan 9, 12:36 pm, "J. Cliff Dyer"  wrote:
>
> > > > > On Fri, 2009-01-09 at 13:13 -0500, Steve Holden wrote:
> > > > > > Aivar Annamaa wrote:
> > > > > > >> As was recently pointed out in a nearly identical thread, the -3
> > > > > > >> switch only points out problems that the 2to3 converter tool 
> > > > > > >> can't
> > > > > > >> automatically fix. Changing print to print() on the other hand is
> > > > > > >> easily fixed by 2to3.
>
> > > > > > >> Cheers,
> > > > > > >> Chris
>
> > > > > > > I see.
> > > > > > > So i gotta keep my own discipline with print() then :)
>
> > > > > > Only if you don't want to run your 2.x code through 2to3 before you 
> > > > > > use
> > > > > > it as Python 3.x code.
>
> > > > > > regards
> > > > > >  Steve
>
> > > > > And mind you, if you follow that route, you are programming in a
> > > > > mightily crippled language.
>
> > > > How do you figure?
>
> > > > I expect that it'd be a PITA in some cases to use the transitional
> > > > dialect (like getting all your Us in place), but that doesn't mean the
> > > > language is crippled.
>
> > > What is this "transitional dialect"? What does "getting all your Us in
> > > place" mean?
>
> > Transitional dialect is the subset of Python 2.6 that can be
> > translated to Python3 with 2to3 tool.
>
> I'd never seen it called "transitional dialect" before.

I had hoped the context would make it clear what I was talking about.

> >  Getting all your Us in place
> > refers to prepending a u to strings to make them unicode objects,
> > which is something 2to3 users are highly advised to do to keep hassles
> > to a minimum.  (Getting Bs in place would be a good idea too.)
>
> Ummm ... I'm not understanding something. 2to3 changes u"foo" to
> "foo", doesn't it? What's the point of going through the code and
> changing all non-binary "foo" to u"foo" only so that 2to3 can rip the
> u off again?

It does a bit more than that.

> What hassles? Who's doing the highly-advising where and
> with what supporting argument?

You add the u so the the constant will be the same data type in 2.6 as
it becomes in 3.0 after applying 2to3.  str and unicode objects aren't
always with smooth with each other, and you have a much better chance
of getting the same behavior in 2.6 and 3.0 if you use an actual
unicode string in both.

A example of this, though not with string constants, was posted here
recently.  Someone found that urllib.open() returns a bytes object in
Python 3.0, which messed him up since in 2.x he was running regexp
searches on the output.  If he had been taking care to use only
unicode objects in 2.x (in this case, by explicitly decoding the
output) then it wouldn't have been an issue.


> "Getting Bs into place" is necessary eventually. Whether it is
> worthwhile trying to find these in advance, or waiting for them to be
> picked up at testing time is a bit of a toss-up.
>
> Let's look at this hypothetical but fairly realistic piece of 2.x
> code:
> OLE2_SIGNATURE = "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
> def is_ole2_file(filepath):
>      return open(filepath, "rb").read(8) == OLE2_SIGNATURE
>
> This is already syntactically valid 3.x code, and won't be changed by
> 2to3, but it won't work in 3.x because b"x" != "x" for all x. In this
> case, the cause of test failures should be readily apparent; in other
> cases the unexpected exception or test failure may happen at some
> distance.
>
> The 3.x version needs to have the effect of:
> OLE2_SIGNATURE = b"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
> def is_ole2_file(filepath):
>      return open(filepath, "rb").read(8) == OLE2_SIGNATURE
>
> So in my regional variation of the transitional dialect, this becomes:
> from timemachine import *
> OLE2_SIGNATURE = BYTES_LITERAL("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1")
> def is_ole2_file(filepath):
>      return open(filepath, "rb").read(8) == OLE2_SIGNATURE
> # NOTE: don't change "rb"
> ...
> and timemachine.py contains (amongst other things):
> import sys
> python_version = sys.version_info[:2] # e.g. version 2.4 -> (2, 4)
> if python_version >= (3, 0):
>     BYTES_LITERAL = lambda x: x.encode('latin1')
> else:
>     BYTES_LITERAL = lambda x: x
>
> It is probably worthwhile taking an up-front inventory of all file open
> () calls and [c]StringIO.StringIO() calls -- is the file being used as
> a text file or a binary file?
> If a text file, check that any default encoding is appropriate.
> If a binary file, ensure there's a "b" in the mode (real file) or you
> supply (in 3.X) an io.BytesIO() instance, not an io.StringIO()
> instance.

Right.  "Taking care of the Us" refered specifically to the act of
prepending Us to string constants, but figuratively it means making
explicit your intentions with all string data.  2to3 can only do so
much; it can't always guess whether your string u

Bug in python [was: Fatal Python error: ceval: tstate mix-up]

2009-01-12 Thread Laszlo Nagy

Laszlo Nagy wrote:
Meanwhile I'm trying to turn off threads in that program one by one. I 
just got this new type of error:


Fatal Python error: PyThreadState_Delete: invalid tstate
After some days, there are now answers to my question. I guess this is 
because nobody knows the answer. I think I need to consider this a bug 
in Python. Where can I post a bug report?


Laszlo

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


Re: ActiveState Python Together with "Regular" Python) ((DLE)

2009-01-12 Thread W. eWatson

John Machin wrote:

On Jan 12, 2:00 pm, "W. eWatson"  wrote:

I installed "Python" 2.5 a few months ago with IDLE, and decided I'd like to
try windowpy from ActiveState. Is having both of these installed going to
cause me trouble?


What is "windowpy from ActiveState"? If you mean you wanted to try the
PythonWin IDE instead of IDLE, all you needed to do was go to
http://sourceforge.net/projects/pywin32/ hit the big download button
and make sure you get the 2.5 version (it's the default atm) and
install it.


Yes, I want to try the PythonWin IDE instead of IDLE. "(DLE" was a typo.

Is it possible to just disable the vanilla (IDLE) version? I may want to 
switch between the two. Most users of the program I'm about to modify use 
the vanilla version. At some point, I may want to go back to it to verify 
that in their world all is OK.


--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


debugging ignored exceptions at cleanup

2009-01-12 Thread Michele Simionato
In some conditions (typically with threads, __del__ methods, etc) the
cleanup mechanism of Python gets in trouble and some exceptions are
not raised but just printed on stderr.
I have an application using Paste and when I run the tests I get some
annoying
ignored exceptions during cleanup. Running the code with the -v option
I get things
like that:


# cleanup[2] sqlalchemy.util
# cleanup[2] sqlalchemy.sql.expression
# cleanup[2] symbol
# cleanup[2] urllib2
# cleanup[2] sqlalchemy.orm.query
# cleanup[2] smweb.config
# cleanup[2] formencode
# cleanup[2] smweb.config.environment
Exception exceptions.TypeError: "'NoneType' object is not callable" in
 at 0x26861b8> ignored
Exception exceptions.TypeError: "'NoneType' object is not callable" in
 at 0x2598578> ignored
# cleanup[2] dbhash
# cleanup[2] xmlrpclib
# cleanup[2] mako.pygen
# cleanup[2] time
# cleanup[2] paste.util.import_string
# cleanup sys
# cleanup __builtin__
# cleanup ints: 5096 unfreed ints in 145 out of 171 blocks
# cleanup floats: 43 unfreed floats in 3 out of 4 blocks

As you see, the exceptions happen during the cleanup of
smweb.config.environment,
which the part of code I wrote, but which has no obvious problems and
imports
*lots* of other stuff. Is there some way to hook in the cleanup
mechanism, start
a pdb and see really what is happening? I tried to trace the execution
flow with the
trace module but without success. There is so much code there that I
am unable
to track down the source of the problem. I suspect there is some
__del__ method
somewhere that tries to call a function which has been set to None by
the cleanup
mechanism, possibly from another thread, but I cannot find it. How am
I supposed to
debug such things?
--
http://mail.python.org/mailman/listinfo/python-list


Re: ActiveState Python Together with "Regular" Python) ((DLE)

2009-01-12 Thread John Machin
On Jan 12, 9:16 pm, "W. eWatson"  wrote:
> John Machin wrote:
> > On Jan 12, 2:00 pm, "W. eWatson"  wrote:
> >> I installed "Python" 2.5 a few months ago with IDLE, and decided I'd like 
> >> to
> >> try windowpy from ActiveState. Is having both of these installed going to
> >> cause me trouble?
>
> > What is "windowpy from ActiveState"? If you mean you wanted to try the
> > PythonWin IDE instead of IDLE, all you needed to do was go to
> >http://sourceforge.net/projects/pywin32/hit the big download button
> > and make sure you get the 2.5 version (it's the default atm) and
> > install it.
>
> Yes, I want to try the PythonWin IDE instead of IDLE. "(DLE" was a typo.
>
> Is it possible to just disable the vanilla (IDLE) version? I may want to
> switch between the two. Most users of the program I'm about to modify use
> the vanilla version. At some point, I may want to go back to it to verify
> that in their world all is OK.

I'll try again. All you need is (a) the official distribution of
Python 2.5 for Windows from http://www.python.org (b) the pywin32
package from the URL I gave you. Install both. Click on Start / All
Programs/ Python 2.5 and will find *BOTH* IDLE and PythonWin. You and/
or your users can use IDLE or PythonWin, only one copy of Python, no
conflicts. Why do you think you need to "disable" IDLE? Just don't use
it.



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


Re: hlep: a text search and rename question

2009-01-12 Thread James Stroud

sensen wrote:

matter description:

when a use an tools to do the ape to flac convert, i can use the cue
file attached with ape, but the problem is the converted flac file
don't name by the title in the cue file but like Track_1.flac,
Track_2.flac ... , so i want to write a script to do this work, system
is xp sp2, the python is 2.44, i have a plone site in this computer,
that is why i want to use python to do this work.

so simplify and example

CDImage.cue, Track_1.flac, Track_2.flac, Track_3.flac, Track_4.flac
all above files in the same folder.

the cue file is just a text file like bellow:


PERFORMER "Dido"
TITLE "Life For Rent"
FILE "Dido - Life for Rent.ape" WAVE
  TRACK 01 AUDIO
TITLE "White Flag"
PERFORMER "Dido"
INDEX 01 00:00:00
  TRACK 02 AUDIO
TITLE "Stoned"
PERFORMER "Dido"
INDEX 00 04:00:03
INDEX 01 04:01:45
  TRACK 03 AUDIO
TITLE "Life For Rent"
PERFORMER "Dido"
INDEX 00 09:56:47
INDEX 01 09:56:53
  TRACK 04 AUDIO
TITLE "Mary's In India"
PERFORMER "Dido"
INDEX 01 13:37:57

the things i want to do

1. search the current folder cue file (only one cue file) contents.
find the TITLE "White Flag" and get the White Flag and maybe we
can save it to a var.
2. then rename the Track_1.flac to the White Flag.flac
3. search the next title TITLE "Stoned" and save ti to var.
4. then rename the Track_2.flac to the Stoned.flac.
5. do the same things to the end title.

someone can give some outline, or code is more wonderful.  thanks in
advance.


This isn't much work in python:

import os, glob, re
cue = iter(open('CDImage.cue').readlines())
titles = []
for line in cue:
  if line.strip().startswith('FILE'):
break
for line in cue:
  if line.strip().startswith('TITLE'):
titles.append(line.split('"')[-2])
flacs = glob.glob('*.flac')
flacs.sort(key=lambda f: int(re.search(r'\d+', f).group(0)))
for old, new in zip(flacs, titles):
  os.rename(old, new)


There is no error checking for properly formatted cue file, etc.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

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


Re: hlep: a text search and rename question

2009-01-12 Thread James Stroud

James Stroud wrote:

cue = iter(open('CDImage.cue').readlines())


It just occurred to me that this could simply be:

cue = open('CDImage.cue')

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

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


Re: change only the nth occurrence of a pattern in a string

2009-01-12 Thread Antoon Pardon
On 2008-12-31, TP  wrote:
> Hi everybody,
>
> I would like to change only the nth occurence of a pattern in a string. The
> problem with "replace" method of strings, and "re.sub" is that we can only
> define the number of occurrences to change from the first one.
>
 v="coucou"
 v.replace("o","i",2)
> 'ciuciu'
 import re
 re.sub( "o", "i", v,2)
> 'ciuciu'
 re.sub( "o", "i", v,1)
> 'ciucou'
>
> What is the best way to change only the nth occurence (occurrence number n)?
>
> Why this default behavior? For the user, it would be easier to put re.sub or
> replace in a loop to change the first n occurences.

I would do it as follows:

1) Change the pattern n times to somethings that doesn't occur in your string
2) Change it back n-1 times
3) Change the remaining one to what you want.

>>> v="coucou" 
>>> v.replace('o', 'O', 2).replace('O', 'o', 1).replace('O', 'i')
'couciu'

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


Re: Problem with -3 switch

2009-01-12 Thread John Machin
On Jan 12, 7:29 pm, Carl Banks  wrote:
> On Jan 12, 12:32 am, John Machin  wrote:
>
>
>
>
>
> > On Jan 12, 12:23 pm, Carl Banks  wrote:
>
> > > On Jan 9, 6:11 pm, John Machin  wrote:
>
> > > > On Jan 10, 6:58 am, Carl Banks  wrote:

> > > > > I expect that it'd be a PITA in some cases to use the transitional
> > > > > dialect (like getting all your Us in place), but that doesn't mean the
> > > > > language is crippled.
>
> > > > What is this "transitional dialect"? What does "getting all your Us in
> > > > place" mean?
>
> > > Transitional dialect is the subset of Python 2.6 that can be
> > > translated to Python3 with 2to3 tool.
>
> > I'd never seen it called "transitional dialect" before.
>
> I had hoped the context would make it clear what I was talking about.

In vain.

>
> > >  Getting all your Us in place
> > > refers to prepending a u to strings to make them unicode objects,
> > > which is something 2to3 users are highly advised to do to keep hassles
> > > to a minimum.  (Getting Bs in place would be a good idea too.)
>
> > Ummm ... I'm not understanding something. 2to3 changes u"foo" to
> > "foo", doesn't it? What's the point of going through the code and
> > changing all non-binary "foo" to u"foo" only so that 2to3 can rip the
> > u off again?
>
> It does a bit more than that.

Like what?

>
> > What hassles? Who's doing the highly-advising where and
> > with what supporting argument?
>
> You add the u so the the constant will be the same data type in 2.6 as
> it becomes in 3.0 after applying 2to3.  str and unicode objects aren't
> always with smooth with each other, and you have a much better chance
> of getting the same behavior in 2.6 and 3.0 if you use an actual
> unicode string in both.

(1) Why specifically 2.6? Do you mean 2.X, or is this related to the
"port to 2.6 first" theory?
(2) We do assume we are starting off with working 2.X code, don't we?
If we change "foo" to u"foo" and get a different answer from the 2.X
code, is that still "working"?

>
> A example of this, though not with string constants,

And therefore irrelevant.

I would like to hear from someone who has actually started with
working 2.x code and changed all their text-like "foo" to
u"foo" [except maybe unlikely suspects like open()'s mode arg]:
* how many places where the 2.x code broke and so did the 3.x code
[i.e. the problem would have been detected without prepending u]
* how many places where the 2.x code broke but the 3.x code didn't
[i.e. prepending u did find the problem]
* whether they thought it was worth the effort

In the meantime I would be interested to hear from anybody with a made-
up example of code where the problem would be detected (sooner |
better | only) by prepending u to text-like string constants.

> 2to3 can only do so
> much; it can't always guess whether your string usage is supposed to
> be character or binary.

AFAICT it *always* guesses text rather than binary; do you have any
examples where it guesses binary (rightly or wrongly)?

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


Re: Unbinding Tkinter default bindings for Listbox

2009-01-12 Thread James Stroud

Roger wrote:

Hi Everyone,

I have a behavior associated with a default binding with Tkinter
Listbox that I want to get rid of but I can't no matter if I return
"break" on the binding or unbind it directly. If you have a Listbox
where the bounding box is not completely revealed in the window that
holds it and you use the mouse to drag the list box, the contents of
the listbox will move in the X direction to reveal the area that's
hidden.  After searching the internet for hours I found the behavior I
want to remove is a default binding as described here:

http://tcltk.free.fr/man/TkCmd/listbox.php3

"[3] If the mouse leaves the listbox window with button 1 down, the
window scrolls away from the mouse, making information visible that
used to be off-screen on the side of the mouse. The scrolling
continues until the mouse re-enters the window, the button is
released, or the end of the listbox is reached. "

After further searching I found that the code for this in tcl is
described here:

http://www.openmash.org/lxr/source/library/listbox.tcl?c=tk8.3

 50 bind Listbox  {
 51 set tkPriv(x) %x
 52 set tkPriv(y) %y
 53 tkListboxMotion %W [%W index @%x,%y]
 54 }

Now I've found no way to successfully unbind B1-Motion from the
listbox, as I said above.  Nor return "break" on receiving the event.
I do want to eventually have my own B1-Motion binding applied to the
listbox for a different reason (with add='+' if necessary).  My next
step was to see if I could just unbind B1-Motion then programmatically
delete tkListboxMotion in my code but tkListboxMotion is not available
in the Tkinter source (it's part of a compiled binary I can't reach?).

Any help would be greatly appreciated.  Thanks!
Roger.


You can directly send commands to the Tcl interpreter via the call 
method of the tk attribute of any Tkinter widget. For example:



py> from Tkinter import *
py> tk = Tk()
py> b = Button(tk)
py> b.pack()
py> b.tk

py> b.tk.call('bind', 'Listbox', '')
'\nset tk::Priv(x) %x\nset tk::Priv(y) %y\ntk::ListboxMotion 
%W [%W index @%x,%y]\n'

py> b.tk.call('bind', 'Listbox', '', _)
''


James



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

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


Re: Unbinding Tkinter default bindings for Listbox

2009-01-12 Thread James Stroud

James Stroud wrote:

py> b.tk.call('bind', 'Listbox', '', _)


You want b.tk.call('bind', 'Listbox', '', "") of course.

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

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


Python logging rollover

2009-01-12 Thread Kottiyath
Hi,

I want to do a log rollover - preferably based on day; size is also
Ok.
I checked logging.TimedRotatingFileHandler, but I am not sure whether
it will suit my purpose.
Mine is a desktop application. So, everytime the machine starts, the
whole logging system is reinitialized.
So, in such a case, can I use TimedRotatingFileHandler? I tested it
with 'midnight' option, but it did not work as I expected.

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


Re: hlep: a text search and rename question

2009-01-12 Thread sensen
On Jan 12, 8:17 pm, James Stroud  wrote:
> James Stroud wrote:
> > cue = iter(open('CDImage.cue').readlines())
>
> It just occurred to me that this could simply be:
>
> cue = open('CDImage.cue')
>
> James
>
> --
> James Stroud
> UCLA-DOE Institute for Genomics and Proteomics
> Box 951570
> Los Angeles, CA 90095
>
> http://www.jamesstroud.com

Thanks you James, i will check it now. thanks again for your kindness
help.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with -3 switch

2009-01-12 Thread Christian Heimes
John Machin schrieb:
> And therefore irrelevant.

No, Carl is talking about the very same issue.

> I would like to hear from someone who has actually started with
> working 2.x code and changed all their text-like "foo" to
> u"foo" [except maybe unlikely suspects like open()'s mode arg]:
> * how many places where the 2.x code broke and so did the 3.x code
> [i.e. the problem would have been detected without prepending u]
> * how many places where the 2.x code broke but the 3.x code didn't
> [i.e. prepending u did find the problem]
> * whether they thought it was worth the effort

Perhaps you also like to hear from a developer who has worked on Python
3.0 itself and who has done lots of work with internationalized
applications. If you want to get it right you must

* decode incoming text data to unicode as early as possible
* use unicode for all internal text data
* encode outgoing unicode as late as possible.

where incoming data is read from the file system, database, network etc.

This rule applies not only to Python 3.0 but to *any* application
written in *any* languate. The urlopen example is a very good example
for the issue. The author didn't think of decoding the incoming bytes to
unicode. In Python 2.x it works fine as long as the site contains ASCII
only. In Python 3.0 however an error is raised because binary data is no
longer implicitly converted to unicode.

Christian

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


Re: Implementing file reading in C/Python

2009-01-12 Thread Sion Arrowsmith
Grant Edwards   wrote:
>On 2009-01-09, Sion Arrowsmith  wrote:
>> Grant Edwards   wrote:
>>>If I were you, I'd try mmap()ing the file instead of reading it
>>>into string objects one chunk at a time.
>> You've snipped the bit further on in that sentence where the
>> OP says that the file of interest is 2GB. Do you still want to
>> try mmap'ing it?
>Sure.  The larger the file, the more you gain from mmap'ing it.
>2GB should easily fit within the process's virtual memory
>space.

Assuming you're in a 64bit world. Me, I've only got 2GB of address
space available to play in -- mmap'ing all of it out of the question.

But I supposed that mmap'ing it chunk at a time instead of reading
chunk at a time might be worth considering.

-- 
\S -- si...@chiark.greenend.org.uk -- 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


Re: Problem with -3 switch

2009-01-12 Thread John Machin
On Jan 12, 11:05 pm, Christian Heimes  wrote:
> John Machin schrieb:
>
> > And therefore irrelevant.
>
> No, Carl is talking about the very same issue.
>
> > I would like to hear from someone who has actually started with
> > working 2.x code and changed all their text-like "foo" to
> > u"foo" [except maybe unlikely suspects like open()'s mode arg]:
> > * how many places where the 2.x code broke and so did the 3.x code
> > [i.e. the problem would have been detected without prepending u]
> > * how many places where the 2.x code broke but the 3.x code didn't
> > [i.e. prepending u did find the problem]
> > * whether they thought it was worth the effort
>
> Perhaps you also like to hear from a developer who has worked on Python
> 3.0 itself and who has done lots of work with internationalized
> applications. If you want to get it right you must
>
> * decode incoming text data to unicode as early as possible
> * use unicode for all internal text data
> * encode outgoing unicode as late as possible.
>
> where incoming data is read from the file system, database, network etc.
>
> This rule applies not only to Python 3.0 but to *any* application
> written in *any* languate.

The above is a story with which I'm quite familiar. However it is
*not* the issue!! The issue is why would anyone propose changing a
string constant "foo" in working 2.x code to u"foo"?

> The urlopen example is a very good example
> for the issue. The author didn't think of decoding the incoming bytes to
> unicode. In Python 2.x it works fine as long as the site contains ASCII
> only. In Python 3.0 however an error is raised because binary data is no
> longer implicitly converted to unicode.

All very true but nothing to do with the "foo" -> u"foo" issue.
Somebody please come up with an example of how changing "foo" to
u"foo" could help a port from 2.x working code to a single codebase
that supports 2.x and 2to3ed 3.x.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6.1 @executable_path

2009-01-12 Thread googler . 1 . webmaster
Hi!

Woow, thanks. The unix command install_name_tool solved my problem.
Thanks a lot.
--
http://mail.python.org/mailman/listinfo/python-list


Encoding Title mail

2009-01-12 Thread Andrea Reginato
Hi to everybody, I'm trying to use libgmail version 0.1.9, to read
some mails from a google account and save the attached files. All
works fine, but when a tile has some letters with accents (like èùàòì)
I read a string like this.

=?ISO-8859-1?Q?=F2=E0=F9+=E8=EC'_0987654321_\?= =?ISO-8859-1?Q?
_=E9*=A7=B0=E7;:=5F_test_chars?=

I tried to use the string.decode(ISO-8859-1) function, but nothing
change.
I'm newbie into python, so I wanted to ask if you know how I could
solve this problem, as with a google search i didn't find the
solution.

Thanks a lot.
--
http://mail.python.org/mailman/listinfo/python-list


strange dict issue

2009-01-12 Thread Heston James - Cold Beans
Ok, this feels like a horribly noobish question to ask guys but I can't
figure this one out.

 

I have code which looks like this:

 

print this_config[1]



this_adapter_config["name"] = this_config[1]["NAME"]

 

Now, the print statement gives me the following:

 

{'value': 'Route66', 'key': 'NAME'}

 

Yet when the second line of my code throws an error saying the key 'NAME'
doesn't exist.

 

Any ideas on what's going on, seems quite senseless to me!?

 

Thanks,

 

Heston

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


Re: Implementing file reading in C/Python

2009-01-12 Thread sturlamolden
On Jan 9, 6:41 pm, Sion Arrowsmith 
wrote:

> You've snipped the bit further on in that sentence where the OP
> says that the file of interest is 2GB. Do you still want to try
> mmap'ing it?

Python's mmap object does not take an offset parameter. If it did, one
could mmap smaller portions of the file.

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


Re: Implementing file reading in C/Python

2009-01-12 Thread Sion Arrowsmith
In case the cancel didn't get through:

Sion Arrowsmith   wrote:
>Grant Edwards   wrote:
>>2GB should easily fit within the process's virtual memory
>>space.
>Assuming you're in a 64bit world. Me, I've only got 2GB of address
>space available to play in -- mmap'ing all of it out of the question.

And today's moral is: try it before posting. Yeah, I can map a 2GB
file no problem, complete with associated 2GB+ allocated VM. The
addressing is clearly not working how I was expecting it too.

-- 
\S -- si...@chiark.greenend.org.uk -- 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


Re: strange dict issue

2009-01-12 Thread Benjamin Kaplan
On Mon, Jan 12, 2009 at 7:24 AM, Heston James - Cold Beans <
heston.ja...@coldbeans.co.uk> wrote:

>  Ok, this feels like a horribly noobish question to ask guys but I can't
> figure this one out.
>
>
>
> I have code which looks like this:
>
>
>
> print this_config[1]
>
>
>
> this_adapter_config[*"name"*] = this_config[1][*"NAME"*]
>
>
>
> Now, the print statement gives me the following:
>
>
>
> {'value': 'Route66', 'key': 'NAME'}
>

>
> Yet when the second line of my code throws an error saying the key 'NAME'
> doesn't exist.
>
>
>
> Any ideas on what's going on, seems quite senseless to me!?
>
>
>
> Thanks,
>
>
>
> Heston
>



A dict stores key/value pairs. When you see the print of the dict, it shows
you {key1:value1, key2:value2}. So your dict has two keys ('value' and
'key') that map to two values ('Route66' and 'Name' respectively). 'Name' is
a value in the dict, not a key, so you can't use that syntax to get it.. In
order for this code to work, the dictionary would have to be {'NAME' :
'Route66'} or you would have to use this_config[1]['value']


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


Re: strange dict issue

2009-01-12 Thread Gary M. Josack

Heston James - Cold Beans wrote:


Ok, this feels like a horribly noobish question to ask guys but I 
can’t figure this one out.


I have code which looks like this:

print this_config[1]

this_adapter_config[/"name"/] = this_config[1][/"NAME"/]

Now, the print statement gives me the following:

{'value': 'Route66', 'key': 'NAME'}

Yet when the second line of my code throws an error saying the key 
‘NAME’ doesn’t exist.


Any ideas on what’s going on, seems quite senseless to me!?

Thanks,

Heston



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


'NAME' is the value, and 'key' is the key. Your dictionary has two keys 
mapped to two values:


'value' -> 'Route66'
'key' -> 'NAME'


You need to lookup the values by their key. Does that make sense?

Thanks,
Gary M. Josack
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with -3 switch

2009-01-12 Thread Christian Heimes
>> Perhaps you also like to hear from a developer who has worked on Python
>> 3.0 itself and who has done lots of work with internationalized
>> applications. If you want to get it right you must
>>
>> * decode incoming text data to unicode as early as possible
>> * use unicode for all internal text data
>> * encode outgoing unicode as late as possible.
>>
>> where incoming data is read from the file system, database, network etc.
>>
>> This rule applies not only to Python 3.0 but to *any* application
>> written in *any* languate.
> 
> The above is a story with which I'm quite familiar. However it is
> *not* the issue!! The issue is why would anyone propose changing a
> string constant "foo" in working 2.x code to u"foo"?

Do I really have to repeat "use unicode for all internal text data"?

"foo" and u"foo" are two totally different things. The former is a byte
sequence "\x66\x6f\x6f" while the latter is the text 'foo'. It just
happens that "foo" and u"foo" are equal in Python 2.x because
"foo".decode("ascii") == u"foo". In Python 3.x does it right, b"foo" is
unequal to "foo".

Christian

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


Re: Encoding Title mail

2009-01-12 Thread Marc 'BlackJack' Rintsch
On Mon, 12 Jan 2009 04:32:35 -0800, Andrea Reginato wrote:

> Hi to everybody, I'm trying to use libgmail version 0.1.9, to read some
> mails from a google account and save the attached files. All works fine,
> but when a tile has some letters with accents (like èùàòì) I read a
> string like this.
> 
> =?ISO-8859-1?Q?=F2=E0=F9+=E8=EC'_0987654321_\?= =?ISO-8859-1?Q?
> _=E9*=A7=B0=E7;:=5F_test_chars?=
> 
> I tried to use the string.decode(ISO-8859-1) function, but nothing
> change.
> I'm newbie into python, so I wanted to ask if you know how I could solve
> this problem, as with a google search i didn't find the solution.

Look into the `email` package in the standard library.

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


RE: strange dict issue

2009-01-12 Thread Heston James - Cold Beans
Gary, Ben,

Thanks for your replies, that makes perfect sense. This is a dict generated
by the ZSI web service library and looks different to what I expect. :-(

I'll work on your suggestions, thanks again for your help.

Heston

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


Python tricks

2009-01-12 Thread RajNewbie
Hi,
   My code has a lot of while loops of the following format:
   while True:
 ...
 if : break

   The danger with such a code is that it might go to an infinite loop
- if the  never occurs.
   Is there a way - a python trick - to have a check such that if the
loop goes for more than x number of steps, it will cause an exception?

   I do understand that we can use the code like -
   i = 0
   while True:
 i++
 if i > 200: raise infinite_Loop_Exception
 ...
 if : break

   But I am not very happy with this code for 3 reasons
   1. Verbosity (i=0 and i++) which doesnt add to the logic
   2. The loop now has dual focus. - incrementing i, etc.
   3.A person looks into the code and thinks 'i'
has special significance. His/her mind will be focused on not the
actual reason for the loop.

   The solution that I had in mind is:
   while True:
 ...
 if : break
 if inifinte_loop(): raise infiinte_loop_exception

  Wherein infinite_loop is a generator, which returns true if i > 200
  def infinite_loop():
 i = 0
 while i < 200:
 i++
 yield False
 yield True

Could somebody let me know whether this is a good option?
One of my main worries is - what will happen if I call this same
procedure from another loop? Will it start again from 0 or will it
again start from my previous stored i? i.e.
def big_proc:
  while True:
   ...
   if infinite_loop: raise

  while True:
...
   if infinite_loop: raise
In such a case, will we run it 200 times both the times or not?

Could someone chip in with other suggestions?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-12 Thread sturlamolden
On Jan 12, 1:52 pm, Sion Arrowsmith 
wrote:

> And today's moral is: try it before posting. Yeah, I can map a 2GB
> file no problem, complete with associated 2GB+ allocated VM. The
> addressing is clearly not working how I was expecting it too.

The virtual memory space of a 32 bit process is 4 GB.

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-12 Thread bieffe62
On 12 Gen, 00:02, Paul Rubin  wrote:
> Carl Banks  writes:
> > and where it was manipulated for that matter.
>
> > This criticism is completely unfair.  Instance variables have to be
> > manipulated somewhere, and unless your object is immutable, that is
> > going to happen outside of __init__.  That's true in Java, C++, and
> > pretty much any other language.
>
> The criticism is very valid.  Some languages do support immutable
> variables (e.g. "final" declarations in Java, "const" in C++, or
> universal immutability in pure functional languages) and they do so
> precisely for the purpose of taming the chaos of uncontrolled
> mutation.  It would be great if Python also supported immutability.
>
> > I'm not going to argue that this doesn't hurt readability, because it
> > does (though not nearly as much as you claim).  But there are other
> > considerations, and in this case the flexibility of defining
> > attributes outside __init__ is worth the potential decrease in
> > readability.
>
> There are cases where this is useful but they're not terribly common.
> I think it would be an improvement if creating new object attributes
> was by default not allowed outside the __init__ method.  In the cases
> where you really do want to create new attributes elsewhere, you'd
> have to explicitly enable this at instance creation time, for example
> by inheriting from a special superclass:
>
>    class Foo (DynamicAttributes, object): pass
>

You cannot do that, but you can establish a fixed set of attributes by
defining
the __slot__ class variable.


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


Re: strange dict issue

2009-01-12 Thread Ferdinand Sousa
James

First off, the computer is always right :-)

>  {'value': 'Route66', 'key': 'NAME'}
>
>
>
> Yet when the second line of my code throws an error saying the key 'NAME'
> doesn't exist.
>
If you look carefully the key NAME indeed does not exist. The dictionary
contains 2 key-value pairs.

This should clear things out:
*key   *  *value
*'value'   'Route66'
'key' 'NAME'


Get it? NAME is a value, not a key. You have used the string 'key' itself as
a dictionary key. My advice would be to not use such conflicting names.

Regards,
Ferdi
--
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing file reading in C/Python

2009-01-12 Thread Hrvoje Niksic
sturlamolden  writes:

> On Jan 9, 6:41 pm, Sion Arrowsmith 
> wrote:
>
>> You've snipped the bit further on in that sentence where the OP
>> says that the file of interest is 2GB. Do you still want to try
>> mmap'ing it?
>
> Python's mmap object does not take an offset parameter. If it did, one
> could mmap smaller portions of the file.

As of 2.6 it does, but that might not be of much use if you're using
2.5.x or earlier.  If you speak Python/C and really need offset, you
could backport the mmap module from 2.6 and compile it under a
different name for 2.5.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python tricks

2009-01-12 Thread Ben Finney
RajNewbie  writes:

> Could someone chip in with other suggestions?

Set up an iterable that will end under the right conditions. Then,
iterate over that with ‘for foo in that_iterable’. This idiom is
usually far more expressive than any tricks with ‘while’ loops and
‘break’ statements.

For tools to work with that can give you such an iterable without
needing to make one from scratch, try the following and use the one
that is most suitable to the problem at hand:

* list
* dict
* list comprehension
* generator expression
* generator function
* functions from the ‘itertools’ module

-- 
 \   “Too many Indians spoil the golden egg.” —Sir Joh |
  `\   Bjelke-Petersen |
_o__)  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with -3 switch

2009-01-12 Thread John Machin
On Jan 13, 12:06 am, Christian Heimes  wrote:
> >> Perhaps you also like to hear from a developer who has worked on Python
> >> 3.0 itself and who has done lots of work with internationalized
> >> applications. If you want to get it right you must
>
> >> * decode incoming text data to unicode as early as possible
> >> * use unicode for all internal text data
> >> * encode outgoing unicode as late as possible.
>
> >> where incoming data is read from the file system, database, network etc.
>
> >> This rule applies not only to Python 3.0 but to *any* application
> >> written in *any* languate.
>
> > The above is a story with which I'm quite familiar. However it is
> > *not* the issue!! The issue is why would anyone propose changing a
> > string constant "foo" in working 2.x code to u"foo"?
>
> Do I really have to repeat "use unicode for all internal text data"?
>
> "foo" and u"foo" are two totally different things. The former is a byte
> sequence "\x66\x6f\x6f" while the latter is the text 'foo'. It just
> happens that "foo" and u"foo" are equal in Python 2.x because
> "foo".decode("ascii") == u"foo". In Python 3.x does it right, b"foo" is
> unequal to "foo".
>

Again, all very true, but irrelevant. b"foo" is *not* involved.

You're ignoring the effect of 2to3:

Original 2.x code: assert "foo" == u"foo" # works
output from 2to3: assert "foo" == "foo" # works

Original 2.x code with u prepended: assert u"foo" == u"foo" # works
output from 2to3: assert "foo" == "foo" # works

I say again, show me a case of working 2.5 code where prepending u to
an ASCII string constant that is intended to be used in a text context
is actually worth the keystrokes.
--
http://mail.python.org/mailman/listinfo/python-list


Re: hlep: a text search and rename question

2009-01-12 Thread sensen
both above works well, but a problem is renamed file is without
filename extension.

only change to the title.

for example, the origin file is track_1.flac, after run the script i
want it to for example, White Flag.flac, but now it change to White
Flag without extension.

could you do a favor to fix it. thank you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-12 Thread Paul Rubin
bieff...@gmail.com writes:
> >    class Foo (DynamicAttributes, object): pass
> >
> You cannot do that, but you can establish a fixed set of attributes by
> defining the __slot__ class variable.

That is not what __slot__ is for.
--
http://mail.python.org/mailman/listinfo/python-list


Re: are there some special about '\x1a' symbol

2009-01-12 Thread sim.sim
On 10 янв, 23:40, John Machin  wrote:
> On Jan 11, 2:45 am, "sim.sim"  wrote:
>
>
>
> > Hi all!
>
> > I had touch with some different python behavior: I was tried to write
> > into a file a string with the '\x1a' symbol, and for FreeBSD system,
> > it gives expected result:
>
> > >>> open("test", "w").write('before\x1aafter')
> > >>> open('test').read()
>
> > 'before\x1aafter'
>
> > but for my WinXP box, it gives some strange:
>
> > >>> open("test", "w").write('before\x1aafter')
> > >>> open('test').read()
>
> > 'before'
>
> > Here I can write all symbols, but not read.
> > I've tested it with python 2.6, 2.5 and 2.2 and WinXP SP2.
>
> > Why is it so and is it possible to fix it?
>
> You've already got two good answers, but this might add a little more
> explanation:
>
> You will aware that in Windows Command Prompt, to exit the interactive
> mode of Python (among others), you need to type Ctrl-Z ...
>
> | C:\junk>python
> | Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
> (Intel)] on
> win32
> | Type "help", "copyright", "credits" or "license" for more
> information.
> | >>> problem = '\x1a'
> | >>> ord(problem)
> | 26
> | >>> # What is the 26th letter of the English/ASCII alphabet?
> | ...
> | >>> ^Z
> |
> | C:\junk>
>
> HTH,
> John

Hi John,

I agree - those two answers are really good. Thanks to Mel and Marc.
I'm sorry if my stupid question was annoyed you.

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


Re: Python tricks

2009-01-12 Thread Tim Chase

   My code has a lot of while loops of the following format:
   while True:
 ...
 if : break

   The danger with such a code is that it might go to an infinite loop
- if the  never occurs.
   Is there a way - a python trick - to have a check such that if the
loop goes for more than x number of steps, it will cause an exception?

   I do understand that we can use the code like -
   i = 0
   while True:
 i++
 if i > 200: raise infinite_Loop_Exception
 ...
 if : break

   But I am not very happy with this code for 3 reasons
   1. Verbosity (i=0 and i++) which doesnt add to the logic
   2. The loop now has dual focus. - incrementing i, etc.
   3.A person looks into the code and thinks 'i'
has special significance. His/her mind will be focused on not the
actual reason for the loop.


My first thought would be to simply not use "while True":

  INFINITE_LOOP_COUNT = 200
  for _ in xrange(INFINITE_LOOP_COUNT):
do_something()
if : break
  else:
raise InfiniteLoopException


   The solution that I had in mind is:
   while True:
 ...
 if : break
 if inifinte_loop(): raise infiinte_loop_exception

  Wherein infinite_loop is a generator, which returns true if i > 200
  def infinite_loop():
 i = 0
 while i < 200:
 i++
 yield False
 yield True

Could somebody let me know whether this is a good option?


To do this, you'd need to do the same sort of thing as you do 
with your i/i++ variable:


  i = infinite_loop()
  while True:
...
if : break
if i.next(): raise InfiniteLoopException

which doesn't gain much, and makes it a whole lot more confusing.


Could someone chip in with other suggestions?


As an aside:  the phrase is "chime in"[1] (to volunteer 
suggestions) "Chip in"[2] usually involves contributing money to 
a common fund ("care to chip in $10 for Sally's wedding gift from 
the office?"  where the pool of money would then be used to buy 
one large/expensive gift for Sally)


-tkc


[1]
http://www.thefreedictionary.com/chime+in

[2]
http://www.english-test.net/forum/ftopic1768.html

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


Re: are there some special about '\x1a' symbol

2009-01-12 Thread John Machin
On Jan 13, 12:45 am, "sim.sim"  wrote:
> On 10 ÑÎ×, 23:40, John Machin  wrote:
>
>
>
>
>
> > On Jan 11, 2:45šam, "sim.sim"  wrote:
>
> > > Hi all!
>
> > > I had touch with some different python behavior: I was tried to write
> > > into a file a string with the '\x1a' symbol, and for FreeBSD system,
> > > it gives expected result:
>
> > > >>> open("test", "w").write('before\x1aafter')
> > > >>> open('test').read()
>
> > > 'before\x1aafter'
>
> > > but for my WinXP box, it gives some strange:
>
> > > >>> open("test", "w").write('before\x1aafter')
> > > >>> open('test').read()
>
> > > 'before'
>
> > > Here I can write all symbols, but not read.
> > > I've tested it with python 2.6, 2.5 and 2.2 and WinXP SP2.
>
> > > Why is it so and is it possible to fix it?
>
> > You've already got two good answers, but this might add a little more
> > explanation:
>
> > You will aware that in Windows Command Prompt, to exit the interactive
> > mode of Python (among others), you need to type Ctrl-Z ...
>
> > | C:\junk>python
> > | Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
> > (Intel)] on
> > win32
> > | Type "help", "copyright", "credits" or "license" for more
> > information.
> > | >>> problem = '\x1a'
> > | >>> ord(problem)
> > | 26
> > | >>> # What is the 26th letter of the English/ASCII alphabet?
> > | ...
> > | >>> ^Z
> > |
> > | C:\junk>
>
> > HTH,
> > John
>
> Hi John,
>
> I agree - those two answers are really good. Thanks to Mel and Marc.
> I'm sorry if my stupid question was annoyed you.

I didn't think your question was stupid. Stupid was (a) CP/M recording
file size as number of 128-byte sectors, forcing the use of an in-band
EOF marker for text files (b) MS continuing to regard Ctrl-Z as an EOF
decades after people stopped writing Ctrl-Z at the end of text files.

And I wasn't annoyed either ... I was merely adding the information
that Ctrl-Z and '\x1a' were the same thing; many people don't make the
connection.

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


Re: Python tricks

2009-01-12 Thread RajNewbie
On Jan 12, 6:51 pm, Tim Chase  wrote:
> >    My code has a lot of while loops of the following format:
> >    while True:
> >      ...
> >      if : break
>
> >    The danger with such a code is that it might go to an infinite loop
> > - if the  never occurs.
> >    Is there a way - a python trick - to have a check such that if the
> > loop goes for more than x number of steps, it will cause an exception?
>
> >    I do understand that we can use the code like -
> >    i = 0
> >    while True:
> >      i++
> >      if i > 200: raise infinite_Loop_Exception
> >      ...
> >      if : break
>
> >    But I am not very happy with this code for 3 reasons
> >    1. Verbosity (i=0 and i++) which doesnt add to the logic
> >    2. The loop now has dual focus. - incrementing i, etc.
> >    3.    A person looks into the code and thinks 'i'
> > has special significance. His/her mind will be focused on not the
> > actual reason for the loop.
>
> My first thought would be to simply not use "while True":
>
>    INFINITE_LOOP_COUNT = 200
>    for _ in xrange(INFINITE_LOOP_COUNT):
>      do_something()
>      if : break
>    else:
>      raise InfiniteLoopException
>
> >    The solution that I had in mind is:
> >    while True:
> >      ...
> >      if : break
> >      if inifinte_loop(): raise infiinte_loop_exception
>
> >   Wherein infinite_loop is a generator, which returns true if i > 200
> >   def infinite_loop():
> >      i = 0
> >      while i < 200:
> >          i++
> >          yield False
> >      yield True
>
> > Could somebody let me know whether this is a good option?
>
> To do this, you'd need to do the same sort of thing as you do
> with your i/i++ variable:
>
>    i = infinite_loop()
>    while True:
>      ...
>      if : break
>      if i.next(): raise InfiniteLoopException
>
> which doesn't gain much, and makes it a whole lot more confusing.
>
> > Could someone chip in with other suggestions?
>
> As an aside:  the phrase is "chime in"[1] (to volunteer
> suggestions) "Chip in"[2] usually involves contributing money to
> a common fund ("care to chip in $10 for Sally's wedding gift from
> the office?"  where the pool of money would then be used to buy
> one large/expensive gift for Sally)
>
> -tkc
>
> [1]http://www.thefreedictionary.com/chime+in
>
> [2]http://www.english-test.net/forum/ftopic1768.html

Thank you very much Tim.
I agree on all counts - esp the fact that my suggestion is very
confusing + (chime in part too :) ).
But, I still feel it would be much more aesthetically pleasing if I
can call a single procedure like
if infinite_loop() -> to do the same.
Is it somehow possible? - say by using static variables, iterators --
anything?

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


Re: Python tricks

2009-01-12 Thread Paul Rubin
RajNewbie  writes:
>I do understand that we can use the code like -
>i = 0
>while True:
>  i++
>  if i > 200: raise infinite_Loop_Exception
>  ...
>  if : break
> 
>But I am not very happy with this code for 3 reasons

I prefer:

   from itertools import count

   for i in count():
   if i > 200: raise infinite_Loop_Exception
   ...

You could also use:

   for i in xrange(200): 
  ...
   else: 
  raise infinite_Loop_Exception

The "else" clause runs only if no break statement is executed.
--
http://mail.python.org/mailman/listinfo/python-list


Re: mod_python: delay in files changing after alteration

2009-01-12 Thread psaff...@googlemail.com
On 6 Jan, 23:31, Graham Dumpleton  wrote:

> Thus, any changes to modules/packages installed on sys.path require a
> full restart of Apache to ensure they are loaded by all Apache child
> worker processes.
>

That will be it. I'm pulling in some libraries of my own from
elsewhere, which are still being modified to accommodate the web app.
These are the changes that are causing the problems. An Apache restart
isn't too onerous - I'll just start doing that.

Thanks,

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


Re: Python tricks

2009-01-12 Thread John Machin
On Jan 13, 12:51 am, Tim Chase  took a
walk on the OT side:

> > Could someone chip in with other suggestions?
>
> As an aside:  the phrase is "chime in"[1] (to volunteer
> suggestions) "Chip in"[2] usually involves contributing money to
> a common fund ("care to chip in $10 for Sally's wedding gift from
> the office?"  where the pool of money would then be used to buy
> one large/expensive gift for Sally)
>

> [1]http://www.thefreedictionary.com/chime+in
> [2]http://www.english-test.net/forum/ftopic1768.html

All rather locale-dependent; see e.g. http://www.answers.com/topic/chip-in

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-12 Thread Roy Smith
In article <7x1vv83afq@ruckus.brouhaha.com>,
 Paul Rubin  wrote:

> bieff...@gmail.com writes:
> > >    class Foo (DynamicAttributes, object): pass
> > >
> > You cannot do that, but you can establish a fixed set of attributes by
> > defining the __slot__ class variable.
> 
> That is not what __slot__ is for.

Right.  And tuples are not immutable lists (ducking and covering).
--
http://mail.python.org/mailman/listinfo/python-list


Egg deinstallation

2009-01-12 Thread mk

Hello everyone,

I googled and googled and can't seem to find the definitive answer: how 
to *properly* deinstall egg? Just delete the folder and/or .py and .pyc 
files from Lib/site-packages? Would that break anything in Python 
installation or not?



Regards,
mk

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


subprocess.Popen stalls

2009-01-12 Thread psaff...@googlemail.com
I'm building a bioinformatics application using the ipcress tool:

http://www.ebi.ac.uk/~guy/exonerate/ipcress.man.html

I'm using subprocess.Popen to execute ipcress, which takes a group of
files full of DNA sequences and returns some analysis on them. Here's
a code fragment:

cmd = "/usr/bin/ipcress ipcresstmp.txt --sequence /home/pzs/genebuilds/
human/*.fasta"
print "checking with ipcress using command", cmd
p = Popen(cmd, shell=True, bufsize=100, stdout=PIPE, stderr=PIPE)
retcode = p.wait()
if retcode != 0:
print "ipcress failed with error code:", retcode
raise Exception
output = p.stdout.read()

If I run the command at my shell, it finishes successfully. It takes
30 seconds - it uses 100% of one core and several hundred MB of memory
during this time. The output is 220KB of text.

However, running it through Python as per the above code, it stalls
after 5 seconds not using any processor at all. I've tried leaving it
for a few minutes with no change. If I interrupt it, it's at the
"retcode = p.wait()" line.

I've tried making the bufsize really large and that doesn't seem to
help. I'm a bit stuck - any suggestions? This same command has worked
fine on other ipcress runs. This one might generate more output than
the others, but 220KB isn't that much, is it?

Peter

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


Re: Egg deinstallation

2009-01-12 Thread Diez B. Roggisch
mk wrote:

> Hello everyone,
> 
> I googled and googled and can't seem to find the definitive answer: how
> to *properly* deinstall egg? Just delete the folder and/or .py and .pyc
> files from Lib/site-packages? Would that break anything in Python
> installation or not?

It depends on how you installed it. If it is done via
setuptools/easy_install, you should also clean up the 

site-packages/easy-install.pth

file.

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


Re: hlep: a text search and rename question

2009-01-12 Thread sensen
On Jan 12, 10:41 pm, sensen  wrote:
> both above works well, but a problem is renamed file is without
> filename extension.
>
> only change to the title.
>
> for example, the origin file is track_1.flac, after run the script i
> want it to for example, White Flag.flac, but now it change to White
> Flag without extension.
>
> could you do a favor to fix it. thank you.

fixed it, i check the reference of python.
the last line  from
   os.rename(old, new)
changed to
   os.rename(old, new+'.flac')

thanks, now i still try to read the reference of python.
i want to the script can know what file it will open. in other words,
the script maybe first do a search, and get the exactly file name of
the cue type file in the folder, then open it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen stalls

2009-01-12 Thread Jean-Paul Calderone

On Mon, 12 Jan 2009 06:37:35 -0800 (PST), "psaff...@googlemail.com" 
 wrote:

I'm building a bioinformatics application using the ipcress tool:

http://www.ebi.ac.uk/~guy/exonerate/ipcress.man.html

I'm using subprocess.Popen to execute ipcress, which takes a group of
files full of DNA sequences and returns some analysis on them. Here's
a code fragment:

cmd = "/usr/bin/ipcress ipcresstmp.txt --sequence /home/pzs/genebuilds/
human/*.fasta"
print "checking with ipcress using command", cmd
p = Popen(cmd, shell=True, bufsize=100, stdout=PIPE, stderr=PIPE)
retcode = p.wait()
if retcode != 0:
print "ipcress failed with error code:", retcode
raise Exception
output = p.stdout.read()

If I run the command at my shell, it finishes successfully. It takes
30 seconds - it uses 100% of one core and several hundred MB of memory
during this time. The output is 220KB of text.

However, running it through Python as per the above code, it stalls
after 5 seconds not using any processor at all. I've tried leaving it
for a few minutes with no change. If I interrupt it, it's at the
"retcode = p.wait()" line.

I've tried making the bufsize really large and that doesn't seem to
help. I'm a bit stuck - any suggestions? This same command has worked
fine on other ipcress runs. This one might generate more output than
the others, but 220KB isn't that much, is it?


You have to read the output.  Otherwise, the process's stdout fills up
and its write attempt eventually blocks, preventing it from continuing.

If you use Twisted's process API instead, the reading will be done for
you (without any of the race conditions that are likely when using the
subprocess module), and things will probably "just work".

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


Re: Egg deinstallation

2009-01-12 Thread mk

Diez B. Roggisch wrote:

Thanks, Diez.



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


Compressed vs uncompressed eggs

2009-01-12 Thread mk

Hello everyone,

Are there *good* reasons to use uncompressed eggs?

Is there a, say, performance penalty in using compressed eggs?

Regards,
mk

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


Re: subprocess.Popen stalls

2009-01-12 Thread mk

psaff...@googlemail.com wrote:


p = Popen(cmd, shell=True, bufsize=100, stdout=PIPE, stderr=PIPE)
output = p.stdout.read()


Better use communicate() method:

standardoutputstr, standarderrorstr = subprocess.communicate(...)

Never had any problem with subprocesses when using subprocess module in 
this manner (well it's possible that standardoutputstr and/or 
standarderrorstr fill up the memory, you get the idea).


Regards,
mk



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


Re: Compressed vs uncompressed eggs

2009-01-12 Thread Diez B. Roggisch
mk wrote:

> Hello everyone,
> 
> Are there *good* reasons to use uncompressed eggs?

Plenty. Mostly that some things simply don't work in compressed eggs. See
the zipsafe-flag in setuptools-docs.
 
> Is there a, say, performance penalty in using compressed eggs?

Not that it matters I'd say - only if you had an application that imports a
lot & runs very short time.

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


Re: ActiveState Python Together with "Regular" Python) ((DLE)

2009-01-12 Thread W. eWatson

John Machin wrote:

On Jan 12, 9:16 pm, "W. eWatson"  wrote:

John Machin wrote:

On Jan 12, 2:00 pm, "W. eWatson"  wrote:

I installed "Python" 2.5 a few months ago with IDLE, and decided I'd like to
try windowpy from ActiveState. Is having both of these installed going to
cause me trouble?

What is "windowpy from ActiveState"? If you mean you wanted to try the
PythonWin IDE instead of IDLE, all you needed to do was go to
http://sourceforge.net/projects/pywin32/hit the big download button
and make sure you get the 2.5 version (it's the default atm) and
install it.

Yes, I want to try the PythonWin IDE instead of IDLE. "(DLE" was a typo.

Is it possible to just disable the vanilla (IDLE) version? I may want to
switch between the two. Most users of the program I'm about to modify use
the vanilla version. At some point, I may want to go back to it to verify
that in their world all is OK.


I'll try again. All you need is (a) the official distribution of
Python 2.5 for Windows from http://www.python.org (b) the pywin32
package from the URL I gave you. Install both. Click on Start / All
Programs/ Python 2.5 and will find *BOTH* IDLE and PythonWin. You and/
or your users can use IDLE or PythonWin, only one copy of Python, no
conflicts. Why do you think you need to "disable" IDLE? Just don't use
it.

Once upon a time, six months ago, I asked a similar question and the answer 
was more complex. I decided to put this off until I truly needed it.


Even better than Start, for me, is that I can choose PythonWin or IDLE from 
the right-click menu which I want. I do see a choice there of Open With-> 
Python. I can see it executed properly, but which 2.5 did it use?


--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-12 Thread bieffe62
On 12 Gen, 14:45, Paul Rubin  wrote:
> bieff...@gmail.com writes:
> > >    class Foo (DynamicAttributes, object): pass
>
> > You cannot do that, but you can establish a fixed set of attributes by
> > defining the __slot__ class variable.
>
> That is not what __slot__ is for.


Really? It seems to work:

>>> class A(object):
... __slots__ = ('a', 'b')
... def __init__(self): self.not_allowed = 1
...
>>> a = A()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in __init__
AttributeError: 'A' object has no attribute 'not_allowed'
>>>

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


Re: Implementing file reading in C/Python

2009-01-12 Thread Grant Edwards
On 2009-01-12, Sion Arrowsmith  wrote:
> Grant Edwards   wrote:
>>On 2009-01-09, Sion Arrowsmith  wrote:
>>> Grant Edwards   wrote:
If I were you, I'd try mmap()ing the file instead of reading it
into string objects one chunk at a time.
>>> You've snipped the bit further on in that sentence where the
>>> OP says that the file of interest is 2GB. Do you still want to
>>> try mmap'ing it?
>>Sure.  The larger the file, the more you gain from mmap'ing it.
>>2GB should easily fit within the process's virtual memory
>>space.
>
> Assuming you're in a 64bit world. Me, I've only got 2GB of address
> space available to play in -- mmap'ing all of it out of the question.

Oh.  I assumed that decent 32-bit OSes would provide at least 3-4GB
of address space to user processes.  What OS are you using?

> But I supposed that mmap'ing it chunk at a time instead of
> reading chunk at a time might be worth considering.

I'd try mmap'ing it in large chunks (512MB maybe).

-- 
Grant Edwards   grante Yow! I feel like a wet
  at   parking meter on Darvon!
   visi.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen stalls

2009-01-12 Thread psaff...@googlemail.com
On 12 Jan, 15:33, mk  wrote:
>
> Better use communicate() method:
>

Oh yes - it's right there in the documentation. That worked perfectly.

Many thanks,

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


Re: Implementing file reading in C/Python

2009-01-12 Thread Grant Edwards
On 2009-01-12, Sion Arrowsmith  wrote:
> In case the cancel didn't get through:
>
> Sion Arrowsmith   wrote:
>>Grant Edwards   wrote:
>>>2GB should easily fit within the process's virtual memory
>>>space.
>>Assuming you're in a 64bit world. Me, I've only got 2GB of address
>>space available to play in -- mmap'ing all of it out of the question.
>
> And today's moral is: try it before posting. Yeah, I can map a 2GB
> file no problem, complete with associated 2GB+ allocated VM. The
> addressing is clearly not working how I was expecting it too.

Cool.  I'd be very interested to to know how the performance
compares to open/read.

-- 
Grant Edwards   grante Yow! I think I am an
  at   overnight sensation right
   visi.comnow!!
--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen stalls

2009-01-12 Thread mk

psaff...@googlemail.com wrote:

On 12 Jan, 15:33, mk  wrote:

Better use communicate() method:



Oh yes - it's right there in the documentation. That worked perfectly.


What's also in the docs and I did not pay attention to before:

Note

The data read is buffered in memory, so do not use this method if the 
data size is large or unlimited.



Regards,
mk

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


Re: Compressed vs uncompressed eggs

2009-01-12 Thread Christian Heimes
> Is there a, say, performance penalty in using compressed eggs?

To the contrary, it can be faster to import from a zip file than from
the file system.

Christian

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


Re: BadZipfile "file is not a zip file"

2009-01-12 Thread webcomm
If anyone's interested, here are my django views...


from django.shortcuts import render_to_response
from django.http import HttpResponse
from xml.etree.ElementTree import ElementTree
import urllib, base64, subprocess

def get_data(request):
service_url = 'http://www.something.com/webservices/someservice/
etc?user=etc&pass=etc'
xml = urllib.urlopen(service_url)
#the base64-encoded string is in a one-element xml doc...
tree = ElementTree()
xml_doc = tree.parse(xml)
datum = ""
for node in xml_doc.getiterator():
 datum = "%s" % (node.text)
decoded = base64.b64decode(datum)

dir = '/path/to/data/'
f = open(dir+'data.zip', 'wb')
f.write(decoded)
f.close()

file = subprocess.call('unzip '+dir+'data.zip -d '+dir,
shell=True)
file = open(dir+'data', 'rb').read()
txt = file.decode('utf_16_le')

return render_to_response('output.html',{
'output' : txt
})

def read_xml(request):
xml = urllib.urlopen('http://www.something.org/get_data/')  #page
using the get_data view
xml = xml.read()
xml = unicode(xml)
xml = '\n'+xml+''

f = open('/path/to/temp.txt','w')
f.write(xml)
f.close()

tree = ElementTree()
xml_doc = tree.parse('/path/to/temp.txt')
datum = ""
for node in xml_doc.getiterator():
 datum = "%s%s - %s" % (datum, node.tag, node.text)

return render_to_response('output.html',{
'output' : datum
})


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


read a password protected xls file

2009-01-12 Thread thomas . steffen75
Hello,
how can I read (and parse) a password protected xls file, perhaps with
the package xlrd?
Thanks for your hints, Thomas
--
http://mail.python.org/mailman/listinfo/python-list


Re: Encoding Title mail

2009-01-12 Thread Andrea Reginato
On Jan 12, 2:06 pm, Marc 'BlackJack' Rintsch  wrote:
> On Mon, 12 Jan 2009 04:32:35 -0800, Andrea Reginato wrote:
> > Hi to everybody, I'm trying to use libgmail version 0.1.9, to read some
> > mails from a google account and save the attached files. All works fine,
> > but when a tile has some letters with accents (like èùàòì) I read a
> > string like this.
>
> > =?ISO-8859-1?Q?=F2=E0=F9+=E8=EC'_0987654321_\?= =?ISO-8859-1?Q?
> > _=E9*=A7=B0=E7;:=5F_test_chars?=
>
> > I tried to use the string.decode(ISO-8859-1) function, but nothing
> > change.

> Look into the `email` package in the standard library.
>
> Ciao,
>         Marc 'BlackJack' Rintsch


I tried to look at it, but nothing to do, sorry.
I used email.Utils.decode_rfc2231(msg['Subject']) that seems what I
need (I could be wrong, but it looks the closer at what I need) but it
do not work out. I'm searching into the library, I'm not able to get
the solution.

Thanks again for your time
--
http://mail.python.org/mailman/listinfo/python-list


Re: BadZipfile "file is not a zip file"

2009-01-12 Thread Chris Mellon
On Sat, Jan 10, 2009 at 1:32 PM, webcomm  wrote:
> On Jan 9, 7:33 pm, John Machin  wrote:
>> It is not impossible for a file with dummy data to have been
>> handcrafted or otherwise produced by a process different to that used
>> for a real-data file.
>
> I knew it was produced by the same process, or I wouldn't have shared
> it. : )
> But you couldn't have known that.
>
>
>> > Not sure if you've seen this 
>> > thread...http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>>
>> Yeah, I've seen it ... (sigh) ... pax Steve Holden, but *please* stick
>> with one thread ...
>
> Thanks... I thought I was posting about separate issues and would
> annoy people who were only interested in one of the issues if I put
> them both in the same thread.  I guess all posts re: the same script
> should go in one thread, even if the questions posed may be unrelated
> and may be separate issues.  There are grey areas.
>
> Problem solved in John Machin's post at
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/d84f42493fe81864/03b8341539d87989?hl=en&lnk=raot#03b8341539d87989
>


It's worth pointing out (although the provider probably doesn't care)
that this isn't really an XML document and this was a bad way of them
to distribute the data. If they'd used a correctly formatted XML
document (with the prelude and everything) with the correct encoding
information, existing XML parsers should have just Done The Right
Thing with the data, instead of you needing to know the encoding a
priori to extract an XML fragment.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-12 Thread Marc 'BlackJack' Rintsch
On Mon, 12 Jan 2009 07:42:47 -0800, bieffe62 wrote:

> On 12 Gen, 14:45, Paul Rubin  wrote:
>> bieff...@gmail.com writes:
>> > >    class Foo (DynamicAttributes, object): pass
>>
>> > You cannot do that, but you can establish a fixed set of attributes
>> > by defining the __slot__ class variable.
>>
>> That is not what __slot__ is for.
> 
> 
> Really? It seems to work:

It works but it is not what `__slot__` was invented for.  Some call it a 
misuse of that feature if you use it to prevent adding attributes 
dynamically.  And it has some drawbacks when you inherit from such 
classes.

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


Re: Python logging rollover

2009-01-12 Thread Scott David Daniels

Kottiyath wrote:

Hi,

I want to do a log rollover   I tested it with 'midnight' option,
but it did not work as I expected.

Please google "smart questions".
All I can conclude from your message is that your expectations are
wrong.  It is not enough to tell us you are confused.  You need to
show us enough so that we can discover what might, or might not, be
going wrong.  At the least, what was your code, what happened, and
what did you expect to happen.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


VTK in python

2009-01-12 Thread Almar Klein
Hi all,

I want to use the Visualisation ToolKit from python.

However, I cannot find an easy way to install it. Of course, I could
download the source, use CMake to build and VS to compile it,
but... yeah, that takes a lot of time and will probably not work the
first time...

I noticed that with VTK 4.0 there was a vtk40python.exe installer
supplied. But that realease if almost 7 years old... Why is such
an installer no longer distributed with the newer releases?

I hope I overlooked something, or else I guess I should go compiling
tomorrow... :(

Thanks for any help,
  Almar

PS: I know entought ships with VTK, but it's such a huge install I try
to avoid that...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Bug in python [was: Fatal Python error: ceval: tstate mix-up]

2009-01-12 Thread Terry Reedy

Laszlo Nagy wrote:

Laszlo Nagy wrote:
Meanwhile I'm trying to turn off threads in that program one by one. I 
just got this new type of error:


Fatal Python error: PyThreadState_Delete: invalid tstate
After some days, there are now answers to my question. I guess this is 
because nobody knows the answer. I think I need to consider this a bug 
in Python. Where can I post a bug report?


bugs.python.org

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


Re: Bug in python [was: Fatal Python error: ceval: tstate mix-up]

2009-01-12 Thread Terry Reedy

Terry Reedy wrote:

Laszlo Nagy wrote:

Laszlo Nagy wrote:
Meanwhile I'm trying to turn off threads in that program one by one. 
I just got this new type of error:


Fatal Python error: PyThreadState_Delete: invalid tstate
After some days, there are now answers to my question. I guess this is 
because nobody knows the answer. I think I need to consider this a bug 
in Python. Where can I post a bug report?


bugs.python.org


Before submitting, search for existing report of same bug.  If none, try 
to write a minimal program that reliably reproduces the bug.  (If you 
can't, it will be hard to fix.).  Make sure the bug occurs with 2.6.1 or 
3.0.  Earlier 2.x is not maintained except for security patches.


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


Re: Python tricks

2009-01-12 Thread Scott David Daniels

RajNewbie wrote:

On Jan 12, 6:51 pm, Tim Chase  wrote:

   [a perfectly fine reply which is how I'd solve it]
>> RajNewbie wrote:

... The solution that I had in mind is:
   while True:
 ...
 if : break
 if inifinte_loop(): raise infiinte_loop_exception
  Wherein infinite_loop is a generator, which returns true if i > 200
  def infinite_loop():
 i = 0
 while i < 200:
 i++
 yield False
 yield True
Could somebody let me know whether this is a good option?

...
But, I still feel it would be much more aesthetically pleasing if I
can call a single procedure like
if infinite_loop() -> to do the same.
Is it somehow possible? - say by using static variables, iterators --
anything?


1) Please cut down quoted text to as little as needed to
   understand the reply.
Yes, it is possible.  After:

def Fuse(count, exception):
for i in range(count):
yield None
raise exception

You can do your loop as:
check_infinite = Fuse(200, ValueError('Infinite Loop')).next
while True:
...
check_infinite()
but I agree with Tim that a for ... else loop for the limit is clearer.


--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Simple CGI-XMLRPC failure

2009-01-12 Thread Mike MacHenry
I am having a difficult time understanding why my very simple
CGI-XMLRPC test isn't working. I created a server to export two
functions, the built-in function "pow" and my own identity function
"i". I run a script to call both of them and the "pow" work fine but
the "i" gives me an error that says my XMLRPC server doesn't support
than name. Here is the code for both files and the output:

#!/usr/bin/env python
#This file is /usr/lib/cgi-bin/roundwarerpc.py
from SimpleXMLRPCServer import CGIXMLRPCRequestHandler
def i(x):
return x
server = CGIXMLRPCRequestHandler()
server.register_function(pow)
server.register_function(i)
server.handle_request()


#!/usr/bin/env python
#This file is ~/test.py
import xmlrpclib
server = xmlrpclib.ServerProxy("http://localhost/cgi-bin/roundwarerpc.py";)
print server.pow(2,3)
print server.i(10)

#This is the STDOUT and STDERR when running ~/test.py
dski...@dskippy-laptop:$ python test.py 8
Traceback (most recent call last):
  File "test.py", line 4, in 
print server.test(10)
  File "/usr/lib/python2.5/xmlrpclib.py", line 1147, in __call__
return self.__send(self.__name, args)
  File "/usr/lib/python2.5/xmlrpclib.py", line 1437, in __request
verbose=self.__verbose
  File "/usr/lib/python2.5/xmlrpclib.py", line 1201, in request
return self._parse_response(h.getfile(), sock)
  File "/usr/lib/python2.5/xmlrpclib.py", line 1340, in _parse_response
return u.close()
  File "/usr/lib/python2.5/xmlrpclib.py", line 787, in close
raise Fault(**self._stack[0])
xmlrpclib.Fault: :method "i"
is not supported'>

Does anyone know what might be wrong with this?

Thanks for the help,
-mike

p.s.
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Server version Apache/2.2.8 (Ubuntu)
Server built: Jun 25 2008 13:54:13
--
http://mail.python.org/mailman/listinfo/python-list


Re: Egg deinstallation

2009-01-12 Thread Ned Deily
In article <6t139nf8ip4...@mid.uni-berlin.de>,
 "Diez B. Roggisch"  wrote:
> mk wrote:
> > I googled and googled and can't seem to find the definitive answer: how
> > to *properly* deinstall egg? Just delete the folder and/or .py and .pyc
> > files from Lib/site-packages? Would that break anything in Python
> > installation or not?
> 
> It depends on how you installed it. If it is done via
> setuptools/easy_install, you should also clean up the 
> 
> site-packages/easy-install.pth
> 
> file.

... and which you can do by editing that file directly or by using the 
easy_install -m option to mark the egg as multi-version before deleting.  
In either case, keep in mind that the egg may have installed one or more 
scripts; those have to be removed manually.



-- 
 Ned Deily,
 n...@acm.org

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


Re: Unbinding Tkinter default bindings for Listbox

2009-01-12 Thread Roger
On Jan 12, 6:31 am, James Stroud  wrote:
> James Stroud wrote:
> > py> b.tk.call('bind', 'Listbox', '', _)
>
> You want b.tk.call('bind', 'Listbox', '', "") of course.
>
> James
>
> --
> James Stroud
> UCLA-DOE Institute for Genomics and Proteomics
> Box 951570
> Los Angeles, CA 90095
>
> http://www.jamesstroud.com

Knowing this is actually extremely useful (looking at Tkinter.py i see
this is equivalent to the unbind method).  Unfortunately it's not
working to unbind B1-Motion from Listbox even though I get the same
output results as you've gotten above.  =(

Thanks a ton James. This is still very educational to me.

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


python3.0 MySQLdb

2009-01-12 Thread gert
I need something to connect to a database, preferably mysql, that
works in python3.0 please.
--
http://mail.python.org/mailman/listinfo/python-list


trying to modify locals() dictionary

2009-01-12 Thread TP
Hi everybody,

I try to modify locals() as an exercise.
According to the context (function or __main__), it works differently (see
below). Why? Thanks

Julien


def try_to_modify_locals( locals_ ):

locals_[ "a" ] = 2
print "locals_[ 'a' ]=", locals_[ "a" ]
return locals_

def test_modify_locals():

a = 3
l = try_to_modify_locals( locals() )
print l is locals()
print a

# Below, a remains equal to 3
print "test from test_modify_locals()"
test_modify_locals()

# But here, it works: a is modified
print "test from __main__"
a = 3
l = try_to_modify_locals( locals() )
print l is locals()
print a

-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
--
http://mail.python.org/mailman/listinfo/python-list


Re: trying to modify locals() dictionary

2009-01-12 Thread Christian Heimes
TP schrieb:
> Hi everybody,
> 
> I try to modify locals() as an exercise.
> According to the context (function or __main__), it works differently (see
> below). Why? Thanks

Because http://docs.python.org/library/functions.html#locals

Warning

The contents of this dictionary should not be modified; changes may not
affect the values of local variables used by the interpreter.

Christian

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


Slow network?

2009-01-12 Thread Laszlo Nagy


 Hi All,

To make the long story short, I have a toy version of an ORB being 
developed, and the biggest problem is slow network speed over TCP/IP.


There is an object called 'endpoint' on both sides, with incoming and 
outgoing message queues. This endpoint object has a socket assigned, 
with nodelay:


conn.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)

The endpoint is running two separate threads - those are dedicated for 
reading/writing messages from/into the socket object, as shown below:



   def _process_incoming(self):
   try:
   while not self.stop_requested.isSet():
   data = self.read_str()
   while not self.stop_requested.isSet():
   try:
   self.incoming.put(data,1)
   break
   except orb.util.smartqueue.Full:
   pass
   if not self.stop_requested.isSet():
   if self.router:
   self.router.on_message_arrived(self)
   except Exception, e:
   if self.router:
   if not isinstance(e,TransportClosedError):
   self.router.logger.error(dumpexc(e))
   self.router.unregister_endpoint(self)
   self.shutdown()
   raise SystemExit(0)

   def _process_outgoing(self):
   try:
   while not self.stop_requested.isSet():
   data_ok = False
   while not self.stop_requested.isSet():
   try:
   data = self.outgoing.get(1)
   data_ok = True
   break
   except orb.util.smartqueue.Empty:
   pass
   if data_ok:
   self.write_str(data)
   except Exception, e:
   if self.router:
   if not isinstance(e,TransportClosedError):
   self.router.logger.error(dumpexc(e))
   self.router.unregister_endpoint(self)
   self.shutdown()
   raise SystemExit(0)


The main point is that the sender does not need to wait for the message 
to be actually written into the socket (unless the outgoing queue 
becomes full).


When I try to send a message and receive an answer for it, I can only 
get as much as 130 request+response message pairs per second. 
Apparently, it is the same from messages size =77bytes to message 
size=16 Kbytes.


However, if I send 100 outgoing messages first, then read back all 
answers then the speed goes up to 1300 message pairs/sec. I suspect that 
this has something to do with TCP/IP. Since this will be used for 
RPC/RMI, it would be very important to lower the time needed to exchange 
messages. Is there any way I can speed this up?


Or do you think that this speed is the best I can get? My friend tried 
to do the same thing in Java, and he said that he could reach 1000 
messages/sec. (Is there a special "socket.flush()" method in Java that 
we do not have in Python?)


Thanks,

  Laszlo





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


Re: Problem with -3 switch

2009-01-12 Thread Christian Heimes
> I say again, show me a case of working 2.5 code where prepending u to
> an ASCII string constant that is intended to be used in a text context
> is actually worth the keystrokes.

Eventually you'll learn it the hard way. *sigh*

Christian

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


Re: Creating new instances of subclasses.

2009-01-12 Thread J. Clifford Dyer
On Fri, 2009-01-09 at 10:46 -0800, Dennis Lee Bieber wrote:
> On Wed, 07 Jan 2009 11:38:29 -0500, "J. Cliff Dyer" 
> declaimed the following in comp.lang.python:
> 
> > I want to be able to create an object of a certain subclass, depending
> > on the argument given to the class constructor.
> > 
> > I have three fields, and one might need to be a StringField, one an
> > IntegerField, and the last a ListField.  But I'd like my class to
> > delegate to the proper subclass automatically, so I can just do:
> > 
> > >>> f1 = Field('abc')
> > >>> f2 = Field('123')
> > >>> f3 = Field('D,E,F')
> 
>   And how do you differentiate a string that contains a comma from a
> purported list?
> 
>   What is expected for:
> 
>   ambiguous = Field('He said "Blast, it won''t work"')

My strings don't look like that.  Nor is that relevant to my question.
I created a simple, uncluttered example to illustrate my issue with
instantiating the proper subclass for a given argument.  I did not try
to make sure my example handled all edge cases properly.

Cheers,
Cliff


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


Re: trying to modify locals() dictionary

2009-01-12 Thread Albert Hopkins
On Mon, 2009-01-12 at 19:51 +0100, TP wrote:
> Hi everybody,
> 
> I try to modify locals() as an exercise.
> According to the context (function or __main__), it works differently (see
> below). Why? Thanks
> 
> Julien

Per the locals() documentation @
http://docs.python.org/library/functions.html

Warning

The contents of this dictionary should not be modified; changes
may not affect the values of local variables used by the
interpreter.




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


Re: python3.0 MySQLdb

2009-01-12 Thread Daniel Fetchinson
> I need something to connect to a database, preferably mysql, that
> works in python3.0 please.

And your question is?


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: Slow network?

2009-01-12 Thread Chris Mellon
On Mon, Jan 12, 2009 at 1:13 PM, Laszlo Nagy  wrote:
>
>  Hi All,
>
> To make the long story short, I have a toy version of an ORB being
> developed, and the biggest problem is slow network speed over TCP/IP.
>
> There is an object called 'endpoint' on both sides, with incoming and
> outgoing message queues. This endpoint object has a socket assigned, with
> nodelay:
>
> conn.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)
>
> The endpoint is running two separate threads - those are dedicated for
> reading/writing messages from/into the socket object, as shown below:
>
>
>   def _process_incoming(self):
>   try:
>   while not self.stop_requested.isSet():
>   data = self.read_str()
>   while not self.stop_requested.isSet():
>   try:
>   self.incoming.put(data,1)
>   break
>   except orb.util.smartqueue.Full:
>   pass
>   if not self.stop_requested.isSet():
>   if self.router:
>   self.router.on_message_arrived(self)
>   except Exception, e:
>   if self.router:
>   if not isinstance(e,TransportClosedError):
>   self.router.logger.error(dumpexc(e))
>   self.router.unregister_endpoint(self)
>   self.shutdown()
>   raise SystemExit(0)
>
>   def _process_outgoing(self):
>   try:
>   while not self.stop_requested.isSet():
>   data_ok = False
>   while not self.stop_requested.isSet():
>   try:
>   data = self.outgoing.get(1)
>   data_ok = True
>   break
>   except orb.util.smartqueue.Empty:
>   pass
>   if data_ok:
>   self.write_str(data)
>   except Exception, e:
>   if self.router:
>   if not isinstance(e,TransportClosedError):
>   self.router.logger.error(dumpexc(e))
>   self.router.unregister_endpoint(self)
>   self.shutdown()
>   raise SystemExit(0)
>
>
> The main point is that the sender does not need to wait for the message to
> be actually written into the socket (unless the outgoing queue becomes
> full).
>
> When I try to send a message and receive an answer for it, I can only get as
> much as 130 request+response message pairs per second. Apparently, it is the
> same from messages size =77bytes to message size=16 Kbytes.
>
> However, if I send 100 outgoing messages first, then read back all answers
> then the speed goes up to 1300 message pairs/sec. I suspect that this has
> something to do with TCP/IP. Since this will be used for RPC/RMI, it would
> be very important to lower the time needed to exchange messages. Is there
> any way I can speed this up?
>

It is very likely that nodelay is actually hurting you here.

Using the select module and doing non-blocking IO will be faster than
using threads for this as well.
--
http://mail.python.org/mailman/listinfo/python-list


efficient interval containment lookup

2009-01-12 Thread Per Freem
hello,

suppose I have two lists of intervals, one significantly larger than
the other.
For example listA = [(10, 30), (5, 25), (100, 200), ...] might contain
thousands
of elements while listB (of the same form) might contain hundreds of
thousands
or millions of elements.
I want to count how many intervals in listB are contained within every
listA. For example, if listA = [(10, 30), (600, 800)] and listB =
[(20, 25), (12, 18)] is the input, then the output should be that (10,
30) has 2 intervals from listB contained within it, while (600, 800)
has 0. (Elements of listB can be contained within many intervals in
listA, not just one.)

What is an efficient way to this?  One simple way is:

for a_range in listA:
  for b_range in listB:
is_within(b_range, a_range):
  # accumulate a counter here

where is_within simply checks if the first argument is within the
second.

I'm not sure if it's more efficient to have the iteration over listA
be on the outside or listB.  But perhaps there's a way to index this
that makes things more efficient?  I.e. a smart way of indexing listA
such that I can instantly get all of its elements that are within some
element
of listB, maybe?  Something like a hash, where this look up can be
close to constant time rather than an iteration over all lists... if
there's any built-in library functions that can help in this it would
be great.

any suggestions on this would be awesome. thank you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Slow network?

2009-01-12 Thread MRAB

Chris Mellon wrote:

On Mon, Jan 12, 2009 at 1:13 PM, Laszlo Nagy  wrote:

 Hi All,

To make the long story short, I have a toy version of an ORB being
developed, and the biggest problem is slow network speed over TCP/IP.

There is an object called 'endpoint' on both sides, with incoming and
outgoing message queues. This endpoint object has a socket assigned, with
nodelay:

conn.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)

The endpoint is running two separate threads - those are dedicated for
reading/writing messages from/into the socket object, as shown below:


  def _process_incoming(self):
  try:
  while not self.stop_requested.isSet():
  data = self.read_str()
  while not self.stop_requested.isSet():
  try:
  self.incoming.put(data,1)
  break
  except orb.util.smartqueue.Full:
  pass
  if not self.stop_requested.isSet():
  if self.router:
  self.router.on_message_arrived(self)
  except Exception, e:
  if self.router:
  if not isinstance(e,TransportClosedError):
  self.router.logger.error(dumpexc(e))
  self.router.unregister_endpoint(self)
  self.shutdown()
  raise SystemExit(0)

  def _process_outgoing(self):
  try:
  while not self.stop_requested.isSet():
  data_ok = False
  while not self.stop_requested.isSet():
  try:
  data = self.outgoing.get(1)
  data_ok = True
  break
  except orb.util.smartqueue.Empty:
  pass
  if data_ok:
  self.write_str(data)
  except Exception, e:
  if self.router:
  if not isinstance(e,TransportClosedError):
  self.router.logger.error(dumpexc(e))
  self.router.unregister_endpoint(self)
  self.shutdown()
  raise SystemExit(0)


The main point is that the sender does not need to wait for the message to
be actually written into the socket (unless the outgoing queue becomes
full).

When I try to send a message and receive an answer for it, I can only get as
much as 130 request+response message pairs per second. Apparently, it is the
same from messages size =77bytes to message size=16 Kbytes.

However, if I send 100 outgoing messages first, then read back all answers
then the speed goes up to 1300 message pairs/sec. I suspect that this has
something to do with TCP/IP. Since this will be used for RPC/RMI, it would
be very important to lower the time needed to exchange messages. Is there
any way I can speed this up?



It is very likely that nodelay is actually hurting you here.

Using the select module and doing non-blocking IO will be faster than
using threads for this as well.

You might also want to replace those 'pass' statements when smartqueue 
is empty or full with time.sleep() to avoid busy waiting.

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


Re: Slow network?

2009-01-12 Thread Laszlo Nagy



It is very likely that nodelay is actually hurting you here.

Using the select module and doing non-blocking IO will be faster than
using threads for this as well.
  
These sockets are non blocking and I'm using select.select indeed. Here 
is how it is implemented:


def read_data(self,size):
res = ""
fd = self.socket.fileno()
while not self.stop_requested.isSet():
remaining = size - len(res)
if remaining<=0:
break
# Give one second for an incoming connection so we can stop the
# server in seconds when needed
ready = select.select([fd], [], [], 0.2)
if fd in ready[0]:
data = self.socket.recv(min(remaining,8192)) # 8192 is recommended by 
socket.socket manual.

if not data:
# select returns the fd but there is no data to read -> connection closed!
raise TransportClosedError("Connection closed.")
else:
res += data
else:
pass
if self.stop_requested.isSet():
raise SystemExit(0)
return res

def write_data(self,data):
fd = self.socket.fileno()
while not self.stop_requested.isSet():
size = len(data)
if size==0:
break
# Give one second for an incoming connection so we can stop the server 
in seconds when needed

ready = select.select([], [fd], [], 0.2)
if fd in ready[1]:
sent = self.socket.send( # 8192 is recommended by socket.socket manual.
data[:max(size,8192)]
)
if not sent:
# select returns the fd but there is no data written -> connection closed!
raise TransportClosedError("Connection closed.")
else:
data = data[sent:] # Not too efficient...
else:
pass
if self.stop_requested.isSet():
raise SystemExit(0)

All other I/O methods are calling write_data() and read_data().

One thing I cannot do is to remove threads. This endpoint class is 
specifically designed so that other threads can put messages into the 
outgoing queue, and they do not need to wait for the message to be 
written out into the socket:


endpoint.send_message(msg) # won't block unless the outgoing queue is full
endpoint.recv_message(msg) # won't block unless the incoming queue is empty

Given this situation, I see no alternative to using threads. Do you?

Here is how the socket is set up on the client side:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)
s.connect((params['address'],params['port']))
return orb.endpoint.SocketEndpoint(s)


And on the server side:

def handle_request(self):
conn,client_address = self.serversocket.accept()
self.logger.info( str(client_address) + ' connecting')
conn.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)
t = orb.endpoint.SocketEndpoint(conn,self.router)

The socket based endpoint starts with setting non-blocking mode:

class SocketEndpoint(Endpoint):
"""Specialized Transport endpoint that communicates over a socket."""
def __init__(self,socket,router=None):
self.socket = socket
self.socket.setblocking(0)
Endpoint.__init__(self,router)

Here are some more test results:

- original example with setblocking(0) and TCP_NODELAY: 130 messages / sec
- same example without TCP_NODELAY: 130 messages/sec (I don't understand 
why?)
- same example without setblocking(0): 130 messages/sec (I guess because 
I'm using select.select already?)
- same example without setblocking(0) and without TCP_NODELAY: 130 
messages / sec


Now I really didn't understand where is the problem? I suspected that 
maybe the overhead for select.select? But nope, tried the same example 
without select.select and without setblocking(0) and it was also 130 
messages/sec.


What is hurting me then? Is it not TCP? :-(

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


Re: Slow network?

2009-01-12 Thread Laszlo Nagy


You might also want to replace those 'pass' statements when smartqueue 
is empty or full with time.sleep() to avoid busy waiting.
It won't do busy waiting, because read_str and write_str are using 
select.select and they will block without using CPU time, until data 
becomes available to read/write.

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


Re: Slow network?

2009-01-12 Thread Laszlo Nagy


You might also want to replace those 'pass' statements when smartqueue 
is empty or full with time.sleep() to avoid busy waiting.
I misunderstood your post, sorry. My smartqueue class has a timeout 
parameter, and it can block for an item, or raise the Full/Empty 
exception after timeout exceeded. That is 1 second in my example program:


self.incoming.put(data,1)
data = self.outgoing.get(1)





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


Re: Slow network?

2009-01-12 Thread MRAB

Laszlo Nagy wrote:



[snip]

Here are some more test results:

- original example with setblocking(0) and TCP_NODELAY: 130 messages / sec
- same example without TCP_NODELAY: 130 messages/sec (I don't understand 
why?)
- same example without setblocking(0): 130 messages/sec (I guess because 
I'm using select.select already?)
- same example without setblocking(0) and without TCP_NODELAY: 130 
messages / sec


Now I really didn't understand where is the problem? I suspected that 
maybe the overhead for select.select? But nope, tried the same example 
without select.select and without setblocking(0) and it was also 130 
messages/sec.


What is hurting me then? Is it not TCP? :-(

Waiting for a response after each send will take longer than doing the 
sends and then the responses. Have you tried pinging the destination to 
see how long the round trip takes? Has your friend?

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


Re: python3.0 MySQLdb

2009-01-12 Thread gert
On Jan 12, 8:25 pm, "Daniel Fetchinson" 
wrote:
> > I need something to connect to a database, preferably mysql, that
> > works in python3.0 please.
>
> And your question is?

MySQLdb or something else for python3.0 ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: python3.0 MySQLdb

2009-01-12 Thread skip

>> > I need something to connect to a database, preferably mysql, that
>> > works in python3.0 please.
>> 
>> And your question is?

gert> MySQLdb or something else for python3.0 ?

Given that Python 3.0 is so new and so few packages have been ported to it
yet, it might be helpful if you explained why Python 2.6 + MySQLdb isn't
sufficient for your needs.

-- 
Skip Montanaro - s...@pobox.com - http://smontanaro.dyndns.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: BadZipfile "file is not a zip file"

2009-01-12 Thread webcomm
On Jan 12, 11:53 am, "Chris Mellon"  wrote:
> On Sat, Jan 10, 2009 at 1:32 PM,webcomm wrote:
> > On Jan 9, 7:33 pm, John Machin  wrote:
> >> It is not impossible for a file with dummy data to have been
> >> handcrafted or otherwise produced by a process different to that used
> >> for a real-data file.
>
> > I knew it was produced by the same process, or I wouldn't have shared
> > it. : )
> > But you couldn't have known that.
>
> >> > Not sure if you've seen this 
> >> > thread...http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> >> Yeah, I've seen it ... (sigh) ... pax Steve Holden, but *please* stick
> >> with one thread ...
>
> > Thanks... I thought I was posting about separate issues and would
> > annoy people who were only interested in one of the issues if I put
> > them both in the same thread.  I guess all posts re: the same script
> > should go in one thread, even if the questions posed may be unrelated
> > and may be separate issues.  There are grey areas.
>
> > Problem solved in John Machin's post at
> >http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> It's worth pointing out (although the provider probably doesn't care)
> that this isn't really an XML document and this was a bad way of them
> to distribute the data. If they'd used a correctly formatted XML
> document (with the prelude and everything) with the correct encoding
> information, existing XML parsers should have just Done The Right
> Thing with the data, instead of you needing to know the encoding a
> priori to extract an XML fragment.

Agreed. I can't say I understand their rationale for doing it this way.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Slow network?

2009-01-12 Thread Laszlo Nagy




Waiting for a response after each send will take longer than doing the 
sends and then the responses. Have you tried pinging the destination 
to see how long the round trip takes? Has your friend?


My test application listens on 127.0.0.1.

gand...@gandalf-desktop:~/Python/Lib/orb/examples/01_lowlevel$ ping 
127.0.0.1

PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.027 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.018 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.021 ms

If we only see the response time, the worst case would be 1000.0/ ( 
2*0.027 ) = 18518 message pairs / sec...


(My friend used 127.0.0.1 too)

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


Re: Problem with -3 switch

2009-01-12 Thread Carl Banks
On Jan 12, 5:26 am, John Machin  wrote:
> On Jan 12, 7:29 pm, Carl Banks  wrote:
>
>
>
> > On Jan 12, 12:32 am, John Machin  wrote:
>
> > > On Jan 12, 12:23 pm, Carl Banks  wrote:
>
> > > > On Jan 9, 6:11 pm, John Machin  wrote:
>
> > > > > On Jan 10, 6:58 am, Carl Banks  wrote:
> > > > > > I expect that it'd be a PITA in some cases to use the transitional
> > > > > > dialect (like getting all your Us in place), but that doesn't mean 
> > > > > > the
> > > > > > language is crippled.
>
> > > > > What is this "transitional dialect"? What does "getting all your Us in
> > > > > place" mean?
>
> > > > Transitional dialect is the subset of Python 2.6 that can be
> > > > translated to Python3 with 2to3 tool.
>
> > > I'd never seen it called "transitional dialect" before.
>
> > I had hoped the context would make it clear what I was talking about.
>
> In vain.

You were one who was mistaken about what Steve and Cliff were talking
about, chief.  Maybe if you'd have paid better attention you would
have gotten it?


> > > >  Getting all your Us in place
> > > > refers to prepending a u to strings to make them unicode objects,
> > > > which is something 2to3 users are highly advised to do to keep hassles
> > > > to a minimum.  (Getting Bs in place would be a good idea too.)
>
> > > Ummm ... I'm not understanding something. 2to3 changes u"foo" to
> > > "foo", doesn't it? What's the point of going through the code and
> > > changing all non-binary "foo" to u"foo" only so that 2to3 can rip the
> > > u off again?
>
> > It does a bit more than that.
>
> Like what?

Never mind; I was confusing it with a different tool.  (Someone had a
source code processing tool that replaced strings with their reprs a
while back.)  My bad.


> > > What hassles? Who's doing the highly-advising where and
> > > with what supporting argument?
>
> > You add the u so the the constant will be the same data type in 2.6 as
> > it becomes in 3.0 after applying 2to3.  str and unicode objects aren't
> > always with smooth with each other, and you have a much better chance
> > of getting the same behavior in 2.6 and 3.0 if you use an actual
> > unicode string in both.
>
> (1) Why specifically 2.6? Do you mean 2.X, or is this related to the
> "port to 2.6 first" theory?

It's not a theory.  2to3 was designed to translate a subset of 2.6
code to 3.0.  It's not designed to translate arbitrary 2.6 code, nor
any 2.5 or lower code.  It might work well enough from 2.5, but it
wasn't designed for it.

> (2) We do assume we are starting off with working 2.X code, don't we?
> If we change "foo" to u"foo" and get a different answer from the 2.X
> code, is that still "working"?

Of course it's not "working" in 2.6, and that's the point: you want it
to work in 2.6 with Unicode strings because it has to run in 3.0 with
Unicode strings.


> > A example of this, though not with string constants,
>
> And therefore irrelevant.

Well, it wasn't from my viewpoint, which was "make sure you are using
only unicode and bytes objects", never str objects.  But if you want
to talk about string constants specifically, ok.


> I would like to hear from someone who has actually started with
> working 2.x code and changed all their text-like "foo" to
> u"foo" [except maybe unlikely suspects like open()'s mode arg]:
> * how many places where the 2.x code broke and so did the 3.x code
> [i.e. the problem would have been detected without prepending u]

I think you're missing the point.  This isn't merely about detecting
errors; it's about making the code in 2.6 behave as similarly to 3.0
as possible, and that includes internal behavior.  When you have mixed
str and unicode objects, 2.6 has to do a lot of encoding and decoding
under the covers; in 3.0 that won't be happening.  That increases the
risk of divergent behavior, and is something you want to avoid.

If you think your test suite is invincible and can catch every
possible edge case where some encoding or decoding mishap occurs, be
my guest and don't do it.

Also, I'm not sure why you think it's preferrable to run tests on 3.0
and have to go back to the 2.6 codebase, run 2to3 again, apply the
patch again, and retest, to fix it.  I don't know, maybe it makes
sense for people with a timemachine.py module, but I don't think it'll
make sense for most people.


> * how many places where the 2.x code broke but the 3.x code didn't
> [i.e. prepending u did find the problem]

If you think this was the main benefit of doing that you are REALLY
missing the point.  The point isn't to find problems in 2.6, it's to
modify 2.6 to behave as similarly to 3.0 as possible.


> * whether they thought it was worth the effort
>
> In the meantime I would be interested to hear from anybody with a made-
> up example of code where the problem would be detected (sooner |
> better | only) by prepending u to text-like string constants.

Here's one for starters.  The mistake was using a multibyte character
in a str object in 2.6.  2to3 would have converted th

Re: efficient interval containment lookup

2009-01-12 Thread Tim Chase

suppose I have two lists of intervals, one significantly larger than
the other.
For example listA = [(10, 30), (5, 25), (100, 200), ...] might contain
thousands
of elements while listB (of the same form) might contain hundreds of
thousands
or millions of elements.
I want to count how many intervals in listB are contained within every
listA. For example, if listA = [(10, 30), (600, 800)] and listB =
[(20, 25), (12, 18)] is the input, then the output should be that (10,
30) has 2 intervals from listB contained within it, while (600, 800)
has 0. (Elements of listB can be contained within many intervals in
listA, not just one.)

What is an efficient way to this?  One simple way is:

for a_range in listA:
  for b_range in listB:
is_within(b_range, a_range):
  # accumulate a counter here


Could you detail the is_within() function?  most importantly, is 
it inclusive, such that "a1 <= b1 <= b2 <= a2", or is it 
overlapping such that "a1 <= b2 or a2 <= b1".  Additionally, do 
you want to count how many intervals of A overlap with intervals 
of B, or do you just want a count of how many intervals in B have 
*any* overlap within A?


My leaning would be to make a custom "is this in A" function, 
iterate over B and test against an "appropriate subset" of A:


  listA = sorted([...])
  min_a1 = min(a1 for (a1, a2) in listA)
  max_a2 = max(a2 for (a1, a2) in listA)
  def in_a(b1, b2):
for a1, a2 in smartly_chosen_subset(listA, b1, b2):
  if is_within((b1, b2), (a1, a2)): return True
return False
  i = 0
  for b1, b2 in sorted(listB):
if b2 < min_a1: continue
if b1 > max_a2: break
if in_a(b1, b2): i += 1

The in_a() function can be optimized to terminate early if a1>b2 
or a2 < b1  (perhaps even choosing an smart starting point for 
iterating).  Short-circuiting by returning True as soon as you 
know there's a match will trim some of the time.


How distributed are the endpoints?  If there's a lot of 
commonality in the data, you might be able to cache results to 
cut even further.


Just a few ideas, and questions that might help develop better 
solutions.


-tkc



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


Re: efficient interval containment lookup

2009-01-12 Thread Scott David Daniels

Are these ranges constrained in any way?
Does preprocessing count in the efficiency cost?
Is the long list or the short list fixed while the other varies?
With no constraints the problem is harder.

> But perhaps there's a way to index this that makes things more
> efficient?  I.e. a smart way of indexing listA such that I can
> instantly get all of its elements that are within some element
> of listB, maybe?  Something like a hash, where this look up can be
> close to constant time rather than an iteration over all lists...
> [what] built-in library functions ... can help?
> any suggestions on this would be awesome. thank you.

If the intervals have a rough size on either side (or both), that could
give you a useful grid for your hashing.

If the total range of the integers in listA is smallish, you could label 
every value in that range with which elements of listA contain that

point.

You could also look for interesting orders of the intervals in listA,
so that you can use the bisect module to get to one end of a range that
will hold all your answers and only check the applicable elements of
the range.

You might also consider how you could set up numpy arrays to test the
set of ranges in listA in a pair of operations.

I hope I haven't helped you with your homework.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: efficient interval containment lookup

2009-01-12 Thread Robert Kern
[Apologies for piggybacking, but I think GMane had a hiccup today and missed the 
original post]


[Somebody wrote]:

suppose I have two lists of intervals, one significantly larger than
the other.
For example listA = [(10, 30), (5, 25), (100, 200), ...] might contain
thousands
of elements while listB (of the same form) might contain hundreds of
thousands
or millions of elements.
I want to count how many intervals in listB are contained within every
listA. For example, if listA = [(10, 30), (600, 800)] and listB =
[(20, 25), (12, 18)] is the input, then the output should be that (10,
30) has 2 intervals from listB contained within it, while (600, 800)
has 0. (Elements of listB can be contained within many intervals in
listA, not just one.)


Interval trees.

http://en.wikipedia.org/wiki/Interval_tree

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-12 Thread Bruno Desthuilliers

Paul Rubin a écrit :

Carl Banks  writes:

The criticism is very valid.  Some languages do support immutable
variables (e.g. "final" declarations in Java, "const" in C++, or
universal immutability in pure functional languages) and they do so
precisely for the purpose of taming the chaos of uncontrolled
mutation.  It would be great if Python also supported immutability.

I don't think what you said (which is fine) makes his criticism valid,
unless you also suggest that all objects should be immutable.


It would be enough to have a way to make specific objects and instance
attributes immutable.


If any objects are mutable, you have to be prepared for objects to
mutated outside the initializer.


Sure, but why have mutable objects all over the place?  And, why
always have attributes visible at all, outside the class definition?
The approach in C++ and Java is to have public and private instance
variables, where the private ones are visible only in the class methods.


Why on earth are you using Python if you don't like the way it work ???
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unbinding Tkinter default bindings for Listbox

2009-01-12 Thread Roger
I'm sorry for harassing the list but any suggestions would be greatly
appreciated. =)
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >