Approximate comparison of two lists of floats

2011-07-28 Thread Christian Doll
Hello,

i have e little performance problem with my code...

i have to compare many lists of very much floats. at moment i have
nested for-loops

for a in range( len(lists) ):
for b in range( a+1 , len(lists) ):
for valuea in lists[a]:
equal=False
for valueb in lists[b]:
if inTolerance( valuea , valueb , 1.0): # inTolerance
is an own function, which checks if the difference of valuea and
valueb is not more then 1.0%
equal=True
break
if equal:
print a , "and" , b , "are equal"

i found a version with set which is faster, but i cannot assign an
tolerance (%)
for a in range( len(lists) ):
for b in range( a+1 , len(lists) ):
if len( lists[a] ) ==
len( set( lists[a] ).intersection( set( lists[b] ) ) ):
print a , "and" , b , "are equal"

have you an idea how i can change my code, that i can compare many
lists of floats with a tolerance in percentage very fast?

(sorry for my bad englisch ;-) )

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


Approximate comparison of two lists of floats

2011-07-28 Thread Christian
Hello,

i have e little performance problem with my code...

i have to compare many lists of very much floats. at moment i have
nested for-loops

for a in range( len(lists) ):
   for b in range( a+1 , len(lists) ):
   for valuea in lists[a]:
   equal=False
   for valueb in lists[b]:
   if inTolerance( valuea , valueb , 1.0): # inTolerance
is an own function, which checks if the difference of valuea and
valueb is not more then 1.0%
   equal=True
   break
   if equal:
   print a , "and" , b , "are equal"

i found a version with set which is faster, but i cannot assign an
tolerance (%)
for a in range( len(lists) ):
   for b in range( a+1 , len(lists) ):
   if len( lists[a] ) ==
len( set( lists[a] ).intersection( set( lists[b] ) ) ):
   print a , "and" , b , "are equal"

have you an idea how i can change my code, that i can compare many
lists of floats with a tolerance in percentage very fast?

(sorry for my bad englisch ;-) )

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


Re: Approximate comparison of two lists of floats

2011-07-28 Thread Peter Otten
Christian Doll wrote:

> i have e little performance problem with my code...
> 
> i have to compare many lists of very much floats. at moment i have
> nested for-loops
> 
> for a in range( len(lists) ):
> for b in range( a+1 , len(lists) ):
> for valuea in lists[a]:
> equal=False
> for valueb in lists[b]:
> if inTolerance( valuea , valueb , 1.0): # inTolerance
> is an own function, which checks if the difference of valuea and
> valueb is not more then 1.0%
> equal=True
> break
> if equal:
> print a , "and" , b , "are equal"

My crystal ball says that if you profile the above you'll find that the 
above spends most of the time in the inTolerance() function that you don't 
provide.
 
> i found a version with set which is faster, but i cannot assign an
> tolerance (%)
> for a in range( len(lists) ):
> for b in range( a+1 , len(lists) ):
> if len( lists[a] ) ==
> len( set( lists[a] ).intersection( set( lists[b] ) ) ):
> print a , "and" , b , "are equal"

I can't think of a problem that can be solved with that ;)
 
> have you an idea how i can change my code, that i can compare many
> lists of floats with a tolerance in percentage very fast?

You can usually speed up number-crunching tasks with numpy, but it looks 
like you don't have a clear notion what vectors should be regarded as equal.

Perhaps you can provide a bit of background information about the problem 
you are trying to solve with the code, preferably in plain (if bad) english.
-- 
http://mail.python.org/mailman/listinfo/python-list


multilanguage application - step by step

2011-07-28 Thread miamia
Hello guys,

I would like to translate all strings in my application for several
languages (eng, es, de, etc) and user should be able to switch app
from one language to another. I am still newbie with python so is
there any "step-by-step" tutorial how to to this? thanks for help
-- 
http://mail.python.org/mailman/listinfo/python-list


multilanguage application - step by step

2011-07-28 Thread Peter Irbizon
Hello guys,

I would like to translate all strings in my application for several
languages (eng, es, de, etc) and user should be able to switch app
from one language to another. I am still newbie with python so is
there any "step-by-step" tutorial how to to this? thanks for help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multilanguage application - step by step

2011-07-28 Thread Chris Rebert
On Thu, Jul 28, 2011 at 2:11 AM, Peter Irbizon  wrote:
> Hello guys,
>
> I would like to translate all strings in my application for several
> languages (eng, es, de, etc) and user should be able to switch app
> from one language to another. I am still newbie with python so is
> there any "step-by-step" tutorial how to to this? thanks for help

Please refrain from double-posting in the future.

The `gettext` module's docs look fairly straightforward:
http://docs.python.org/library/gettext.html#internationalizing-your-programs-and-modules
See also the "Here’s an example of typical usage for this API:" code snippet.

For the translation file workflow, the Wikipedia article seems enlightening:
http://en.wikipedia.org/wiki/GNU_gettext

Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyWart: PEP8: A cauldron of inconsistencies.

2011-07-28 Thread Chris Angelico
On Thu, Jul 28, 2011 at 8:34 AM, rantingrick  wrote:
>> --
>> Encodings (PEP 263)
>>
>> Code in the core Python distribution should always use the
>> ASCII or Latin-1 encoding (a.k.a. ISO-8859-1).  For Python
>> 3.0 and beyond, UTF-8 is preferred over Latin-1, see PEP
>> 3120.
>> --
>
> no, NO, NO!. We should never be writing source in ANYTHING besides
> ASCII. Can you offer any "good reason" why we should? No, because
> there are no good reasons. This is just more fluffy feel good crap and
> it makes me sick! Are we trying to keep xah happy? I know he likes
> Unicode.

There's nothing wrong with using UTF-8 for source files. It means that:
a) Unicode string literals can incorporate copied-and-pasted Unicode
characters (assuming the editor knows to save with the right
encoding), instead of forcing them to be translated into \u
notation; and
b) identifiers are not limited to the ASCII character set.

There's no reason to deny either of these. Rick, you're welcome to
stay in the US and never send any code to the rest of the world, but
some of us do.

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


monotonically increasing memory usage

2011-07-28 Thread Pedro Larroy
Hi

pickling

Just crossposting this from stackoverflow:

http://stackoverflow.com/questions/6857006/python-monotonically-increasing-memory-usage-leak

Any hints?


Pedro.

-- 
Pedro Larroy Tovar   |    http://pedro.larroy.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


unexpected regexp behaviour using 'A|B|C.....'

2011-07-28 Thread AlienBaby
When using re patterns of the form 'A|B|C|...'  the docs seem to
suggest that once any of A,B,C.. match, it is captured and no further
patterns are tried.  But I am seeing,

st='  Id NameProv Type  CopyOf  BsId
Rd -Detailed_State-Adm Snp  Usr VSize'

p='Type *'
re.search(p,st).group()
'Type  '

p='Type *|  *Type'
re.search(p,st).group()
' Type'


Shouldn’t the second search return the same as the first, if further
patterns are not tried?

The documentation appears to suggest the first match should be
returned, or am I misunderstanding?

'|'
A|B, where A and B can be arbitrary REs, creates a regular expression
that will match either A or B. An arbitrary number of REs can be
separated by the '|' in this way. This can be used inside groups (see
below) as well. As the target string is scanned, REs separated by '|'
are tried from left to right.

 When one pattern completely matches, that branch is accepted. This
means that once A matches, B will not be tested further, even if it
would produce a longer overall match.

In other words, the '|' operator is never greedy. To match a literal
'|', use \|, or enclose it inside a character class, as in [|].


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


Re: multilanguage application - step by step

2011-07-28 Thread Peter Irbizon
Hello,

thank you for reply.
I tried this:
# -*- coding: utf-8 -*-
import gettext
gettext.bindtextdomain('multilanguage',
'E:\folder')
gettext.textdomain('multilanguage')
_ = gettext.gettext
# ...
lang1 = gettext.translation('multilanguage', languages=['sk'])
lang1.install()
print _('This is a translatable string.')

but ErrNo 2 no translation file found for domain: 'multilanguage'
I am doing something wrong way. I would like to switch languages in my
program 'on the fly' without affecting windows.

P.S. sorry for double posting but when I post my message on googlegroups I
can't see it in googlegroups (don't know why)
thanks
2011/7/28 Chris Rebert 

>  On Thu, Jul 28, 2011 at 2:11 AM, Peter Irbizon 
> wrote:
> > Hello guys,
> >
> > I would like to translate all strings in my application for several
> > languages (eng, es, de, etc) and user should be able to switch app
> > from one language to another. I am still newbie with python so is
> > there any "step-by-step" tutorial how to to this? thanks for help
>
> Please refrain from double-posting in the future.
>
> The `gettext` module's docs look fairly straightforward:
>
> http://docs.python.org/library/gettext.html#internationalizing-your-programs-and-modules
> See also the "Here’s an example of typical usage for this API:" code
> snippet.
>
> For the translation file workflow, the Wikipedia article seems
> enlightening:
> http://en.wikipedia.org/wiki/GNU_gettext
>
> Cheers,
> Chris
> --
> http://rebertia.com
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multilanguage application - step by step

2011-07-28 Thread Andrew Berg
On 2011.07.28 05:12 AM, Peter Irbizon wrote:
> P.S. sorry for double posting but when I post my message on googlegroups I
> can't see it in googlegroups (don't know why)
Last time I looked at this newsgroup (which was not that long ago) on
Google Groups, it was 2 days behind.

-- 
CPython 3.2.1 | Windows NT 6.1.7601.17592 | Thunderbird 5.0
PGP/GPG Public Key ID: 0xF88E034060A78FCB
-- 
http://mail.python.org/mailman/listinfo/python-list


notify when a process exits

2011-07-28 Thread Ryan
Is there anyway in python to get a notification when a process exits?
To be completely clear, I am looking for a notification solution,
similar to pyinotify, not a polling one (I know I can poll a process
using os.kill(pid, 0)).

BTW, pyinotify will not work on /proc/pid as a solution. I have
already tried. /proc/pid is not a real directory (in the strictest
sense of the word). So, pyinotify can not watch it.

Ryan



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


Re: unexpected regexp behaviour using 'A|B|C.....'

2011-07-28 Thread Peter Otten
AlienBaby wrote:

> When using re patterns of the form 'A|B|C|...'  the docs seem to
> suggest that once any of A,B,C.. match, it is captured and no further
> patterns are tried.  But I am seeing,
> 
> st='  Id NameProv Type  CopyOf  BsId
> Rd -Detailed_State-Adm Snp  Usr VSize'
> 
> p='Type *'
> re.search(p,st).group()
> 'Type  '
> 
> p='Type *|  *Type'
> re.search(p,st).group()
> ' Type'
> 
> 
> Shouldn’t the second search return the same as the first, if further
> patterns are not tried?
> 
> The documentation appears to suggest the first match should be
> returned, or am I misunderstanding?

All alternatives are tried at a given starting position in the string before 
the algorithm advances to the next position. The second alternative 
"  *Type", at least one space followed by the character sequence "Type" 
matches right after "Prov" in  your example, therefore the first 
alternative, "Type" and any following spaces, which would match after 
"Prov " is never tried. 

Maybe you accidentally typed one extra " "? If you didn't " +Type" would be 
clearer.

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


Is it bad practise to write __all__ like that

2011-07-28 Thread Karim


Hello,

__all__ = 'api db input output tcl'.split()

or

__all__ = """
   api
   db
   input
   output
   tcl
   """.split()

for lazy boy ;o). It is readable as well.
What do you think?

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


Re: Is it bad practise to write __all__ like that

2011-07-28 Thread Ben Finney
Karim  writes:

> Hello,
>
> __all__ = 'api db input output tcl'.split()
>
> or
>
> __all__ = """
>api
>db
>input
>output
>tcl
>""".split()

Maybe this:

__all__ = [x.__name__ for x in [
api,
db,
input,
output,
tcl,
]]

presuming these are all objects with an accurate ‘__name__’ attribute.

I'm starting to yearn for the ability in Python to get a string
representation of a name itself, by using the name and not writing it as
a string literal. That would make the above more robust.

But really, this is all obfuscatory. Why not:

__all__ = [
'api',
'db',
'input',
'output',
'tcl',
]

It's clear and explicit and really not difficult to type or maintain.

-- 
 \   “I do not believe in forgiveness as it is preached by the |
  `\church. We do not need the forgiveness of God, but of each |
_o__)other and of ourselves.” —Robert G. Ingersoll |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I access IDLE in Win7

2011-07-28 Thread W. eWatson

On 7/27/2011 9:46 PM, Dennis Lee Bieber wrote:

On Wed, 27 Jul 2011 17:28:38 -0700, "W. eWatson"
  declaimed the following in
gmane.comp.python.general:



For junk.py, I tried Open With->Choose default program. I selected
idle.pyw. When I tried the new default for getting to IDLE, it
complained it was not a valid 32-bit app. That's very strange.


Quite expected... idle.pyw is a Python byte code file... IT needs to
be run using a Python interpreter (pythonw.exe to suppress the shell
window).

What happens IN the command/shell if you type (replace the
with the correct string on your installation)

python -vidle.pyw

This is my install:

E:\UserData\Wulfraed\My Documents>e:\Python25\python -v
e:\Python25\Lib\idlelib\idle.pyw

# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# e:\Python25\lib\site.pyc matches e:\Python25\lib\site.py
import site # precompiled from e:\Python25\lib\site.pyc
# e:\Python25\lib\os.pyc matches e:\Python25\lib\os.py
import os # precompiled from e:\Python25\lib\os.pyc
import errno # builtin
import nt # builtin
# e:\Python25\lib\ntpath.pyc matches e:\Python25\lib\ntpath.py
import ntpath # precompiled from e:\Python25\lib\ntpath.pyc
# e:\Python25\lib\stat.pyc matches e:\Python25\lib\stat.py
import stat # precompiled from e:\Python25\lib\stat.pyc
# e:\Python25\lib\UserDict.pyc matches e:\Python25\lib\UserDict.py
import UserDict # precompiled from e:\Python25\lib\UserDict.pyc
# e:\Python25\lib\copy_reg.pyc matches e:\Python25\lib\copy_reg.py
import copy_reg # precompiled from e:\Python25\lib\copy_reg.pyc
# e:\Python25\lib\types.pyc matches e:\Python25\lib\types.py
import types # precompiled from e:\Python25\lib\types.pyc
import _types # builtin
# zipimport: found 45 names in
e:\Python25\lib\site-packages\simplejson-2.0.3-py2.5.egg
# zipimport: found 33 names in
e:\Python25\lib\site-packages\ruledispatch-0.5a1.dev_r2506-py2.5-
win32.egg
# zipimport: found 15 names in
e:\Python25\lib\site-packages\decoratortools-1.7-py2.5.egg
# zipimport: found 9 names in
e:\Python25\lib\site-packages\configobj-4.5.3-py2.5.egg
# zipimport: found 15 names in
e:\Python25\lib\site-packages\extremes-1.1-py2.5.egg
# zipimport: found 43 names in
e:\Python25\lib\site-packages\pyprotocols-1.0a0-py2.5-win32.egg
# e:\Python25\lib\locale.pyc matches e:\Python25\lib\locale.py
import locale # precompiled from e:\Python25\lib\locale.pyc
import encodings # directory e:\Python25\lib\encodings
# e:\Python25\lib\encodings\__init__.pyc matches
e:\Python25\lib\encodings\__init__.py
import encodings # precompiled from
e:\Python25\lib\encodings\__init__.pyc
# e:\Python25\lib\codecs.pyc matches e:\Python25\lib\codecs.py
import codecs # precompiled from e:\Python25\lib\codecs.pyc
import _codecs # builtin
# e:\Python25\lib\encodings\aliases.pyc matches
e:\Python25\lib\encodings\aliases.py
import encodings.aliases # precompiled from
e:\Python25\lib\encodings\aliases.pyc
import _locale # builtin
# e:\Python25\lib\re.pyc matches e:\Python25\lib\re.py
import re # precompiled from e:\Python25\lib\re.pyc
# e:\Python25\lib\sre_compile.pyc matches e:\Python25\lib\sre_compile.py
import sre_compile # precompiled from e:\Python25\lib\sre_compile.pyc
import _sre # builtin
# e:\Python25\lib\sre_constants.pyc matches
e:\Python25\lib\sre_constants.py
import sre_constants # precompiled from
e:\Python25\lib\sre_constants.pyc
# e:\Python25\lib\sre_parse.pyc matches e:\Python25\lib\sre_parse.py
import sre_parse # precompiled from e:\Python25\lib\sre_parse.pyc
import operator # builtin
# e:\Python25\lib\encodings\cp1252.pyc matches
e:\Python25\lib\encodings\cp1252.py
import encodings.cp1252 # precompiled from
e:\Python25\lib\encodings\cp1252.pyc
# e:\Python25\lib\warnings.pyc matches e:\Python25\lib\warnings.py
import warnings # precompiled from e:\Python25\lib\warnings.pyc
# e:\Python25\lib\linecache.pyc matches e:\Python25\lib\linecache.py
import linecache # precompiled from e:\Python25\lib\linecache.pyc
ActivePython 2.5.2.2 (ActiveState Software Inc.) based on
Python 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import idlelib # directory e:\Python25\lib\idlelib
# e:\Python25\lib\idlelib\__init__.pyc matches
e:\Python25\lib\idlelib\__init__.py
import idlelib # precompiled from e:\Python25\lib\idlelib\__init__.pyc
# e:\Python25\lib\idlelib\PyShell.pyc matches
e:\Python25\lib\idlelib\PyShell.py
import idlelib.PyShell # precompiled from
e:\Python25\lib\idlelib\PyShell.pyc
# e:\Python25\lib\string.pyc matches e:\Python25\lib\string.py
import string # precompiled from e:\Python25\lib\string.pyc
import strop # builtin
# e:\Python25\lib\getopt.pyc matches e:\Python25\lib\getopt.py
import getopt # precompiled from e:\Python25\lib\getopt.pyc
# e:\Python25\lib\socket.pyc matches e:\Python25\lib\socket.py
import socket # precompiled from e:\Python25\lib\socket.pyc
import _socket # dynamically loaded

Re: How do I access IDLE in Win7

2011-07-28 Thread Thomas Jollans

On 2011-07-28 13:56, W. eWatson wrote:

On 7/27/2011 9:46 PM, Dennis Lee Bieber wrote:


{and that was captured by a  in the command window, "select
all", another  to capture, then move to the newreader and
  to paste}
I'm quite willing to do this in the command window, but I know of no 
way to copy it.


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


Re: Is it bad practise to write __all__ like that

2011-07-28 Thread Thomas Rachel

Am 28.07.2011 13:32 schrieb Karim:


Hello,

__all__ = 'api db input output tcl'.split()

or

__all__ = """
api
db
input
output
tcl
""".split()

for lazy boy ;o). It is readable as well.
What do you think?


Why not? But you could even do

class AllList(list):
"""list which can be called in order to be used as a __all__-adding 
decorator"""

def __call__(self, obj):
"""for decorators"""
self.append(obj.__name__)
return obj

__all__ = AllList()

@__all__
def api(): pass

@__all__
def db(): pass

@__all__
def input(): pass

@__all__
def output(): pass

@__all__
def tcl(): pass

HTH,

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


Re: multilanguage application - step by step

2011-07-28 Thread Thomas Jollans

I used gettext in xmm2tray. You can have a look at the code as an example:
http://code.jollybox.de/hg/xmms2tray/file/04443c59a7a1/src/xmms2tray/__init__.py

On 2011-07-28 12:12, Peter Irbizon wrote:

I tried this:
# -*- coding: utf-8 -*-
import gettext
gettext.bindtextdomain('multilanguage', 'E:\folder')
'\f' is, IIRC, the form feed character. Use forward slashes or escape 
your backslashes properly.

gettext.textdomain('multilanguage')
_ = gettext.gettext
I specified the translation file location in the gettext.translation 
call, but I'm no expert; I assume this'll work too.

# ...
lang1 = gettext.translation('multilanguage', languages=['sk'])
use fallback=True here if you want it to work without translation files 
(simply using the string passed to _())

lang1.install()
print _('This is a translatable string.')
but ErrNo 2 no translation file found for domain: 'multilanguage'
I am doing something wrong way. I would like to switch languages in my 
program 'on the fly' without affecting windows.
The usual way of using gettext is to use the system locale to determine 
the right language. Of course, you can have different translations and 
install() them (I expect), but you'll have to re-load all the strings 
displayed in your application when you switch language, which might be 
tricky.


P.S. sorry for double posting but when I post my message on 
googlegroups I can't see it in googlegroups (don't know why)

thanks
2011/7/28 Chris Rebert mailto:c...@rebertia.com>>

On Thu, Jul 28, 2011 at 2:11 AM, Peter Irbizon
mailto:peterirbi...@gmail.com>> wrote:
> Hello guys,
>
> I would like to translate all strings in my application for several
> languages (eng, es, de, etc) and user should be able to switch app
> from one language to another. I am still newbie with python so is
> there any "step-by-step" tutorial how to to this? thanks for help

Please refrain from double-posting in the future.

The `gettext` module's docs look fairly straightforward:

http://docs.python.org/library/gettext.html#internationalizing-your-programs-and-modules
See also the "Here’s an example of typical usage for this API:"
code snippet.

For the translation file workflow, the Wikipedia article seems
enlightening:
http://en.wikipedia.org/wiki/GNU_gettext

Cheers,
Chris
--
http://rebertia.com 






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


Re: Is it bad practise to write __all__ like that

2011-07-28 Thread Karim

On 07/28/2011 02:29 PM, Thomas Rachel wrote:

__all__ = AllList()


Hello Thomas,

Very beautiful and elegant code. Having both at the same time an 
instance and a method...
With this 'small' topic, you taught me something today on property 
application!


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


Re: How do I access IDLE in Win7

2011-07-28 Thread Jerry Hill
On Wed, Jul 27, 2011 at 5:26 PM, W. eWatson  wrote:
> .py=Python.File
> .pyw=Python.NoConFile
> Python.File="C:\Python25\python.exe" "%1" %*
> Python.File="C:\Python25\python.exe" "%1" %*
> Python.NoConFile="C:\Python25\pythonw.exe" "%1" %*

That all looks good.

> I cannot copy from the cmd window. It ends with [errorno 13] Permission
> denied to c:||Users\\Wayne\\idlerc\\recent-files.lst'

That sounds like the root of the problem, then.  I'm assuming Wayne is
your username, but I don't know why you wouldn't have permission to
access something in your own user directory.  Can you try deleting
that file in the windows explorer?  You could try messing with the
permissions, but I doubt you care about a recent file list that sounds
several months old.  You might even try removing (or renaming) the
whole C:\Users\Wayne\idlerc folder.  Idle should re-build anything it
needs if it's not there when you start up.

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


Re: Is it bad practise to write __all__ like that

2011-07-28 Thread mark ferguson
HI Thomas,

I've not really got the hang of decorators yet, so I was wondering why one
might use your approach rather than just using Karim's original method?

I only really use python for smallish, utility programs, so I suppose I
haven't come across an issue complex enough to see a clear advantage in
using decorators. I'm still looking for that 'a ha!' moment when it all
becomes clear to me. Could it be that simple examples of decorators end up
being overkill for the problem they are solving, elegant though the code may
be?

For anyone else, what was itch that decorators solved, such that they became
an additional tool in the their kit bag?

On 28 July 2011 14:00, Karim  wrote:

> On 07/28/2011 02:29 PM, Thomas Rachel wrote:
>
>> __all__ = AllList()
>>
>
> Hello Thomas,
>
> Very beautiful and elegant code. Having both at the same time an instance
> and a method...
> With this 'small' topic, you taught me something today on property
> application!
>
> Cheers
> Karim
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


gettext localize strings with variables

2011-07-28 Thread miamia
Hello,
I have
variable OUHH and
print _('This is %(OUHH)s a translatable string.' % locals())

how can I translate this ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: gettext localize strings with variables

2011-07-28 Thread Thomas Jollans
On 28/07/11 15:33, miamia wrote:
> Hello,
> I have
> variable OUHH and
> print _('This is %(OUHH)s a translatable string.' % locals())
> 
> how can I translate this ?

Get the translation first, insert values second.

_('This string contains a variable: {0}. Amazing').format(OUHH)

Depending on what the actual string is, you may need to consider
plurals. See gettext.ngettext()
-- 
http://mail.python.org/mailman/listinfo/python-list


Can Epydoc be used to document decorated function in Python?

2011-07-28 Thread Victor Khangulov

Hello,

is there a way to use epydoc to document a Python function that has been 
decorated?


@decorator
def func1():
""" My function func1 """
print "In func1"

The resulting output of epydoc is that func1 gets listed as a variable 
with no description.

I am using Epydoc v3.0.1.

Thanks

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


Re: How do I access IDLE in Win7

2011-07-28 Thread W. eWatson

On 7/28/2011 6:19 AM, Jerry Hill wrote:

On Wed, Jul 27, 2011 at 5:26 PM, W. eWatson  wrote:

.py=Python.File
.pyw=Python.NoConFile
Python.File="C:\Python25\python.exe" "%1" %*
Python.File="C:\Python25\python.exe" "%1" %*
Python.NoConFile="C:\Python25\pythonw.exe" "%1" %*


That all looks good.


I cannot copy from the cmd window. It ends with [errorno 13] Permission
denied to c:||Users\\Wayne\\idlerc\\recent-files.lst'


That sounds like the root of the problem, then.  I'm assuming Wayne is
your username, but I don't know why you wouldn't have permission to
access something in your own user directory.  Can you try deleting
that file in the windows explorer?  You could try messing with the
permissions, but I doubt you care about a recent file list that sounds
several months old.  You might even try removing (or renaming) the
whole C:\Users\Wayne\idlerc folder.  Idle should re-build anything it
needs if it's not there when you start up.

It's a hidden file. I tried deleting w/o success so changed the name. 
The file was dated 5/??/2010.


I tried the usual method of right-click on junk.py to see if Edit with 
IDLE would appear in the menu. It didn't, so I tried a right-click to 
Choose default program to idle.pyw. Using it again gave me an invalid 
32-bit message.


I am able to get a proper response at the command level by entering 
pythonw.exe.  >>> print "abc" works fine.  I've forgotten how to get out 
of >>>. I'm killing the command window.

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


Re: How do I access IDLE in Win7

2011-07-28 Thread W. eWatson

On 7/28/2011 8:10 AM, W. eWatson wrote:

On 7/28/2011 6:19 AM, Jerry Hill wrote:

On Wed, Jul 27, 2011 at 5:26 PM, W. eWatson
wrote:

.py=Python.File
.pyw=Python.NoConFile
Python.File="C:\Python25\python.exe" "%1" %*
Python.File="C:\Python25\python.exe" "%1" %*
Python.NoConFile="C:\Python25\pythonw.exe" "%1" %*


That all looks good.


I cannot copy from the cmd window. It ends with [errorno 13] Permission
denied to c:||Users\\Wayne\\idlerc\\recent-files.lst'


That sounds like the root of the problem, then. I'm assuming Wayne is
your username, but I don't know why you wouldn't have permission to
access something in your own user directory. Can you try deleting
that file in the windows explorer? You could try messing with the
permissions, but I doubt you care about a recent file list that sounds
several months old. You might even try removing (or renaming) the
whole C:\Users\Wayne\idlerc folder. Idle should re-build anything it
needs if it's not there when you start up.


It's a hidden file. I tried deleting w/o success so changed the name.
The file was dated 5/??/2010.

I tried the usual method of right-click on junk.py to see if Edit with
IDLE would appear in the menu. It didn't, so I tried a right-click to
Choose default program to idle.pyw. Using it again gave me an invalid
32-bit message.

I am able to get a proper response at the command level by entering
pythonw.exe. >>> print "abc" works fine. I've forgotten how to get out
of >>>. I'm killing the command window.

idle.pyw brings up IDLE at the command level.
--
http://mail.python.org/mailman/listinfo/python-list


NoneType and new instances

2011-07-28 Thread Ethan Furman

Consider:

Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit 
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
--> for ins in ({0:'0'}, (1,), set([2, 3]), [4, 5], 6, 'seven',
... 8.0, True, None):
...   print(type(ins))
...   type(ins)()
...

{}

()

set()

[]

0

''

0.0

False

Traceback (most recent call last):
  File "", line 3, in 
TypeError: cannot create 'NoneType' instances

Why is NoneType unable to produce a None instance?  I realise that None 
is a singleton, but so are True and False, and bool is able to handle 
returning them:


--> bool(0) is bool(0)
True

This feels like a violation of 'Special cases aren't special enough to 
break the rules.'


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


Re: NoneType and new instances

2011-07-28 Thread Billy Mays

On 07/28/2011 11:39 AM, Ethan Furman wrote:


Traceback (most recent call last):
File "", line 3, in 
TypeError: cannot create 'NoneType' instances

Why is NoneType unable to produce a None instance? I realise that None
is a singleton, but so are True and False, and bool is able to handle
returning them:

--> bool(0) is bool(0)
True

This feels like a violation of 'Special cases aren't special enough to
break the rules.'

~Ethan~



Probably for the same reason Ellipsis and NotImplemented also can't be 
instantiated.  What that reason is I don't know.  Related:


http://bugs.python.org/issue6477#msg90641

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


Re: Seeking an example on using Queue to update variable while threading

2011-07-28 Thread Kushal Kumaran
On Thu, Jul 28, 2011 at 11:13 AM, Danny Wong (dannwong)
 wrote:
> Hi Python experts,
>        I'm trying to use a dict structure to store and update information 
> from X number of threads. How do I share this dict structure between threads? 
> I heard of using a queue, but I'm not familiar with how it works. Does anyone 
> have an example of using a queue to store variables/dicts between threads?
>

With a queue, you would not actually share data.  One of your threads
will "own" the data, and the other threads will send messages to the
owner to update the data.  These messages will be sent on a queue,
which the owner thread is waiting on.  A dummy example follows:

import threading
import time
import random
import queue
import collections
import logging
import sys

def generator_func(q, name):
for i in range(1000):
time.sleep(random.randint(1, 10))
value = random.randint(1, 100)
logging.info('generated {}'.format(value))
q.put(value)
logging.info('finished')
q.put(None)

def counter_func(q, num_generators):
counts = collections.defaultdict(int)
finished = 0
while True:
item = q.get()
if item is None:
finished += 1
if finished == num_generators:
return
logging.info('received {}'.format(item))
counts[item] += 1

def main():
num_generators = 50

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout,
format="%(asctime)s %(threadName)s %(message)s")

q = queue.Queue()
counter = threading.Thread(name='counter', target=counter_func,
   args=(q, num_generators))
counter.start()

generators = []
for i in range(num_generators):
generators.append(threading.Thread(name='generator-{:03}'.format(i),
   target=generator_func, args=(q, i)))
generators[-1].start()

counter.join()
for g in generators:
g.join()

if __name__ == '__main__':
main()

The "counter" thread runs the counter_func function.  This function
waits on a queue and updates a hash when it receives an item on the
queue.  The "generator" threads run the generator_func function, which
puts random values into the queue at random intervals.  So, in a
sense, the "generator" threads update the hash by sending messages to
the "counter" thread.

If you run this example, you might see output like this:

2011-07-28 21:23:40,445 generator-001 generated 55
2011-07-28 21:23:40,448 generator-026 generated 38
2011-07-28 21:23:40,448 generator-015 generated 62
2011-07-28 21:23:40,448 generator-009 generated 90
2011-07-28 21:23:40,459 counter received 55
2011-07-28 21:23:40,455 generator-039 generated 72
2011-07-28 21:23:40,460 counter received 38
2011-07-28 21:23:40,460 counter received 90
2011-07-28 21:23:40,460 counter received 62
2011-07-28 21:23:40,461 counter received 72
2011-07-28 21:23:41,447 generator-012 generated 12
2011-07-28 21:23:41,448 generator-029 generated 49
2011-07-28 21:23:41,448 generator-023 generated 11
2011-07-28 21:23:41,449 counter received 12
2011-07-28 21:23:41,449 generator-041 generated 91
2011-07-28 21:23:41,449 generator-049 generated 20
2011-07-28 21:23:41,449 generator-042 generated 73
2011-07-28 21:23:41,450 counter received 49
2011-07-28 21:23:41,451 counter received 11
2011-07-28 21:23:41,451 counter received 91
2011-07-28 21:23:41,452 counter received 20
2011-07-28 21:23:41,452 counter received 73
2011-07-28 21:23:41,461 generator-039 generated 85
2011-07-28 21:23:41,462 counter received 85
...


-- 
regards,
kushal
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: shlex parsing

2011-07-28 Thread Karim



Hello You have you feet on earth Web Dreamer!

Very clever!
Beautiful hack!

Many Thanks

Karim

On 07/28/2011 05:48 PM, Web Dreamer wrote:

Karim a écrit ce mercredi 27 juillet 2011 21:30 
dans  :


Hello All,

I would like to parse this TCL command line with shlex:

'-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'

And I want to get the splitted list:

['-option1', '[get_rule A1 B2]', '-option2',  '$VAR', '-option3',  'TAG']

Then I will gather in tuple 2 by 2 the arguments.


Do this:


s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'

Now if you don't enclose [get_rule A1 B2] in cotes, you will pull your hair off!
So:

s = s.replace('[','"[')
s = s.replace(']',']"')

Now:

s

'-option1 "[get_rule A1 B2]" -option2 $VAR -option3 TAG'

Lets continue:

import shlex
optionlist = shlex.split(s)
optionlist

['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG']

Now to get your tupple of two by two arguments:

argtuple = tuple([(option, value) for option,value in 
zip(optionlist[0::2],optionlist[1::2])])
argtuple

(('-option1', '[get_rule A1 B2]'), ('-option2', '$VAR'), ('-option3', 'TAG'))


whole code:

import shlex
s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
s = s.replace('[','"[')
s = s.replace(']',']"')
optionlist = shlex.split(s)
argtuple = tuple([(option, value) for option,value in 
zip(optionlist[0::2],optionlist[1::2]))


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


Re: shlex parsing

2011-07-28 Thread Nobody
On Thu, 28 Jul 2011 17:48:34 +0200, Web Dreamer wrote:

>> I would like to parse this TCL command line with shlex:
>> 
>> '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'

 s = s.replace('[','"[')
 s = s.replace(']',']"')

Note that this approach won't work if you have nested brackets or braces.
That would require a real parser.

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


Fw: Programming Python for Absolute Beginners

2011-07-28 Thread Patty

I replied to 'm' but I really wanted to reply to the whole group -

Patty

Here it is:


- Original Message - 
From: "Patty" 

To: 
Sent: Thursday, July 28, 2011 8:10 AM
Subject: Re: Programming Python for Absolute Beginners




- Original Message - 
From: "harrismh777" 

Newsgroups: comp.lang.python
To: 
Sent: Wednesday, July 27, 2011 11:02 PM
Subject: Re: Programming Python for Absolute Beginners



Billy Mays wrote:


No one cares and don't spam the list.


   ... ouch, now I feel really bad... has someone not had their coffee 
this morning?






kind regards,

--
m harris

FSF  ...free as in freedom/
http://webpages.charter.net/harrismh777/gnulinux/gnulinux.htm
--
http://mail.python.org/mailman/listinfo/python-list




Oh!  And I felt completely differently about this!  I appreciate your 
story and I also really liked Dawsons book and do have the 3rd edition. 
And I was drinking a good cup of coffee while reading your email message - 
wow!  I love stories, storytelling, history and C programming and python 
programming and Language -- I would think that all of these interests --  
and quiet time for quiet studying and pursuits -- would Expand your 
mind -- I would rather like having you around in my working environment 
'm' !  And my husband and I just realized that I may very well never be 
employed again -- and trying to figure out what to do with me -- so having 
varied interests outside of programming may be a Very Good Thing for your 
future.


Regards,

Patty 


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


Re: monotonically increasing memory usage

2011-07-28 Thread Nobody
On Thu, 28 Jul 2011 11:52:25 +0200, Pedro Larroy wrote:

> pickling
> 
> Just crossposting this from stackoverflow:
> 
> http://stackoverflow.com/questions/6857006/
> 
> Any hints?

AFAIK, it's because the Pickler object keeps a reference to each object so
that pointer-sharing works; if you write the same object multiple times,
you get multiple references to a single object, not multiple objects.

This means that the dictionaries aren't deleted while the Pickler object
lives.

It would seem that this issue could be avoided if pickle used weak
references, but there may be issues which I have overlooked.

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


Re: NoneType and new instances

2011-07-28 Thread Ian Kelly
On Thu, Jul 28, 2011 at 9:39 AM, Ethan Furman  wrote:
> Why is NoneType unable to produce a None instance?  I realise that None is a
> singleton, but so are True and False, and bool is able to handle returning
> them:

The bool constructor works (actually just returns one of the existing
singletons) so that you can do things like this:

truth_value = bool(x + 5)
return my_dict[truth_value]

Why would you ever need to instantiate NoneType?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: shlex parsing

2011-07-28 Thread Karim


Just a little modification:

>>> tuple([(option, value) for option,value in 
zip(optionlist[0::2],optionlist[1::2])]) == 
tuple(zip(optionlist[0::2],optionlist[1::2]))

True

Indeed:

tuple(zip(optionlist[0::2],optionlist[1::2]))

shorter than:

tuple([(option, value) for option,value in 
zip(optionlist[0::2],optionlist[1::2])])



Karim

PS: I am from Grenoble, which place in France are from?

On 07/28/2011 06:37 PM, Nobody wrote:

On Thu, 28 Jul 2011 17:48:34 +0200, Web Dreamer wrote:


I would like to parse this TCL command line with shlex:

'-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'

s = s.replace('[','"[')
s = s.replace(']',']"')

Note that this approach won't work if you have nested brackets or braces.
That would require a real parser.



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


Re: Is it bad practise to write __all__ like that

2011-07-28 Thread Ian Kelly
On Thu, Jul 28, 2011 at 7:22 AM, mark ferguson  wrote:
> I've not really got the hang of decorators yet, so I was wondering why one
> might use your approach rather than just using Karim's original method?

The advantage of Thomas's decorator here is that it lets you place the
denotation of whether a function is exported alongside its definition,
whereas simply declaring the __all__ list forces you to separate them.
 It also avoids the problem of possibly mistyping the function's name
in the list.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Approximate comparison of two lists of floats

2011-07-28 Thread Dan Stromberg
You'd probably better explain in English which things truly need to be
compared with what.  Right now, your first version is, I believe, an O(n^4)
algorithm, which is extremely expensive, while your second (set-based)
version appears to be O(n^3), which is quite a bit better, but still not
stellar.

I suspect you could just flatten your list of lists of numbers down to a
single list of numbers, and then extract the maximum and minimum numbers
using the min and max functions, and then compare those two results within
tolerance, but further discussion should determine if that's correct.  If
this would give the correct result, it should be an O(n) algorithm, which is
vastly faster.

The main question in my mind is: Are the inner lists (in your list of lists)
things that need to be somehow compared as a unit against all the other such
units, or are they all the same kind of thing that just happens to be in an
unnecessarily structured representation right now.

On Thu, Jul 28, 2011 at 12:11 AM, Christian Doll wrote:

> Hello,
>
> i have e little performance problem with my code...
>
> i have to compare many lists of very much floats. at moment i have
> nested for-loops
>
> for a in range( len(lists) ):
>for b in range( a+1 , len(lists) ):
>for valuea in lists[a]:
>equal=False
>for valueb in lists[b]:
>if inTolerance( valuea , valueb , 1.0): # inTolerance
> is an own function, which checks if the difference of valuea and
> valueb is not more then 1.0%
>equal=True
>break
>if equal:
>print a , "and" , b , "are equal"
>
> i found a version with set which is faster, but i cannot assign an
> tolerance (%)
> for a in range( len(lists) ):
>for b in range( a+1 , len(lists) ):
>if len( lists[a] ) ==
> len( set( lists[a] ).intersection( set( lists[b] ) ) ):
>print a , "and" , b , "are equal"
>
> have you an idea how i can change my code, that i can compare many
> lists of floats with a tolerance in percentage very fast?
>
> (sorry for my bad englisch ;-) )
>
> thanks
> christian
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


list comprehension to do os.path.split_all ?

2011-07-28 Thread gry
[python 2.7] I have a (linux) pathname that I'd like to split
completely into a list of components, e.g.:
   '/home/gyoung/hacks/pathhack/foo.py'  -->  ['home', 'gyoung',
'hacks', 'pathhack', 'foo.py']

os.path.split gives me a tuple of dirname,basename, but there's no
os.path.split_all function.

I expect I can do this with some simple loop, but I have such faith in
the wonderfulness of list comprehensions, that it seems like there
should be a way to use them for an elegant solution of my problem.
I can't quite work it out.  Any brilliant ideas?   (or other elegant
solutions to the problem?)

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


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Stefaan Himpe

Hi,


[python 2.7] I have a (linux) pathname that I'd like to split
completely into a list of components, e.g.:
'/home/gyoung/hacks/pathhack/foo.py'  -->   ['home', 'gyoung',
'hacks', 'pathhack', 'foo.py']


Not sure what your exact requirements are, but the following seems to work:

pathname = '/home/gyoung/hacks/pathhack/foo.py'
print pathname[1:].split("/")

Note that this would only work for absolute linux paths (i.e. starting 
with "/").


Best regards,
Stefaan.
--
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Neil Cerutti
On 2011-07-28, gry  wrote:
> [python 2.7] I have a (linux) pathname that I'd like to split
> completely into a list of components, e.g.:
>'/home/gyoung/hacks/pathhack/foo.py'  -->  ['home', 'gyoung',
> 'hacks', 'pathhack', 'foo.py']
>
> os.path.split gives me a tuple of dirname,basename, but there's
> no os.path.split_all function.
>
> I expect I can do this with some simple loop, but I have such
> faith in the wonderfulness of list comprehensions, that it
> seems like there should be a way to use them for an elegant
> solution of my problem. I can't quite work it out.  Any
> brilliant ideas?   (or other elegant solutions to the problem?)

If an elegant solution doesn't occur to me right away, then I
first compose the most obvious solution I can think of. Finally,
I refactor it until elegance is either achieved or imagined.

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


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Ian Kelly
On Thu, Jul 28, 2011 at 2:18 PM, gry  wrote:
> [python 2.7] I have a (linux) pathname that I'd like to split
> completely into a list of components, e.g.:
>   '/home/gyoung/hacks/pathhack/foo.py'  -->  ['home', 'gyoung',
> 'hacks', 'pathhack', 'foo.py']
>
> os.path.split gives me a tuple of dirname,basename, but there's no
> os.path.split_all function.
>
> I expect I can do this with some simple loop, but I have such faith in
> the wonderfulness of list comprehensions, that it seems like there
> should be a way to use them for an elegant solution of my problem.
> I can't quite work it out.  Any brilliant ideas?   (or other elegant
> solutions to the problem?)

path = '/home/gyoung/hacks/pathhack/foo.py'
parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
parts.reverse()
print parts

But that's horrendously ugly.  Just write a generator with a while
loop or something.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Alan Meyer

On 7/28/2011 4:18 PM, gry wrote:

[python 2.7] I have a (linux) pathname that I'd like to split
completely into a list of components, e.g.:
'/home/gyoung/hacks/pathhack/foo.py'  -->   ['home', 'gyoung',
'hacks', 'pathhack', 'foo.py']

os.path.split gives me a tuple of dirname,basename, but there's no
os.path.split_all function.

I expect I can do this with some simple loop, but I have such faith in
the wonderfulness of list comprehensions, that it seems like there
should be a way to use them for an elegant solution of my problem.
I can't quite work it out.  Any brilliant ideas?   (or other elegant
solutions to the problem?)

-- George


This is not properly portable to all OS, but you could simply split on 
the slash character, e.g.,


pathname.split('/')

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


Re: NoneType and new instances

2011-07-28 Thread Ethan Furman

Ian Kelly wrote:

On Thu, Jul 28, 2011 at 9:39 AM, Ethan Furman  wrote:

Why is NoneType unable to produce a None instance?  I realise that None is a
singleton, but so are True and False, and bool is able to handle returning
them:


The bool constructor works (actually just returns one of the existing
singletons) so that you can do things like this:

truth_value = bool(x + 5)
return my_dict[truth_value]

Why would you ever need to instantiate NoneType?


I'm glad you asked!  I'm using dictionaries to describe fields and what 
their return values should be.  There happen to be two special cases: 
empty and Null.  So a portion of the dictionary looks like:


fielddef = { 'empty':some_func, 'null':some_func }

Depending on the needs of the user, `some_func` might be `str`, `int`, 
`custom_class`, or `NoneType`.  Somewhere else in the code I have:


if not value: # or some such
cls = fielddef['empty']
return cls()

This works for *everything* except NoneType.  grrr.  ;)

~Ethan~

PS
I'll use a lambda to get around it, but that's not very elegant.  Why 
shouldn't NoneType be able to return the singleton None?

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


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Ian Kelly
On Thu, Jul 28, 2011 at 2:44 PM, Ian Kelly  wrote:
> path = '/home/gyoung/hacks/pathhack/foo.py'
> parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
> parts.reverse()
> print parts
>
> But that's horrendously ugly.  Just write a generator with a while
> loop or something.

Also, note that if the path does not start with '/', the result will
be an infinite loop.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Ethan Furman

Neil Cerutti wrote:

If an elegant solution doesn't occur to me right away, then I
first compose the most obvious solution I can think of. Finally,
I refactor it until elegance is either achieved or imagined.


+1 QOTW

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


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Ian Kelly
On Thu, Jul 28, 2011 at 2:47 PM, Ian Kelly  wrote:
> On Thu, Jul 28, 2011 at 2:44 PM, Ian Kelly  wrote:
>> path = '/home/gyoung/hacks/pathhack/foo.py'
>> parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
>> parts.reverse()
>> print parts
>>
>> But that's horrendously ugly.  Just write a generator with a while
>> loop or something.
>
> Also, note that if the path does not start with '/', the result will
> be an infinite loop.

Oh, and this won't work at all in Python 3, because it relies on the
lambda having access to the list comprehension scope.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Alexander Kapps

On 28.07.2011 22:44, Ian Kelly wrote:

On Thu, Jul 28, 2011 at 2:18 PM, gry  wrote:

[python 2.7] I have a (linux) pathname that I'd like to split
completely into a list of components, e.g.:
   '/home/gyoung/hacks/pathhack/foo.py'  -->['home', 'gyoung',
'hacks', 'pathhack', 'foo.py']

os.path.split gives me a tuple of dirname,basename, but there's no
os.path.split_all function.

I expect I can do this with some simple loop, but I have such faith in
the wonderfulness of list comprehensions, that it seems like there
should be a way to use them for an elegant solution of my problem.
I can't quite work it out.  Any brilliant ideas?   (or other elegant
solutions to the problem?)


path = '/home/gyoung/hacks/pathhack/foo.py'
parts = [part for path, part in iter(lambda: os.path.split(path), ('/', ''))]
parts.reverse()
print parts

But that's horrendously ugly.  Just write a generator with a while
loop or something.



pathname = '/home/gyoung/hacks/pathhack/foo.py'
parts = [part for part in pathname.split(os.path.sep) if part]
print parts

['home', 'gyoung', 'hacks', 'pathhack', 'foo.py']
--
http://mail.python.org/mailman/listinfo/python-list


Any suggestion to start more threads at the same time?

2011-07-28 Thread smith jack
I start many threads in order to make the work done, when the
concurrent number is set to 300, all thing just works fine, but when
the number is set to 350 or higher, error just comes out? what's wrong
? the error info is just as follows:   failed to start .

I am confused, does this have something to do with the operating
system, i am now using Linux, any suggestion to make the system to
support more python threads?

thank you :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Emile van Sebille

On 7/28/2011 1:18 PM gry said...

[python 2.7] I have a (linux) pathname that I'd like to split
completely into a list of components, e.g.:
'/home/gyoung/hacks/pathhack/foo.py'  -->   ['home', 'gyoung',
'hacks', 'pathhack', 'foo.py']

os.path.split gives me a tuple of dirname,basename, but there's no
os.path.split_all function.



Why not just split?

'/home/gyoung/hacks/pathhack/foo.py'.split(os.sep)

Emile

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


Re: NoneType and new instances

2011-07-28 Thread Chris Angelico
On Fri, Jul 29, 2011 at 7:03 AM, Ethan Furman  wrote:
> I'll use a lambda to get around it, but that's not very elegant.  Why
> shouldn't NoneType be able to return the singleton None?

Why a lambda?

def ThisFunctionWillReturnNone():
pass

Although, since the returning of None is crucial to it, it'd probably
be better to explicitly "return None". But minimalist useful functions
are amusing.

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


Re: list comprehension to do os.path.split_all ?

2011-07-28 Thread Ian Kelly
On Thu, Jul 28, 2011 at 3:15 PM, Emile van Sebille  wrote:
> On 7/28/2011 1:18 PM gry said...
>>
>> [python 2.7] I have a (linux) pathname that I'd like to split
>> completely into a list of components, e.g.:
>>    '/home/gyoung/hacks/pathhack/foo.py'  -->   ['home', 'gyoung',
>> 'hacks', 'pathhack', 'foo.py']
>>
>> os.path.split gives me a tuple of dirname,basename, but there's no
>> os.path.split_all function.
>>
>
> Why not just split?
>
> '/home/gyoung/hacks/pathhack/foo.py'.split(os.sep)

Using os.sep doesn't make it cross-platform. On Windows:

>>> os.path.split(r'C:\windows')
('C:\\', 'windows')
>>> os.path.split(r'C:/windows')
('C:/', 'windows')
>>> r'C:\windows'.split(os.sep)
['C:', 'windows']
>>> r'C:/windows'.split(os.sep)
['C:/windows']
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: @PyNoobs: The Fundamental Five Built-in Functions, and Beyond!

2011-07-28 Thread rantingrick
On Jul 26, 9:53 pm, Terry Reedy  wrote:
> On 7/26/2011 8:01 PM, rantingrick wrote:
>
> > Most new user think that printing an object to stdout is all they'll
> > ever need. However when you call print -- or sys.stdout.write(object)
> > -- you are only seeing a "friendly" version of the object.
>
> This mostly applies to strings, which *do* have 2 printed versions.  It
> is occasionally very important for debugging string problems.

Actually you have to be careful of objects too. Consider this:

py> import Tkinter
py> root = Tkinter.Tk()
py> print root
.
py> print repr(root)

py> label = Tkinter.Label(root)
py> print label
.46012656
py> print repr(label)


...as you can see, if someone overrides __str__ you can get some very
confusing return values from a naked print(obj) -- which simply calls
"obj.__str__()" under the covers.

> > --
> >   5. id()
> > --
> > http://docs.python.org/py3k/library/functions.html#id
> >
> > [...}
> >
> This is the most dangerous of the builtins, as it sometimes mislead
> newbies into 'discovering' non-existent 'bugs'. The main point is that
> the id of immutable objects is mostly an irrelevant implementation
> detail, while the id of mutables may be critical. Lists of lists is a
> particular area where id() is really useful.

Yes this one can cause some major confusion to newcomers. I wonder if
python should throw a warning anytime you call id() on an immutable
type.

py> a = "12345"
py> id(a)
45313728
py> b = "12345"
py> id(b)
45313728

That first call to id should have thrown a warning:

 Traceback (most recent call last):
   File "", line 1, in 
 id(a)
 Warning: Comparing ids of immutable types is unreliable!

and an Exception if someone tries an assertion!

 py> assert id(a) != id(b)

 Traceback (most recent call last):
   File "", line 1, in 
 assert id(a) != id(b)
 Exception: Cannot compare immutable types!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NoneType and new instances

2011-07-28 Thread Ben Finney
Ethan Furman  writes:

> Why is NoneType unable to produce a None instance? I realise that None
> is a singleton

That answers your question. Because None is a singleton, the job of its
type is to make sure there are no other instances.

> but so are True and False, and bool is able to handle returning them:

Well, they don't meet the definition of a singleton, because there are
two instances of ‘bool’ :-)

There may be code out there which depends on the fact that there once
was never a ‘bool’ type, or depends on the fact that bools are also
‘int’ instances, and it's been deemed not-worth-the-trouble to break
that code.

> This feels like a violation of 'Special cases aren't special enough to
> break the rules.'

In the case of ‘bool’, the rule was broken before being introduced.

-- 
 \  “Isn't it enough to see that a garden is beautiful without |
  `\  having to believe that there are fairies at the bottom of it |
_o__) too?” —Douglas Adams |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NoneType and new instances

2011-07-28 Thread Ethan Furman

Chris Angelico wrote:

On Fri, Jul 29, 2011 at 7:03 AM, Ethan Furman  wrote:

I'll use a lambda to get around it, but that's not very elegant.  Why
shouldn't NoneType be able to return the singleton None?


Why a lambda?

def ThisFunctionWillReturnNone():
pass

Although, since the returning of None is crucial to it, it'd probably
be better to explicitly "return None". But minimalist useful functions
are amusing.


More amusing still:

def NoneType():
"Returns None"

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


Re: NoneType and new instances

2011-07-28 Thread Ethan Furman

Ben Finney wrote:

Ethan Furman  writes:


Why is NoneType unable to produce a None instance? I realise that None
is a singleton


That answers your question. Because None is a singleton, the job of its
type is to make sure there are no other instances.


Which it can do quite easily by returning the single instance of None -- 
it is not necessary to raise an exception to fulfill its duty.




but so are True and False, and bool is able to handle returning them:


Well, they don't meet the definition of a singleton, because there are
two instances of ‘bool’ :-)


Okay, a slightly relaxed definition of singleton, since there is only 
ever one instance with the value of True, or the value of False; 
likewise, there is only ever one instance with the value of None.




This feels like a violation of 'Special cases aren't special enough to
break the rules.'


In the case of ‘bool’, the rule was broken before being introduced.


I think we disagree on what the rule is.  I see it as "Return an 
instance if you can."  Nobody has yet pointed out a good reason on why 
NoneType, NotImplementedType, and ellipsis (to be thorough ;) cannot or 
should not return the single instance that exists, when every other 
built-in will return either a new object, or the single object that 
exists for that value.


~Ethan~

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


Re: Any suggestion to start more threads at the same time?

2011-07-28 Thread Irmen de Jong
On 28-7-2011 23:07, smith jack wrote:
> I start many threads in order to make the work done, when the
> concurrent number is set to 300, all thing just works fine, but when
> the number is set to 350 or higher, error just comes out? what's wrong
> ? the error info is just as follows:   failed to start .
> 
> I am confused, does this have something to do with the operating
> system, i am now using Linux, any suggestion to make the system to
> support more python threads?
> 
> thank you :)

I don't think that many threads are going to help you in any meaningful way. 
Especially
with Python's GIL. Can't you redesign your program to use a fixed number of 
threads such
as 1 per cpu core?

Or check out the multiprocessing module.

But yeah, I think your OS is preventing you from creating more threads (either 
due to
some artificial limit or due to lack of system memory).

Irmen

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


PyWart: PEP8: a seething cauldron of inconsistencies.

2011-07-28 Thread rantingrick

I believe the current Python style guide is inconsistent. The author
again allowed his emotion to get in the way of logic. I will be
posting blocks of text from the PEP8 and commenting below them.

> --
> One of Guido's key insights is that code is read much more
> often than it is written.  The guidelines provided here
> are intended to improve the readability of code and make
> it consistent across the wide spectrum of Python code.  As
> PEP 20 [6] says, "Readability counts".
> --

Yes but the style guide itself is contradictory!

> --
> A style guide is about consistency.  Consistency with this
> style guide is important.  Consistency within a project is
> more important. Consistency within one module or function
> is most important.
> --

Hmm, Yes. And CONSISTENCY within a style guide that promotes
CONSISTENCY should trump them all! However as we'll see shortly
"CONSISTENCY" has been sacrificed at the alter of fluffy feelings and
multi-style-ism.

> --
> But most importantly: know when to be inconsistent --
> sometimes the style guide just doesn't apply.  When in
> doubt, use your best judgment.
> --

WRONG! When in doubt be consistent! There should be one AND ONLY ONE
obvious way to do it! When we have multiple ways of doing the same
thing we induce an overhead into our work flows. Instead we need to
follow the zen; in other words, BE CONSISTENT!

> --
> Look at other examples and
> decide what looks best.  And don't hesitate to ask!
> --

NO, NO! Follow the community standards. NEVER make up your own. We
cannot keep propagating multiplicity or else we will drown in it! We
cannot have people just making up syntax for iterations can we? No!.
Likewise we cannot allow people to make up styles as they please.

> --
> Two good reasons to break a particular rule:
>
> (1) When applying the rule would make the code less
> readable, even for someone who is used to reading code
> that follows the rules.
>
> (2) To be consistent with surrounding code that also
> breaks it (maybe for historic reasons) -- although this is
> also an opportunity to clean up someone else's mess (in
> true XP style).
> --

Number two is the ONLY reason why ANYONE should not follow the style
guide to a "T"! However, code of this style should be shamed by the
community.

> --
> Use 4 spaces per indentation level.
> --

This should be the only acceptable indention level allowed by the
interpreter. All other indention should cast plagues of syntax errors
on the unrighteous.

> --
> For really old code that you don't want to mess up, you
> can continue to use 8-space tabs.
> --

NO! You should convert the code to 4 space tabs and move into the 21st
century. Stop living in the past. We must move forward... kicking and
screaming if necessary.

> --
> Tabs or Spaces?
>
> Never mix tabs and spaces.
>
> The most popular way of indenting Python is with spaces
> only.  The second-most popular way is with tabs only.
> Code indented with a mixture of tabs and spaces should be
> converted to using spaces exclusively.  When invoking the
> Python command line interpreter with the -t option, it
> issues warnings about code that illegally mixes tabs and
> spaces.  When using -tt these warnings become errors.
> These options are highly recommended!
> --

Here is another missed opportunity to force compliance of a style
convention. Mixing tabs and spaces should throw syntax errors! Is it
too much to ask that people at least choose one over the other? Sheez,
are we that afraid of offending them? Are these people emotional
basket cases riving with suicidal tendencies? *rolls-eyes* If they are
then the Python style guide is the LEAST of their problems.

> --
> Blank Lines
>
> Separate top-level function and class definitions with two
> blank lines.
>
> Method definitions inside a class are separated by a
> single blank line.
> --

This should be the law. Unfortunately many stdlib modules do not
follow this suggestion. In Python4000 i'm making it a syntax error to
inclu

Re: NoneType and new instances

2011-07-28 Thread Ben Finney
Ethan Furman  writes:

> Ben Finney wrote:
> > Ethan Furman  writes:
> >> This feels like a violation of 'Special cases aren't special enough
> >> to break the rules.'
> >
> > In the case of ‘bool’, the rule was broken before being introduced.
>
> I think we disagree on what the rule is.  I see it as "Return an instance
> if you can."

That's not the rule for singletons, though. You've already said what the
rule is:

> Nobody has yet pointed out a good reason on why NoneType,
> NotImplementedType, and ellipsis (to be thorough ;) cannot or should
> not return the single instance that exists

So we agree on what the rule is. The ‘bool’ type breaks that rule, for
the reasons given earlier.

You may want to advocate for a *change* to that rule, or what the rule
*should be*, but that's a different issue.

> when every other built-in will return either a new object, or the
> single object that exists for that value.

What other built-in singleton types are there?

-- 
 \“It's all in the mind, you know.” —The Goon Show |
  `\   |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Any suggestion to start more threads at the same time?

2011-07-28 Thread Dan Stromberg
You could try Jython.

Other than that, you probably want a threadpool, or perhaps to try
multiprocessing - but that much forking could be a problem as well.

On Thu, Jul 28, 2011 at 2:07 PM, smith jack  wrote:

> I start many threads in order to make the work done, when the
> concurrent number is set to 300, all thing just works fine, but when
> the number is set to 350 or higher, error just comes out? what's wrong
> ? the error info is just as follows:   failed to start .
>
> I am confused, does this have something to do with the operating
> system, i am now using Linux, any suggestion to make the system to
> support more python threads?
>
> thank you :) 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyWart: PEP8: a seething cauldron of inconsistencies.

2011-07-28 Thread harrismh777

rantingrick wrote:

I believe the current Python style guide is inconsistent. The author
again allowed his emotion to get in the way of logic.


   Well, does not PEP8 state, "A Foolish Consistency is the Hobgoblin 
of Little Minds" ?


   Your parody is witty, and not without serious commentary; not 
withstanding good remarks overall toward less ambiguous forced style 
mongering... not to mention the ill-fated readability quest !



   That's why I like  C  !


   The compiler should ignore *all* white space and let the coder use 
what-ever-the-heck style she wants to ...   if you can't read it, what 
does she care?   ... its all math anyway...  only the machine needs to 
read it...   ;-)




   Back in the day, I could tell who wrote an anonymous piece of 
hacking based on the encoding style...  no kidding here...   and now 
folks want to force the personality out of our code... NO I say, NO !




   ... just saying, probably don't really mean it.


:)




--
m harris

FSF  ...free as in freedom/
http://webpages.charter.net/harrismh777/gnulinux/gnulinux.htm
--
http://mail.python.org/mailman/listinfo/python-list


Re: Any suggestion to start more threads at the same time?

2011-07-28 Thread Christian Heimes
Am 28.07.2011 23:07, schrieb smith jack:
> I start many threads in order to make the work done, when the
> concurrent number is set to 300, all thing just works fine, but when
> the number is set to 350 or higher, error just comes out? what's wrong
> ? the error info is just as follows:   failed to start .
> 
> I am confused, does this have something to do with the operating
> system, i am now using Linux, any suggestion to make the system to
> support more python threads?

300 threads is an insanely high amount of threads for almost every
environment. For CPython threads won't speed up your program if it's CPU
bound and spends most of its time in Python space (no C code that
releases the GIL). For IO bound code you are going to get much better
performance with some kind of async event loop (select, poll, epoll).
Twisted's event reactor may help here, too.

With that many threads any program (not limited to Python) will spend a
considerable amount of CPU resources to manage the threads. Every thread
also consumes a fair amount of memory since every thread needs its own C
stack. On most systems the C stack for every thread is about 8 MB. There
are also some management things that need memory.

You can reduce the stack size *before* you start any threads:

import sys
import thread
import resource

sys.setrecursionlimit(500) # default is 1000
stacksize = 4 * 1024 * 1024 # default is 8 MB on most systems
thread.stack_size(stacksize)
resource.setrlimit(resource.RLIMIT_STACK, (stacksize, -1))

That should give you some more spare memory.

Christian

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


Re: NoneType and new instances

2011-07-28 Thread Steven D'Aprano
Ethan Furman wrote:

> Why is NoneType unable to produce a None instance?  I realise that None
> is a singleton, but so are True and False, and bool is able to handle
> returning them:

I've asked this question myself. As I recall the answer, it's just a matter
of personal preference. Some people consider that singletons should raise
an error if you try to instantiate them a second time. I think that's
the "standard" behaviour referenced in the Gang Of Four design patterns
book, and it's certainly very common in Java. Others think that it should
just return the same instance.

The advantage of raising an error is that if the singleton holds state, then
the caller won't be fooled into thinking they got a second instance. They
have to explicitly refer to the one instance each time. But since None
doesn't hold state, there is no advantage here.

As for True and False, bool has to be able to return them, because the whole
purpose of exposing bool is so people can call it: if bool(some_value) was
an error, that would defeat the purpose of having bool!



-- 
Steven

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


gettext switch language on the fly

2011-07-28 Thread Peter Irbizon
hello,
I am using gettext fo localization
local_path=os.path.join(module_path(), lang_folder)
gettext.bindtextdomain(lang_name, local_path)
gettext.textdomain(lang_name)
# Get the language to use
lang = gettext.translation(lang_name, local_path, languages=['sk'],
fallback = True)
lang.install()

Now I would like to switch all texts in my app when I click on item in menu.
Unfortunatelly this not switch texts immediately. How can I do this?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NoneType and new instances

2011-07-28 Thread Steven D'Aprano
Ian Kelly wrote:

> Why would you ever need to instantiate NoneType?


Well, you probably wouldn't deliberately, but if you have code like this:


types = [type(x) for x in list_of_objects]
# later on...
for t in types:
instance = t()
do_something_with(instance)


it would be nice if it didn't explode when list_of_objects contains None.


-- 
Steven

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


Re: NoneType and new instances

2011-07-28 Thread Steven D'Aprano
Chris Angelico wrote:

> On Fri, Jul 29, 2011 at 7:03 AM, Ethan Furman  wrote:
>> I'll use a lambda to get around it, but that's not very elegant.  Why
>> shouldn't NoneType be able to return the singleton None?
> 
> Why a lambda?
> 
> def ThisFunctionWillReturnNone():
> pass

This is a good use-case for a lambda. Ethan's use-case is a dict of
constructors:

     fielddef = { 'empty':some_func, 'null':some_func }

There's no need to expose the constructor outside of the dict, hence:

     fielddef = { 'empty': str, 'null': lambda: None }

does the job. No need for a top-level named function.


-- 
Steven

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


Re: NoneType and new instances

2011-07-28 Thread Terry Reedy

On 7/28/2011 5:03 PM, Ethan Furman wrote:


I'm glad you asked! I'm using dictionaries to describe fields and
what their return values should be. There happen to be two special
cases: empty and Null. So a portion of the dictionary looks like:

fielddef = { 'empty':some_func, 'null':some_func }

Depending on the needs of the user, `some_func` might be `str`,
`int`, `custom_class`, or `NoneType`. Somewhere else in the code I
have:

if not value: # or some such cls = fielddef['empty'] return cls()

This works for *everything* except NoneType. g
~Ethan~

PS I'll use a lambda to get around it, but that's not very elegant.
Why shouldn't NoneType be able to return the singleton None?


Possibly historical accident. You could ask on python-ideas
or put a feature request on the tracker. Start with the first in case 
there is a ready answer I do not know of.

The big question is whether there is code depending on current behavior.

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


Re: gettext switch language on the fly

2011-07-28 Thread Chris Rebert
On Thu, Jul 28, 2011 at 6:20 PM, Peter Irbizon  wrote:
> hello,
> I am using gettext fo localization

> Now I would like to switch all texts in my app when I click on item in menu.
> Unfortunatelly this not switch texts immediately. How can I do this?

Which GUI toolkit are you using?

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


Re: How do I access IDLE in Win7

2011-07-28 Thread W. eWatson
I've given this problem over to the Python Tutor mail-list. I can 
capture screens more easily than manipulate in cmd.exe. It may be a 
preference problem on who owns what. SYSTEM seems to be the owner, and 
not me.


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


Re: Is it bad practise to write __all__ like that

2011-07-28 Thread Erik Max Francis

Thomas Rachel wrote:

Why not? But you could even do

class AllList(list):
"""list which can be called in order to be used as a __all__-adding 
decorator"""

def __call__(self, obj):
"""for decorators"""
self.append(obj.__name__)
return obj

__all__ = AllList()

@__all__
def api(): pass

@__all__
def db(): pass

@__all__
def input(): pass

@__all__
def output(): pass

@__all__
def tcl(): pass


Bravo!

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
 San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Jabber erikmaxfrancis
  Smaller than the eye can see / Bigger than the mind can conceive
   -- India Arie
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is it bad practise to write __all__ like that

2011-07-28 Thread Thomas Rachel

Am 28.07.2011 20:01 schrieb Ian Kelly:


The advantage of Thomas's decorator here is that it lets you place the
denotation of whether a function is exported alongside its definition,
whereas simply declaring the __all__ list forces you to separate them.
  It also avoids the problem of possibly mistyping the function's name
in the list.


Thank you. For the ones who do not like @__all__ because it might look 
ugly, here is an alternative:


__all__ = []

def export(obj):
__all__.append(obj.__name__)
return obj

It is nearly the same, but without an extra class and with the more 
readable


@export
def func(): pass


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