Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Arnaud Delobelle
Duncan Booth  writes:

> Duncan Booth  wrote:
>
>> John Posner  wrote:
>> 
>>> Do know what in the itertools implementation causes adding a 'if p <=
>>> sqrt(n)' clause to *decrease* performance, while adding a
>>> 'takewhile()' clause *increases* performance? 
>> 
>> I haven't timed it, but I would guess that the takewhile was faster 
>> only because the sqrt(n) had been factored out of the loop. Try the 
>> original loop again precalculating the sqrt(n) and see how that compares.
>> 
> Which of course is rubbish, extracting the sdqrt will have an effect but 
> the main factor is that takewhile exits the loop as soon as the condition 
> is false whereas a conditional in a generator comprehension doesn't stop 
> the loop continuing to the end.

Absolutely!  Since you hadn't quoted the original code, I'd forgotten
that it wasn't stopping after n**0.5.

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


Re: HTML Conversion in python

2009-04-12 Thread Gabriel Genellina
En Mon, 13 Apr 2009 02:47:30 -0300, venutaurus...@gmail.com  
 escribió:



Is there any library defined in Python which can convert a
given text file into a html page. Basically, I require functions for
creating tables or filling background colours for the html pages etc
instead of writing each and every tag in my script..


You're looking for a templating engine -- see  
http://wiki.python.org/moin/Templating


--
Gabriel Genellina

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


Re: Regular Expression Help

2009-04-12 Thread Graham Breed

Jean-Claude Neveu wrote:

Hello,

I was wondering if someone could tell me where I'm going wrong with my 
regular expression. I'm trying to write a regexp that identifies whether 
a string contains a correctly-formatted currency amount. I want to 
support dollars, UK pounds and Euros, but the example below deliberately 
omits Euros in case the Euro symbol get mangled anywhere in email or 
listserver processing. I also want people to be able to omit the 
currency symbol if they wish.


If Euro symbols can get mangled, so can Pound signs. 
They're both outside ASCII.



My regexp that I'm matching against is: "^\$\£?\d{0,10}(\.\d{2})?$"

Here's how I think it should work (but clearly I'm wrong, because it 
does not actually work):


^\$\£?  Require zero or one instance of $ or £ at the start of the 
string.


^[$£]? is correct.  And, as you're using re.match, the ^ is 
superfluous.  (A previous message suggested ^[\$£]? which 
will also work.  You generally need to escape a Dollar sign 
but not here.)


You should also think about the encoding.  In my terminal, 
"£" is identical to '\xc2\xa3'.  That is, two bytes for a 
UTF-8 code point.  If you assume this encoding, it's best to 
make it explicit.  And if you don't assume a specific 
encoding it's best to convert to unicode to do the 
comparisons, so for 2.x (or portability) your string should 
start u"



d{0,10} Next, require between zero and ten alpha characters.


There's a backslash missing, but not from your original 
expression.  Digits are not "alpha characters".


(\.\d{2})?  Optionally, two characters can follow. They must be preceded 
by a decimal point.


That works.  Of course, \d{2} is longer than the simpler \d\d

Note that you can comment the original expression like this:

rex = u"""(?x)
^[$£]?# Zero or one instance of $ or £
   # at the start of the string.
\d{0,10}   # Between zero and ten digits
(\.\d{2})? # Optionally, two digits.
   # They must be preceded by a decimal point.
$  # End of line
"""

Then anybody (including you) who comes to read this in the 
future will have some idea what you were trying to do.


\> Examples of acceptable input should be:


$12.42
$12
£12.42
$12,482.96  (now I think about it, I have not catered for this in my 
regexp)


Yes, you need to think about that.


   Graham

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


Re: choosing background color in tkfiledialog.askopenfile()

2009-04-12 Thread Gabriel Genellina
En Sun, 12 Apr 2009 22:05:21 -0300, Soumen banerjee   
escribió:



I want to know how i can choose a background color in
tkfiledialog.askopenfilename()?
Due to a certain theme i am using, i cant see any text and changing
the theme settings dont seem to affect the file dialog


Looks like a bug in tkinter -- please report it at http://bugs.python.org/

--
Gabriel Genellina

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


Re: OverflowError while sending large file via socket

2009-04-12 Thread Gabriel Genellina

En Sun, 12 Apr 2009 19:28:43 -0300, > escribió:

Ryniek90  writes:



When i wanted to send an .iso file of 4GB length, i had traceback:
"OverflowError: requested number of bytes is more than a Python string
can hold"


You're not supposed to put the 4GB all in one string.  Open the
socket and send smaller packets through it.


And instead of reinventing the wheel again, use the shutil module to do  
exactly that.


--
Gabriel Genellina

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


Re: GUI Programming

2009-04-12 Thread Gabriel Genellina

En Sun, 12 Apr 2009 19:28:18 -0300, Python  escribió:


On 12 apr 2009, at 15:07, Gabriel wrote:


I'm python newbie and i need to write gui for my school work in python.
I need to write it really quick, because i haven't much time .)


I chose wxpython because of it's excellent crossplatform compatibility
it has a native look on all platforms too and no commercial license
whatsoever...
there are a huge lot of examples oonline f almost anything you want
ideal for schoolwork ;)


I second that.

--
Gabriel Genellina

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


Re: a newbie question

2009-04-12 Thread larryzhang
On Apr 12, 11:01 pm, Peter Otten <__pete...@web.de> wrote:
> zhangle2...@gmail.com wrote:
> > I am just learning Python and get a problem when trying this example:
>
> > from urllib import urlopen
> > doc=urlopen("http://www.python.org";).read()
> > print(doc)
>
> > when i run this, i was tould that 'cannot import name "urlopen"
>
> > What's wrong with this code? Do i need to place my code in some
> > specific directory? Thanks a lot. '
>
> This function has been moved in Python 3.0. Change your import to
>
> from urllib.request import urlopen
>
> If you are learning with a book using Python 2.x consider switching to
> Python 2.5 or 2.6 to try out the examples.
>
> Peter

It works. Thanks a lot Peter.

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


HTML Conversion in python

2009-04-12 Thread venutaurus...@gmail.com
Hello All,
Is there any library defined in Python which can convert a
given text file into a html page. Basically, I require functions for
creating tables or filling background colours for the html pages etc
instead of writing each and every tag in my script..

Thank you,
Venu Madhav.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Definition of Pythonic?

2009-04-12 Thread Gabriel Genellina
En Sun, 12 Apr 2009 08:16:19 -0300, Lawrence D'Oliveiro  
 escribió:



Pythonic--an acid, capable of reacting with bases to form pythanates?

Would it be an organic or inorganic acid?

Deprive it of a bit of oxygen, and it becomes pythonous, reacting to form
pythonites.

What do you mean, the fume cupboard's broken down?


Definitely!


Honestly I feel fine...


Sure... just don't drive :)

--
Gabriel Genellina

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


download robot

2009-04-12 Thread larryzhang
Hi,
Being a newbie for Python, I am trying to write a code that can act as
a downloading robot.

The website provides information for companies. Manually, I can search
by company name and then click the “download” button to get the data
in excel or word format, before saving the file in a local directory.
The program is to do this automatically.

I have met several problems when writing the codes:
1. The website needs user ID and password, is there a way that I can
pass my ID and password to the server in my python code?
2. Can Python hit the “download” button automatically and choose the
type of file format as I can do manually?
3. The url of each downloading webpage is not unique (webpages point
to different data files may share the same url), which prevent me from
working directly with the url as the address to find a certain file.
Is there any solution for this? Does this mean I have to work directly
with the database stored in the server rather than with the webpage
displayed?

Thank you very much for any comments and suggestions.

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


Re: multiprocessing and Locks

2009-04-12 Thread gvv
On Apr 13, 3:30 am, Piet van Oostrum  wrote:
> > gvv  (G) wrote:
> >G> Hi All,
> >G> I am trying to understand multiprocessing, but I am getting a Runtime
> >G> error on the
> >G> code below. What am I missing or doing wrong?
> >G> Error is:
> >G> RuntimeError: Lock objects should only be shared between processes
> >G> through inheritance
>
> [code deleted]
>
> I guess you can't share locks (and probably other objects) between
> processes from a Pool. Maybe because there is no direct parent-child
> relation or so (there is a separate thread involved). There is nothing
> in the doc that explicitely forbids it AFAICT but it says that you have
> to be careful with sharing. But it could be a bug.
>
> You can do it with a manager, however, but this involves an additional
> process under the hood.
>
> if __name__ == "__main__":
>     manager = multiprocessing.Manager()
>     lock = manager.Lock()
>     pool = multiprocessing.Pool(processes=5)
>     for i in xrange(100):
>         pool.apply_async(func=RunFunc, args=(i,lock))
>     pool.close()
>     pool.join()
>
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p...@vanoostrum.org

Hi Piet,

Thanks for your help. It worked.
--
http://mail.python.org/mailman/listinfo/python-list


Re: "str object is not callable" error

2009-04-12 Thread Gabriel Genellina
En Wed, 08 Apr 2009 18:11:37 -0300, venkat sanaka   
escribió:



i was using python/c api to call a python function from c and I know the
name of the function which i want to call.Is there any way to do that??
This is the method i tried...

for eg:This is the python function i wants to call.
 >>>def add(x):
...   return x+10


This is my code in C:
PyObject *result = NULL;
int arg;
PyObject *arglist;
arg = 123;
my_callback = "add";
arglist = Py_BuildValue("(i)", arg);
result = PyObject_CallObject(my_callback, arglist);
Py_DECREF(arglist);
return result;

I was getting a error like "str object is not callable".From the error i
came to know that i was assigning "add" as a string which caused this
error.Then how to make it a callable object??


This is what one would write in Python:

import some_module
some_module.add(123)

Do the same in C:

callback = PyObject_GetAttrString(some_module, "add");
if (!callback) ...error...
result = PyObject_CallFunction(callback, "i", arg);
Py_DECREF(callback);
return result;

--
Gabriel Genellina

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


Re: Data Model:

2009-04-12 Thread Aaron Brady
On Apr 12, 10:33 pm, Anthony  wrote:
> On Apr 12, 8:10 pm, Aaron Brady  wrote:
>
>
>
> > On Apr 12, 9:14 pm, Anthony  wrote:
>
> > > I'm struggling on whether or not to implement GroupItem (below) with
> > > two separate models, or with one model that has a distinguishing key:
>
> > > Given:
> > > class ParentGroup:
> > >     a group of values represented by class GroupItem
>
> > > class ChildGroup:
> > >     a group of values represented by class GroupItem
> > >     foreign-key to ParentGroup (many Children sum to one Parent)
>
> > > Option A:
> > > class GroupItem:
> > >     foreign-key to ParentGroup
> > >     foreign-key to ChildGroup
> > >     GroupItemType in (ParentItem, ChildItem)
> > >     value
> > >     value-type
>
> > > Option B:
> > > class ParentGroupItem
> > >     foreign-key to ParentGroup
> > >     value
> > >     value-type
>
> > > class ChildGroupItem
> > >     foreign-key to ChildGroup
> > >     value
> > >     value-type
>
> > > What are my considerations when making this decision?
>
> > > Thanks!
>
> > You want a ChildItem to have membership in two collections:
> > ParentGroup and ChildGroup.  You also want a ParentItem to have
> > membership in one collection.  For example:
>
> > parentA: itemPA1, itemPA2, childA, childB
> > childA: itemCA1, itemCA2
> > childB: itemCB1, itemCB2
>
> > Or, listing by child,
>
> > itemPA1: parentA
> > itemPA2: parentA
> > itemCA1: childA
> > itemCA2: childA
> > itemCB1: childB
> > itemCB2: childB
> > childA: parentA
> > childB: parentA
>
> > Correct so far?
>
> Thanks for the insightful response.
>
> Yes, everything you say is correct, with one clarification:  The
> ChildItem can be a member of ParentGroup OR ChildGroup, but never both
> at the same time.

I see.  You described a collection class.  Its members are items or
other collections.  They are never nested more than two levels deep.

However, in your example, you implied a collection class whose
attributes are aggregates of its members'.  For simplicity, you can
use methods to compute the aggregate attributes.

class Group:
  def calculate_total_produced( self ):
total= sum( x.total_produced for x in self.members )

If you want to cache them for performance, the children will have to
notify the parent when one of their attributes changes, which is at
least a little more complicated.  The class in the simpler structure
could even derive from 'set' or other built-in collection if you
want.  Are you interested in the more complicated faster technique?

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


Re: Regex similar to "^(?u)\w$", but without digits?

2009-04-12 Thread Mark Tolonen


"Andreas Pfrengle"  wrote in message 
news:26d3bec3-8329-4432-a680-05c17f930...@3g2000yqk.googlegroups.com...

On 12 Apr., 02:31, "Mark Tolonen"  wrote:

"Andreas"  wrote in message

news:f953c845-3660-4bb5-8ba7-00b93989c...@b1g2000vbc.googlegroups.com...

> Hello,

> I'd like to create a regex that captures any unicode character, but
> not the underscore and the digits 0-9. "^(?u)\w$" captures them also.
> Is there a possibility to restrict an expression like "\w" to "\w
> without [0-9_]"?

'(?u)[^\W0-9_]' removes 0-9_ from \w.

-Mark


Hello Mark,

haven't tried it yet, but it looks good!
@John: Sorry for being imprecise, I meant *letters*, not *characters*,
so requirement 2 fits my needs.


Note that \w matches alphanumeric Unicode characters.  If you only want 
letters, consider superscripts(¹²³), fractions (¼½¾), and other characters 
are also numbers to Unicode.  See the unicodedata.category function and 
http://www.unicode.org/Public/UNIDATA/UCD.html#General_Category_Values.


If you only want letters as considered by the Unicode standard, something 
this would give you only Unicode letters (it could be optimized to list 
ranges of characters):


u'(?u)[' + u''.join(unichr(n) for n in xrange(65536) if 
ud.category(unichr(n))[0]=='L') + u']'


Hmm, maybe Python 3.0 with its default Unicode strings needs a regex 
extension to specify the Unicode category to match.


-Mark


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


Re: regex for multiple whitespace-only lines

2009-04-12 Thread Mark Tolonen


"Phil Mayes"  wrote in message 
news:5.2.1.1.0.20090412194731.013d0...@olivebr.com...

I am trying to search for 1 or more empty or white-space-only lines.
Empty lines work:
>>> re.compile('(?m)(^$){1,2}')
<_sre.SRE_Pattern object at 0x010F9218>

One line with possible whitespace works:
>>> re.compile('(?m)(^\s*$)')
<_sre.SRE_Pattern object at 0x010F7860>

Multiple lines with possible whitespace don't:
>>> re.compile('(?m)(^\s*$)+')
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\lib\re.py", line 180, in compile
return _compile(pattern, flags)
  File "C:\Python25\lib\re.py", line 233, in _compile
raise error, v # invalid expression
error: nothing to repeat

Does anyone know a work-round?


PythonWin 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit 
(Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for 
further copyright information.

import re
data='hello\n\n\n\n\ngoodbye\n' # 4 blank lines
re.search('(?m)^\s*\n',data).group(0)

'\n\n\n\n'

Note that $ in MULTILINE mode matches just before a newline or the end of a 
string, so it won't include the last newline.  \s* is greedy and will match 
multiple lines.


-Mark


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


Re: Data Model:

2009-04-12 Thread Anthony
On Apr 12, 8:10 pm, Aaron Brady  wrote:
> On Apr 12, 9:14 pm, Anthony  wrote:
>
>
>
> > I'm struggling on whether or not to implement GroupItem (below) with
> > two separate models, or with one model that has a distinguishing key:
>
> > Given:
> > class ParentGroup:
> >     a group of values represented by class GroupItem
>
> > class ChildGroup:
> >     a group of values represented by class GroupItem
> >     foreign-key to ParentGroup (many Children sum to one Parent)
>
> > Option A:
> > class GroupItem:
> >     foreign-key to ParentGroup
> >     foreign-key to ChildGroup
> >     GroupItemType in (ParentItem, ChildItem)
> >     value
> >     value-type
>
> > Option B:
> > class ParentGroupItem
> >     foreign-key to ParentGroup
> >     value
> >     value-type
>
> > class ChildGroupItem
> >     foreign-key to ChildGroup
> >     value
> >     value-type
>
> > What are my considerations when making this decision?
>
> > Thanks!
>
> You want a ChildItem to have membership in two collections:
> ParentGroup and ChildGroup.  You also want a ParentItem to have
> membership in one collection.  For example:
>
> parentA: itemPA1, itemPA2, childA, childB
> childA: itemCA1, itemCA2
> childB: itemCB1, itemCB2
>
> Or, listing by child,
>
> itemPA1: parentA
> itemPA2: parentA
> itemCA1: childA
> itemCA2: childA
> itemCB1: childB
> itemCB2: childB
> childA: parentA
> childB: parentA
>
> Correct so far?

Thanks for the insightful response.

Yes, everything you say is correct, with one clarification:  The
ChildItem can be a member of ParentGroup OR ChildGroup, but never both
at the same time.
--
http://mail.python.org/mailman/listinfo/python-list


Re: video capture in Python ? (Tim Roberts)

2009-04-12 Thread Miles Lee
Hi,
You could try the python wrapper for OpenCV,
here is the link: http://code.google.com/p/ctypes-opencv/

Regards
Miles


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


Re: Python-list Digest, Vol 67, Issue 192

2009-04-12 Thread Aaron Brady
On Apr 12, 6:29 pm, Ryniek90  wrote:
> Chris Rebert pisze:
>
>
>
> > On Sun, Apr 12, 2009 at 3:38 PM, Ryniek90  wrote:
>
> >>> Paul Rubin 
> >>> Ryniek90  writes:
>
>  When i wanted to send an .iso file of 4GB length, i had traceback:
>  "OverflowError: requested number of bytes is more than a Python string
>  can hold"
>
> >>> You're not supposed to put the 4GB all in one string.  Open the
> >>> socket and send smaller packets through it.
>
> >>>  
>
> >> Ok, so i will split that data for smaller files. But i've still haven't got
> >> answer for question: "What's the max. length of string bytes which Python
> >> can hold?
>
> > sys.maxsize
> >     The largest positive integer supported by the platform’s
> > Py_ssize_t type, and thus the maximum size lists, strings, dicts, and
> > many other containers can have.
>
> > Cheers,
> > Chris
>
> Thanks. I've wanted to check very carefully what's up, and i found this:
> "strings (currently restricted to 2GiB)".
> It's here, in PEP #353 (PEP 0353
> ). Besides of this, i've found
> in sys module's docstring this:
>
> maxint = 2147483647
> maxunicode = 1114111
>
> Which when added gives us 2148597758.0 bytes, which are equal to
> 2049.0624980926514  MiB's.
> So files larger than 2049.06 MiB's should be splitted into smaller ones
> and sent partially.
> Correct me if i'm wrong.

Since socket.send doesn't guarantee the entire string is sent anyway,
why not just seek in your file to the position that has been sent so
far, then try the subsequent 1K or 10K bytes?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Data Model:

2009-04-12 Thread Aaron Brady
On Apr 12, 9:14 pm, Anthony  wrote:
> I'm struggling on whether or not to implement GroupItem (below) with
> two separate models, or with one model that has a distinguishing key:
>
> Given:
> class ParentGroup:
>     a group of values represented by class GroupItem
>
> class ChildGroup:
>     a group of values represented by class GroupItem
>     foreign-key to ParentGroup (many Children sum to one Parent)
>
> Option A:
> class GroupItem:
>     foreign-key to ParentGroup
>     foreign-key to ChildGroup
>     GroupItemType in (ParentItem, ChildItem)
>     value
>     value-type
>
> Option B:
> class ParentGroupItem
>     foreign-key to ParentGroup
>     value
>     value-type
>
> class ChildGroupItem
>     foreign-key to ChildGroup
>     value
>     value-type
>
> What are my considerations when making this decision?
>
> Thanks!

You want a ChildItem to have membership in two collections:
ParentGroup and ChildGroup.  You also want a ParentItem to have
membership in one collection.  For example:

parentA: itemPA1, itemPA2, childA, childB
childA: itemCA1, itemCA2
childB: itemCB1, itemCB2

Or, listing by child,

itemPA1: parentA
itemPA2: parentA
itemCA1: childA
itemCA2: childA
itemCB1: childB
itemCB2: childB
childA: parentA
childB: parentA

Correct so far?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Data Model:

2009-04-12 Thread Anthony
On Apr 12, 7:46 pm, Aaron Watters  wrote:
> On Apr 12, 10:14 pm, Anthony  wrote:
>
>
>
> > I'm struggling on whether or not to implement GroupItem (below) with
> > two separate models, or with one model that has a distinguishing key:
>
> > Given:
> > class ParentGroup:
> >     a group of values represented by class GroupItem
>
> > class ChildGroup:
> >     a group of values represented by class GroupItem
> >     foreign-key to ParentGroup (many Children sum to one Parent)
>
> > Option A:
> > class GroupItem:
> >     foreign-key to ParentGroup
> >     foreign-key to ChildGroup
> >     GroupItemType in (ParentItem, ChildItem)
> >     value
> >     value-type
>
> > Option B:
> > class ParentGroupItem
> >     foreign-key to ParentGroup
> >     value
> >     value-type
>
> > class ChildGroupItem
> >     foreign-key to ChildGroup
> >     value
> >     value-type
>
> > What are my considerations when making this decision?
>
> > Thanks!
>
> It looks to me that the two designs
> might be useful for different
> purposes.  What are you trying to do?
>
>   -- Aaron Watters
>
> 
> whiff.sourceforge.nethttp://aaron.oirt.rutgers.edu/myapp/root/misc/erdTest

The group values represent statistics that I'm tracking, based on
activity groups.  Some samples:

Group: Johnson, Total Units Produced = 10, Total Units Consumed = 5
Chris Johnson, Units Produced = 6, Units Consumed = 3
Jim Johnson, Units Produced = 4, Units Consumed = 2

Group: Smith, Total Units Produced = 15, Total Units Consumed = 8
Mark Smith, Units Produced = 7, Units Consumed = 5
Bob Smith, Units Produced = 8, Units Consumed = 3

The groups will be responsible for entering their own statistics, so I
will have to do some validation at data entry.  The ability to create
new statistic types (e.g. Units Broken) for new groups in the future
is important.

What would be the advantages of using option A versus option B?

Thanks for the quick response.
--
http://mail.python.org/mailman/listinfo/python-list


Overflow error (was Vol 67, Issue 192)

2009-04-12 Thread Dave Angel



Ryniek90 wrote:
Chris 
Rebert pisze:

On Sun, Apr 12, 2009 at 3:38 PM, Ryniek90  wrote:

Paul Rubin 
Ryniek90  writes:


When i wanted to send an .iso file of 4GB length, i had traceback:
"OverflowError: requested number of bytes is more than a Python 
string

can hold"


You're not supposed to put the 4GB all in one string. Open the
socket and send smaller packets through it.

 

Ok, so i will split that data for smaller files. But i've still 
haven't got
answer for question: "What's the max. length of string bytes which 
Python

can hold?


sys.maxsize
The largest positive integer supported by the platform’s
Py_ssize_t type, and thus the maximum size lists, strings, dicts, and
many other containers can have.

Cheers,
Chris



Thanks. I've wanted to check very carefully what's up, and i found 
this: "strings (currently restricted to 2GiB)".
It's here, in PEP #353 (PEP 0353 
). Besides of this, i've 
found in sys module's docstring this:


maxint = 2147483647
maxunicode = 1114111

Which when added gives us 2148597758.0 bytes, which are equal to 
2049.0624980926514 MiB's.
So files larger than 2049.06 MiB's should be splitted into smaller 
ones and sent partially.

Correct me if i'm wrong.




How much RAM is in your system? Unless it's at least 50 gb, in a 64bit 
OS, I'd keep my max chunk size to much smaller than 2gb. For a typical 
32bit system with 2 to 4gb of RAM, I'd probably chunk the file a meg or 
so at a time. Using large sizes is almost always a huge waste of resources.



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


Re: design question, metaclasses?

2009-04-12 Thread Aaron Brady
On Apr 12, 4:53 pm, Darren Dale  wrote:
> On Apr 12, 4:50 pm, Kay Schluehr  wrote:
>
>
>
> > On 11 Apr., 20:15, Darren Dale  wrote:
>
> > > I am working on a project that provides a high level interface to hdf5
> > > files by implementing a thin wrapper around h5py.
> > > I would like to
> > > generalize the project so the same API can be used with other formats,
> > > like netcdf or ascii files. The format specific code exists in File,
> > > Group and Dataset classes, which I could reimplement for each format.
> > > But there are other classes deriving from Group and Dataset which do
> > > not contain any format-specific code, and I would like to find a way
> > > to implement the functionality once and apply uniformly across
> > > supported formats.
>
> > Seems like you are doing it wrong. The classical OO approach is to add
> > more details / refining classes in subclasses instead of doing it the
> > other way round and derive the less specific classes from the more
> > specific ones.
>
> I think I am following the classical OO approach, refining details in
> subclasses. I just want a given subclass implementation describing a
> complex dataset to be able to work on top of multiple hierarchical
> file formats (like NetCDF or HDF5) by deriving from either NetCDF or
> HDF5 base classes that have an identical API. Those base classes
> encapsulate all the format-specific details, the subclasses allow a
> uniform image to be handled differently than a nonuniform image with a
> mask (for example). Maybe I should be delegating rather than
> subclassing.

You made me think of the 'mixin' design pattern, also known as
multiple inheritance.  The subclass derives from multiple classes.
One is the domain-specific description of the data class; the other is
the file format.

class NetCDF_DataA_Writer( NetCDF_Writer, DataA_Description ):
pass

The two classes can't have conflicting method names, however; or
you'll have to resolve them by hand, or assign them unique names.  In
this example, the subclass would favor the first super class, and you
could access the second super class with special names.  It kind of
makes uniform access unreliable.

class NetCDF_DataA_Writer( NetCDF_Writer, DataA_Description ):
data_write= DataA_Description.write
file_write= NetCDF_Writer.write

The standard library has a couple of examples of this, as I recall,
such as the UDPServerMixin (sp).
--
http://mail.python.org/mailman/listinfo/python-list


regex for multiple whitespace-only lines

2009-04-12 Thread Phil Mayes

I am trying to search for 1 or more empty or white-space-only lines.
Empty lines work:
>>> re.compile('(?m)(^$){1,2}')
<_sre.SRE_Pattern object at 0x010F9218>

One line with possible whitespace works:
>>> re.compile('(?m)(^\s*$)')
<_sre.SRE_Pattern object at 0x010F7860>

Multiple lines with possible whitespace don't:
>>> re.compile('(?m)(^\s*$)+')
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\lib\re.py", line 180, in compile
return _compile(pattern, flags)
  File "C:\Python25\lib\re.py", line 233, in _compile
raise error, v # invalid expression
error: nothing to repeat

Does anyone know a work-round?

Thanks,
Phil

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


Re: Data Model:

2009-04-12 Thread Aaron Watters
On Apr 12, 10:14 pm, Anthony  wrote:
> I'm struggling on whether or not to implement GroupItem (below) with
> two separate models, or with one model that has a distinguishing key:
>
> Given:
> class ParentGroup:
>     a group of values represented by class GroupItem
>
> class ChildGroup:
>     a group of values represented by class GroupItem
>     foreign-key to ParentGroup (many Children sum to one Parent)
>
> Option A:
> class GroupItem:
>     foreign-key to ParentGroup
>     foreign-key to ChildGroup
>     GroupItemType in (ParentItem, ChildItem)
>     value
>     value-type
>
> Option B:
> class ParentGroupItem
>     foreign-key to ParentGroup
>     value
>     value-type
>
> class ChildGroupItem
>     foreign-key to ChildGroup
>     value
>     value-type
>
> What are my considerations when making this decision?
>
> Thanks!

It looks to me that the two designs
might be useful for different
purposes.  What are you trying to do?

  -- Aaron Watters


whiff.sourceforge.net
http://aaron.oirt.rutgers.edu/myapp/root/misc/erdTest

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


Re: video capture in Python ?

2009-04-12 Thread Tim Roberts
Stef Mientki  wrote:
>
>has anyone got video capturing (from a webcam)  successful  running
>in a wxPython application under winXP ?
>
>I got some links, but it seems quit complicated to get everything 
>installed,
>and all the packages you need don't describe which versions of the other 
>parts you need,
>so a lot trial and error I expect.

What packages have you tried?

Video capture is not a trivial task.  There is a lot to do, although most
of it is boilerplate that you will reuse from app to app.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


Data Model:

2009-04-12 Thread Anthony
I'm struggling on whether or not to implement GroupItem (below) with
two separate models, or with one model that has a distinguishing key:

Given:
class ParentGroup:
a group of values represented by class GroupItem

class ChildGroup:
a group of values represented by class GroupItem
foreign-key to ParentGroup (many Children sum to one Parent)

Option A:
class GroupItem:
foreign-key to ParentGroup
foreign-key to ChildGroup
GroupItemType in (ParentItem, ChildItem)
value
value-type

Option B:
class ParentGroupItem
foreign-key to ParentGroup
value
value-type

class ChildGroupItem
foreign-key to ChildGroup
value
value-type

What are my considerations when making this decision?

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


Ann: WHIFF -- WSGI/HTTP INTEGRATED FILESYSTEM FRAMES

2009-04-12 Thread Aaron Watters
announcing:

WHIFF -- WSGI/HTTP INTEGRATED
 FILESYSTEM FRAMES

WHIFF is an infrastructure for easily building
complex Python/WSGI Web applications by
combining smaller and simpler WSGI components
organized within file system trees.

To read about WHIFF view the WHIFF documentation at

http://aaron.oirt.rutgers.edu/myapp/docs/W.intro.

To PLAY WITH WHIFF try the demos listed in the demos page at

http://aaron.oirt.rutgers.edu/myapp/docs/W1300.testAndDemo.

To DOWNLOAD WHIFF go to the WHIFF project
information page at

http://sourceforge.net/projects/whiff

and follow the download instructions.

To get the latest clone the
WHIFF Mercurial repository located at

http://aaron.oirt.rutgers.edu/cgi-bin/whiffRepo.cgi.

Why WHIFF?
==
WHIFF (WSGI HTTP Integrated Filesystem Frames)
is intended to make it easier to create, deploy,
and maintain large and complex Python based WSGI
Web applications. I created WHIFF to address
complexity issues I encounter when creating and
fixing sophisticated Web applications which
include complex database interactions
and dynamic features such as AJAX
(Asynchronous JavaScript and XML).

The primary tools which reduce complexity are
an infrastructure for managing web application
name spaces, a configuration template language
for wiring named components into an application,
and an applications programmer interface for
accessing named components from Python and
javascript modules.

All supporting conventions and tools offered by
WHIFF are optional. WHIFF is designed to work well
with other modules conformant to the
WSGI (Web Service Gateway Interface) standard.
Developers and designers are free to use those
WHIFF tools that work for them and ignore or
replace the others.

WHIFF does not provide a "packaged cake mix"
for baking a web application.
Instead WHIFF is designed to provide a set of
ingredients which can be easily combined to make
web applications (with no need to refine your own
sugar or mill your own wheat).

I hope you like it.  -- Aaron Watters

===
Why is a giraffe's neck so long?
Because its head is so far from its body!

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


Re: How to create a virtual serial port?

2009-04-12 Thread Grant Edwards
On 2009-04-13, Lawrence D'Oliveiro  wrote:
> In message , Grant 
> Edwards wrote:
>
>> On Linux: no.
>
> What about this .

It's a kernel-mode serial driver that talks to an
Ethernet-attached device server.  We were talking about an
application in user-space creating a virtual serial port
weren't we?

If the question was can one write a kernel-mode serial driver
for Unix systems (e.g. Linux), then the answer is obviously
"yes". There's no doubt you can implement a serial device if
you write a serial driver kernel module: there are dozens of
such drivers.

Most of them talk to UARTs on a local ISA or PCI bus boards.
There are also quite a few that talk to USB-attached hardware,
and there are probably at least a half-dozen companies that
sell Ethernet-attached serial hardware with Linux drivers. (I
maintained an Ethernet-attached serial driver for one of those
vendors for many years).

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


Re: OverflowError while sending large file via socket

2009-04-12 Thread Benjamin Peterson
Steven D'Aprano  REMOVE-THIS-cybersource.com.au> writes:
> 
> which suggests to me that it will be implementation dependent

The length of sequences is constrained by sys.maxsize
(and no, you can't change it).




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


Login to Drupal 6 site using pycurl

2009-04-12 Thread David

I can not get this to work;

#!/usr/bin/python
import urllib
import pycurl

user_agent = 'Mozilla/4.0 (compatible: MSIE 6.0)'
crl = pycurl.Curl()
crl.setopt(pycurl.URL, 'http://beta.gentooligans.com')
crl.setopt(pycurl.HEADER, 1)
crl.setopt(pycurl.USERAGENT, user_agent)
crl.setopt(pycurl.FOLLOWLOCATION, 1)
crl.setopt(pycurl.COOKIEFILE, '/tmp/cookie.txt')
crl.setopt(pycurl.COOKIEJAR, '/tmp/cookie.txt')
login = [('name', 'username'),
('pass', 'password'),
('form-id', 'user_login'),
('op', 'Log in')]
login_data = urllib.urlencode(login)
crl.setopt(pycurl.POSTFIELDS, login_data)
crl.setopt(pycurl.URL, 'http://beta.gentooligans.com/user/login')
crl.perform()
crl.close()

I tried it like this also;

('edit[name]', user),
('edit[pass]', password),


output

[snip]

  
action="/user/login?destination=user%2Flogin"  accept-charset="UTF-8" 
method="post" id="user-login-form">


 Username: title="This field is required.">*
 size="15" value="" class="form-text required" />



 Password: title="This field is required.">*
 size="15"  class="form-text required" />


class="form-submit" />


[snip]

Here are some guides I used;
http://code.activestate.com/recipes/267255/
http://drupal.org/node/87711

thanks,
-david

--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: OverflowError while sending large file via socket

2009-04-12 Thread Jean-Paul Calderone

On 13 Apr 2009 01:45:56 GMT, Steven D'Aprano 
 wrote:

On Mon, 13 Apr 2009 00:21:34 +0200, Ryniek90 wrote:


When i wanted to send an .iso file of 4GB length, i had traceback:
"OverflowError: requested number of bytes is more than a Python string
can hold"

Sockets are being used in every network app, i.e: p2p progs (like
BitTorrent), and exchanged data is often bigger than 4GB.


But they don't transfer the entire file as ONE packet. Split your data
into smaller packets. I don't know what a good size for each packet would
be, but if I were doing this, I'd probably start with 4096 or 8192
*bytes*.


Everything in that paragraph is true, but it's rather misleading.  The
size of the packets you send is *not* determined by the length of the
string you pass to socket.send.  Packet size in a TCP connection is
determined by various things beyond the control of an application using
the BSD socket API.

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


Re: OverflowError while sending large file via socket

2009-04-12 Thread Steven D'Aprano
On Mon, 13 Apr 2009 00:21:34 +0200, Ryniek90 wrote:

> When i wanted to send an .iso file of 4GB length, i had traceback:
> "OverflowError: requested number of bytes is more than a Python string
> can hold"
> 
> Sockets are being used in every network app, i.e: p2p progs (like
> BitTorrent), and exchanged data is often bigger than 4GB.

But they don't transfer the entire file as ONE packet. Split your data 
into smaller packets. I don't know what a good size for each packet would 
be, but if I were doing this, I'd probably start with 4096 or 8192 
*bytes*.

http://www.amk.ca/python/howto/sockets/


> So why i've 
> had that Traceback? How many number of bytes Python string can hold?

The documentation doesn't seem to specify a maximum string length:

http://docs.python.org/library/stdtypes.html

which suggests to me that it will be implementation dependent. However, 
I'd guess that the current CPython implementation will have a hard limit 
of 2**32 bytes (4GB), and a soft limit on the amount of memory that you 
have. You're trying to create a single, continuous block of memory 4GB in 
size! Unless you've got *at least* 4GB of RAM, this is impossible even in 
principle, and in practice you need more than that to allow for the 
overhead of the operating system, Python, and any other applications you 
have running.



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


ANN: PyGUI 2.0.1

2009-04-12 Thread Greg Ewing

PyGUI 2.0.1 is available:

  http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/

Fixes some problems in setup.py affecting installation
on Linux and Windows.


What is PyGUI?
--

PyGUI is a cross-platform GUI toolkit designed to be lightweight
and have a highly Pythonic API.

--
Gregory Ewing
greg.ew...@canterbury.ac.nz
http://www.cosc.canterbury.ac.nz/greg.ewing/
--
http://mail.python.org/mailman/listinfo/python-list


choosing background color in tkfiledialog.askopenfile()

2009-04-12 Thread Soumen banerjee
Hello,
I want to know how i can choose a background color in
tkfiledialog.askopenfilename()?
Due to a certain theme i am using, i cant see any text and changing
the theme settings dont seem to affect the file dialog
--
http://mail.python.org/mailman/listinfo/python-list


Re: win32 wins settings

2009-04-12 Thread Aahz
In article ,
Toff   wrote:
>
>I don't understand why this doesn't woks.

In what way is it not working?  BTW, you should look up "raw strings".

See also http://www.catb.org/~esr/faqs/smart-questions.html
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a virtual serial port?

2009-04-12 Thread Lawrence D'Oliveiro
In message , Grant 
Edwards wrote:

> On Linux: no.

What about this .

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


Re: Python-list Digest, Vol 67, Issue 192

2009-04-12 Thread Ryniek90

Chris Rebert pisze:

On Sun, Apr 12, 2009 at 3:38 PM, Ryniek90  wrote:
  

Paul Rubin 
Ryniek90  writes:

  

When i wanted to send an .iso file of 4GB length, i had traceback:
"OverflowError: requested number of bytes is more than a Python string
can hold"



You're not supposed to put the 4GB all in one string.  Open the
socket and send smaller packets through it.

 
  

Ok, so i will split that data for smaller files. But i've still haven't got
answer for question: "What's the max. length of string bytes which Python
can hold?



sys.maxsize
The largest positive integer supported by the platform’s
Py_ssize_t type, and thus the maximum size lists, strings, dicts, and
many other containers can have.

Cheers,
Chris

  


Thanks. I've wanted to check very carefully what's up, and i found this: 
"strings (currently restricted to 2GiB)".
It's here, in PEP #353 (PEP 0353 
). Besides of this, i've found 
in sys module's docstring this:


maxint = 2147483647
maxunicode = 1114111

Which when added gives us 2148597758.0 bytes, which are equal to 
2049.0624980926514  MiB's.
So files larger than 2049.06 MiB's should be splitted into smaller ones 
and sent partially.

Correct me if i'm wrong.

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


Re: Regex similar to "^(?u)\w$", but without digits?

2009-04-12 Thread Andreas Pfrengle
On 12 Apr., 02:31, "Mark Tolonen"  wrote:
> "Andreas"  wrote in message
>
> news:f953c845-3660-4bb5-8ba7-00b93989c...@b1g2000vbc.googlegroups.com...
>
> > Hello,
>
> > I'd like to create a regex that captures any unicode character, but
> > not the underscore and the digits 0-9. "^(?u)\w$" captures them also.
> > Is there a possibility to restrict an expression like "\w" to "\w
> > without [0-9_]"?
>
> '(?u)[^\W0-9_]' removes 0-9_ from \w.
>
> -Mark

Hello Mark,

haven't tried it yet, but it looks good!
@John: Sorry for being imprecise, I meant *letters*, not *characters*,
so requirement 2 fits my needs.

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


Re: Regex similar to "^(?u)\w$", but without digits?

2009-04-12 Thread Andreas Pfrengle
On 12 Apr., 02:31, "Mark Tolonen"  wrote:
> "Andreas"  wrote in message
>
> news:f953c845-3660-4bb5-8ba7-00b93989c...@b1g2000vbc.googlegroups.com...
>
> > Hello,
>
> > I'd like to create a regex that captures any unicode character, but
> > not the underscore and the digits 0-9. "^(?u)\w$" captures them also.
> > Is there a possibility to restrict an expression like "\w" to "\w
> > without [0-9_]"?
>
> '(?u)[^\W0-9_]' removes 0-9_ from \w.
>
> -Mark

Hello Mark,

haven't tried it yet, but it looks good!
@John: Sorry for being imprecise, I meant *letters*, not *characters*,
so requirement 2 fits my needs.

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


Re: PyHeapTypeObject

2009-04-12 Thread Benjamin Peterson
Brendan Miller  catphive.net> writes:

> 
> What's the point of PyHeapTypeObject in Include/object.h? Why does the
> layout of object types need to be different on the heap vs statically
> allocated?

Heap types have to do some extra book-keeping like garbage collection.




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


Re: Python-list Digest, Vol 67, Issue 192

2009-04-12 Thread Chris Rebert
On Sun, Apr 12, 2009 at 3:38 PM, Ryniek90  wrote:
>> Paul Rubin 
>> Ryniek90  writes:
>>
>>>
>>> When i wanted to send an .iso file of 4GB length, i had traceback:
>>> "OverflowError: requested number of bytes is more than a Python string
>>> can hold"
>>>
>>
>> You're not supposed to put the 4GB all in one string.  Open the
>> socket and send smaller packets through it.
>>
>>  
>
> Ok, so i will split that data for smaller files. But i've still haven't got
> answer for question: "What's the max. length of string bytes which Python
> can hold?

sys.maxsize
The largest positive integer supported by the platform’s
Py_ssize_t type, and thus the maximum size lists, strings, dicts, and
many other containers can have.

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python-list Digest, Vol 67, Issue 192

2009-04-12 Thread Ryniek90





Temat:
Re: OverflowError while sending large file via socket
Od:
Paul Rubin 
Data:
12 Apr 2009 15:28:43 -0700
Do:
python-list@python.org

Do:
python-list@python.org


Ryniek90  writes:
  

When i wanted to send an .iso file of 4GB length, i had traceback:
"OverflowError: requested number of bytes is more than a Python string
can hold"



You're not supposed to put the 4GB all in one string.  Open the
socket and send smaller packets through it.

  

Ok, so i will split that data for smaller files. But i've still haven't 
got answer for question: "What's the max. length of string bytes which 
Python can hold?

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


Pydev 1.4.5 Released

2009-04-12 Thread Fabio Zadrozny
Hi All,

Pydev and Pydev Extensions 1.4.5 have been released

Details on Pydev Extensions: http://www.fabioz.com/pydev
Details on Pydev: http://pydev.sf.net
Details on its development: http://pydev.blogspot.com

Release Highlights in Pydev Extensions:
-

* Derived resources are no longer analyzed
* No longer giving spurios warnings when file being analyzed is removed


Release Highlights in Pydev:
--

* Better error handling in the grammar
* Code Formatter
  o Can be applied from context menu (recursively applied for folders)
  o Can trim whitespaces from the end of the lines
  o Can add new a line to the end of the file
  o Can automatically apply code-formatting on save
  o Fixed issues with unary operators and exponential
  o Fixed issues where parenthesis was lost if no closing
parenthesis was available
* Python 3.0
  o Parser supporting unicode identifiers
  o Star expr recognized
* Python 3.1 version acknowledged (and proper grammar used)
* Pydev package explorer
  o Can show working sets as top-level elements
  o Folders without __init__.py are no longer shown as packages
* Interactive console
  o When waiting for user input, the prompt is not shown
  o Console initial commands compatible with Python 3.0
  o Timeout for starting console communication while the shell is
not fully initilized
  o More info is available if connection fails
* Alt+R working (mnemonics set for pydev contributed menus)
* With Ctrl+2, matches will no longer take into acount the case
* Code completion: Can get args from docstring when '*' is present
* Better heuristics for automatic insertion of "self" and "import"
* Fixed problem configuring external jars and zip files
* Launch getting interpreter from project on default config
* After a parenthesis, 'n' indentation levels may be applied (patch by
Radim Kubacki)
* .pyc files are now marked as derived (note that this will only
happen when they're changed)
* Fixed debugger issue with Jython 2.5b3
* Jython: completions working for static members access
* Hover works on Eclipse 3.2



What is PyDev?
---

PyDev is a plugin that enables users to use Eclipse for Python and
Jython development -- making Eclipse a first class Python IDE -- It
comes with many goodies such as code completion, syntax highlighting,
syntax analysis, refactor, debug and many others.


Cheers,

-- 
Fabio Zadrozny
--
Software Developer

Aptana
http://aptana.com/python

Pydev Extensions
http://www.fabioz.com/pydev

Pydev - Python Development Enviroment for Eclipse
http://pydev.sf.net
http://pydev.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: OverflowError while sending large file via socket

2009-04-12 Thread Paul Rubin
Ryniek90  writes:
> When i wanted to send an .iso file of 4GB length, i had traceback:
> "OverflowError: requested number of bytes is more than a Python string
> can hold"

You're not supposed to put the 4GB all in one string.  Open the
socket and send smaller packets through it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: GUI Programming

2009-04-12 Thread Python


On 12 apr 2009, at 15:07, Gabriel wrote:


Hello,

I'm python newbie and i need to write gui for my school work in  
python.

I need to write it really quick, because i haven't much time .)
So question is, which of gui toolkits should i pick and learn? I  
heard PyGTK and Glade are best for quick gui programming? Is it good  
for beginner? Or is there something better?

I have some experience with java swing, btw..
--
http://mail.python.org/mailman/listinfo/python-list



I chose wxpython because of it's excellent crossplatform compatibility
it has a native look on all platforms too and no commercial license
whatsoever...

there are a huge lot of examples oonline f almost anything you want
ideal for schoolwork ;)

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


OverflowError while sending large file via socket

2009-04-12 Thread Ryniek90

When i wanted to send an .iso file of 4GB length, i had traceback:
"OverflowError: requested number of bytes is more than a Python string 
can hold"


Sockets are being used in every network app, i.e: p2p progs (like 
BitTorrent), and exchanged data is often bigger than 4GB. So why i've 
had that Traceback? How many number of bytes Python string can hold?

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


Re: Extracting zip files containing directories with ZipFile

2009-04-12 Thread Márcio Faustino
On Apr 12, 5:01 pm, MRAB  wrote:
> Strictly speaking, zip files don't contain nested folders, but only a
> flat list of files.

Didn't know that, thanks.

On Apr 12, 8:01 pm, "Martin v. Löwis"  wrote:
> That depends on the version of Python you are using. 
> Seehttp://bugs.python.org/4710

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


Re: named pipe and Linux

2009-04-12 Thread Cameron Simpson
On 08Apr2009 16:13, Thomas Bellman  wrote:
| Cameron Simpson  wrote:
| > On 07Apr2009 10:08, akineko  wrote:
| >| I'm trying to use named pipes to fuse a Python program and a C
| >| program.
| >| One side creates pipes using os.mkfifo() and both sides use the same
| >| named pipes (one side reads, another side writes). The read side uses
| >| select.select() to wait for incoming messages and read the message
| >| when select.select() says it is ready.
| >| The length of the message is unknown to the read side.
| 
| > That's a serious flaw in the message protocol.
[...]
| > No!
| > You should use os.read() with the maximum size of a message.
| > It _should_ return with the number of bytes in the message, provided the
| > C program writes messages with a single OS-level write() call.
| 
| No!
| 
| That's still broken.  You can't know if the writer has managed to
| write one or several messages onto the pipe.

I did think of that. It's an insurmountable problem, which is why he
needs a message length in the protocol, or other way to recognise
message boundaries. akineko has said elsewhere that's he's chasing that
side of things.

The os.read() thing simply avoids the tedium of polling; you can block
on the read without the annoyance of file.read() blocking waiting for
"too much" data.

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

The Government that robs Peter to pay Paul, can always count on Paul for
support.- George Bernard Shaw
--
http://mail.python.org/mailman/listinfo/python-list


Re: design question, metaclasses?

2009-04-12 Thread Darren Dale
On Apr 12, 4:50 pm, Kay Schluehr  wrote:
> On 11 Apr., 20:15, Darren Dale  wrote:
>
> > I am working on a project that provides a high level interface to hdf5
> > files by implementing a thin wrapper around h5py.
> > I would like to
> > generalize the project so the same API can be used with other formats,
> > like netcdf or ascii files. The format specific code exists in File,
> > Group and Dataset classes, which I could reimplement for each format.
> > But there are other classes deriving from Group and Dataset which do
> > not contain any format-specific code, and I would like to find a way
> > to implement the functionality once and apply uniformly across
> > supported formats.
>
> Seems like you are doing it wrong. The classical OO approach is to add
> more details / refining classes in subclasses instead of doing it the
> other way round and derive the less specific classes from the more
> specific ones.

I think I am following the classical OO approach, refining details in
subclasses. I just want a given subclass implementation describing a
complex dataset to be able to work on top of multiple hierarchical
file formats (like NetCDF or HDF5) by deriving from either NetCDF or
HDF5 base classes that have an identical API. Those base classes
encapsulate all the format-specific details, the subclasses allow a
uniform image to be handled differently than a nonuniform image with a
mask (for example). Maybe I should be delegating rather than
subclassing.
--
http://mail.python.org/mailman/listinfo/python-list


Re: save error with embedded python 2.5

2009-04-12 Thread Gabriel Genellina
En Fri, 10 Apr 2009 09:40:11 -0300, eric_dex...@msn.com  
 escribió:



I seem to be getting one extra value when I create my values with

for yy in range(1, maxy):
  for xx in range(1, maxx):
count = count + 1
#squaret.append[count]
squaret.append( square(xx * 20, yy * 20))


yy goes from 1 to maxy-1, xx goes from 1 to maxx-1, there are  
(maxy-1)*(maxx-1) squares generated. Is that what you want?



and saving boxes with

if squaresave.state == '1':
  outfile = open('grid.dat','w')
  count = 0
  for s in squaret:
count = count + 1
outfile.write(s.state)
outfile.write(' ')
if count == maxy:
  outfile.write('\n')
  count = 0


Here, you're counting from 1 to maxy inclusive.

Why don't you use the same two-loop design both times? Or better, use a  
bidimensional data structure (like a list of lists, or the numpy library  
if you're mostly concerned with numbers).


--
Gabriel Genellina

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


Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Duncan Booth
Duncan Booth  wrote:

> John Posner  wrote:
> 
>> Do know what in the itertools implementation causes adding a 'if p <=
>> sqrt(n)' clause to *decrease* performance, while adding a
>> 'takewhile()' clause *increases* performance? 
> 
> I haven't timed it, but I would guess that the takewhile was faster 
> only because the sqrt(n) had been factored out of the loop. Try the 
> original loop again precalculating the sqrt(n) and see how that compares.
> 
Which of course is rubbish, extracting the sdqrt will have an effect but 
the main factor is that takewhile exits the loop as soon as the condition 
is false whereas a conditional in a generator comprehension doesn't stop 
the loop continuing to the end.
--
http://mail.python.org/mailman/listinfo/python-list


Re: design question, metaclasses?

2009-04-12 Thread Kay Schluehr
On 11 Apr., 20:15, Darren Dale  wrote:

> I am working on a project that provides a high level interface to hdf5
> files by implementing a thin wrapper around h5py.
> I would like to
> generalize the project so the same API can be used with other formats,
> like netcdf or ascii files. The format specific code exists in File,
> Group and Dataset classes, which I could reimplement for each format.
> But there are other classes deriving from Group and Dataset which do
> not contain any format-specific code, and I would like to find a way
> to implement the functionality once and apply uniformly across
> supported formats.

Seems like you are doing it wrong. The classical OO approach is to add
more details / refining classes in subclasses instead of doing it the
other way round and derive the less specific classes from the more
specific ones.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Recommendations on Pythonic tree data structure design techniques

2009-04-12 Thread Gabriel Genellina

En Thu, 09 Apr 2009 13:18:27 -0300,  escribió:


Any recommendations on Python based tree data structures that I
can study? I'm working on an application that will model a basic
outline structure (simple tree) and am looking for ideas on
Pythonic implementation techniques.


I'd use ElementTree. An Element is a generic container for hierarchical  
data - doesn't have to be XML.


See http://effbot.org/zone/element-index.htm and  
http://docs.python.org/library/xml.etree.elementtree.html


--
Gabriel Genellina

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


Re: Reading only headers

2009-04-12 Thread Gabriel Genellina
En Thu, 09 Apr 2009 07:13:08 -0300, S.Selvam   
escribió:


I want to read headers from web page and check whether its Content-Type  
is

xml or not.I used the following code
 ...
 request = urllib2.Request(url, None, USER_AGENT)
 opener = urllib2.build_opener()
 datastream = opener.open(request)
 if datastream.headers.get('Content-Type','').find('xml') == -1:
raise "Its not xml!"
 else:
"""
  Read the content and process
"""
...

Is this the good way to read headers ? ,as i do not want the content  
,unless

it is xml.
Please suggest me,if there are some other good methods to read only the
headers.


The best way is to issue an HTTP HEAD request -- so only the headers are  
sent. Search this newsgroup for some ways of doing that. Ok, see this  
posts:


http://groups.google.com/group/comp.lang.python/t/b1060b62b2f12a04/
http://groups.google.com/group/comp.lang.python/t/bbac82df3d64d48e/

--
Gabriel Genellina

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


Re: moving to processing from threading, global variables

2009-04-12 Thread Dave Angel

rkmr...@gmail.com wrote:

no one? how can i share variables in processing?
please help out!

On Thu, Apr 9, 2009 at 8:05 AM, rkmr...@gmail.com  wrote:

  

hi
i am trying to move from threading to processing package. this is the
controller i used to spawn new threads, and it used the global
variable done to check if it needs to spawn more threads or not. it worked
great for me. it checks if there is new data to be processed every
30 seconds, and spawns of new threads to do the work, till all work is
done.


but now in processing, anychange made to global variable done in sendalert
method, is not reflected in the controller method.

can please point out what is wrong?
thanks a lot!


code


done = False
def sendalert():
   global done
   users = q.get('xx')
   if not users:
 done = True
 return

   done = False
   for u in users:
do stuff


def control(number_threads_min=3, number_threads_max = 100):
global done
while True:
number_threads = len(processing.activeChildren())
if not done and number_threads
Some of the discussion below varies between operating systems, 
especially historically.  For example., 16 bit Windows (say version 3.1 
for example) didn't have separate address spaces or threads.  And 
certainly some of the capabilities vary by programming language.


There are several different models of two routines running 
"simultaneously" :
  1) co-routines - closest thing that Python has is generators, where 
the generator runs for a while, then yields up some time to its caller.  
Co-routines share everything, but they can be a pain to use in the 
general case.
  2) threads - these run in the same process (and address space), and 
share variables, file handles, etc.  The only real distinction between 
the threads is scheduling.
  3) processes - these run in separate address spaces.  By default 
nothing is shared between them.  But there are OS functions that allow 
them to share pretty intimately.
  4) computers - these run on separate computers.  They communicate 
only by I/O operations, such as pipes, sockets, etc.


There are reasons in many environments to switch from threads to 
processes.  Most common is that something you need done is already 
encapsulated in an executable, so you spawn that as a separte process.


Another reason is that some library the two tasks use is not threadsafe, 
so you don't dare have both operations in the same process.  Simplest 
example of that is GUI programming, where most of the GUI code must run 
in a single thread, with a single event loop.


But now to your question.  When you have two separate processes, you can 
share by various means the OS provides.  Simplest is to use the command 
line to pass parameters to the child process, and the return code to 
pass a  (int) value back.  Next easiest is to simply write data to a 
file that you can both see.  There are ways to lock the file so you're 
not both updating it at the same time.  Next after that is a pipe (or 
socket, which is somewhat more general).  And finally shared memory.  
Shared memory is the easiest to conceptualize.  Basically you both map a 
hunk of shared memory and take turns reading and writing it.  But as 
with all the others, you have to be careful about concurrent or 
overlapping updates.  For example, if process A is changing string (not 
Python string class, but a similar abstraction) from "Howard" to 
"Gloria".  You don't want process B to fetch the value "Hooria" by mistake.


Generally, you want to do the simplest sharing that can solve the need.  
In your case, if 'done' was the only global,  I'd recommend using a 
presence or absence of a file (s) as your communications method.  
Process A writes it or deletes it, and Process B checks for its 
existence, and maybe its timestamp.  As for the filename, you can pass 
that on the command line when you start the second process.



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


Re: design question, metaclasses?

2009-04-12 Thread Darren Dale
On Apr 12, 3:23 pm, Aaron Brady  wrote:
> On Apr 12, 1:30 pm, Darren Dale  wrote:
>
>
>
> > On Apr 11, 2:15 pm, Darren Dale  wrote:
>
> _
>
> > > format1.Group # implementation of group in format1
> > > format2.Group # ...
> > > Base.DerivedGroup # base implementation of DerivedGroup, not directly
> > > useful
> > > format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful
> > > format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful
>
> _
>
> > class Group1(object):
>
> >     def origin(self):
> >         return "Group1"
>
> > class Group2(object):
>
> >     def origin(self):
> >         return "Group2"
>
> > def _SubGroup(superclass):
>
> >     class SubGroup(superclass):
> >         pass
>
> >     return SubGroup
>
> > SubGroup = _SubGroup(Group2)
> > sub_group = SubGroup()
>
> > print sub_group.origin()
>
> You can create new types in one statement:
>
> SubGroup= type( "SubGroup", ( BaseGroup, ), { } )

But how can I implement the *instance* behavior of SubGroup with this
example? In my original example:

format1.Group # implementation of group in format1
format2.Group # implementation of group in format2
Base.DerivedGroup # base implementation of DerivedGroup, must subclass
a group
format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful
format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful

I'm trying to achieve uniform behavior of my derived groups across
supported formats. My derived groups are abstracted such that they do
not need to be reimplemented for each format, I only need to implement
Group for each format. This is a real mind bender for me, even my
factory function gets hairy because I have additional classes that
derive from DerivedGroup. Maybe what I need is the ability to provide
context at import time, is that possible?
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a list contains an item

2009-04-12 Thread Tim Chase

online.service@gmail.com wrote:

python doesn't have a list.contains()   method?


Because it doesn't need one?

  >>> x = [1,2,3,4]
  >>> 2 in x
  True
  >>> 42 in x
  False


pedantic side-note, objects can have a __contains__ method which 
is used under the covers for "in" testing, so your custom objects 
can also test for containment.


-tkc



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


Re: How to check if a list contains an item

2009-04-12 Thread Peter Otten
online.service@gmail.com wrote:

> python doesn't have a list.contains()   method?

It has. It's called list.__contains__() and used indirectly through the 'in'
operator:

>>> "red" in ["red", "green", "blue"]
True
>>> "yellow" in ["red", "green", "blue"]
False

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


Re: How to check if a list contains an item

2009-04-12 Thread MRAB

online.service@gmail.com wrote:

python doesn't have a list.contains()   method?


It does have a __contains__ method, but that's not called directly. Use
"item in my_list" instead (and the opposite is "item not in my_list").
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a list contains an item

2009-04-12 Thread Esmail

online.service@gmail.com wrote:

python doesn't have a list.contains()   method?



How about the 'in' operator?

In [1]: l1=['aa', 'bb', 'cc']

In [2]: 'aa' in l1
Out[2]: True

In [3]: 'ab' in l1
Out[3]: False

In [4]: 'cc' in l1
Out[4]: True

Will this help?

Esmail


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


Re: design question, metaclasses?

2009-04-12 Thread Aaron Brady
On Apr 12, 1:30 pm, Darren Dale  wrote:
> On Apr 11, 2:15 pm, Darren Dale  wrote:
>
>
_
>
> > format1.Group # implementation of group in format1
> > format2.Group # ...
> > Base.DerivedGroup # base implementation of DerivedGroup, not directly
> > useful
> > format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful
> > format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful
>
_
>
> class Group1(object):
>
>     def origin(self):
>         return "Group1"
>
> class Group2(object):
>
>     def origin(self):
>         return "Group2"
>
> def _SubGroup(superclass):
>
>     class SubGroup(superclass):
>         pass
>
>     return SubGroup
>
> SubGroup = _SubGroup(Group2)
> sub_group = SubGroup()
>
> print sub_group.origin()

You can create new types in one statement:

SubGroup= type( "SubGroup", ( BaseGroup, ), { } )
--
http://mail.python.org/mailman/listinfo/python-list


How to check if a list contains an item

2009-04-12 Thread online . service . com
python doesn't have a list.contains()   method?


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


Re: Extracting zip files containing directories with ZipFile

2009-04-12 Thread Martin v. Löwis
> Does the ZipFile class correctly handles directories contained within
> zip files?

That depends on the version of Python you are using. See
http://bugs.python.org/4710

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


Re: GUI Programming

2009-04-12 Thread Emmanuel Surleau
Howdy,

> I'm python newbie and i need to write gui for my school work in python.
> I need to write it really quick, because i haven't much time .)
> So question is, which of gui toolkits should i pick and learn? I heard
> PyGTK and Glade are best for quick gui programming? Is it good for
> beginner? Or is there something better?
> I have some experience with java swing, btw..

You might want to have a look at PyQt4 
(http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html) if you 
don't want something "ugly" (native look on all platforms). Also, it has 
excellent documentation.

You can use Qt Designer with it, additionally.

Cheers,

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


Re: moving to processing from threading, global variables

2009-04-12 Thread MRAB

rkmr...@gmail.com wrote:

no one?


It's Easter! :-)


how can i share variables in processing?
please help out!


Threads run in the same address space, so they can share variables;
processes run in different address spaces, so they can't (normally).

You'll need to read the documentation carefully to see what the
recommended techniques are (and I doubt that sharing a global Boolean
variable is one of them...).

On Thu, Apr 9, 2009 at 8:05 AM, rkmr...@gmail.com 
 > wrote:


hi
i am trying to move from threading to processing package. this is
the controller i used to spawn new threads, and it used the global
variable done to check if it needs to spawn more threads or not. it
worked great for me. it checks if there is new data to be processed
every
30 seconds, and spawns of new threads to do the work, till all work
is done.
 
 
but now in processing, anychange made to global variable done in

sendalert method, is not reflected in the controller method.
 
can please point out what is wrong?

thanks a lot!
 
 
code
 
 
done = False

def sendalert():
   global done
   users = q.get('xx')
   if not users:
 done = True
 return
 
   done = False

   for u in users:
do stuff
 
 
def control(number_threads_min=3, number_threads_max = 100):

global done
while True:
number_threads = len(processing.activeChildren())
if not done and number_threads 
if __name__ == '__main__':

processing.Process(target=control).start()





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


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


Re: GUI Programming

2009-04-12 Thread Gabriel

edexter wrote:

On Apr 12, 8:07 am, Gabriel  wrote:
  

Hello,

I'm python newbie and i need to write gui for my school work in python.
I need to write it really quick, because i haven't much time .)
So question is, which of gui toolkits should i pick and learn? I heard
PyGTK and Glade are best for quick gui programming? Is it good for
beginner? Or is there something better?
I have some experience with java swing, btw..



pygame may work for you (sdl doesn't work on my compaq though so test
the examples befour reading the manual)...  most of the other ones are
going to be "ugly"..  If what you want is realy simple and pygame
doesn't work for you may want pycap (a gaming system with python
embeded) ..  What do you need it to do?? just simple buttons???
--
http://mail.python.org/mailman/listinfo/python-list

  
I definitely need some kind of treeview, text area to represents text 
(hexa strings .)), buttons, maybe some kind of menu..

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


Re: design question, metaclasses?

2009-04-12 Thread Darren Dale
On Apr 11, 2:15 pm, Darren Dale  wrote:
> I am working on a project that provides a high level interface to hdf5
> files by implementing a thin wrapper around h5py. I would like to
> generalize the project so the same API can be used with other formats,
> like netcdf or ascii files. The format specific code exists in File,
> Group and Dataset classes, which I could reimplement for each format.
> But there are other classes deriving from Group and Dataset which do
> not contain any format-specific code, and I would like to find a way
> to implement the functionality once and apply uniformly across
> supported formats. This is really abstract, but I was thinking of
> something along the lines of:
>
> format1.Group # implementation of group in format1
> format2.Group # ...
> Base.DerivedGroup # base implementation of DerivedGroup, not directly
> useful
> format1.DerivedGroup = Base.DerivedGroup(format1.Group) # useful
> format2.DerivedGroup = Base.DerivedGroup(format2.Group) # useful
>
> Could anyone please offer a comment, is this an appropriate use of
> metaclassing, or is there maybe an easier/better alternative?

I don't fully understand metaclasses, but I think I have convinced
myself that they are not what I was looking for. I think this will do
what I want it to:

class Group1(object):

def origin(self):
return "Group1"


class Group2(object):

def origin(self):
return "Group2"


def _SubGroup(superclass):

class SubGroup(superclass):
pass

return SubGroup


SubGroup = _SubGroup(Group2)
sub_group = SubGroup()

print sub_group.origin()
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread John Posner

Duncan Booth wrote:

John Posner  wrote:
  

Do know what in the itertools implementation causes adding a 'if p <=
sqrt(n)' clause to *decrease* performance, while adding a
'takewhile()' clause *increases* performance? 



I haven't timed it, but I would guess that the takewhile was faster 
only because the sqrt(n) had been factored out of the loop. Try the 
original loop again precalculating the sqrt(n) and see how that compares.
Here's my "timeit" scorecard, with repeat=5 (only the minimum value 
reported) and number=5000:


 all prev. primes
3.97347791658

 prev. primes that don't exceed SQRT
6.23250413579

 [CACHED] prev. primes that don't exceed SQRT
3.45557325672

 [TAKEWHILE] prev. primes that don't exceed SQRT
0.371197373201

 [TAKEWHILE] squares, using *, of prev. primes that don't exceed N
0.358001074011

 [TAKEWHILE] squares, using **, of prev. primes that don't exceed N
0.383540147515

 [TAKEWHILE, CACHED] squares, using *, of prev. primes that don't 
exceed N

0.410309506343

 [TAKEWHILE, CACHED] squares, using **, of prev. primes that don't 
exceed N

0.401269222462


So ... adding the SQRT optimization degrades the performance 
significantly, but adding CACHING to the SQRT optimization swings the 
performance pendulum back to the "benefit" side. TAKEWHILE is a big win, 
but adding CACHING to TAKEWHILE degrades performance.


The code (110 lines) is at http://cl1p.net/python_prime_generators/

-John

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


Re: any(), all() and empty iterable

2009-04-12 Thread Eduardo O. Padoan
On Sun, Apr 12, 2009 at 8:53 AM, Tim Chase
 wrote:
>>> From the docs:
>>
>> all(iterable)
>>                Return True if all elements of the iterable are true.
>> Equivalent
>>        to:
>>                def all(iterable):
>>            for element in iterable:
>>                if not element:
>>                    return False
>>            return True
>
> Then I'd say the comment is misleading.  An empty list has no item that is
> true (or false), yet it returns true.  The comment in the docs should read
> "Return False if any element of the iterable is not true" or "Return True if
> all elements of the iterable are true or if the iterable is empty."

I didn't knew about "Vacuous True" (my fault, it seems Discrete Math
101) until reading about on this thread (thanks everyone!), and
reading on wikipedia it answered this exact question.


> To get the behavior the original comment describes, would seem to require an
> implementation something like this:
>
>  def all(iterable):
>    iterable = iter(iterable)
>    try:
>      element = iterable.next()
>    except StopIteration:
>      raise UnderdefinedBecauseNoElementsToCompareToTrue
>    while element:
>      try:
>        element = iterable.next()
>      except StopIteration:
>        return True
>    return False
>
>
> Tweaking the documentation seems like an easier and more backwards
> compatible solution to me :)
>
> -tkc
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Eduardo de Oliveira Padoan
http://importskynet.blogspot.com
http://djangopeople.net/edcrypt/

"Distrust those in whom the desire to punish is strong."
   -- Goethe, Nietzsche, Dostoevsky
--
http://mail.python.org/mailman/listinfo/python-list


Re: any(), all() and empty iterable

2009-04-12 Thread Tim Chase

That's why you ask "Do you have any books called 'Robinson Crusoe'?" rather
than "Are all your books called 'Robinson Crusoe'?".


Mu.  If I don't have any books..."Have you stopped beating all 
your wives?"  The question makes the presumption that I have 
books (or that you've been beating your wife).


-tkc


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


Re: GUI Programming

2009-04-12 Thread Ryniek90
I think you should get involved with Eagle Python GUI 
 
or Kiwi GUI Framework  . Good luck.

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


Re: multiprocessing and Locks

2009-04-12 Thread Piet van Oostrum
> gvv  (G) wrote:

>G> Hi All,
>G> I am trying to understand multiprocessing, but I am getting a Runtime
>G> error on the
>G> code below. What am I missing or doing wrong?
>G> Error is:
>G> RuntimeError: Lock objects should only be shared between processes
>G> through inheritance
[code deleted]

I guess you can't share locks (and probably other objects) between
processes from a Pool. Maybe because there is no direct parent-child
relation or so (there is a separate thread involved). There is nothing
in the doc that explicitely forbids it AFAICT but it says that you have
to be careful with sharing. But it could be a bug.

You can do it with a manager, however, but this involves an additional
process under the hood.

if __name__ == "__main__":
manager = multiprocessing.Manager()
lock = manager.Lock()
pool = multiprocessing.Pool(processes=5)
for i in xrange(100):
pool.apply_async(func=RunFunc, args=(i,lock))
pool.close()
pool.join()

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: GUI Programming

2009-04-12 Thread Steven D'Aprano
On Sun, 12 Apr 2009 15:07:11 +0200, Gabriel wrote:

> Hello,
> 
> I'm python newbie and i need to write gui for my school work in python.
> I need to write it really quick, because i haven't much time .) So
> question is, which of gui toolkits should i pick and learn? I heard
> PyGTK and Glade are best for quick gui programming? Is it good for
> beginner? Or is there something better? I have some experience with java
> swing, btw..

I've never used it, but I've heard good things about PythonCard.

http://pythoncard.sourceforge.net/


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


Re: Best Compatible JS Lib for Django

2009-04-12 Thread lkcl
On Apr 4, 7:20 pm, Daniel Fetchinson 
wrote:
> > Does anyone have experience with using JS Libraries with Django?

 yes - pyjamas - http://pyjs.org - although, strictly speaking, it's a
python widget-set and python-to-javascript compiler.

 the possibility now of being able to program in python both client-
side (to generate the front-end) and server-side is just _such_ a
relief.

 i now almost exclusively use django and pyjamas, for all web
development.

> > Do some work better than others and are easier to code with?

 i just did a quick review of one of the alternatives to pyjamas -
qooxdoo.org - which actually looks pretty good, except then you think
"hang on a minute - this is near-identical functionality to pyjamas,
i'm a python programmer, i'd have to get involved with javascript,
what the _hell_ am i _thinking_!"

http://groups.google.com/group/django-users/browse_thread/thread/9dad05e276c99a0c/d0c6dee57d89d376?lnk=gst&q=pyjamas#d0c6dee57d89d376

basically, pyjamas turns the web browser into a desktop widget set
platform.  apps are written in the same declarative style as python-
qt4 and python-gtk2 and python-wxwidgets.

all the browser incompatibilities are taken care of, with seamless per-
platform overrides for Netscape, Mozilla, Safari, Opera and IE.

so it couldn't _get_ any easier.

you don't even need to know any javascript - just a bit of CSS and
some HTML.

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


Re: any(), all() and empty iterable

2009-04-12 Thread Peter Otten
John O'Hagan wrote:

> Or to put it another way, if I ask someone "Amongst your books, is one of
> them 'Robinson Crusoe'?", and they don't have any books, they could
> answer 'yes' (or 'no' equally truthfully), but I'd rather they told me
> that no, they don't have  'Robinson Crusoe'.

That's why you ask "Do you have any books called 'Robinson Crusoe'?" rather
than "Are all your books called 'Robinson Crusoe'?".

The difference between logic and natural language is more striking
with "or". When you ask "Do you take the bus at nine or eleven?" you
certainly don't expect "Yes" as the answer.

Peter

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


Re: moving to processing from threading, global variables

2009-04-12 Thread rkmr...@gmail.com
no one? how can i share variables in processing?
please help out!

On Thu, Apr 9, 2009 at 8:05 AM, rkmr...@gmail.com  wrote:

> hi
> i am trying to move from threading to processing package. this is the
> controller i used to spawn new threads, and it used the global
> variable done to check if it needs to spawn more threads or not. it worked
> great for me. it checks if there is new data to be processed every
> 30 seconds, and spawns of new threads to do the work, till all work is
> done.
>
>
> but now in processing, anychange made to global variable done in sendalert
> method, is not reflected in the controller method.
>
> can please point out what is wrong?
> thanks a lot!
>
>
> code
>
>
> done = False
> def sendalert():
>global done
>users = q.get('xx')
>if not users:
>  done = True
>  return
>
>done = False
>for u in users:
> do stuff
>
>
> def control(number_threads_min=3, number_threads_max = 100):
> global done
> while True:
> number_threads = len(processing.activeChildren())
> if not done and number_threads processing.Process(target=sendalert).start()
> if done and number_threads processing.Process(target=sendalert).start()
> time.sleep(30)
>
> if __name__ == '__main__':
> processing.Process(target=control).start()
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: GUI Programming

2009-04-12 Thread edexter
On Apr 12, 8:07 am, Gabriel  wrote:
> Hello,
>
> I'm python newbie and i need to write gui for my school work in python.
> I need to write it really quick, because i haven't much time .)
> So question is, which of gui toolkits should i pick and learn? I heard
> PyGTK and Glade are best for quick gui programming? Is it good for
> beginner? Or is there something better?
> I have some experience with java swing, btw..

pygame may work for you (sdl doesn't work on my compaq though so test
the examples befour reading the manual)...  most of the other ones are
going to be "ugly"..  If what you want is realy simple and pygame
doesn't work for you may want pycap (a gaming system with python
embeded) ..  What do you need it to do?? just simple buttons???
--
http://mail.python.org/mailman/listinfo/python-list


Re: any(), all() and empty iterable

2009-04-12 Thread John O'Hagan
On Sun, 12 Apr 2009, Chris Rebert wrote:
> On Sat, Apr 11, 2009 at 9:00 PM, John O'Hagan  wrote:
> > Hi,
> >
> > I was getting some surprising false positives as a result of not
> > expecting this:
> >
> > all(element in item for item in iterable)
> >
> > to return True when 'iterable' is empty.

[...]

>
> This is justified in formal logic by the concept of vacuous truth. See
> http://en.wikipedia.org/wiki/Vacuous_truth
>
> all(P(x) for x in y) #Original Python statement
> ∀x∈y. P(x)  #equivalent in formal logic
> ¬¬[∀x∈y. P(x)]  #apply Double Negation
> ¬[∃x∈y. ¬P(x)]  #negate Universal Quantifier
>
> English interpretation: There does not exist a counterexample `x` in
> the set `y` which fails to satisfy predicate `P`.
> Comment: Indeed, in the case the set `y` is empty, there is no
> counterexample to be had, and thus both the final statement and the
> initial equivalent one are vacuously true. Yes, this may seem
> unintuitive at first. Logic is like that sometimes.
>
> Python equivalent to final statement: not any(not P(x) for x in y)
> Comment: Likewise, in the case of a empty `y`, any() returns False, so
> the code simplifies to `not False`, which obviously simplifies further
> to just True.
>
> So, if you agree with the behavior of any(), you must logically agree
> with the behavior of all(). ;-)

I do like this reasoning, but when you're testing for the ubiquity of 
something you don't expect to find it in an empty space - I was checking 
lists of sets to see which possible set members were in all the sets of a 
list, and found they all were: if the list contained no sets! I think that is 
surprising.

Or to put it another way, if I ask someone "Amongst your books, is one of 
them 'Robinson Crusoe'?", and they don't have any books, they could 
answer 'yes' (or 'no' equally truthfully), but I'd rather they told me that 
no, they don't have  'Robinson Crusoe'.

Still, point taken, and quibble easily solved with a little extra test for 
emptiness.

Thanks,

John


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


[ANN] Pyjamas 0.5p1 Web Widget Set and python-to-javascript Compiler released

2009-04-12 Thread Luke Kenneth Casson Leighton
Pyjamas 0.5p1 - http://pyjs.org - is a bug-fix release.

Pyjamas is a Web Widget Framework, written in python, that is compiled
to javascript using its stand-alone python-to-javascript compiler.  It
began as a port of GWT, to python.

Many of the issues faced by web developers - javascript and CSS
incompatibilities and difficulties - simply go away, with Pyjamas,
thanks to the declarative style used for Pyjamas application
development, that's near-identical to that of python-qt4, python-gtk2
and python-wxWidgets.

The use of Pyjamas for application development turns web browsers into
a desktop widget application development platform, with the added
advantage over traditional desktop widget sets of having full access
to the complete set of HTML features of web browsers.

The stand-alone python-to-javascript compiler is also beginning to
develop into a python accelerator.  The combination of pyjs and pyv8,
or pyjs and python-spidermonkey, offers some interesting possibilities
for JIT compilation of python applications into x86 or ARM assembler
(using pyv8), by way of intermediate compilation to javascript.
Anyone who is interested in the possibilities should contact the
pyjamas developers at http://groups.google.com/group/pyjamas-dev

Downloads:
http://code.google.com/p/pyjamas
http://sf.net/projects/pyjamas
http://pypi.python.org/pypi/Pyjamas/0.5p1

Web Site:
http://pyjs,org
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a virtual serial port?

2009-04-12 Thread Stuart Davenport
On Apr 11, 6:56 pm, Grant Edwards  wrote:
> On 2009-04-11, Grant Edwards  wrote:
>
> > You can write a port redirector in user-space in MS-Windows,
> > but you can't in Linux/Unix.  On Unix systems you have to
> > write a kernel module that sits below the tty layer.
>
> Perhaps I should elucidate further.
>
> That's what the "pty" driver on Unix is: a kernel module that
> sits underneath the tty layer where the "normal" serial-port
> UART drivers sit.
>
> However, Unix pty drivers only handles a subset of the normal
> serial-port API.  [I'm not sure why pty drivers have never been
> "finished" so that they fully emulate a serial port, but it's
> been that way for 20+ years].
>
> If RouteBuddy doesn't try to do things like get/set modem
> control/status lines, then the OP might be able to use a pty.
>
> Each pty consists of two devices: a master end and a slave end.
> RouteBuddy would be told to use the slave end, and the OP would
> write a program that would transfer data between a network
> connection and the master end.
>
> A pty devices is what is used by programs like xterm to run
> programs like bash.  Xterm transfers data between a network
> connection and the master end of a pty.  Bash is connected to
> the slave end of that pty and "thinks" it's connected to a
> serial port.  Bash uses little of the serial port API, so it's
> happy with the limited API provided by a pty slave-end.
>
> --
> Grant

Grant - HERO! THIS EXACTLY WHAT I WANT TO DO...

I didn't realise people were still posting on this thread and have
spent the last 3 hours trying to get some example code to create a
pseudo TTY (pty) in Python, I get that I have to use the pty module,
but finding an example is like searching for... lets go festing ...an
easter egg in a forest ;)

You wouldn't have any pointers to initiating a PTY and how to write to
it? This is all I want to achieve.


My point in all this is actually that I ordered a USB GPS Receiver and
it wont arrive for another two weeks, being my impatient self, I am
writing an app for my iPhone to broadcast its GPS location over the
network to my laptop. I then want to get this data into the NMEA
format and push this data onto a PTY - this will in-effect replace the
USB GPS Receiver and the GPS software can read it :)

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


Re: GUI Programming

2009-04-12 Thread r
On Apr 12, 11:04 am, Gabriel  wrote:
[snip]
>
> It seems ugly to me..

OK, i shall inform the BDFL of this "inadequacy".
Good Luck!
--
http://mail.python.org/mailman/listinfo/python-list


Re: any(), all() and empty iterable

2009-04-12 Thread Arnaud Delobelle
Tim Chase  writes:

> Arnaud Delobelle wrote:
>> Paul Rubin  writes:
>>
>>> Tim Chase  writes:
> Return True if all elements of the iterable are
> true. ...
 Then I'd say the comment is misleading.  An empty list has no item
 that is true (or false), yet it returns true. 
>>> The comment is correct.  "All the items of the iterable are true"
>>> means EXACTLY the same thing as "there are no items of the iterable
>>> that are false".  The empty list has no false items.  Therefore
>>> all(empty_list) = True is the correct behavior.
>>>
>>>
>>> Another possible implementation:
>>>
>>> import operator,itertools
>>> def all(xs):
>>>  return reduce(operator.and_, itertools.imap(bool, xs), True)
>>
>> A contest! My entry:
>>
>> def all(iterable):
>> return not sum(not x for x in iterable)
>
> Problem with both entries:  short-circuit evaluation.
>
>   def test_me(how_many=9):
> yield False
> for _ in xrange(how_many): yield True
>   print all(test_me())
>
> The stdlib version wisely bails on the first False.  A particularly
> useful aspect when test_me() does something time-consuming:
>
>  def test_me(times=100)
>for _ in xrange(times):
>  yield some_long_running_process_that_usually_returns_false()
>
> where that process may do something like slurp a web-page across the
> planet, or calculate some expensive expression.
>
> -tkc

I was aware of this but I mimicked the behaviour of Paul's
implementation.  It's even worse if the iterable is something like 

 itertools.repeat(False)

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


Re: any(), all() and empty iterable

2009-04-12 Thread John O'Hagan
On Sun, 12 Apr 2009, Paul Rubin wrote:
> Tim Chase  writes:
> > > Return True if all elements of the iterable are
> > > true. ...
> >
> > Then I'd say the comment is misleading.  An empty list has no item
> > that is true (or false), yet it returns true.
>
> The comment is correct.  "All the items of the iterable are true"
> means EXACTLY the same thing as "there are no items of the iterable
> that are false".  The empty list has no false items.  Therefore
> all(empty_list) = True is the correct behavior.
>

[...]

By the same argument, all the items of the iterable are also False, or blue, 
or anything else you care to say about them, because there aren't any. From 
the Wikipedia article on vacuous truth (see Chris Rebert's reply in this 
thread):

"All pink rhinoceros are carnivores."
"All pink rhinoceros are herbivores."

But you're right, by this logic the comment is correct, and although the 
choice of True seems arbitrary, I've learnt something about logic today.

Regards,

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


Re: GUI Programming

2009-04-12 Thread Gabriel

r wrote:

On Apr 12, 8:07 am, Gabriel  wrote:
  

Hello,

I'm python newbie and i need to write gui for my school work in python.
I need to write it really quick,


[snip]

Tkinter is built-in, why not start there?

from Tkinter import *
root = Tk()
root.mainloop()





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

  


It seems ugly to me..
--
http://mail.python.org/mailman/listinfo/python-list


Re: Extracting zip files containing directories with ZipFile

2009-04-12 Thread MRAB

Márcio Faustino wrote:

Hi,

Does the ZipFile class correctly handles directories contained within
zip files?

For example, calling "extractall" on an archive with only "folder/
file.txt" in it results in an IOError saying "No such file or
directory" for "./folder/file.txt", because it created a file named
"folder" instead of a directory. (I've tested this with version 2.6.1
on Windows XP.) However, changing that method to the following, seems
to solve this particular problem:

#--
def extractall(self, path = os.path.curdir, members = None, password =
None):
if members is None:
members = self.namelist()

# Place directories first to create them before extracting any
file.
members.sort()

for name in members:
if name.endswith('/'):
os.makedirs(os.path.join(path, name))
else:
self.extract(name, path, password)
#--


Strictly speaking, zip files don't contain nested folders, but only a
flat list of files.

However, by convention a folder hierarchy is shown by the use of slashes
in the names eg. "foo/bar.txt" is a folder "foo" containing a file
"bar.txt". You can also represent an empty folder with a (zero-length)
file ending with a slash, eg "foo/" is an empty folder "foo".

Just create any intermediate folders on demand if they don't already
exist.
--
http://mail.python.org/mailman/listinfo/python-list


Extracting zip files containing directories with ZipFile

2009-04-12 Thread Márcio Faustino
Hi,

Does the ZipFile class correctly handles directories contained within
zip files?

For example, calling "extractall" on an archive with only "folder/
file.txt" in it results in an IOError saying "No such file or
directory" for "./folder/file.txt", because it created a file named
"folder" instead of a directory. (I've tested this with version 2.6.1
on Windows XP.) However, changing that method to the following, seems
to solve this particular problem:

#--
def extractall(self, path = os.path.curdir, members = None, password =
None):
if members is None:
members = self.namelist()

# Place directories first to create them before extracting any
file.
members.sort()

for name in members:
if name.endswith('/'):
os.makedirs(os.path.join(path, name))
else:
self.extract(name, path, password)
#--

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


Re: GUI Programming

2009-04-12 Thread r
On Apr 12, 8:07 am, Gabriel  wrote:
> Hello,
>
> I'm python newbie and i need to write gui for my school work in python.
> I need to write it really quick,
[snip]

Tkinter is built-in, why not start there?

from Tkinter import *
root = Tk()
root.mainloop()





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


Re: a newbie question

2009-04-12 Thread Peter Otten
zhangle2...@gmail.com wrote:

> I am just learning Python and get a problem when trying this example:
> 
> from urllib import urlopen
> doc=urlopen("http://www.python.org";).read()
> print(doc)
> 
> when i run this, i was tould that 'cannot import name "urlopen"
> 
> 
> What's wrong with this code? Do i need to place my code in some
> specific directory? Thanks a lot. '

This function has been moved in Python 3.0. Change your import to

from urllib.request import urlopen

If you are learning with a book using Python 2.x consider switching to
Python 2.5 or 2.6 to try out the examples.

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


Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Arnaud Delobelle
Duncan Booth  writes:

> John Posner  wrote:
>
>> Do know what in the itertools implementation causes adding a 'if p <=
>> sqrt(n)' clause to *decrease* performance, while adding a
>> 'takewhile()' clause *increases* performance? 
>
> I haven't timed it, but I would guess that the takewhile was faster 
> only because the sqrt(n) had been factored out of the loop. Try the 
> original loop again precalculating the sqrt(n) and see how that compares.

Most likely

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


Re: any(), all() and empty iterable

2009-04-12 Thread Arnaud Delobelle
Paul Rubin  writes:

> Tim Chase  writes:
>> > Return True if all elements of the iterable are
>> > true. ...
>> Then I'd say the comment is misleading.  An empty list has no item
>> that is true (or false), yet it returns true. 
>
> The comment is correct.  "All the items of the iterable are true"
> means EXACTLY the same thing as "there are no items of the iterable
> that are false".  The empty list has no false items.  Therefore
> all(empty_list) = True is the correct behavior.
>
>
> Another possible implementation:
>
> import operator,itertools
> def all(xs):
>  return reduce(operator.and_, itertools.imap(bool, xs), True)

A contest! My entry:

def all(iterable):
return not sum(not x for x in iterable)

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


Re: a newbie question

2009-04-12 Thread Eugene Perederey
use urllib2

2009/4/12  :
> hi,
>
> I am just learning Python and get a problem when trying this example:
>
> from urllib import urlopen
> doc=urlopen("http://www.python.org";).read()
> print(doc)
>
> when i run this, i was tould that 'cannot import name "urlopen"
>
>
> What's wrong with this code? Do i need to place my code in some
> specific directory? Thanks a lot. '
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Sincerely yours, Eugene Perederey
--
http://mail.python.org/mailman/listinfo/python-list


a newbie question

2009-04-12 Thread zhangle2002
hi,

I am just learning Python and get a problem when trying this example:

from urllib import urlopen
doc=urlopen("http://www.python.org";).read()
print(doc)

when i run this, i was tould that 'cannot import name "urlopen"


What's wrong with this code? Do i need to place my code in some
specific directory? Thanks a lot. '
--
http://mail.python.org/mailman/listinfo/python-list


Subscribe My Feed RSS And Commenting Then I Will To Do Too!

2009-04-12 Thread clickkt.com
Subscribe My Feed RSS And Commenting Then I Will To Do Too!
My Feed: http://feeds2.feedburner.com/clickkt
My blog http://clickkt.com
And if you and my link at your blog, I will to do too!
--
http://mail.python.org/mailman/listinfo/python-list


Re: any(), all() and empty iterable

2009-04-12 Thread Tim Chase

Arnaud Delobelle wrote:

Paul Rubin  writes:


Tim Chase  writes:

Return True if all elements of the iterable are
true. ...

Then I'd say the comment is misleading.  An empty list has no item
that is true (or false), yet it returns true. 

The comment is correct.  "All the items of the iterable are true"
means EXACTLY the same thing as "there are no items of the iterable
that are false".  The empty list has no false items.  Therefore
all(empty_list) = True is the correct behavior.


Another possible implementation:

import operator,itertools
def all(xs):
 return reduce(operator.and_, itertools.imap(bool, xs), True)


A contest! My entry:

def all(iterable):
return not sum(not x for x in iterable)


Problem with both entries:  short-circuit evaluation.

  def test_me(how_many=9):
yield False
for _ in xrange(how_many): yield True
  print all(test_me())

The stdlib version wisely bails on the first False.  A 
particularly useful aspect when test_me() does something 
time-consuming:


 def test_me(times=100)
   for _ in xrange(times):
 yield some_long_running_process_that_usually_returns_false()

where that process may do something like slurp a web-page across 
the planet, or calculate some expensive expression.


-tkc



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


Re: llvm vs. parrot

2009-04-12 Thread Fuzzyman
On Apr 11, 12:16 am, Paul Watson  wrote:
> Is Parrot out of favor these days?  It appears that Google is going to
> use llvm.
>
> http://code.google.com/p/unladen-swallow/

Has Parrot ever been in favour?

Actually they're quite different things.

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


GUI Programming

2009-04-12 Thread Gabriel

Hello,

I'm python newbie and i need to write gui for my school work in python.
I need to write it really quick, because i haven't much time .)
So question is, which of gui toolkits should i pick and learn? I heard 
PyGTK and Glade are best for quick gui programming? Is it good for 
beginner? Or is there something better?

I have some experience with java swing, btw..
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a virtual serial port?

2009-04-12 Thread Grant Edwards
On 2009-04-12, JanC  wrote:
> Grant Edwards wrote:
>
>> On 2009-04-10, Stuart Davenport  wrote:
>>
>>> I am trying to work out if its possible, to create a virtual serial
>>> port with Python?
>>
>> On Linux: no.
>
> I wonder if there is no way to emulate ptys from userspace?

Didn't I just answer that question?

On Linux: no.

There have been a couple projects that attempted to allow
drivers to be written in user-space.

  http://www.circlemud.org/~jelson/software/fusd/
  http://www.gelato.unsw.edu.au/IA64wiki/UserLevelDrivers  

However, AFAICT, both of these projects are defunct and neither
provided an interface to the TTY layer.  Without a tty-layer
interface, you would have to duplicate the entire tty layer in
your user-space driver driver as well.

> (Like you can use FUSE to implement filesystems in python.)

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


Re: Re: Generators/iterators, Pythonicity, and primes

2009-04-12 Thread Duncan Booth
John Posner  wrote:

> Do know what in the itertools implementation causes adding a 'if p <=
> sqrt(n)' clause to *decrease* performance, while adding a
> 'takewhile()' clause *increases* performance? 

I haven't timed it, but I would guess that the takewhile was faster 
only because the sqrt(n) had been factored out of the loop. Try the 
original loop again precalculating the sqrt(n) and see how that compares.
--
http://mail.python.org/mailman/listinfo/python-list


Re: any(), all() and empty iterable

2009-04-12 Thread Paul Rubin
Tim Chase  writes:
> > Return True if all elements of the iterable are
> > true. ...
> Then I'd say the comment is misleading.  An empty list has no item
> that is true (or false), yet it returns true. 

The comment is correct.  "All the items of the iterable are true"
means EXACTLY the same thing as "there are no items of the iterable
that are false".  The empty list has no false items.  Therefore
all(empty_list) = True is the correct behavior.


Another possible implementation:

import operator,itertools
def all(xs):
 return reduce(operator.and_, itertools.imap(bool, xs), True)
--
http://mail.python.org/mailman/listinfo/python-list


Re: any(), all() and empty iterable

2009-04-12 Thread Tim Chase

From the docs:


all(iterable)

Return True if all elements of the iterable are true. Equivalent

to:

def all(iterable):

for element in iterable:
if not element:
return False
return True


Then I'd say the comment is misleading.  An empty list has no 
item that is true (or false), yet it returns true.  The comment 
in the docs should read "Return False if any element of the 
iterable is not true" or "Return True if all elements of the 
iterable are true or if the iterable is empty."


To get the behavior the original comment describes, would seem to 
require an implementation something like this:


  def all(iterable):
iterable = iter(iterable)
try:
  element = iterable.next()
except StopIteration:
  raise UnderdefinedBecauseNoElementsToCompareToTrue
while element:
  try:
element = iterable.next()
  except StopIteration:
return True
return False


Tweaking the documentation seems like an easier and more 
backwards compatible solution to me :)


-tkc



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


  1   2   >