Re: type, object hierarchy?

2008-02-04 Thread Hrvoje Niksic
7stud <[EMAIL PROTECTED]> writes:

> --output:--
> (, , )
>
> The output suggests that Dog actually is a subclass of type--despite
> the fact that issubclass(Dog, type) returns False.

What was it in the output that gave you the impression that Dog is a
subclass of type?  The last entry is only for the "object" type, which
you already knew Dog was a subclass of.

>>> object

>>> type

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


Re: Elementary string-parsing

2008-02-04 Thread Marc 'BlackJack' Rintsch
On Mon, 04 Feb 2008 03:21:18 +, Odysseus wrote:

> def extract_data():
> i = 0
> while i < len(names):
> name = names[i][6:] # strip off "Name: "
> found[name] = {'epoch1': cells[10 * i + na],
>'epoch2': cells[10 * i + na + 1],
>'time': cells[10 * i + na + 5],
>'score1': cells[10 * i + na + 6],
>'score2': cells[10 * i + na + 7]}

Here and in later code you use a ``while`` loop although it is known at
loop start how many times the loop body will be executed.  That's a job
for a ``for`` loop.  If possible not over an integer that is used later
just as index into list, but the list itself.  Here you need both, index
and objects from `names`.  There's the `enumerate()` function for creating
an iterable of (index, name) from `names`.

I'd put all the relevant information that describes a field of the
dictionary that is put into `found` into tuples and loop over it.  There
is the cell name, the index of the cell and function that converts the
string from that cell into an object that is stored in the dictionary. 
This leads to (untestet):

def extract_data(names, na, cells):
found = dict()
for i, name in enumerate(names):
data = dict()
cells_index = 10 * i + na
for cell_name, index, parse in (('epoch1', 0, parse_date),
('epoch2', 1, parse_date),
('time', 5, parse_number),
('score1', 6, parse_number),
('score2', 7, parse_number)):
data[cell_name] = parse(cells[cells_index + index])
assert name.startswith('Name: ')
found[name[6:]] = data
return found

The `parse_number()` function could look like this:

def parse_number(string):
try:
return float(string.replace(',', ''))
except ValueError:
return string

Indeed the commas can be replaced a bit more elegant.  :-)

`parse_date()` is left as an exercise for the reader.

> for k in ('epoch1', 'epoch2'):
> dlist = found[name][k].split(" ")
> m = 0
> while m < 12:
> if m_abbrevs[m] == dlist[1]:
> dlist[1] = m + 1
> break
> m += 1
> tlist = dlist[3].split(":")
> found[name][k] = timegm((int(dlist[2]), int(dlist[1]),
>  int(dlist[0]), int(tlist[0]),
>  int(tlist[1]), int(tlist[2]),
>  -1, -1, 0))
> i += 1
> 
> The function appears to be working OK as is, but I would welcome any & 
> all suggestions for improving it or making it more idiomatic.

As already said, that ``while`` loop should be a ``for`` loop.  But if you
put `m_abbrevs` into a `list` you can replace the loop with a single call
to its `index()` method: ``dlist[1] = m_abbrevs.index(dlist[1]) + 1``.

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


Re: Elementary string-parsing

2008-02-04 Thread Odysseus
In article <[EMAIL PROTECTED]>,
 Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:



> Here and in later code you use a ``while`` loop although it is known at
> loop start how many times the loop body will be executed.  That's a job
> for a ``for`` loop.  If possible not over an integer that is used later
> just as index into list, but the list itself.  Here you need both, index
> and objects from `names`.  There's the `enumerate()` function for creating
> an iterable of (index, name) from `names`.

Thanks, that will be very useful. I was casting about for a replacement 
for PostScript's "for" loop, and the "while" loop (which PS lacks -- and 
which I've never missed there) was all I could come up with.

> I'd put all the relevant information that describes a field of the
> dictionary that is put into `found` into tuples and loop over it.  There
> is the cell name, the index of the cell and function that converts the
> string from that cell into an object that is stored in the dictionary. 
> This leads to (untestet):
> 
> def extract_data(names, na, cells):
> found = dict()

The problem with initializing the 'super-dictionary' within this 
function is that I want to be able to add to it in further passes, with 
a new set of "names" & "cells" each time.

BTW what's the difference between the above and "found = {}"?

> for i, name in enumerate(names):
> data = dict()
> cells_index = 10 * i + na
> for cell_name, index, parse in (('epoch1', 0, parse_date),
> ('epoch2', 1, parse_date),
> ('time', 5, parse_number),
> ('score1', 6, parse_number),
> ('score2', 7, parse_number)):
> data[cell_name] = parse(cells[cells_index + index])

This looks a lot more efficient than my version, but what about the 
strings that don't need parsing? Would it be better to define a 
'pass-through' function that just returns its input, so they can be 
handled by the same loop, or to handle them separately with another loop?

> assert name.startswith('Name: ')

I looked up "assert", but all I could find relates to debugging. Not 
that I think debugging is something I can do without ;) but I don't 
understand what this line does.

> found[name[6:]] = data
> return found
> 
> The `parse_number()` function could look like this:
> 
> def parse_number(string):
> try:
> return float(string.replace(',', ''))
> except ValueError:
> return string
> 
> Indeed the commas can be replaced a bit more elegant.  :-)

Nice, but I'm somewhat intimidated by the whole concept of 
exception-handling (among others). How do you know to expect a 
"ValueError" if the string isn't a representation of a number? Is there 
a list of common exceptions somewhere? (Searching for "ValueError" 
turned up hundreds of passing mentions, but I couldn't find a definition 
or explanation.)


> 
> As already said, that ``while`` loop should be a ``for`` loop.  But if you
> put `m_abbrevs` into a `list` you can replace the loop with a single call
> to its `index()` method: ``dlist[1] = m_abbrevs.index(dlist[1]) + 1``.

I had gathered that lists shouldn't be used for storing constants. Is 
that more of a suggestion than a rule? I take it tuples don't have an 
"index()" method.

Thanks for the detailed advice. I'll post back if I have any trouble 
implementing your suggestions.

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


Re: type, object hierarchy?

2008-02-04 Thread 7stud
On Feb 4, 12:49 am, Hrvoje Niksic <[EMAIL PROTECTED]> wrote:
> 7stud <[EMAIL PROTECTED]> writes:
> > --output:--
> > (, , )
>
> > The output suggests that Dog actually is a subclass of type--despite
> > the fact that issubclass(Dog, type) returns False.
>
> What was it in the output that gave you the impression that Dog is a
> subclass of type?
>

The fact that Dog.__mro__ produces any output at all--instead of
producing an error.  object does not have an __mro__attribute, so
where did Dog inherit that attribute from?  The output of dir(type)
shoes that type has an __mro__ attribute.  Some of the evidence
suggests this might be the hierarchy:

object ---> type
 |
 V
Mammal
 |
 V
Dog


But since Dog seems to inherit __mro__ from type, that hierarchy does
not appear to be correct.  Rather this hierarchy seems more likely:


object
 |
 V
type
 |
 V
Mammal
 |
 V
Dog



> And if you want to really blow your mind,
>
> print isinstance(type, object)  # True
> print isinstance(object, type)  # True
>

Yep, I investigated that before I posted.  It doesn't seem to fit with
the latter hierarchy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: type, object hierarchy?

2008-02-04 Thread Bruno Desthuilliers
7stud a écrit :
> print dir(type)  #__mro__ attribute is in here
> print dir(object)   #no __mro__ attribute
> 
> 
> class Mammals(object):
> pass
> class Dog(Mammals):
> pass
> 
> print issubclass(Dog, type)   #False
> print Dog.__mro__
> 
> --output:--
> (, , )
> 
> 
> The output suggests that Dog actually is a subclass of type

Nope. It suggests that Dog is a subclass of object (which is not really 
surprising).


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


Re: Elementary string-parsing

2008-02-04 Thread Paul Hankin
On Feb 4, 3:21 am, Odysseus <[EMAIL PROTECTED]> wrote:
> The next one is much messier. A couple of the strings represent times,
> which I think will be most useful in 'native' form, but the input is in
> the format "DD Mth  HH:MM:SS UTC".

time.strptime will do this!

You can find the documentation at http://docs.python.org/lib/module-time.html

Untested:
time.strptime(my_date, '%d %b %y %H:%M:%S %Z')

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


Re: type, object hierarchy?

2008-02-04 Thread Hrvoje Niksic
7stud <[EMAIL PROTECTED]> writes:

> On Feb 4, 12:49 am, Hrvoje Niksic <[EMAIL PROTECTED]> wrote:
>> 7stud <[EMAIL PROTECTED]> writes:
>> > --output:--
>> > (, , )
>>
>> > The output suggests that Dog actually is a subclass of type--despite
>> > the fact that issubclass(Dog, type) returns False.
>>
>> What was it in the output that gave you the impression that Dog is a
>> subclass of type?
>
> The fact that Dog.__mro__ produces any output at all--instead of
> producing an error.  object does not have an __mro__attribute, so
> where did Dog inherit that attribute from?  The output of dir(type)
> shoes that type has an __mro__ attribute.

I see some confusion.

For one, while Dog is not a subclass of type, it's certainly an
*instance* of type.  All instances of type have an __mro__ instance,
including object, regardless of whether dir() is aware of it.  (Try
object.__mro__, it evaluates just fine.)

Second, simply the fact that an object has an attribute doesn't say
anything about its chain of inheritance.  The final word on
inheritance is the contents of attributes such as __bases__ (shallow)
and __mro__ (deep).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anyone else use this little idiom?

2008-02-04 Thread BJörn Lindqvist
> In Python, the direct translation of this is a for loop.  When the
> index doesn't matter to me, I tend to write it as:
>
> for _ in xrange (1,n):
>some code
>
> An alternative way of indicating that you don't care about the loop
> index would be
>
> for dummy in xrange (1,n):
>some code

I usually use _ when I know that i18n doesn't matter. dummy is just to
long when unpacking sequences:

for dummy, email, dummy, dummy in persons:
sendmail(email)

for _, email, _, _ in persons:
sendmail(email)


-- 
mvh Björn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What should I use under *nix instead of freeze?

2008-02-04 Thread Eric Brunel
On Sat, 02 Feb 2008 00:08:21 +0100, Mike Kent <[EMAIL PROTECTED]> wrote:

> In a comment Guido made on a recent bug report for the 'freeze'
> utility, he stated:
>
> "I think nobody really cares about freeze any more -- it isn't
> maintained."
>
> That being the case, what is the preferred/best replacement for freeze
> on a *nix platform?  I'm looking for something that, like freeze,
> turns my application into a single-file executable, of the smallest
> size possible, that can be executed on a machine with no Python
> installation or development system.

Never used it, but it seems cx_Freeze  
(http://python.net/crew/atuining/cx_Freeze/) does just that... Don't know  
if it's maintained anymore, but versions are available for the latest  
Python version (2.5).

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


Re: type, object hierarchy?

2008-02-04 Thread Steven D'Aprano
On Mon, 04 Feb 2008 10:50:10 +0100, Hrvoje Niksic wrote:

> ...regardless of whether dir() is aware of it.

I've always thought that having dir(obj) return only some attributes of 
obj, according to some arbitrary, implementation and version dependent 
algorithm, was an ugly wart.

help(dir) =>
"Return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it:"


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


Re: Elementary string-parsing

2008-02-04 Thread John Machin
On Feb 4, 8:43 pm, Odysseus <[EMAIL PROTECTED]> wrote:
> In article <[EMAIL PROTECTED]>,
>  Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> > found = dict()
> BTW what's the difference between the above and "found = {}"?

{} takes 4 fewer keystrokes, doesn't have the overhead of a function
call, and works with Pythons at least as far back as 1.5.2 -- apart
from that, it's got absolutely nothing going for it ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it explicitly specified?

2008-02-04 Thread Steven D'Aprano
On Sun, 03 Feb 2008 15:31:49 -0800, Paul Boddie wrote:

> I don't know whether I can offer much better advice than others, but I
> have noticed that a lot of my own code has moved in the direction of not
> having specific default values in function/method signatures. So,
> instead of this...
> 
>   def f(x=123):
> ...
> 
> ...I have this:
> 
>   def f(x=None):
> if x is None:
>   x = 123


For the love of Pete, WHY??

I understand why you would do it for a mutable default, but immutable???




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


Need Smart Phone with new features? please click here

2008-02-04 Thread Farooq
www.enmac.com.hk
GSM Mobile Phones, Digital iPods, Digital Clocks, Digital Pens,
Digital Quran. Enjoy these products with Islamic Features (Complete
Holy Quran with Text and Audio, Tafaseer books, Ahadees Books, Daily
Supplications, Universal Qibla Direction, Prayer Timing and much more)
visit our website for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: polling for output from a subprocess module

2008-02-04 Thread Thomas Bellman
[EMAIL PROTECTED] wrote:

>   try:
> test = Popen(test_path,
>   stdout=PIPE,
>   stderr=PIPE,
>   close_fds=True,
>   env=test_environ)

> while test.poll() == None:
> ready = select.select([test.stderr], [], [])

> if test.stderr in ready[0]:
> t_stderr_new = test.stderr.readlines()
> if t_stderr_new != []:
> print "STDERR:", "\n".join(t_stderr_new)
> t_stderr.extend(t_stderr_new)
[...]
> The problem is, that it seems that all the output from the subprocess
> seems to be coming at once. Do I need to take a different approach?

The readlines() method will read until it reaches end of file (or
an error occurs), not just what is available at the moment.  You
can see that for your self by running:

$ python -c 'import sys; print sys.stdin.readlines()'

The call to sys.stdin.readlines() will not return until you press
Ctrl-D (or, I think, Ctrl-Z if you are using MS-Windows).

However, the os.read() function will only read what is currently
available.  Note, though, that os.read() does not do line-based
I/O, so depending on the timing you can get incomplete lines, or
multiple lines in one read.


-- 
Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"Adde parvum parvo magnus acervus erit"   ! bellman @ lysator.liu.se
  (From The Mythical Man-Month)   ! Make Love -- Nicht Wahr!
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Too many open files

2008-02-04 Thread Jeff
Why don't you start around 50 threads at a time to do the file
writes?  Threads are effective for IO.  You open the source file,
start a queue, and start sending data sets to be written to the
queue.  Your source file processing can go on while the writes are
done in other threads.
-- 
http://mail.python.org/mailman/listinfo/python-list


PyOpenGL

2008-02-04 Thread [EMAIL PROTECTED]
Hi all,

I apologize if this question was already answered before but I was
unable to find a proper solution to my problem. Anyways, I am trying
to run shaderobjects.py on Windows (Python 2.5.1) by just double-
clicking, and I got the following error:

[...]
File "/usr/lib/python2.5/site-packages/OpenGL/extensions.py", line 13,
in hasGLExtension
AVAILABLE_GL_EXTENSIONS[:] = glGetString( GL_EXTENSIONS ).split()
AttributeError: 'NoneType' object has no attribute 'split'

_I think_ I have all requirements and packages properly installed.

Any clues? Thanks in advance !

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


Catching a non-Exception object (KeyboardInterrupt)

2008-02-04 Thread Michael Goerz
Hi,

when I try to catch ctrl+c with

except KeyboardInterrupt:

pychecker tells me

Catching a non-Exception object (KeyboardInterrupt)

It works fine, but the message indicates that it's not completely clean. 
  How should I write the exception correctly?

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


Re: Too many open files

2008-02-04 Thread Steven D'Aprano
On Mon, 04 Feb 2008 13:57:39 +0100, AMD wrote:

> The problem I have under windows is that as soon as I get to 500 files I
> get the Too many open files message. I tried the same thing in Delphi
> and I can get to 3000 files. How can I increase the number of open files
> in Python?

Windows XP has a limit of 512 files opened by any process, including 
stdin, stdout and stderr, so your code is probably failing after file 
number 509.

http://forums.devx.com/archive/index.php/t-136946.html

It's almost certainly not a Python problem, because under Linux I can 
open 1000+ files without blinking.

I don't know how Delphi works around that issue. Perhaps one of the 
Windows gurus can advise if there's a way to increase that limit from 512?



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


Re: Too many open files

2008-02-04 Thread Christian Heimes
Jeff wrote:
> Why don't you start around 50 threads at a time to do the file
> writes?  Threads are effective for IO.  You open the source file,
> start a queue, and start sending data sets to be written to the
> queue.  Your source file processing can go on while the writes are
> done in other threads.

I'm sorry, but you are totally wrong. Threads are a very bad idea for IO
bound operation. Asynchronous event IO is the best answer for any IO
bound problem. That is select, poll, epoll, kqueue or IOCP.

Christian

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


Re: Meta Class

2008-02-04 Thread Trevor Johnson
You're right I totally misunderstood it. And your idea is obvious and simple
enough :)

On Feb 1, 2008 6:33 PM, Gabriel Genellina <[EMAIL PROTECTED]> wrote:

> En Fri, 01 Feb 2008 15:46:05 -0200, Trevor Johnson
> <[EMAIL PROTECTED]> escribió:
>
> > I think I have a good candidate for a meta class here. Never done this
> > before and would like someone to help. In the code that follows, there
> is
> > one variable that needs to be changed: the letter 'a' as inserted in
> > construction of the variable 'word'. In other applications, I will need
> > to
> > change that to two variables, but they are independent within this code.
> > How
> > do I go about abstracting these variables to make a meta class?
>
> I think you totally misunderstood the metaclass concept. A class is an
> instance of its metaclass, that is, a metaclass is the "thing" used to
> create a new class. You don't even use (custom) classes in your example.
>
> If you want to make a more generic function, that is, something that works
> for other letters instead of just 'a', you want a function parameter:
>
> >  >>> def testing(searched_letter):
> > ...   for word in wordPool:
> ...
>
> and replace all occurences of 'a' with searched_letter, and 'aaa' with
> searched_letter*3
>
> Usage: testing('a'), it should give the same results as before. Try
> testing('e') etc.
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: type, object hierarchy?

2008-02-04 Thread Christian Heimes
7stud wrote:
> The output suggests that Dog actually is a subclass of type--despite
> the fact that issubclass(Dog, type) returns False.  In addition, the
> output of dir(type) and dir(object):

No, type is the meta class of the class object:

>>> issubclass(object, type)
False
>>> isinstance(object, type)
True

As you can see object is not a subclass of type but an instance of type.
This may look confusing at first but it's easy to explain. Like a class
is the blue print of an instance, a meta class is the blue print of a
class. In Python everything is a direct or indirect instance of type.

o = someobject
while o is not type:
o = type(o)
print o

The code will eventually print "type".

Christian

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


Re: [2.4.2/Linux] Getting Python to fork?

2008-02-04 Thread Christian Heimes
Jon Ribbens wrote:
> This should work I believe:
> 
>   if os.fork():
> os._exit(0)
>   os.setsid()
>   os.chdir("/")
>   fd = os.open("/dev/null", os.O_RDWR)
>   os.dup2(fd, 0)
>   os.dup2(fd, 1)
>   os.dup2(fd, 2)
>   if fd > 2:
> os.close(fd)
>   # do stuff
> 
> Although bear in mind it's pretty UNIX-y.

IIRC you have to fork a second time after you have changed the working
dir and created a new session group.

Christian

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


Re: Catching a non-Exception object (KeyboardInterrupt)

2008-02-04 Thread Gabriel Genellina
En Mon, 04 Feb 2008 11:53:52 -0200, Michael Goerz  
<[EMAIL PROTECTED]> escribi�:

> when I try to catch ctrl+c with
>
> except KeyboardInterrupt:
>
> pychecker tells me
>
> Catching a non-Exception object (KeyboardInterrupt)
>
> It works fine, but the message indicates that it's not completely clean.
>   How should I write the exception correctly?

Your code is right; the latest official release of pychecker doesn't  
support Python 2.5.
The exception hierarchy has changed a little, now you have BaseException  
-> Exception -> all standard exceptions. KeyboardInterrupt and SystemExit  
are now direct subclasses of BaseException, not of Exception.
According to the changelog [1] the development version does support 2.5

[1]  
http://pychecker.cvs.sourceforge.net/pychecker/pychecker/CHANGELOG?view=markup

-- 
Gabriel Genellina

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

Custom widgets

2008-02-04 Thread Whiplash
I want to build some basic kid games for Linux (Ubuntu/EdUbuntu) and
so after looking around I've decided that Python would probably be the
path of least resistance.

I have a need to create custom gui elements (widgets?). I want these
games to be very graphical. Not 3d or anything crazy, I just want them
to look fun and so the buttons and things will all need to be custom.
For instance, I might have spaceship or alien shaped buttons and the
background of my window might be a photo of a galaxy. Make sense?

So I've started looking at Tkinter, and PyGTK but I don't want to get
too deep without knowing if they'll work for me. Do either of these
toolkits provide the ability to create custom widgets? Is there
something else I should be looking at instead? I'm not concerned with
cross platform compatibility, I just want to use something that will
be readily available in your average Ubuntu install.

Thanks!

Dana

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


Re: Catching a non-Exception object (KeyboardInterrupt)

2008-02-04 Thread Duncan Booth
Hrvoje Niksic <[EMAIL PROTECTED]> wrote:

> Michael Goerz <[EMAIL PROTECTED]> writes:
> 
>> when I try to catch ctrl+c with
>>
>> except KeyboardInterrupt:
>>
>> pychecker tells me
>>
>> Catching a non-Exception object (KeyboardInterrupt)
> 
> Looks like a pychecker bug.  It might be confused by KeyboardInterrupt
> being derived not from Exception, but from BaseException.
> 

There's a patch for this see: 
https://thomas.apestaart.org/thomas/trac/changeset/938?format=diff&new=938 
but it looks like pychecker isn't being maintained because nothing has 
changed for more than 2 years.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Catching a non-Exception object (KeyboardInterrupt)

2008-02-04 Thread Hrvoje Niksic
Michael Goerz <[EMAIL PROTECTED]> writes:

> when I try to catch ctrl+c with
>
> except KeyboardInterrupt:
>
> pychecker tells me
>
> Catching a non-Exception object (KeyboardInterrupt)

Looks like a pychecker bug.  It might be confused by KeyboardInterrupt
being derived not from Exception, but from BaseException.
-- 
http://mail.python.org/mailman/listinfo/python-list


Unexpected timing results with file I/O

2008-02-04 Thread Steven D'Aprano
After reading an earlier thread about opening and closing lots of files, 
I thought I'd do a little experiment.

Suppose you have a whole lot of files, and you need to open each one, 
append a string, then close them. There's two obvious ways to do it: 
group your code by file, or group your code by procedure.

# Method one: grouped by file.
for each file:
open the file, append the string, then close it


# Method two: grouped by procedure.
for each file:
open the file
for each open file:
append the string
for each open file:
close the file


If you have N files, both methods make the same number of I/O calls: N 
opens, N writes, N closes. Which is faster?

Intuitively, the first method has *got* to be faster, right? It's got one 
loop instead of three and it doesn't build an intermediate list of open 
file objects. It's so *obviously* going to be faster that it is hardly 
worth bothering to check it with timeit, right?

Well, I wouldn't be writing unless that intuitive result was wrong. So 
here's my test results:


Method 1:

>>> import timeit
>>> names = ['afile' + str(n) for n in range(1000)]
>>> T = timeit.Timer('''for name in names:
... fp = open(name, 'a'); fp.write('xyz\\n'); fp.close()
... ''', 'from __main__ import names')
>>> min(T.repeat(6, 500))
17.391216039657593


Method 2:

>>> for name in names:  # reset the files to an empty state.
... fp = open(name, 'w'); fp.close()
...
>>> T = timeit.Timer('''files = [open(name, 'a') for name in names]
... for fp in files:
... fp.write('xyz\\n')
... for fp in files:
... fp.close()
... ''', '''from __main__ import names''')
>>> min(T.repeat(6, 500))
16.823362112045288


Surprisingly, Method 2 is a smidgen faster, by about half a second over 
500,000 open-write-close cycles. It's not much faster, but it's 
consistent, over many tests, changing many of the parameters (e.g. the 
number of files, the number of runs per timeit test, etc.).

I'm using Linux and Python 2.5.

So, what's going on? Can anyone explain why the code which does more work 
takes less time?



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


Re: Does anyone else use this little idiom?

2008-02-04 Thread Steven D'Aprano
On Mon, 04 Feb 2008 15:08:44 +, Bob Martin wrote:

> Rexx's method is the way to do it : "do 50"

I tried writing Rexx code and executing it in Python, but I got 
unexpected results, mostly SyntaxError exceptions. Is that a bug in 
Python?



No-I'm-not-really-serious-ly yours, 


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


Re: Custom widgets

2008-02-04 Thread Diez B. Roggisch
Whiplash wrote:

> I want to build some basic kid games for Linux (Ubuntu/EdUbuntu) and
> so after looking around I've decided that Python would probably be the
> path of least resistance.
> 
> I have a need to create custom gui elements (widgets?). I want these
> games to be very graphical. Not 3d or anything crazy, I just want them
> to look fun and so the buttons and things will all need to be custom.
> For instance, I might have spaceship or alien shaped buttons and the
> background of my window might be a photo of a galaxy. Make sense?
> 
> So I've started looking at Tkinter, and PyGTK but I don't want to get
> too deep without knowing if they'll work for me. Do either of these
> toolkits provide the ability to create custom widgets? Is there
> something else I should be looking at instead? I'm not concerned with
> cross platform compatibility, I just want to use something that will
> be readily available in your average Ubuntu install.

Maybe using pygame is the better alternative. You don't have widgets there,
but an event-system + all rendering capabilities you can thing of.
Applications like FrozenBubble (which is written in Perl, but also based on
the SDL as pygame is) show off the power of this approach.

AFAIK there are even simple GUI-toolkits on top of pygame available.

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


Re: Does anyone else use this little idiom?

2008-02-04 Thread Bob Martin
in 332496 20080204 102153 "=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=" <[EMAIL 
PROTECTED]> wrote:
>> In Python, the direct translation of this is a for loop.  When the
>> index doesn't matter to me, I tend to write it as:
>>
>> for _ in xrange (1,n):
>>some code
>>
>> An alternative way of indicating that you don't care about the loop
>> index would be
>>
>> for dummy in xrange (1,n):
>>some code
>
>I usually use _ when I know that i18n doesn't matter. dummy is just to
>long when unpacking sequences:
>
>for dummy, email, dummy, dummy in persons:
>sendmail(email)
>
>for _, email, _, _ in persons:
>sendmail(email)

Rexx's method is the way to do it : "do 50"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting Python to fork?

2008-02-04 Thread Jon Ribbens
On 2008-02-04, Bernard <[EMAIL PROTECTED]> wrote:
> #Fork and commit suicide
> if os.fork():
> sys.exit(0)

I'm pretty sure that should be os._exit(0)

> #What to do in parent process

This is now the child process.

> sys.stdin = open('/dev/null')
> sys.stdout = open('/dev/null', 'w')
> sys.stderr = open('/dev/null', 'w')

I think that's changing Python's idea of stdin etc but not the
operating system's idea of them. You won't be closing the original
file descriptors, and if you run any subprocesses they will end up
with the original stdin/out/err. Unless sys.stdin is more magic
than I'm aware of.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python feature request : operator for function composition

2008-02-04 Thread Dustan
On Feb 2, 11:09 pm, Kay Schluehr <[EMAIL PROTECTED]> wrote:
[snip]

While you're waiting for it to be implemented, you can build your own
version as a decorator. Here's an example written in haste:

>>> class composer(object):
def __init__(self, *funcs):
self.funcs = funcs
def __and__(self, other):
if isinstance(other, composer):
return composer(*(self.funcs+other.funcs))
else:
return composer(*(self.funcs+(other,)))
def __call__(self, *args, **kargs):
for func in reversed(self.funcs):
args = (func(*args, **kargs),)
if kargs:
kargs = {}
return args[0]


>>> @composer
def double(x):
return 2*x

>>> @composer
def square(x):
return x*x

>>> double_square = double & square
>>> square_double = square & double
>>> double_square(2)
8
>>> square_double(2)
16
>>> double_square(3)
18
>>> square_double(3)
36
>>> double_square(4)
32
>>> square_double(4)
64

Probably not the best implementation, but you get the idea.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Too many open files

2008-02-04 Thread Gary Herron
AMD wrote:
> Hello,
>
> I need to split a very big file (10 gigabytes) into several thousand 
> smaller files according to a hash algorithm, I do this one line at a 
> time. The problem I have is that opening a file using append, writing 
> the line and closing the file is very time consuming. I'd rather have 
> the files all open for the duration, do all writes and then close them 
> all at the end.
> The problem I have under windows is that as soon as I get to 500 files I 
> get the Too many open files message. I tried the same thing in Delphi 
> and I can get to 3000 files. How can I increase the number of open files 
> in Python?
>
> Thanks in advance for any answers!
>
> Andre M. Descombes
>   
Try something like this:

Instead of opening several thousand files:

* Create several thousand lists.

* Open the input file and process each line, dropping it into the
correct list.

* Whenever a single list passes some size threshold, open its file,
write the batch, and immediately close the file. 

* Similarly at the end (or when the total of all lists passes sme size
threshold), loop through the several thousand lists, opening, writing,
and closing.

This will keep the open/write/closes operations to a minimum, and you'll
never have more than 2 files open at a time.  Both of those are wins for
you.

Gary Herron

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


Re: Python GUI toolkit

2008-02-04 Thread Chris Mellon
On Feb 4, 2008 9:57 AM, Kevin Walzer <[EMAIL PROTECTED]> wrote:
> Chris Mellon wrote:
>
> > Nitpick, but an important one. It emulates *look*. Not feel. Native
> > look is easy and totally insufficient for a "native" app - it's the
> > feel that's important.
>
> Is this opinion based on firsthand experience with use of the Tile/ttk
> widgets on any of the relevant platforms?
>
> I'm not a Windows user, so I can't speak about that platform, but I have
> worked very hard to make my Python-Tile-Tk app consistent with both the
> look and feel of OS X: keyboard shortcuts, menu behavior, and so on.
> It's mainly a matter of attention to detail, and listening to user
> feedback. I've gotten good feedback on my applications in recent months
> as I've implemented more and more platform native behavior, and sales of
> these applications (I'm a shareware developer) reflect that.
>
> I'd be interested to hear how, in your experience, Tk/Tile is inherently
> unable to deliver native platform "feel," in a way that reflects on the
> toolkit rather than the developer.  It's fine to focus on Windows if
> that's your area of expertise.


I didn't say inherently unable, I said the toolkit doesn't provide it.
Note that you said that you did a lot of work to follow OS X
conventions and implement behavior. The toolkit doesn't help you with
any of this. A mac-native toolkit (or one that strives for native
behavior, like wxPython)  eliminates a lot of this work (although, of
course, not all).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [2.4.2/Linux] Getting Python to fork?

2008-02-04 Thread Christian Heimes
Jon Ribbens wrote:
> Why? I don't think you do.
> Neither does BSD daemon.c or glibc daemon.c

The problem is well documented at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012

"""
The second fork _is_ necessary, Jonathan Bartlett, 2003/10/31
The first fork accomplishes two things - allow the shell to return, and
allow you to do a setsid().

The setsid() removes yourself from your controlling terminal. You see,
before, you were still listed as a job of your previous process, and
therefore the user might accidentally send you a signal. setsid() gives
you a new session, and removes the existing controlling terminal.

The problem is, you are now a session leader. As a session leader, if
you open a file descriptor that is a terminal, it will become your
controlling terminal (oops!). Therefore, the second fork makes you NOT
be a session leader. Only session leaders can acquire a controlling
terminal, so you can open up any file you wish without worrying that it
will make you a controlling terminal.

So - first fork - allow shell to return, and permit you to call setsid()

Second fork - prevent you from accidentally reacquiring a controlling
terminal.
"""

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


Re: Getting Python to fork?

2008-02-04 Thread Christian Heimes
Jon Ribbens wrote:
> I think that's changing Python's idea of stdin etc but not the
> operating system's idea of them. You won't be closing the original
> file descriptors, and if you run any subprocesses they will end up
> with the original stdin/out/err. Unless sys.stdin is more magic
> than I'm aware of.

Jon is correct here. You must close or redirect the underlying C file
descriptor. Python's sys.std streams don't magically do this for you
because Python keeps a backup of the standard streams for internal
purpose in sys.__std*__. os.dup2 is the best solution.

Christian

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


Pysqlite issue no attribute 'autocommit'

2008-02-04 Thread Andy Smith

Hi there,

  Im trying to run a Python based program which uses MySQL with python-sqlite 
and Im recieving this error,

'Connection' object has no attribute 'autocommit'

I´ve had a google for this and its seems like it may be a bug python-sqlite or 
sqlite bug , but also I tried searching 
for it on the python issue traker and didnt find anything. Is anyone else aware 
of this issue and any solution?

thanks for any help! Andy.

PS sorry if I didnt include much info, hoping its a known issue (and also I 
didnt write the code, so not sure what
else to include off the top of my head! :P)-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Getting Python to fork?

2008-02-04 Thread Bernard
On 3 fév, 21:52, Gilles Ganault <[EMAIL PROTECTED]> wrote:
> Hello
>
> I need to launch a Python script, and fork it so that the calling
> script can resume with the next step will the Python script keeps
> running.
>
> I tried those two, but they don't work, as the calling script is stuck
> until the Python script ends:
>
> sys.stdout = open(os.devnull, 'w')
>
> =
> #if os.fork():
> pid = os.fork()
> if pid > 0:
> sys.exit(0)
> =
>
> Should I use another library to do this?
>
> Thank you.

this works out for me:

def tryToFork(cbk, fork=True):
'''
If possible, start the process as a daemon under linux
otherwise start it normally under Windows

the 'fork' flag may deactivate the forking if it is set to False
'''

#UNIX/LINUX: FORK
if fork:
try:
#Fork and commit suicide
if os.fork():
sys.exit(0)

#What to do in parent process
else:
os.setsid()
sys.stdin = open('/dev/null')
sys.stdout = open('/dev/null', 'w')
sys.stderr = open('/dev/null', 'w')
cbk()

#WINDOWS: JUST RUN
except AttributeError:
cbk()

#PLAIN, NORMAL RUN
else:
cbk()

def whateverFunctionToFork():
pass
tryToFork(whateverFunctionToFork, True)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [2.4.2/Linux] Getting Python to fork?

2008-02-04 Thread Jon Ribbens
On 2008-02-04, Christian Heimes <[EMAIL PROTECTED]> wrote:
>> Although bear in mind it's pretty UNIX-y.
>
> IIRC you have to fork a second time after you have changed the working
> dir and created a new session group.

Why? I don't think you do.
Neither does BSD daemon.c or glibc daemon.c
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python GUI toolkit

2008-02-04 Thread Nikola Stjelja
On Feb 4, 2008 8:18 AM, David Cook <[EMAIL PROTECTED]> wrote:

> On 2008-02-03, [EMAIL PROTECTED] <[EMAIL PROTECTED]>
> wrote:
> > what would be the best python GUI toolkit, it must be cross platform.
> >
> > i have tried gtk, but it interface are real bad and its coding was
> difficult
> > so i dropped it,
> >
> > the only remaining are qt4 and wx, i would like to know if one of these
> or
> > any other toolkit is capable of creating good-looking GUI's, like in
> other
> > apps, for e.g, .net apps.
> >
> > i m a noob, and willing to learn, so difficulty is no problem
>
> The real way to see is by installing each along with their respective
> demos,
> looking over some of the demo code, and perhaps coding some simple apps in
> each.
>
> Qt4 has the most polished and complete API.  When judging looks under the
> X-Window System, make sure to run qtconfig and try some of the other
> themes.
>
> WX seems to have more design warts (the layout system takes some getting
> used to), but is quite capable and flexible and probably has the most
> native
> look and feel under win32.  The license is much more liberal for
> commercial
> use.  Also, it comes with OS/X (both Tiger and Leopard), is easy to
> install under MSWindows).


I wouldn't agree with you on that. I use wx on both  windows and gnome
respectively, and on both platforms wx widgets look and feel native. The
layout system is really simple to use, but powerfull, and if you've done
some web design in the past you are going to wrap your head over it just
after a few examples.

I think the most powerfull feature of the wxwidget toolkit it's a very
consistent API. Each widget is created in a very similar manner, so is very
easy to pick and implement new features of the widget. Which can speed up
the learning process quite a bit.

I would sincerly hope to see in the future a python distribution that
incorporates wxwidgets as its basic gui toolkit instead of tkinter.

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



-- 
Please visit this site and play my RPG!

http://www.1km1kt.net/rpg/Marinci.php

My Poetry Book:
http://www.lulu.de/browse/book_view.php?fCID=222964&fBuyItem=5

My OS Projects:

http://lab.linux.hr/~nstjelja/drawit/
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: PyOpenGL

2008-02-04 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote:

> Hi all,
> 
> I apologize if this question was already answered before but I was
> unable to find a proper solution to my problem. Anyways, I am trying
> to run shaderobjects.py on Windows (Python 2.5.1) by just double-
> clicking, and I got the following error:
> 
> [...]
> File "/usr/lib/python2.5/site-packages/OpenGL/extensions.py", line 13,
> in hasGLExtension
> AVAILABLE_GL_EXTENSIONS[:] = glGetString( GL_EXTENSIONS ).split()
> AttributeError: 'NoneType' object has no attribute 'split'
> 
> _I think_ I have all requirements and packages properly installed.
> 
> Any clues? Thanks in advance !

http://pyopengl.sourceforge.net/documentation/manual/glGetString.3G.html

"""
Notes
 If an error is generated, glGetString returns 0. 
"""

seems to be the case here. Check for whatever the error-code is - glGetError
is your friend.

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


Too many open files

2008-02-04 Thread AMD
Hello,

I need to split a very big file (10 gigabytes) into several thousand 
smaller files according to a hash algorithm, I do this one line at a 
time. The problem I have is that opening a file using append, writing 
the line and closing the file is very time consuming. I'd rather have 
the files all open for the duration, do all writes and then close them 
all at the end.
The problem I have under windows is that as soon as I get to 500 files I 
get the Too many open files message. I tried the same thing in Delphi 
and I can get to 3000 files. How can I increase the number of open files 
in Python?

Thanks in advance for any answers!

Andre M. Descombes
-- 
http://mail.python.org/mailman/listinfo/python-list


Python-URL! - weekly Python news and links (Feb 4)

2008-02-04 Thread Gabriel Genellina
QOTW:  "Everyone with a PC knows that eventually their computer will slow down,
crash unexpectedly, and develop problems with applications." - promotional
materials for award-winning *Degunking Windows* book

"It's a very good idea to read the entire FAQ as soon as you've gotten past
the very basic Python level, so that you can save yourself and others a lot
of time stumbling over the traditional problems that everyone goes through. 
You'll learn a lot of useful things in the process." - Peter Hansen


Module/package hierarchy and its separation from file structure:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/5a9580e76484f73/

Notes on running multiple interpreters in the same process:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/f044df524c426027/

Type annotations, type inference and static typing for Python:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/e7d39693de66e6f1/

Is Python "standardised" or not? (Wikipedia entry):

http://groups.google.com/group/comp.lang.python/browse_thread/thread/a828c6585be8a682/

Should be a better way of saying `do_something n
times` than this: `for i in range(n): do_something`

http://groups.google.com/group/comp.lang.python/browse_thread/thread/cd391ce323f3d8f6/

Dictionary comprehensions: discarded in the past, now on Python 3.0:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/8f3650ba5da77543/



Everything Python-related you want is probably one or two clicks away in
these pages:

Python.org's Python Language Website is the traditional
center of Pythonia
http://www.python.org
Notice especially the master FAQ
http://www.python.org/doc/FAQ.html

PythonWare complements the digest you're reading with the
marvelous daily python url
 http://www.pythonware.com/daily
Mygale is a news-gathering webcrawler that specializes in (new)
World-Wide Web articles related to Python.
 http://www.awaretek.com/nowak/mygale.html
While cosmetically similar, Mygale and the Daily Python-URL
are utterly different in their technologies and generally in
their results.

Just beginning with Python?  This page is a great place to start:
http://wiki.python.org/moin/BeginnersGuide/Programmers

The Python Papers aims to publish "the efforts of Python enthusiats":
http://pythonpapers.org/
The Python Magazine is a technical monthly devoted to Python:
http://pythonmagazine.com

Readers have recommended the "Planet" sites:
http://planetpython.org
http://planet.python.org

comp.lang.python.announce announces new Python software.  Be
sure to scan this newsgroup weekly.

http://groups.google.com/groups?oi=djq&as_ugroup=comp.lang.python.announce

Python411 indexes "podcasts ... to help people learn Python ..."
Updates appear more-than-weekly:
http://www.awaretek.com/python/index.html

Steve Bethard continues the marvelous tradition early borne by
Andrew Kuchling, Michael Hudson, Brett Cannon, Tony Meyer, and Tim
Lesher of intelligently summarizing action on the python-dev mailing
list once every other week.
http://www.python.org/dev/summary/

The Python Package Index catalogues packages.
http://www.python.org/pypi/

The somewhat older Vaults of Parnassus ambitiously collects references
to all sorts of Python resources.
http://www.vex.net/~x/parnassus/

Much of Python's real work takes place on Special-Interest Group
mailing lists
http://www.python.org/sigs/

Python Success Stories--from air-traffic control to on-line
match-making--can inspire you or decision-makers to whom you're
subject with a vision of what the language makes practical.
http://www.pythonology.com/success

The Python Software Foundation (PSF) has replaced the Python
Consortium as an independent nexus of activity.  It has official
responsibility for Python's development and maintenance.
http://www.python.org/psf/
Among the ways you can support PSF is with a donation.
http://www.python.org/psf/donate.html

Kurt B. Kaiser publishes a weekly report on faults and patches.
http://www.google.com/groups?as_usubject=weekly%20python%20patch

Although unmaintained since 2002, the Cetus collection of Python
hyperlinks retains a few gems.
http://www.cetus-links.org/oo_python.html

Python FAQTS
http://python.faqts.com/

The Cookbook is a collaborative effort to capture useful and
interesting recipes.
http://aspn.activestate.com/ASPN/Cookbook/Python

Many Python conferences around the world are in preparation.
Watch this space for links to them

Re: Python feature request : operator for function composition

2008-02-04 Thread Arnaud Delobelle
On Feb 4, 3:00 pm, Dustan <[EMAIL PROTECTED]> wrote:
> On Feb 2, 11:09 pm, Kay Schluehr <[EMAIL PROTECTED]> wrote:
> [snip]
>
> While you're waiting for it to be implemented, you can build your own
> version as a decorator. Here's an example written in haste:
>
> >>> class composer(object):
>
> def __init__(self, *funcs):
> self.funcs = funcs
> def __and__(self, other):
> if isinstance(other, composer):
> return composer(*(self.funcs+other.funcs))
> else:
> return composer(*(self.funcs+(other,)))
> def __call__(self, *args, **kargs):
> for func in reversed(self.funcs):
> args = (func(*args, **kargs),)
> if kargs:
> kargs = {}
> return args[0]
>
> >>> @composer
>
> def double(x):
> return 2*x
>
> >>> @composer
>
> def square(x):
> return x*x
>
> >>> double_square = double & square
> >>> square_double = square & double
> >>> double_square(2)
> 8
> >>> square_double(2)
> 16
> >>> double_square(3)
> 18
> >>> square_double(3)
> 36
> >>> double_square(4)
> 32
> >>> square_double(4)
>
> 64
>
> Probably not the best implementation, but you get the idea.

This is nice.
* I wouldn't choose '&' as the composing operator as when I read
'double & square' I think 'take an x, double it & square it' which is
the wrong interpretation (perhaps << instead?).
* I would call the decorator 'composable'.

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


Re: Unexpected timing results with file I/O

2008-02-04 Thread Christian Heimes
Steven D'Aprano wrote:
> So, what's going on? Can anyone explain why the code which does more work 
> takes less time?

Short answer: CPU and RAM are much faster than hard disks.

The three loops and the creation of a list costs only a few CPU cycles
compared to flushing the new data to disk.

Christian

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


Re: Python-URL! - weekly Python news and links (Feb 4)

2008-02-04 Thread Arnaud Delobelle
On Feb 4, 4:24 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> "It's a very good idea to read the entire FAQ as soon as you've gotten past
> the very basic Python level, so that you can save yourself and others a lot
> of time stumbling over the traditional problems that everyone goes through.
> You'll learn a lot of useful things in the process." - Peter Hansen

Maybe that should go in the FAQ ;-)

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


Client side GUI-like web framework ?

2008-02-04 Thread USCode
Wouldn't it be handy if there was a web framework that allowed you to 
create pages and control the interface like you would using a 
client-side GUI framework such as Tkinter?

The framework would need a small, fast web server that would 
automatically fire up when you ran your application and you then control 
the interface just like you would with client-side GUI widgets (within 
the limitations of browsers of course).  It would handle all the 
complexities of HTTP, HTML, Javascript, etc. letting you focus on adding 
functionality to your application.

Essentially you would be using the browser as your cross-platform 
client-side interface.  You would just interact with all the widgets 
like trees, grids, paned windows, checkboxes, buttons, etc.

There wouldn't happen to be anything like that available, would there? 
I've seen CherryPy but that doesn't quite seem to be what I described.

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


Re: Python GUI toolkit

2008-02-04 Thread Chris Mellon
On Feb 4, 2008 10:46 AM, Kevin Walzer <[EMAIL PROTECTED]> wrote:
> Chris Mellon wrote:
>
> >
> > I didn't say inherently unable, I said the toolkit doesn't provide it.
> > Note that you said that you did a lot of work to follow OS X
> > conventions and implement behavior. The toolkit doesn't help you with
> > any of this. A mac-native toolkit (or one that strives for native
> > behavior, like wxPython)  eliminates a lot of this work (although, of
> > course, not all).
>
>
> If the toolkit doesn't provide it, then I think that means "inherently
> unable."
>

Of course it doesn't, because you can implement it yourself. Which is
what you do. Tile itself is inherently unable to provide this because
it's a theming mechanism.

> On the Mac, Tk provides a mechanism that allows hooking up arbitrary
> events to arbitrary keyboard. Something like:
>
>   self.bind('', lambda event:
> self.authorizeCommand(self.installPackage))
>
> I have to specify the key-combination--that's all. One line of code. No
> implementing of keyboard bindings in C or at the Tcl/Tk or even
> Python-Tkinter level--it's all already defined by the toolkit.
>

I know how toolkits work.

> By contrast, Cocoa/Objective-C has a rather complex system for defining
> keyboard events:
>
> http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/HandlingKeyEvents/chapter_6_section_1.html#//apple_ref/doc/uid/1060i-CH7-SW1
>
> PyObjC is a thin wrapper over Objective-C, so you'd have to do the
> equivalent calls in Python to implement custom keyboard behavior.
>
> It is true that Cocoa some convenience methods in Interface Builder,
> i.e. common menu commands don't require any extra code: if you are
> developing a document-based application, "Command-C" is implemented  in
> the menu and in the frameworks as "copy (text, image, whatever) to
> clipboard." But "Command-C" also works identically in the Tk console.
>
> Tk does lack some things. It doesn't support drag-and-drop on the Mac.
> It doesn't hook into some of the newer Mac visual styles. Getting access
> to this would require an extension or patching Tk's core. In those
> events, I emulate the behavior or appeareance, which Tk makes very easy.
>

I'm not sure how you keep referring to things you implement yourself
as something that the toolkit supports. As a brief example, some
features that wxPython provides to aid in native "feel" and not just
look:

Stock button support allows you to have native button location, order
and labelling.
Sizer support allows for differently sized controls to be positioned
with the same layout.
Stock menu IDs (like preferences and "About this application...") will
be labelled and placed correctly.
Accelerators will automatically map between Command and Control.
Standard path support to find appropriate user directories.


All of these are things that you can do yourself, of course. You could
have implemented Tile yourself, too, to get the (mostly) native look.
The point is what the toolkit provides, and Tk+Tile, while it mostly
provides native look, does *not* provide native feel. If you want
native feel, you need to implement it yourself.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Too many open files

2008-02-04 Thread Gabriel Genellina
En Mon, 04 Feb 2008 12:50:15 -0200, Christian Heimes <[EMAIL PROTECTED]>  
escribi�:

> Jeff wrote:
>> Why don't you start around 50 threads at a time to do the file
>> writes?  Threads are effective for IO.  You open the source file,
>> start a queue, and start sending data sets to be written to the
>> queue.  Your source file processing can go on while the writes are
>> done in other threads.
>
> I'm sorry, but you are totally wrong. Threads are a very bad idea for IO
> bound operation. Asynchronous event IO is the best answer for any IO
> bound problem. That is select, poll, epoll, kqueue or IOCP.

The OP said that he has this problem on Windows. The available methods  
that I am aware of are:
- using synchronous (blocking) I/O with multiple threads
- asynchronous I/O using OVERLAPPED and wait functions
- asynchronous I/O using IO completion ports

Python does not (natively) support any of the latter ones, only the first.  
I don't have any evidence proving that it's a very bad idea as you claim;  
altough I wouldn't use 50 threads as suggested above, but a few more than  
the number of CPU cores.

-- 
Gabriel Genellina

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

Re: Elementary string-parsing

2008-02-04 Thread Marc 'BlackJack' Rintsch
On Mon, 04 Feb 2008 09:43:04 +, Odysseus wrote:

> In article <[EMAIL PROTECTED]>,
>  Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> 
>> def extract_data(names, na, cells):
>> found = dict()
> 
> The problem with initializing the 'super-dictionary' within this 
> function is that I want to be able to add to it in further passes, with 
> a new set of "names" & "cells" each time.

Then you can either pass in `found` as argument instead of creating it
here, or you collect the passes in the calling code with the `update()`
method of `dict`.  Something like this:

found = dict()
for pass in passes:
# ...
found.update(extract_data(names, na, cells))

> BTW what's the difference between the above and "found = {}"?

I find it more "explicit".  ``dict`` and ``list`` are easier to
distinguish than ``{}`` and ``[]`` after a lng coding session or when
printed/displayed in a small font.  It's just a matter of taste.

>> for i, name in enumerate(names):
>> data = dict()
>> cells_index = 10 * i + na
>> for cell_name, index, parse in (('epoch1', 0, parse_date),
>> ('epoch2', 1, parse_date),
>> ('time', 5, parse_number),
>> ('score1', 6, parse_number),
>> ('score2', 7, parse_number)):
>> data[cell_name] = parse(cells[cells_index + index])
> 
> This looks a lot more efficient than my version, but what about the 
> strings that don't need parsing? Would it be better to define a 
> 'pass-through' function that just returns its input, so they can be 
> handled by the same loop, or to handle them separately with another loop?

I'd handle them in the same loop.  A "pass-through" function for strings
already exists:

In [255]: str('hello')
Out[255]: 'hello'

>> assert name.startswith('Name: ')
> 
> I looked up "assert", but all I could find relates to debugging. Not 
> that I think debugging is something I can do without ;) but I don't 
> understand what this line does.

It checks if `name` really starts with 'Name: '.  This way I turned the
comment into code that checks the assertion in the comment.

>> The `parse_number()` function could look like this:
>> 
>> def parse_number(string):
>> try:
>> return float(string.replace(',', ''))
>> except ValueError:
>> return string
>> 
>> Indeed the commas can be replaced a bit more elegant.  :-)
> 
> Nice, but I'm somewhat intimidated by the whole concept of 
> exception-handling (among others). How do you know to expect a 
> "ValueError" if the string isn't a representation of a number?

Experience.  I just tried what happens if I feed `float()` with a string
that is no number:

In [256]: float('abc')
---
Traceback (most recent call last)

/home/bj/ in ()

: invalid literal for float(): abc

> Is there a list of common exceptions somewhere? (Searching for
> "ValueError" turned up hundreds of passing mentions, but I couldn't find
> a definition or explanation.)

The definition is quite vague.  The type of an argument is correct, but
there's something wrong with the value.

See http://docs.python.org/lib/module-exceptions.html for an overview of
the built in exceptions.

>> As already said, that ``while`` loop should be a ``for`` loop.  But if
>> you put `m_abbrevs` into a `list` you can replace the loop with a
>> single call to its `index()` method: ``dlist[1] =
>> m_abbrevs.index(dlist[1]) + 1``.
> 
> I had gathered that lists shouldn't be used for storing constants. Is
> that more of a suggestion than a rule?

Some suggest this.  Others say tuples are for data where the position of
an element has a "meaning" and lists are for elements that all have the
same "meaning" for some definition of meaning.  As an example ('John',
'Doe', 'Dr.') vs. ['Peter', 'Paul', 'Mary'].  In the first example we have
name, surname, title and in the second example all elements are just
names.  Unless the second example models a relation like child, father,
mother, or something like that.  Anyway, if you can make the source simpler
and easier to understand by using the `index()` method, use a list.  :-)

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


Re: [2.4.2/Linux] Getting Python to fork?

2008-02-04 Thread Jon Ribbens
On 2008-02-04, Christian Heimes <[EMAIL PROTECTED]> wrote:
> Jon Ribbens wrote:
>> Why? I don't think you do.
>> Neither does BSD daemon.c or glibc daemon.c
>
> The problem is well documented at
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012

OK I understand what is being said here, although it seems a bit
unlikely (daemon process opens a tty), but what is puzzling me is
why, if it's so necessary, does neither Linux nor *BSD do this in
their daemon() functions? It would seem surprising if the operating
system authors don't know how to make a daemon process in their
own OS ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected timing results with file I/O

2008-02-04 Thread rdahlstrom
It doesn't matter how many doors opening and closing there are, it
matters the order in which the opening, walking through, and closing
are done.  That's my point.  In the second example, all of the disk
operations are done at the same time.  That's what I meant by people
going through the doors.  Maybe it was more clear in my head.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Client side GUI-like web framework ?

2008-02-04 Thread USCode
[EMAIL PROTECTED] wrote:
> You just described what XUL aims to be
> http://developer.mozilla.org/en/docs/The_Joy_of_XUL
> http://developer.mozilla.org/en/docs/XULRunner
> 
> At present it lacks for sure documentation (or maybe it isn't
> organized really well)

Just took a look at XUL and it in some ways describes what I was 
thinking except it doesn't appear to deliver it's interface via a 
browser/web server.  Then your application wouldn't be accessible via a 
web browser through the internet.  The XUL application still appears to 
only execute locally on the client machine?

Also, personally I find having to describe your interface directly via 
XML (XUL) is just plain ugly.

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


Re: Unexpected timing results with file I/O

2008-02-04 Thread Marc 'BlackJack' Rintsch
On Mon, 04 Feb 2008 10:48:32 -0800, rdahlstrom wrote:

> It doesn't matter how many doors opening and closing there are, it
> matters the order in which the opening, walking through, and closing
> are done.  That's my point.  In the second example, all of the disk
> operations are done at the same time.  That's what I meant by people
> going through the doors.  Maybe it was more clear in my head.

But my timing shows that method two is slower on my computer.  So there is
no obvious winner.

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


Re: Unexpected timing results with file I/O

2008-02-04 Thread Carl Banks
On Feb 4, 12:53 pm, rdahlstrom <[EMAIL PROTECTED]> wrote:
> On Feb 4, 10:17 am, Steven D'Aprano <[EMAIL PROTECTED]
>
>
>
> cybersource.com.au> wrote:
> > After reading an earlier thread about opening and closing lots of files,
> > I thought I'd do a little experiment.
>
> > Suppose you have a whole lot of files, and you need to open each one,
> > append a string, then close them. There's two obvious ways to do it:
> > group your code by file, or group your code by procedure.
>
> > # Method one: grouped by file.
> > for each file:
> > open the file, append the string, then close it
>
> > # Method two: grouped by procedure.
> > for each file:
> > open the file
> > for each open file:
> > append the string
> > for each open file:
> > close the file
>
> > If you have N files, both methods make the same number of I/O calls: N
> > opens, N writes, N closes. Which is faster?
>
> > Intuitively, the first method has *got* to be faster, right? It's got one
> > loop instead of three and it doesn't build an intermediate list of open
> > file objects. It's so *obviously* going to be faster that it is hardly
> > worth bothering to check it with timeit, right?
>
> > Well, I wouldn't be writing unless that intuitive result was wrong. So
> > here's my test results:
>
> > Method 1:
>
> > >>> import timeit
> > >>> names = ['afile' + str(n) for n in range(1000)]
> > >>> T = timeit.Timer('''for name in names:
>
> > ... fp = open(name, 'a'); fp.write('xyz\\n'); fp.close()
> > ... ''', 'from __main__ import names')>>> min(T.repeat(6, 500))
>
> > 17.391216039657593
>
> > Method 2:
>
> > >>> for name in names:  # reset the files to an empty state.
>
> > ... fp = open(name, 'w'); fp.close()
> > ...>>> T = timeit.Timer('''files = [open(name, 'a') for name in names]
>
> > ... for fp in files:
> > ... fp.write('xyz\\n')
> > ... for fp in files:
> > ... fp.close()
> > ... ''', '''from __main__ import names''')>>> min(T.repeat(6, 500))
>
> > 16.823362112045288
>
> > Surprisingly, Method 2 is a smidgen faster, by about half a second over
> > 500,000 open-write-close cycles. It's not much faster, but it's
> > consistent, over many tests, changing many of the parameters (e.g. the
> > number of files, the number of runs per timeit test, etc.).
>
> > I'm using Linux and Python 2.5.
>
> > So, what's going on? Can anyone explain why the code which does more work
> > takes less time?
>
> > --
> > Steven
>
> The code that does more work takes more time.  The second one does
> quite a bit less work.  Think of it like this:
>
> You have 500,000 people to fit through a door.  Here are your options:
>
> 1.  For each person, open the door, walk through the door, then close
> the door.
> 2.  Open the door, allow everyone to walk through, then close the
> door.
>
> Which one would you say would be a more efficient way to fit 500,000
> people through the door?

Bad analogy.  A better analogy would be if each person has their own
door to walk through.

My hunch is that is has to do with the OS I/O scheduling.  Closing a
file triggers a cache flush, which in turn triggers the I/O to
schedule a write to disk; the OS scheduler is perhaps more efficient
(for a given number of total writes) when it can combine many writes
at the same time.


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


Re: Unexpected timing results with file I/O

2008-02-04 Thread rdahlstrom
On Feb 4, 10:17 am, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> After reading an earlier thread about opening and closing lots of files,
> I thought I'd do a little experiment.
>
> Suppose you have a whole lot of files, and you need to open each one,
> append a string, then close them. There's two obvious ways to do it:
> group your code by file, or group your code by procedure.
>
> # Method one: grouped by file.
> for each file:
> open the file, append the string, then close it
>
> # Method two: grouped by procedure.
> for each file:
> open the file
> for each open file:
> append the string
> for each open file:
> close the file
>
> If you have N files, both methods make the same number of I/O calls: N
> opens, N writes, N closes. Which is faster?
>
> Intuitively, the first method has *got* to be faster, right? It's got one
> loop instead of three and it doesn't build an intermediate list of open
> file objects. It's so *obviously* going to be faster that it is hardly
> worth bothering to check it with timeit, right?
>
> Well, I wouldn't be writing unless that intuitive result was wrong. So
> here's my test results:
>
> Method 1:
>
> >>> import timeit
> >>> names = ['afile' + str(n) for n in range(1000)]
> >>> T = timeit.Timer('''for name in names:
>
> ... fp = open(name, 'a'); fp.write('xyz\\n'); fp.close()
> ... ''', 'from __main__ import names')>>> min(T.repeat(6, 500))
>
> 17.391216039657593
>
> Method 2:
>
> >>> for name in names:  # reset the files to an empty state.
>
> ... fp = open(name, 'w'); fp.close()
> ...>>> T = timeit.Timer('''files = [open(name, 'a') for name in names]
>
> ... for fp in files:
> ... fp.write('xyz\\n')
> ... for fp in files:
> ... fp.close()
> ... ''', '''from __main__ import names''')>>> min(T.repeat(6, 500))
>
> 16.823362112045288
>
> Surprisingly, Method 2 is a smidgen faster, by about half a second over
> 500,000 open-write-close cycles. It's not much faster, but it's
> consistent, over many tests, changing many of the parameters (e.g. the
> number of files, the number of runs per timeit test, etc.).
>
> I'm using Linux and Python 2.5.
>
> So, what's going on? Can anyone explain why the code which does more work
> takes less time?
>
> --
> Steven



The code that does more work takes more time.  The second one does
quite a bit less work.  Think of it like this:

You have 500,000 people to fit through a door.  Here are your options:

1.  For each person, open the door, walk through the door, then close
the door.
2.  Open the door, allow everyone to walk through, then close the
door.

Which one would you say would be a more efficient way to fit 500,000
people through the door?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python feature request : operator for function composition

2008-02-04 Thread Kay Schluehr
This won't work for builtin functions. It hardly works for functions
and methods defined in 3rd party modules and in no way for functions
defined in C extensions. It adds boilerplate statically to remove it
at runtime.

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


Pydev 1.3.13 Released

2008-02-04 Thread Fabio Zadrozny
Hi All,

Pydev and Pydev Extensions 1.3.13 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:
-

* Debug Console: will print exceptions raised during the evaluation.
* Debug Console: will print the result of the evaluation if a valid
statement is sent (so, 'print' is not needed for simple evaluations
anymore).


Release Highlights in Pydev:
--

* Outline view: working correctly again.
* Keybinding conflict: Alt+shift+T+XXX refactoring keybindings are now
only defined in the pydev scope.
* Hyperlink: Using new hyperlink mechanism (added at Eclipse 3.3).


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

ESSS - Engineering Simulation and Scientific Software
http://www.esss.com.br

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: Too many open files

2008-02-04 Thread Duncan Booth
Steven D'Aprano <[EMAIL PROTECTED]> wrote:

> On Mon, 04 Feb 2008 13:57:39 +0100, AMD wrote:
> 
>> The problem I have under windows is that as soon as I get to 500 files I
>> get the Too many open files message. I tried the same thing in Delphi
>> and I can get to 3000 files. How can I increase the number of open files
>> in Python?


> Windows XP has a limit of 512 files opened by any process, including 
> stdin, stdout and stderr, so your code is probably failing after file 
> number 509.

No, the C runtime has a limit of 512 files, the OS limit is actually 2048. 
See http://msdn2.microsoft.com/en-us/library/6e3b887c(VS.71).aspx

> I don't know how Delphi works around that issue. Perhaps one of the 
> Windows gurus can advise if there's a way to increase that limit from
> 512? 
> 

Call the C runtime function _setmaxstdio(n) to set the maxmimum the number 
of open files to n up to 2048. Alternatively os.open() and os.write() 
should bypass the C runtime limit.

It would probably be better though to implement some sort of caching scheme 
in memory and avoid having to mess with the limits at all. Or do it in two 
passes: creating 100 files on the first pass and splitting each of those in 
a second pass.

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


Re: Python GUI toolkit

2008-02-04 Thread Kevin Walzer
Chris Mellon wrote:

> Nitpick, but an important one. It emulates *look*. Not feel. Native
> look is easy and totally insufficient for a "native" app - it's the
> feel that's important.

Is this opinion based on firsthand experience with use of the Tile/ttk 
widgets on any of the relevant platforms?

I'm not a Windows user, so I can't speak about that platform, but I have 
worked very hard to make my Python-Tile-Tk app consistent with both the 
look and feel of OS X: keyboard shortcuts, menu behavior, and so on. 
It's mainly a matter of attention to detail, and listening to user 
feedback. I've gotten good feedback on my applications in recent months 
as I've implemented more and more platform native behavior, and sales of 
these applications (I'm a shareware developer) reflect that.

I'd be interested to hear how, in your experience, Tk/Tile is inherently 
unable to deliver native platform "feel," in a way that reflects on the 
toolkit rather than the developer.  It's fine to focus on Windows if 
that's your area of expertise.

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pysqlite issue no attribute 'autocommit' RESOLVED

2008-02-04 Thread Andy Smith
Ok, simple fix... Updated to MySQL_python-1.2.2 and all ok now! :D

  - Original Message - 
  From: Andy Smith 
  To: [email protected] 
  Sent: Monday, February 04, 2008 3:45 PM
  Subject: Pysqlite issue no attribute 'autocommit'



  Hi there,

Im trying to run a Python based program which uses MySQL with python-sqlite 
and Im recieving this error,

  'Connection' object has no attribute 'autocommit'

  I´ve had a google for this and its seems like it may be a bug python-sqlite 
or sqlite bug , but also I tried searching 
  for it on the python issue traker and didnt find anything. Is anyone else 
aware of this issue and any solution?

  thanks for any help! Andy.

  PS sorry if I didnt include much info, hoping its a known issue (and also I 
didnt write the code, so not sure what
  else to include off the top of my head! :P)-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Terse Syntax through External Methods

2008-02-04 Thread aggergren
On Jan 29, 11:50 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Jens schrieb:
>
>
>
> > On Jan 25, 3:19 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> >> Jens schrieb:
>
> >>> Hello Everyone
> >>> I'm newbie toZopeand i have a few questions regarding external
> >>> methods. What i wan't to do
> >>> is provide a terse syntax for converting  urls to special tracking
> >>> urls:
> >>> http://myurl/')">
> >>> turns the provided url into something like
> >>>http://host/tracking?url=http%3A%2F%2Fmyurl%2F
> >>> in the output.
> >>> i've been trying to use a external procedure like this.
> >>> ## Script (Python) "track_link"
> >>> ##bind container=container
> >>> ##bind context=context
> >>> ##bind namespace=_
> >>> ##bind script=script
> >>> ##bind subpath=traverse_subpath
> >>> ##parameters=self,url
> >>> ##title=track link
> >>> ##
> >>> return "%s%s" % (self.tracking_prefix, url_quote(url))
> >>> This doesn't work because because the method doesn't have access to
> >>> the environment. Obviously I don't wan't to pass everything explicitly
> >>> into the function as this would defeat the purpose of the exercise,
> >>> namely to provide a terse syntax.
> >>> I have a background in other languages so I might be missing something
> >>> conceptually with regardZopeand DTML.. Is there a better was of
> >>> doing this, perhaps without using external methods? Currently im doing
> >>> the following which isn't very elegant:
> >>> in content document
> >>> http://www.mylink.com/";> >>> tracking>">link
> >>> ...
> >>> tracking:
> >>> 
> >>> Appreciate any input you might have on this-
> >> Is it really needed to use an external method for this, or isn't a
> >> "normal" python script enough already?
>
> >> If it has to be an External method, you can't access such a context
> >> AFAIK. But then you can create a python script that _has_ this context,
> >> and passese it to the external method. Not the nicest solution, but
> >> should work.
>
> >> Diez
>
> > Like I said i'm a newbie. I though the deal withZopewas that i
> > couldn't really do inline scripting (for security reasons)
> > like in php but had to use these external methods. how does one go
> > about creating a "normal" python script exactly and
> > how do I invoke it's functionality?
>
> Read the docs:
>
> http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/Scripting...
>
> There's everything in there you need.
>
> Diez

Thanks, this is exactly what i needed. And Btw. the answer is to use
the 'context' keyword.

- Jens

P.S. And thanks to everyone else for their feedback :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [2.4.2/Linux] Getting Python to fork?

2008-02-04 Thread Rolf van de Krol
To create a deamon, you indeed need to fork two times. For more 
information and a working example see: 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731 . I'm 
quite sure this works, because I used it several times to create a deamon.


Jon Ribbens wrote:
> On 2008-02-04, Christian Heimes <[EMAIL PROTECTED]> wrote:
>   
>>> Although bear in mind it's pretty UNIX-y.
>>>   
>> IIRC you have to fork a second time after you have changed the working
>> dir and created a new session group.
>> 
>
> Why? I don't think you do.
> Neither does BSD daemon.c or glibc daemon.c
>   
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python feature request : operator for function composition

2008-02-04 Thread Dustan
On Feb 4, 10:11 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:
> This is nice.

Thanks.

> * I wouldn't choose '&' as the composing operator as when I read
> 'double & square' I think 'take an x, double it & square it' which is
> the wrong interpretation (perhaps << instead?).

A very good point that I didn't think about; I just blindly took the
OP's chosen operator. Another thing I realized after writing this was
that I could have also written a corresponding __rand__ method
(__rlshift__ with your alternative operator), in case the object to
the right of the operator is a composer object and to the left is a
simple function.

> * I would call the decorator 'composable'.

The thing about that, though, is that this can also be used as a
composition in function style. However, I can't think of any name that
encompasses both uses. And you're right in that composer wasn't a very
good choice of name. As I say, it was written in haste.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python GUI toolkit

2008-02-04 Thread bockman

>
> Another toolkit you might look into is Tkinter. I think it is something
> like the "official" toolkit for python. I also think it is an adapter
> for other toolkits, so it will use gtk widgets on gnome, qt widgets on
> kde and some other strange widgets on windows.
>

Not t so,  AFAIK. Tkinter is the python adapter for Tk, the toolkit
originally developed for Tcl language.

The latest version of Tk (not yet integrated in Python, maybe in 2.6)
has themes, which emulates
the look-and-feel of native toolkit at list for XP and OS X. For unix,
the last time I checked, there was only a theme that looked like a
plainer version of Gtk default theme. No Gnome or Kde themes yet.

The latest version of Tk also increased the set of available widgets,
which now is similar to the set of widgets offered by Qt/Gtk..
However, how much of these widgets will be available through Tkinter
will depend on people stepping in and upgrading Tkinter beyond simpy
ensuring that the old widgets still works. Given that many GUI-
developing python programmers have moved to other toolkits, I'm not
sure this will ever happen.

Ciao

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


Re: Extending the import mechanism - what is recommended?

2008-02-04 Thread dbr517
On Jan 29, 2:36 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > I need to extend the import mechanism to support another file type.
> > I've already written the necessary C library to read the file and
> > return a python code object.
>
> > I found one example which just sub-classed imputil.ImportManager like
> > this:
>
> > from myLib import pye_code as pye_code
> > class MyImporter(imputil.ImportManager):
> > def __init__(self):
> > imputil.ImportManager.__init__(self)
> > self.add_suffix('.pye', self.import_pye)
> > self.install()
>
> > def import_pye(self, filepath, fileinfo, filename):
> > data = pye_code(filepath)
> > return 0, data, {}
>
> > This actually works fine if the module is just a few lines of code,
> > but it won't chain to the "built-in" importers; if the module that I'm
> > importing does something as simple as 'import re', it fails.
>
> > It may be that my confusion here is because (even after reading the
> > code), I'm not clear on the purposes of imputil.ImportManager vs.
> > imputil.Importer :-(
>
> > What is the "preferred" way to do this type of extension?  One other
> > note; at this time, I just need to import individual module files with
> > this extension; I don't need to import packages.
>
> Here's an importer I wrote some time ago to bring modules in from a
> relational database. I won't trouble you with the code to compile the
> modules/packages and add them into the database, but perhaps the
> attached code will be enough to show you what you are doing that's not
> working.
>
> regards
>   Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
> [dbimp.py]#
> # Import modules from a database
> #
> # NOTE: could use sys version info to select appropriate module version
> #   - could ask whether to install if absent ... heh, heh :-)
> #
> import sys, db, marshal
> VER = sys.hexversion
--->
> def install():
> sys.path_hooks.append(dbimporter)
> sys.path_importer_cache.clear() # probably not necessary
> sys.path.insert(0, "*db*") # probably not needed with a metea-path hook?

Steve -

Thanks!  Got this to work with one interesting problem . . . if I use
sys.path.insert(0) and insert my hook at the head of the path,
then I can't import anything EXCEPT my special modules . . . If I use
sys.path.append("*pye*") then I'm OK.

One thing I changed was that my load_module function raises
ImportError if it fails; my understanding from reading PEP302 is that
that's what's SUPPOSED to happen . . .

At any rate, I can now import my custom modules . . . thanks!

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


Re: type, object hierarchy?

2008-02-04 Thread Jason
On Feb 3, 10:31 pm, 7stud <[EMAIL PROTECTED]> wrote:
> On Feb 3, 10:28 pm, 7stud <[EMAIL PROTECTED]> wrote:
>
> > From the docs:
>
> > issubclass(class, classinfo)
> > Return true if class is a subclass (direct or indirect) of classinfo.
>
> print issubclass(Dog, object)  #True
> print issubclass(type, object)  #True
> print issubclass(Dog, type)   #False

Yes.  That is exactly what is expected.

Dog is not subclassed from type.  It is subclassed from Mammals, which
is subclassed from object.

Dog is, however, an instance of type.  This is true of all new-style
Python classes.

All of Python's classes are objects.  They have to be an instance of /
something/.  New-style classes are instances of the class type.  That
doesn't mean that they are considered subclasses from class type.
This means that the Dog object has all the methods, properties, and
class data that the class type has.  Class type is Dog's metaclass --
the class of the class.

If you create an instance of Dog, you can access the various class
attributes that Dog has through the instance.  It doesn't mean that
the instance of Dog is subclassed from Dog.

>>> terrier = Dog()
>>> issubclass(terrier, Dog)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: issubclass() arg 1 must be a class

In Python, you can create a subclass of class type.  This isn't very
useful in most cases.  If you set the __metaclass__ magick attribute,
however, your class object will be an instance of the class referred
to by that attribute.  This lets you experiment with the way classes
are created and how the MRO works, and lets you break Python at a
pretty low level.  For example:

>>> class NewTalky(type):
... def __new__(*args, **keyargs):
... print "I am called when Python creates a class whose
metaclass is me."
... print "I am responsible for creating a class object."
... print "My ordered arguments:", args
... print "My keyword arguments:", keyargs
... classObject = type.__new__(*args, **keyargs)
... print "The result of type.__new__():", classObject
... return classObject
...
>>> class Reptile(object):
... __metaclass__ = NewTalky
...
I am called when Python creates a class whose metaclass is me.
I am responsible for creating a class object.
My ordered arguments: (, 'Reptile', (,), {'__module__': '__main__', '__metaclass__': })
My keyword arguments: {}
The result of type.__new__(): 
>>>
>>> type(Dog)  # Dog is an instance of class type

>>> type(Reptile)  # Reptile is an instance of class NewTalky

>>>
>>> issubclass(Reptile, type)  # Reptile is not subclassed from type...
False
>>> issubclass( type(Reptile), type )  # ...but NewTalky is
True
>>> issubclass( type(Dog), type )
True
>>>


Again, everything in Python is an object.  Your Python classes are
class objects.  They are instances of the class type.  In the first
example above, terrier is an instance of class Dog, but is not a
subclass of class Dog.  Similarly, Dog is an instance of class type,
not a subclass of class type.  Since class Dog is an instance of class
type, class type is considered to be Dog's metaclass.

You can create your own metaclasses.  To quote another Pythoneer, if
you think you need a metaclass, you probably don't.  If you know you
need a metaclass, you definitely do not need a metaclass.  They are
tricky and mind-bending.

Hope this helps clear things up for you.

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


Re: [2.4.2/Linux] Getting Python to fork?

2008-02-04 Thread Jon Ribbens
On 2008-02-04, Gilles Ganault <[EMAIL PROTECTED]> wrote:
>   I need to launch a Python script, and fork it so that the calling
> script can resume with the next step will the Python script keeps
> running.
>
> I tried those two, but they don't work, as the calling script is stuck
> until the Python script ends:

This should work I believe:

  if os.fork():
os._exit(0)
  os.setsid()
  os.chdir("/")
  fd = os.open("/dev/null", os.O_RDWR)
  os.dup2(fd, 0)
  os.dup2(fd, 1)
  os.dup2(fd, 2)
  if fd > 2:
os.close(fd)
  # do stuff

Although bear in mind it's pretty UNIX-y.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyOpenGL

2008-02-04 Thread Mike C. Fletcher
[EMAIL PROTECTED] wrote:
> Hi all,
>
> I apologize if this question was already answered before but I was
> unable to find a proper solution to my problem. Anyways, I am trying
> to run shaderobjects.py on Windows (Python 2.5.1) by just double-
> clicking, and I got the following error:
>
> [...]
> File "/usr/lib/python2.5/site-packages/OpenGL/extensions.py", line 13,
> in hasGLExtension
> AVAILABLE_GL_EXTENSIONS[:] = glGetString( GL_EXTENSIONS ).split()
> AttributeError: 'NoneType' object has no attribute 'split'
>
> _I think_ I have all requirements and packages properly installed.
>
> Any clues? Thanks in advance !
>   
That *looks* like a problem with calling glGetString before you have a
valid context.  Normally that actually works on GLX platforms, but
apparently not in this case.  Try delaying your import until after you
have the context setup and see if that lets glGetString return a valid
pointer.

HTH,
Mike

-- 

  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com

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


Re: Python GUI toolkit

2008-02-04 Thread Eric Brunel
On Sun, 03 Feb 2008 20:38:41 +0100, Thomas Dybdahl Ahle <[EMAIL PROTECTED]>  
wrote:
[snip]
> Another toolkit you might look into is Tkinter. I think it is something
> like the "official" toolkit for python. I also think it is an adapter
> for other toolkits, so it will use gtk widgets on gnome, qt widgets on
> kde and some other strange widgets on windows.

No. Tkinter doesn't use any other library and draws its own widgets, which  
until now look like very old-style Motif widgets on Unix. There's a new  
version of tcl/tk (on which Tkinter is based) that has a new look for  
widgets, basically looking like gtk's default theme, but it doesn't use  
gtk at all.

This can be seen as an advantage or a drawback depending on what you're  
looking for:
- this keeps the toolkit very small and independent (no dependency hell)
- this basically ensures that the look won't be "native"

And BTW, the new version of tcl/tk is supposed to look native on Windows &  
MacOS too. Unfortunately, this version is very new and the Tkinter module  
has not been adapted for it yet.

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


Re: MySQLdb: commit before cursor close, or after?

2008-02-04 Thread Frank Aune
On Monday 04 February 2008 19:14:13 John Nagle wrote:
>I'm getting some wierd commit-related behavior from MySQLdb.  I'm
> using InnoDB, so transactions really matter.
>
> I'm currently doing
>
>   cursor = db.cursor()
>   cursor.execute(...)
>   cursor.close()
>   db.commit()
>
> Is that the correct order, or should I call "db.commit()" before
> "cursor.close()"?  Does anyone know for sure?  The MySQLdb documentation
> ("http://mysql-python.sourceforge.net/MySQLdb.html";) doesn't
> say.  There are some discussions of this in blogs, but nobody
> really seems to know.
>
>   John Nagle


No, you obviously need to commit your changes before closing the cursor. I'm 
surprised if your code above even works if adding content to the db.

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


Re: When is min(a, b) != min(b, a)?

2008-02-04 Thread Albert van der Horst
In article <[EMAIL PROTECTED]>,
Steven D'Aprano  <[EMAIL PROTECTED]> wrote:
>On Thu, 24 Jan 2008 13:34:56 +, Antoon Pardon wrote:
>
>> On 2008-01-21, Steven D'Aprano <[EMAIL PROTECTED]>
>> wrote:
>>> On Sun, 20 Jan 2008 21:15:02 -0600, Albert Hopkins wrote:
>>>
>>> According to the IEEE-754 standard the usual trichotomy of "x is less
>>> than y, x is equal to y, or x is greater than y" has to be extended to
>>> include "x and y are unordered". Comparisons with NaNs are unordered,
>>> and so expressions like "x < nan" should signal an exception.
>>
>> That doesn't follow. The problem is not that x < nan returns False
>> because that is correct since x isn't smaller than nan.
>
>Comparisons between things which are not comparable risk being terribly
>misleading, and depend very much on how you define "less than" and
>"greater than". If you insist that everything must have a boolean yes/no
>answer ("Does the colour red have a better chance of becoming President
>than a kick to the head?") then False is not an entirely unreasonable
>result to return.
>
>But if you consider that having "x is not smaller than y" be equivalent
>to "x is greater than or equal to y" is more important than forcing a
>boolean answer in the first place, then you need something to signal
>Undefined, and an exception is the right solution unless you have multi-
>valued logic system (True, False, Maybe, Undefined, ...)
>
>SANE (Standard Apple Numerics Environment) explicitly states that it
>signals an exception when doing ordered comparisons against NaNs because
>to return False would be misleading. Apple went on to use the same rule
>in their PowerPC Numerics. That's straight out of the Zen: Practicality
>Beats Purity.

This is the more so, keeping in mind that the original motivation for
Nan's is to avoid exceptions. In principle to keep algorithms clean,
you would like to have exceptions as soon as you divide by zero, or
overflow. This is extremely costly on high efficiency floating point
hardware, (you may have a pipeline stall to allow the point of
exception to be defined, even if the exception doesn't occur)
so IEEE allows to propagate the exception via Nan to occur at
a convenient time, e.g. when the output of a matrix multiplication
is inspected.

Comparisons mark the moment that a decision is made.
Now masking the problem, by taking an invalid hence arbitrary
decision based on an invalid result, is insane. The Nan is
forever gone, something not allowed by IEEE only in code that

[A case can be made however that min and max just propagate a
Nan and don't throw an exception, yet. ]

So python should throw. That is practicality *and* purity.

>--
>Steven

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
[EMAIL PROTECTED]&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: Client side GUI-like web framework ?

2008-02-04 Thread Salvatore
[EMAIL PROTECTED] a écrit :
> 
> You just described what XUL aims to be
> http://developer.mozilla.org/en/docs/The_Joy_of_XUL
> http://developer.mozilla.org/en/docs/XULRunner

XUL is great but it does not allow yet to use Python.
There use to be such a beast, it was named 'Nufox'...

Give a try to  http://www.appcelerator.org/index.html

Regards

Salvatore A.K.A ArtyProg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: type, object hierarchy?

2008-02-04 Thread 7stud
Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Client side GUI-like web framework ?

2008-02-04 Thread [EMAIL PROTECTED]
On 4 Feb, 18:45, USCode <[EMAIL PROTECTED]> wrote:
> Wouldn't it be handy if there was a web framework that allowed you to
> create pages and control the interface like you would using a
> client-side GUI framework such as Tkinter?
>
> The framework would need a small, fast web server that would
> automatically fire up when you ran your application and you then control
> the interface just like you would with client-side GUI widgets (within
> the limitations of browsers of course).  It would handle all the
> complexities of HTTP, HTML, Javascript, etc. letting you focus on adding
> functionality to your application.
>
> Essentially you would be using the browser as your cross-platform
> client-side interface.  You would just interact with all the widgets
> like trees, grids, paned windows, checkboxes, buttons, etc.
>
> There wouldn't happen to be anything like that available, would there?
> I've seen CherryPy but that doesn't quite seem to be what I described.
>
> Thanks!


You just described what XUL aims to be
http://developer.mozilla.org/en/docs/The_Joy_of_XUL
http://developer.mozilla.org/en/docs/XULRunner

At present it lacks for sure documentation (or maybe it isn't
organized really well)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected timing results with file I/O

2008-02-04 Thread Marc 'BlackJack' Rintsch
On Mon, 04 Feb 2008 15:17:18 +, Steven D'Aprano wrote:

> # Method one: grouped by file.
> for each file:
> open the file, append the string, then close it
> 
> 
> # Method two: grouped by procedure.
> for each file:
> open the file
> for each open file:
> append the string
> for each open file:
> close the file
>
> Method 1:
> 
> 17.391216039657593
> 
> Method 2:
> 
> 16.823362112045288
> 
> 
> Surprisingly, Method 2 is a smidgen faster, by about half a second over 
> 500,000 open-write-close cycles. It's not much faster, but it's 
> consistent, over many tests, changing many of the parameters (e.g. the 
> number of files, the number of runs per timeit test, etc.).
> 
> I'm using Linux and Python 2.5.
> 
> So, what's going on? Can anyone explain why the code which does more work 
> takes less time?

Can't confirm this (Linux, Python 2.5):

Method 1: 15.380897998809814
Method 2: 18.085366010665894

I guess it's really all about the disk IO as my system monitor applet
shows that almost all of the time is spend in the kernel and very little
in user space.

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


Re: Is it explicitly specified?

2008-02-04 Thread André Malo
* Steven D'Aprano wrote:

> On Sun, 03 Feb 2008 15:31:49 -0800, Paul Boddie wrote:
> 
>> I don't know whether I can offer much better advice than others, but I
>> have noticed that a lot of my own code has moved in the direction of not
>> having specific default values in function/method signatures. So,
>> instead of this...
>> 
>>   def f(x=123):
>> ...
>> 
>> ...I have this:
>> 
>>   def f(x=None):
>> if x is None:
>>   x = 123
> 
> 
> For the love of Pete, WHY??
> 
> I understand why you would do it for a mutable default, but immutable???

I'm observing myself doing the same, for the following reason:

Consider the function being part of a bigger system, where it's called from
another function or method which should "inherit" the default value of the
function, like:

def g(foo, bar, x=None):
   ...
   f(x=x)

Now if you change the default value of f(x) for some reason, you don't have
to wind up all the possible caller signatures to reflect that change.

nd
-- 
Winnetous Erbe: 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb: commit before cursor close, or after?

2008-02-04 Thread Carsten Haese
On Mon, 2008-02-04 at 19:53 +0100, Frank Aune wrote:
> No, you obviously need to commit your changes before closing the cursor. I'm 
> surprised if your code above even works if adding content to the db.

Why is that obvious? Is this some MySQL-specific oddity? In other
databases, it's the cursor's execute() method that adds the content to
the db (pending a commit of the transaction), and closing the cursor
simply means that you are explicitly releasing the resources that the
cursor used. Whether the cursor is closed before or after the
transaction is committed, or even whether the cursor is explicitly
closed at all or not, should make no difference whatsoever.

-- 
Carsten Haese
http://informixdb.sourceforge.net


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


Re: Python GUI toolkit

2008-02-04 Thread Kevin Walzer
Chris Mellon wrote:

> 
> I didn't say inherently unable, I said the toolkit doesn't provide it.
> Note that you said that you did a lot of work to follow OS X
> conventions and implement behavior. The toolkit doesn't help you with
> any of this. A mac-native toolkit (or one that strives for native
> behavior, like wxPython)  eliminates a lot of this work (although, of
> course, not all).


If the toolkit doesn't provide it, then I think that means "inherently 
unable."

On the Mac, Tk provides a mechanism that allows hooking up arbitrary 
events to arbitrary keyboard. Something like:

  self.bind('', lambda event: 
self.authorizeCommand(self.installPackage))

I have to specify the key-combination--that's all. One line of code. No 
implementing of keyboard bindings in C or at the Tcl/Tk or even 
Python-Tkinter level--it's all already defined by the toolkit.

By contrast, Cocoa/Objective-C has a rather complex system for defining 
keyboard events:

http://developer.apple.com/documentation/Cocoa/Conceptual/EventOverview/HandlingKeyEvents/chapter_6_section_1.html#//apple_ref/doc/uid/1060i-CH7-SW1

PyObjC is a thin wrapper over Objective-C, so you'd have to do the 
equivalent calls in Python to implement custom keyboard behavior.

It is true that Cocoa some convenience methods in Interface Builder, 
i.e. common menu commands don't require any extra code: if you are 
developing a document-based application, "Command-C" is implemented  in 
the menu and in the frameworks as "copy (text, image, whatever) to 
clipboard." But "Command-C" also works identically in the Tk console.

Tk does lack some things. It doesn't support drag-and-drop on the Mac. 
It doesn't hook into some of the newer Mac visual styles. Getting access 
to this would require an extension or patching Tk's core. In those 
events, I emulate the behavior or appeareance, which Tk makes very easy.

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: type, object hierarchy?

2008-02-04 Thread Eduardo O. Padoan
On Feb 4, 2008 1:36 AM, 7stud <[EMAIL PROTECTED]> wrote:
> print dir(type)  #__mro__ attribute is in here
> print dir(object)   #no __mro__ attribute
>
>
> class Mammals(object):
> pass
> class Dog(Mammals):
> pass
>
> print issubclass(Dog, type)   #False
> print Dog.__mro__
>
> --output:--
> (, , )
>
>
> The output suggests that Dog actually is a subclass of type--despite
> the fact that issubclass(Dog, type) returns False.  In addition, the
> output of dir(type) and dir(object):
>
>
> ['__base__', '__bases__', '__basicsize__', '__call__', '__class__',
> '__cmp__', '__delattr__', '__dict__', '__dictoffset__', '__doc__',
> '__flags__', '__getattribute__', '__hash__', '__init__',
> '__itemsize__', '__module__', '__mro__', '__name__', '__new__',
> '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
> '__subclasses__', '__weakrefoffset__', 'mro']
>
> ['__class__', '__delattr__', '__doc__', '__getattribute__',
> '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
> '__repr__', '__setattr__', '__str__']
>
> suggests that type inherits from object since type has all the same
> attributes as object plus some additional ones.  That seems to
> indicate a hierarchy like this:
>
>
> object
>  |
>  V
> type
>  |
>  V
> Mammals
>  |
>  V
> Dog
>
>
> But then why does issubclass(Dog, type) return False?


Here you should find The Answer (tm):
http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html

(Beautifully ilustrated, BTW)

-- 
http://www.advogato.org/person/eopadoan/
Bookmarks: http://del.icio.us/edcrypt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [2.4.2/Linux] Getting Python to fork?

2008-02-04 Thread Jon Ribbens
On 2008-02-04, Rolf van de Krol <[EMAIL PROTECTED]> wrote:
> To create a deamon, you indeed need to fork two times. For more 
> information and a working example see: 
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731 . I'm 
> quite sure this works, because I used it several times to create a deamon.

That doesn't mean it works. That just means it hasn't failed while
you were watching.

(Not that I am saying it's necessarily wrong, I'm just saying that
"it worked when I tried it" is a very bad way of deciding if something
is correct code.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Elementary string-parsing

2008-02-04 Thread Odysseus
In article <[EMAIL PROTECTED]>,
 Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:



>   Rather complicated description... A sample of the real/actual input
> /file/ would be useful.

Sorry, I didn't want to go on too long about the background, but I guess 
more context would have helped. The data actually come from a web page; 
I use a class based on SGMLParser to do the initial collection. The 
items in the "names" list were originally "title" attributes of anchor 
tags and are obtained with a "start_a" method, while "cells" holds the 
contents of the  tags, obtained by a "handle_data" method according 
to the state of a flag that's set to True by a "start_td" method and to 
False by an "end_td". I don't care about anything else on the page, so I 
didn't define most of the tag-specific methods available.



>   cellRoot = 10 * i + na  #where did na come from?
>   #heck, where do 
> names and cells
>   #come from? 
> Globals? Not recommended..

The variable "na" is the number of 'not applicable' items (headings and 
whatnot) preceding the data I'm interested in.

I'm not clear on what makes an object global, other than appearing as an 
operand of a "global" statement, which I don't use anywhere. But "na" is 
assigned its value in the program body, not within any function: does 
that make it global? Why is this not recommended? If I wrap the 
assignment in a function, making "na" a local variable, how can 
"extract_data" then access it?

The lists of data are attributes (?) of my SGMLParser class; in my 
misguided attempt to pare irrelevant details from "extract_data" I 
obfuscated this aspect. I have a "parse_page(url)" function that returns 
an instance of the class, as "captured", and the lists in question are 
actually called "captured.names" and "captured.cells". The 
"parse_page(url)" function is called in the program body; does that make 
its output global as well?

>   use
> 
> def extract_data(names, na, cells):
> 
>   and 
> 
>   return 

What should it return? A Boolean indicating success or failure? All the 
data I want should all have been stored in the "found" dictionary by the 
time the function finishes traversing the list of names.

> > for k in ('time', 'score1', 'score2'):
> > v = found[name][k]
> > if v != "---" and v != "n/a": # skip non-numeric data
> > v = ''.join(v.split(",")) # remove commas between 000s
> > found[name][k] = float(v)
> 
>   I'd suggest splitting this into a short function, and invoking it in
> the preceding... say it is called "parsed"
> 
>   "time" : parsed(cells[cellRoot + 5]),

Will do. I guess part of my problem is that being unsure of myself I'm 
reluctant to attempt too much in a single complex statement, finding it 
easier to take small and simple (but inefficient) steps. I'll have to 
learn to consolidate things as I go.

>   Did you check the library for time/date parsing/formatting
> operations?
> 
> >>> import time
> >>> aTime = "03 Feb 2008 20:35:46 UTC"#DD Mth  HH:MM:SS UTC
> >>> time.strptime(aTime, "%d %b %Y %H:%M:%S %Z")
> (2008, 2, 3, 20, 35, 46, 6, 34, 0)

I looked at the documentation for the "time" module, including 
"strptime", but I didn't realize the "%b" directive would match the 
month abbreviations I'm dealing with. It's described as "Locale's 
abbreviated month name"; if someone were to run my program on a French 
system e.g., wouldn't it try to find a match among "jan", "fév", ..., 
"déc" (or whatever) and fail? Is there a way to declare a "locale" that 
will override the user's settings? Are the locale-specific strings 
documented anywhere? Can one assume them to be identical in all 
English-speaking countries, at least? Now it's pretty unlikely in this 
case that such an 'international situation' will arise, but I didn't 
want to burn any bridges ...

I was also somewhat put off "strptime" on reading the caveat "Note: This 
function relies entirely on the underlying platform's C library for the 
date parsing, and some of these libraries are buggy. There's nothing to 
be done about this short of a new, portable implementation of 
strptime()." If it works, however, it'll be a lot tidier than what I was 
doing. I'll make a point of testing it on its own, with a variety of 
inputs.

> Note that the %Z is a problematic entry...

> ValueError: time data did not match format:  data=03 Feb 2008 
> 20:35:46 PST  fmt=%d %b %Y %H:%M:%S %Z

All the times are UTC, so fortunately this is a non-issue for my 
purposes of the moment. May I assume that leaving the zone out will 
cause the time to be treated as UTC?

Thanks for your help, and for bearing with my elementary questions and 
my fumbling about.

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


polling for output from a subprocess module

2008-02-04 Thread jakub . hrozek
Hello,
My program uses the subprocess module to spawn a child and capture its
output. What I'd like to achieve is that stdout is parsed after the
subprocess finishes, but anything that goes to stderr is printed
immediately. The code currently looks like:

  try:
test = Popen(test_path,
  stdout=PIPE,
  stderr=PIPE,
  close_fds=True,
  env=test_environ)

while test.poll() == None:
ready = select.select([test.stderr], [], [])

if test.stderr in ready[0]:
t_stderr_new = test.stderr.readlines()
if t_stderr_new != []:
print "STDERR:", "\n".join(t_stderr_new)
t_stderr.extend(t_stderr_new)

except OSError, e:
print >>sys.stderr, _("Test execution failed"), e
else:
self.result.return_code = test.returncode
self.result.process(test.stdout.readlines(), t_stderr)


The problem is, that it seems that all the output from the subprocess
seems to be coming at once. Do I need to take a different approach?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Too many open files

2008-02-04 Thread Larry Bates
AMD wrote:
> Hello,
> 
> I need to split a very big file (10 gigabytes) into several thousand 
> smaller files according to a hash algorithm, I do this one line at a 
> time. The problem I have is that opening a file using append, writing 
> the line and closing the file is very time consuming. I'd rather have 
> the files all open for the duration, do all writes and then close them 
> all at the end.
> The problem I have under windows is that as soon as I get to 500 files I 
> get the Too many open files message. I tried the same thing in Delphi 
> and I can get to 3000 files. How can I increase the number of open files 
> in Python?
> 
> Thanks in advance for any answers!
> 
> Andre M. Descombes

Not quite sure what you mean by "a hash algorithm" but if you sort the file 
(with external sort program) on what you want to split on, then you only have 
to 
have 1 file at a time open.

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


Re: Is it explicitly specified?

2008-02-04 Thread George Sakkis
On Feb 4, 6:53 am, André Malo <[EMAIL PROTECTED]> wrote:
> * Steven D'Aprano wrote:
> > On Sun, 03 Feb 2008 15:31:49 -0800, Paul Boddie wrote:
>
> >> I don't know whether I can offer much better advice than others, but I
> >> have noticed that a lot of my own code has moved in the direction of not
> >> having specific default values in function/method signatures. So,
> >> instead of this...
>
> >>   def f(x=123):
> >> ...
>
> >> ...I have this:
>
> >>   def f(x=None):
> >> if x is None:
> >>   x = 123
>
> > For the love of Pete, WHY??
>
> > I understand why you would do it for a mutable default, but immutable???
>
> I'm observing myself doing the same, for the following reason:
>
> Consider the function being part of a bigger system, where it's called from
> another function or method which should "inherit" the default value of the
> function, like:
>
> def g(foo, bar, x=None):
>...
>f(x=x)
>
> Now if you change the default value of f(x) for some reason, you don't have
> to wind up all the possible caller signatures to reflect that change.

You might find handy a decorator I've written exactly for this
scenario, reusing default arguments across functions:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440702

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


Re: Elementary string-parsing

2008-02-04 Thread Marc 'BlackJack' Rintsch
On Mon, 04 Feb 2008 12:25:24 +, Odysseus wrote:

> I'm not clear on what makes an object global, other than appearing as an 
> operand of a "global" statement, which I don't use anywhere. But "na" is 
> assigned its value in the program body, not within any function: does 
> that make it global?

Yes.  The term "global" usually means "module global" in Python.

> Why is this not recommended?

Because the functions depend on some magic data coming from "nowhere" and
it's much harder to follow the data flow in a program.  If you work with
globals you can't be sure what the following will print:

def spam():
global x
x = 42
beep()
print x

`beep()` might change `x` or any function called by `beep()` and so on. 

Another issue is testing.  If you rely on global names it's harder to test
individual functions.  If I want to test your `extract_data()` I first have
to look through the whole function body and search all the global
references and bind those names to values before I can call the function. 
This might not be enough, any function called by `extract_data()` might
need some global assignments too.  This way you'll get quite soon to a
point where the single parts of a program can't be tested in isolation and
are not reusable for other programs.

In programs without such global names you see quite clearly in the
``def`` line what the function expects as input.

> If I wrap the assignment in a function, making "na" a local variable, how
> can "extract_data" then access it?

Give it as an argument.  As a rule of thumb values should enter a function
as arguments and leave it as return values.

It's easy to "enforce" if you have minimal code on the module level.  The
usual idiom is:

def main():
# Main program comes here.

if __name__ == '__main__':
main()

Then main is called when the script is called as program, but not called if
you just import the script as module.  For example to test functions or to
reuse the code from other scripts.

>> def extract_data(names, na, cells):
>> 
>>  and
>> 
>>  return 
> 
> What should it return? A Boolean indicating success or failure? All the
> data I want should all have been stored in the "found" dictionary by the
> time the function finishes traversing the list of names.

Then create the `found` dictionary in that function and return it at the
end.

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


Re: Windows - remote system window text

2008-02-04 Thread [EMAIL PROTECTED]
I can understand that. But look at the bright side, you don't have to
rely on windows authentication, you just need an open port. Now i
don't know what you are building, but with a client/server setup you
can also get to other data that you might need, like mouse movement to
detect for activity, username, etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: type, object hierarchy?

2008-02-04 Thread Mel
7stud wrote:
> On Feb 3, 10:28 pm, 7stud <[EMAIL PROTECTED]> wrote:
>> From the docs:
>>
>> issubclass(class, classinfo)
>> Return true if class is a subclass (direct or indirect) of classinfo.
> 
> 
> print issubclass(Dog, object)  #True
> print issubclass(type, object)  #True
> print issubclass(Dog, type)   #False
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> issubclass (object, type)
False
 >>> isinstance (object, type)
True

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


Re: Extending the import mechanism - what is recommended?

2008-02-04 Thread Steve Holden
[EMAIL PROTECTED] wrote:
> On Jan 29, 2:36 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
>> [EMAIL PROTECTED] wrote:
>>> I need to extend the import mechanism to support another file type.
>>> I've already written the necessary C library to read the file and
>>> return a python code object.
>>> I found one example which just sub-classed imputil.ImportManager like
>>> this:
>>> from myLib import pye_code as pye_code
>>> class MyImporter(imputil.ImportManager):
>>> def __init__(self):
>>> imputil.ImportManager.__init__(self)
>>> self.add_suffix('.pye', self.import_pye)
>>> self.install()
>>> def import_pye(self, filepath, fileinfo, filename):
>>> data = pye_code(filepath)
>>> return 0, data, {}
>>> This actually works fine if the module is just a few lines of code,
>>> but it won't chain to the "built-in" importers; if the module that I'm
>>> importing does something as simple as 'import re', it fails.
>>> It may be that my confusion here is because (even after reading the
>>> code), I'm not clear on the purposes of imputil.ImportManager vs.
>>> imputil.Importer :-(
>>> What is the "preferred" way to do this type of extension?  One other
>>> note; at this time, I just need to import individual module files with
>>> this extension; I don't need to import packages.
>> Here's an importer I wrote some time ago to bring modules in from a
>> relational database. I won't trouble you with the code to compile the
>> modules/packages and add them into the database, but perhaps the
>> attached code will be enough to show you what you are doing that's not
>> working.
>>
[...]
>> [dbimp.py]#
>> # Import modules from a database
>> #
>> # NOTE: could use sys version info to select appropriate module version
>> #   - could ask whether to install if absent ... heh, heh :-)
>> #
>> import sys, db, marshal
>> VER = sys.hexversion
> --->
>> def install():
>> sys.path_hooks.append(dbimporter)
>> sys.path_importer_cache.clear() # probably not necessary
>> sys.path.insert(0, "*db*") # probably not needed with a metea-path hook?
> 
> Steve -
> 
> Thanks!  Got this to work with one interesting problem . . . if I use
> sys.path.insert(0) and insert my hook at the head of the path,
> then I can't import anything EXCEPT my special modules . . . If I use
> sys.path.append("*pye*") then I'm OK.
> 
> One thing I changed was that my load_module function raises
> ImportError if it fails; my understanding from reading PEP302 is that
> that's what's SUPPOSED to happen . . .
> 
> At any rate, I can now import my custom modules . . . thanks!
> 
Excellent news, thanks for letting me know. I can't say that code was 
particularly well-tested, so it's possible that my version makes some 
error that stops the import from running against further path elements 
if the search by the custom importer fails.

May have a few minutes to look at this later, but not for a while. 
Anyway, glad the code helped. Perhaps someone else with more PEP302-fu 
would be able to point out my error.

Any other changes you'd like to feed back besides the ImportError?

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

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


Re: polling for output from a subprocess module

2008-02-04 Thread Christian Heimes
Thomas Bellman wrote:
> The readlines() method will read until it reaches end of file (or
> an error occurs), not just what is available at the moment.  You
> can see that for your self by running:

Bad idea ;)

readlines() on a subprocess Popen instance will block when you PIPE more
than one stream and the buffer of the other stream is full.

You can find some insight at http://bugs.python.org/issue1606. I
discussed the matter with Guido a while ago.

Christian

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


Re: Python GUI toolkit

2008-02-04 Thread Chris Mellon
On Feb 4, 2008 9:19 AM,  <[EMAIL PROTECTED]> wrote:
>
> >
> > Another toolkit you might look into is Tkinter. I think it is something
> > like the "official" toolkit for python. I also think it is an adapter
> > for other toolkits, so it will use gtk widgets on gnome, qt widgets on
> > kde and some other strange widgets on windows.
> >
>
> Not t so,  AFAIK. Tkinter is the python adapter for Tk, the toolkit
> originally developed for Tcl language.
>
> The latest version of Tk (not yet integrated in Python, maybe in 2.6)
> has themes, which emulates
> the look-and-feel of native toolkit at list for XP and OS X. For unix,
> the last time I checked, there was only a theme that looked like a
> plainer version of Gtk default theme. No Gnome or Kde themes yet.
>
Nitpick, but an important one. It emulates *look*. Not feel. Native
look is easy and totally insufficient for a "native" app - it's the
feel that's important.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Project naming suggestions?

2008-02-04 Thread Kay Schluehr
On Feb 3, 7:17 pm, [EMAIL PROTECTED] wrote:
> I'm considering writing a little interpreter for a python-like
> language and I'm looking for name suggestions. :-)
>
> Basically, I don't want to change a whole lot about Python.  In fact,
> I see myself starting with the compiler module from Python 2.5 and
> building from there.
>
> This language would be more or less "Python modulo a few
> (incompatible) changes, but it'd be recognizable by Python
> programmers.  I'm talking about stuff like "allowing the character '?'
> in identifier names," and "a better way to express 'for dummy in
> xrange (n):' when the index isn't needed."  I'd also like to
> implement  most of the planned Python 3000 changes.
>
> Any suggestions?  I'm thinking "Ophidian," for the snake connection,
> or, possibly, "Circus," from "Monty Python's Flying Circus."
>
> Thanks :-)

What about "Argh!". Sounds like Paul Grahams Arc but is more Monty
Pythonesque than "Circus".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Smart Debugger (Python)

2008-02-04 Thread kraman
On Feb 3, 3:55 pm, Norm Matloff <[EMAIL PROTECTED]> wrote:
> I have something of an obsession with debuggers, so I was glad to see
> this posting.  While we're on the subject, I might as well add my own
> small contribution, which I call Xpdb.
>
> Xpdb is available athttp://heather.cs.ucdavis.edu/~matloff/xpdb.html
> Quoting from the beginning of that page:
>
>I tend to use GUI debugging tools with Python, but often find that
>PDB is adequate or even superior. It loads instantly, doesn't take up
>much real estate on the screen, and its ability to set up aliases/macros
>is very valuable. (Note too Rocky Bernstein's new PYDB, at
>http://sourceforge.net/project/showfiles.php?
>group_id=61395&package_id=175827.)
>
>However, I missed having a window that displays my source code and my
>current position in it, so I added such a window to PDB, using
>curses, somewhat analogously to the CGDB variant of GDB (and the -tui
>option in GDB). The result, Xpdb, is available at
>http://heather.cs.ucdavis.edu/~matloff/Python/Xpdb/Code/.  It is
>nothing fancy at all, mainly just a source window capability added
>to PDB (though with a couple of extra new features).
>
> Norm Matloff

It'll be helpful if you provide some screenshots on how this work
-- 
http://mail.python.org/mailman/listinfo/python-list


App idea, Any idea on implementation?

2008-02-04 Thread Dr Mephesto
Hi Guru's,

As a python newb, I was thinking about trying to implement a program
that converts whistled music into midi; the idea coming from a
proposed  application in the Mydreamapp competition hosted last year,
more details here: http://stratfordisland.com/whistler/

So, does anyone have a suggestion as to the best way to implement
this? I was thinking maybe a fast fourier ever 20th of a second,
selecting the dominant frequency, and then mapping that onto a real
musical scale, then stretching or cutting the notes to a preselected
timing scheme. Is this a stupid idea? Can one do a real time fourier
transform in python? Or is there some other way to find the frequency
of a whistle easily? I want to do this in real time, so as you whistle
a tune, you see the notes appear, but if this is too hard, the
prerecorded should be also good I guess.

Im just looking for some vague opinion on how to start, so please post
any idea you might have, every thing helps!  Also, If you know of any
code that already exist that might help, please post a link!

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


Windows - remote system window text

2008-02-04 Thread rdahlstrom
OK - I know how to get the text/title of the windows on a local system
by using the window handle.  What I want to do is to get the text/
title of the windows on a remote system.  Enumerating the window
handles will of course not work remotely, I know that.  Does anyone
know anything short of a client/server setup that *will* work?

Everything would be Windows 2003 server, and I can assume that I have
administrative rights to all affected machines.

Any ideas?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unexpected timing results with file I/O

2008-02-04 Thread rdahlstrom
On Feb 4, 1:12 pm, Carl Banks <[EMAIL PROTECTED]> wrote:
> On Feb 4, 12:53 pm, rdahlstrom <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Feb 4, 10:17 am, Steven D'Aprano <[EMAIL PROTECTED]
>
> > cybersource.com.au> wrote:
> > > After reading an earlier thread about opening and closing lots of files,
> > > I thought I'd do a little experiment.
>
> > > Suppose you have a whole lot of files, and you need to open each one,
> > > append a string, then close them. There's two obvious ways to do it:
> > > group your code by file, or group your code by procedure.
>
> > > # Method one: grouped by file.
> > > for each file:
> > > open the file, append the string, then close it
>
> > > # Method two: grouped by procedure.
> > > for each file:
> > > open the file
> > > for each open file:
> > > append the string
> > > for each open file:
> > > close the file
>
> > > If you have N files, both methods make the same number of I/O calls: N
> > > opens, N writes, N closes. Which is faster?
>
> > > Intuitively, the first method has *got* to be faster, right? It's got one
> > > loop instead of three and it doesn't build an intermediate list of open
> > > file objects. It's so *obviously* going to be faster that it is hardly
> > > worth bothering to check it with timeit, right?
>
> > > Well, I wouldn't be writing unless that intuitive result was wrong. So
> > > here's my test results:
>
> > > Method 1:
>
> > > >>> import timeit
> > > >>> names = ['afile' + str(n) for n in range(1000)]
> > > >>> T = timeit.Timer('''for name in names:
>
> > > ... fp = open(name, 'a'); fp.write('xyz\\n'); fp.close()
> > > ... ''', 'from __main__ import names')>>> min(T.repeat(6, 500))
>
> > > 17.391216039657593
>
> > > Method 2:
>
> > > >>> for name in names:  # reset the files to an empty state.
>
> > > ... fp = open(name, 'w'); fp.close()
> > > ...>>> T = timeit.Timer('''files = [open(name, 'a') for name in names]
>
> > > ... for fp in files:
> > > ... fp.write('xyz\\n')
> > > ... for fp in files:
> > > ... fp.close()
> > > ... ''', '''from __main__ import names''')>>> min(T.repeat(6, 500))
>
> > > 16.823362112045288
>
> > > Surprisingly, Method 2 is a smidgen faster, by about half a second over
> > > 500,000 open-write-close cycles. It's not much faster, but it's
> > > consistent, over many tests, changing many of the parameters (e.g. the
> > > number of files, the number of runs per timeit test, etc.).
>
> > > I'm using Linux and Python 2.5.
>
> > > So, what's going on? Can anyone explain why the code which does more work
> > > takes less time?
>
> > > --
> > > Steven
>
> > The code that does more work takes more time.  The second one does
> > quite a bit less work.  Think of it like this:
>
> > You have 500,000 people to fit through a door.  Here are your options:
>
> > 1.  For each person, open the door, walk through the door, then close
> > the door.
> > 2.  Open the door, allow everyone to walk through, then close the
> > door.
>
> > Which one would you say would be a more efficient way to fit 500,000
> > people through the door?
>
> Bad analogy.  A better analogy would be if each person has their own
> door to walk through.
>
> My hunch is that is has to do with the OS I/O scheduling.  Closing a
> file triggers a cache flush, which in turn triggers the I/O to
> schedule a write to disk; the OS scheduler is perhaps more efficient
> (for a given number of total writes) when it can combine many writes
> at the same time.
>
> Carl Banks

The analogy holds.  It's faster to open the door, do what you need to
do, then close the door than it is to open and close the door each
time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows - remote system window text

2008-02-04 Thread rdahlstrom
On Feb 4, 2:17 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> Well, i guess you will need a process on each machine you need to
> monitor, and then you do have a client server setup.
>
> This can be easily accomplished with fx Pyro (http://
> pyro.sourceforge.net/) for communication, and the Win32 Python library
> (https://sourceforge.net/projects/pywin32/) for creating a service.

Crap, that's what I didn't want to do.  I am slowly coming to the
realization that I'm going to have to, but I really didn't want to do
that.  That brings up a whole host of other things that I would then
have to do - remote installation, etc.  I guess I could do it, but I'd
really rather not.
-- 
http://mail.python.org/mailman/listinfo/python-list


MySQLdb: commit before cursor close, or after?

2008-02-04 Thread John Nagle
   I'm getting some wierd commit-related behavior from MySQLdb.  I'm
using InnoDB, so transactions really matter.

I'm currently doing

cursor = db.cursor()
cursor.execute(...)
cursor.close()  
db.commit() 

Is that the correct order, or should I call "db.commit()" before 
"cursor.close()"?  Does anyone know for sure?  The MySQLdb documentation
("http://mysql-python.sourceforge.net/MySQLdb.html";) doesn't
say.  There are some discussions of this in blogs, but nobody
really seems to know.

John Nagle  

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


Re: polling for output from a subprocess module

2008-02-04 Thread jakub . hrozek
On 4 Ún, 11:49, Thomas Bellman <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> >   try:
> > test = Popen(test_path,
> >   stdout=PIPE,
> >   stderr=PIPE,
> >   close_fds=True,
> >   env=test_environ)
> > while test.poll() == None:
> > ready = select.select([test.stderr], [], [])
> > if test.stderr in ready[0]:
> > t_stderr_new = test.stderr.readlines()
> > if t_stderr_new != []:
> > print "STDERR:", "\n".join(t_stderr_new)
> > t_stderr.extend(t_stderr_new)
> [...]
> > The problem is, that it seems that all the output from the subprocess
> > seems to be coming at once. Do I need to take a different approach?
>
> The readlines() method will read until it reaches end of file (or
> an error occurs), not just what is available at the moment.  You
> can see that for your self by running:
>
> $ python -c 'import sys; print sys.stdin.readlines()'
>
> The call to sys.stdin.readlines() will not return until you press
> Ctrl-D (or, I think, Ctrl-Z if you are using MS-Windows).
>
> However, the os.read() function will only read what is currently
> available.  Note, though, that os.read() does not do line-based
> I/O, so depending on the timing you can get incomplete lines, or
> multiple lines in one read.
>

Right, I didn't realize that. I'll try the os.read() method. Reading
what's available (as opposed to whole lines) shouldn't be an issue in
this specific case. Thanks for the pointer!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb: commit before cursor close, or after?

2008-02-04 Thread Steve Holden
Carsten Haese wrote:
> On Mon, 2008-02-04 at 19:53 +0100, Frank Aune wrote:
>> No, you obviously need to commit your changes before closing the cursor. I'm 
>> surprised if your code above even works if adding content to the db.
> 
> Why is that obvious? Is this some MySQL-specific oddity? In other
> databases, it's the cursor's execute() method that adds the content to
> the db (pending a commit of the transaction), and closing the cursor
> simply means that you are explicitly releasing the resources that the
> cursor used. Whether the cursor is closed before or after the
> transaction is committed, or even whether the cursor is explicitly
> closed at all or not, should make no difference whatsoever.
> 
Certainly isn't "obvious" to me. The whole point of the way 
connection/cursor relationship is structured is to allow the possibility 
of several cursors on the same connection. So cursors can be created, 
used and closed at will without any effect on the underlying connection.

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

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


  1   2   >