Re: Scanning a file

2005-10-30 Thread Peter Otten
David Rasmussen wrote:

> None of the solutions presented in this thread is nearly as fast as the
> 
> print file("filename", "rb").read().count("\x00\x00\x01\x00")
 
Have you already timed Scott David Daniel's approach with a /large/
blocksize? It looks promising.

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


Re: Scanning a file

2005-10-30 Thread Fredrik Lundh
Paul Watson wrote:

> This is Cyngwin on Windows XP.

using cygwin to analyze performance characteristics of portable API:s
is a really lousy idea.

here are corresponding figures from a real operating system:

using a 16 MB file:

$ time python2.4 scanmap.py
real0m0.080s
user0m0.070s
sys 0m0.010s

$ time python2.4 scanpaul.py
real0m0.458s
user0m0.450s
sys 0m0.010s

using a 256 MB file (50% of available memory):

$ time python2.4 scanmap.py
real0m0.913s
user0m0.810s
sys 0m0.100s

$ time python2.4 scanpaul.py
real0m7.149s
user0m6.950s
sys 0m0.200s

using a 1024 MB file (200% of available memory):

$ time python2.4 scanpaul.py
real0m34.274s
user0m28.030s
sys 0m1.350s

$ time python2.4 scanmap.py
real0m20.221s
user0m3.120s
sys 0m1.520s

(Intel(R) Pentium(R) 4 CPU 2.80GHz, 512 MB RAM, relatively slow ATA
disks, relatively recent Linux, best result of multiple mixed runs shown.
scanmap performance would probably improve if Python supported the
"madvise" API, but I don't have time to test that today...)





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


Re: Scanning a file

2005-10-30 Thread Peter Otten
David Rasmussen wrote:

> None of the solutions presented in this thread is nearly as fast as the
> 
> print file("filename", "rb").read().count("\x00\x00\x01\x00")
 
Have you already timed Scott David Daniels' approach with a /large/
blocksize? It looks promising.

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


Re: How do I sort these?

2005-10-30 Thread Peter Otten
Bengt Richter wrote:

> On Fri, 28 Oct 2005 20:00:42 +0100, Steve Holden <[EMAIL PROTECTED]>
> wrote:
> 
>>KraftDiner wrote:
>>> I have two lists.
>>> I want to sort by a value in the first list and have the second list
>>> sorted as well... Any suggestions on how I should/could do this?
>>> 
>> >>> first = [1, 3, 5, 7, 9, 2, 4, 6, 8]
>> >>> second = ['one', 'three', 'five', 'seven', 'nine', 'two', 'four',
>>'six', 'eight']
>> >>> both = zip(first, second)
>> >>> both.sort()
>> >>> [b[0] for b in both]
>>[1, 2, 3, 4, 5, 6, 7, 8, 9]
>> >>> [b[1] for b in both]
>>['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
>> >>>
>>
>>You mean like this?
> ISTM there could be a subtle requirement in the way the OP stated what he
> wanted to do. I.e., it sounds like he would like to sort the first list
> and have a second list undergo the same shuffling as was required to sort
> the first. That's different from having the data of the second participate
> in the sort as order-determining data, if equals in the first list are not
> to be re-ordered:
> 
>  >>> first = [2]*5 + [1]*5
>  >>> first
>  [2, 2, 2, 2, 2, 1, 1, 1, 1, 1]
>  >>> sorted(first)
>  [1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
>  >>> second = [chr(ord('A')+i) for i in xrange(9,-1,-1)]
>  >>> second
>  ['J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A']
>  >>> sorted(second)
>  ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
> 
> Now the zipped sort unzipped:
>  >>> zip(*sorted(zip(first,second)))
>  [(1, 1, 1, 1, 1, 2, 2, 2, 2, 2), ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
>  ['I', 'J')]
> 
> Now suppose we sort the first and use the elements' indices to preserve
> order where equal
>  >>> sorted((f,i) for i,f in enumerate(first))
>  [(1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 0), (2, 1), (2, 2), (2, 3),
>  [(2, 4)]
> 
> Separate out the first list elements:
>  >>> [t[0] for t in sorted((f,i) for i,f in enumerate(first))]
>  [1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
> 
> Now select from the second list, by first-element position correspondence:
>  >>> [second[t[1]] for t in sorted((f,i) for i,f in enumerate(first))]
>  ['E', 'D', 'C', 'B', 'A', 'J', 'I', 'H', 'G', 'F']
> 
> Which did the OP really want? ;-)

I don't know, but there certainly is no subtle requirement to not provide
the key argument:

>>> import operator
>>> first = [2]*5 + [1]*5
>>> second = list(reversed("ABCDEFGHIJ"))
>>> [s for f, s in sorted(zip(first, second))]
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
>>> [s for f, s in sorted(zip(first, second), key=operator.itemgetter(0))]
['E', 'D', 'C', 'B', 'A', 'J', 'I', 'H', 'G', 'F']

Peter

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


Re: How do I sort these?

2005-10-30 Thread bearophileHUGS
# This can be a solution:

from operator import itemgetter
seq1a = ([2] * 4) + ([1] * 4)
seq2a = [complex(3-el,7-el) for el in range(1, 9)]
assert len(seq1a) == len(seq2a)
print seq1a, seq2a, "\n"

mix = zip(seq1a, seq2a)
# mix.sort() # Not possible
mix.sort(key=itemgetter(0))

# If you need tuples output:
seq1b, seq2b = zip(*mix)
print seq1b, seq2b, "\n"

# Alternative, lists output:
seq1b = [el[0] for el in mix]
seq2b = [el[1] for el in mix]
print seq1b, seq2b, "\n"

Bye,
bearophile

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


Re: Recursive generators and backtracking search

2005-10-30 Thread Diez B. Roggisch
 >>for pos in qsearch( pos ):
 >> yield pos

> Um - do you really want to reuse the variable pos here? Yeah, it
> works, but this strikes me as very confusing. I'm not sure that it
> might not be implementation dependent.

Certainly not. pos is - and that is standard python  semantics - just a 
name. Passing the bound _value_ of pos to some function and rebinding 
the the name afterwards is perfectly legal and will work in all 
implementations.

The question of style though remains. I wouldn't do that either, but 
what I frequntly do is something like this:


pos = "10"
pos = int(pos)

Thus when I'm sort of transforming a value, I think it's ok, as the name 
still refers to the same conceptual entity.

Regards,

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


Re: Scanning a file

2005-10-30 Thread Steven D'Aprano
On Sat, 29 Oct 2005 16:41:42 -0700, Alex Martelli wrote:

> Steven D'Aprano <[EMAIL PROTECTED]> wrote:
>...
>> I should also point out that for really serious work, the idiom:
>> 
>> f = file("parrot")
>> handle(f)
>> f.close()
>> 
>> is insufficiently robust for production level code. That was a detail I
>> didn't think I needed to drop on the original newbie poster, but depending
>> on how paranoid you are, or how many exceptions you want to insulate the
>> user from, something like this might be needed:
>> 
>> try:
>> f = file("parrot")
>> try:
>> handle(f)
>> finally:
>> try:
>> f.close()
>> except:
>> print "The file could not be closed; see your sys admin."
>> except:
>> print "The file could not be opened."
> 
> The inner try/finally is fine, but both the try/except are total, utter,
> unmitigated disasters: they will hide a lot of information about
> problems, let the program continue in a totally erroneous state, give
> mistaken messages if handle(f) causes any kind of error totally
> unrelated to opening the file (or if the user hits control-C during a
> lengthy run of handle(f)), emit messages that can erroneously end up in
> the redirected stdout of your program... VERY, VERY bad things.

Of course. That's why I said "something like this" and not "do this" :-) I
don't expect people to "handle" exceptions with a print statement in
anything more serious than a throw-away script.

For serious, production-level code, more often than not you will end up
spending more time and effort handling errors than you spend on handling
the task your application is actually meant to do. But I suspect I'm not
telling Alex anything he doesn't already know.


> Don't ever catch and ``handle'' exceptions in such ways.  In particular,
> each time you're thinking of writing a bare 'except:' clause, think
> again, and you'll most likely find a much better approach.

What would you -- or anyone else -- recommend as a better approach?

Is there a canonical list somewhere that states every possible exception
from a file open or close?



-- 
Steven.

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


Re: query a port

2005-10-30 Thread Lawrence Oluyede
Il 2005-10-30, [EMAIL PROTECTED] <[EMAIL PROTECTED]> ha scritto:
> hi
> in python, how do one query a port to see whether it's up or not?
> thanks
>

Have a look at this:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286240


-- 
Lawrence
http://www.oluyede.org/blog
-- 
http://mail.python.org/mailman/listinfo/python-list


Problem with exe from py2exe

2005-10-30 Thread Francach
Hi,

I'm using python 2.4.1, wxPython 2.6.1.0 and py2exe 1.6.3 on Windows
XP.

My script runs fine with python, but the .exe produced with py2exe
crashes out with:

Traceback (most recent call last):
  File "App1.py", line 4, in ?
  File "wx\__init__.pyc", line 42, in ?
  File "wx\_core.pyc", line 3163, in ?
AttributeError: 'module' object has no attribute
'wxEVT_SCROLL_ENDSCROLL'

I've looked under
http://starship.python.net/crew/theller/moin.cgi/Py2Exe to see if this
is a known issue, but no luck. Anybody seen this one before?

Thanks,
Martin.

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


Re: dreaming in Python

2005-10-30 Thread David Poundall
Sounds like someone may have the kernal of an idea for a book here ;-)

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


Re: dreaming in Python

2005-10-30 Thread Lasse Vågsæther Karlsen
The Eternal Squire wrote:
> All,
> 
> I have to tell all of you this, at least for some laughs.  Honestly, I
> had the silliest dream involving the language last night.  I dreamt
> that it was a decade into the future and that Grand Central Station in
> NYC was installing a cement and steel "computer core" for directing its
> trains and station elevators... and for some reason I was supervising a
> technician on the project.  I asked the technician, what version of
> Python was being used, and he said 2.1... I said better install 2.4!
> So he loaded a chip into the 15 ton cylindrical core, then the core was
> lowered by winch into a manhole cover.  The lights in the station lit,
> and I shouted:  One Python to Rule Them All And In The Darkness Bind
> Them!   Then I took an elevator downstairs to log into the terminal.
> Who did I meet on a neighboring terminal other than the BDFL?  He was
> doing something esoteric and strange on the terminal but I was just
> having trouble logging in.  End of dream.
> 
> Anyone ever have a wierd engineering dream sometime in thier career?
> 
> The Eternal Squire
> 

Just sit still while the men in white come to pick you up :)

-- 
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:[EMAIL PROTECTED]
PGP KeyID: 0x2A42A1C2
-- 
http://mail.python.org/mailman/listinfo/python-list


parsing a tuple in embedded python

2005-10-30 Thread jenkins . justin
I am returning a tuple from my python method and am stuck trying to
figure out how to read it into a C array using PyArg_Parse.
My C Code:
int array[3];
PyArg_Parse(return, "(iii)", &array);

My Python Code:
mytuple = (1,2,3)
return mytuple

That gives me a segmentation fault. What am I doing wrong?

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


Re: query a port

2005-10-30 Thread Dan M
On Sat, 29 Oct 2005 23:21:16 -0700, eight02645999 wrote:

> thanks alot!
> that's all there is to it..so it's just a simple connect.

If all you want to do is check that the given port is open on the given
host, that's it. I tried it on my local box. When connecting to port 25,
it made the connection fine. Trying to connect to port 26 raised a
socket.error.

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


Re: Automatic binding of **kwargs to variables

2005-10-30 Thread Steven D'Aprano
On Sat, 29 Oct 2005 11:01:02 -0700, [EMAIL PROTECTED] wrote:

> Mike Meyer wrote:
> [snip]
>>for name, value in kwargs.items():
>>if name in ('a', 'list', 'of', 'valid', 'keywords'):
>>   exec "%s = %s" % (name, value)
>>else:
>>   raise ValueError, "Unrecognized keyword " + name
>>
>> Others will probably tell you that you really shouldn't be using exec.
> 
> What about using setattr?
> 
> for name, value in kwargs.items():
> if name in ('a', 'list', 'of', 'valid', 'keywords'):
>setattr(self, name, value)
> else:
>raise ValueError, "Unrecognized keyword " + name
> 
> I'd probably turn the list of valid keywords into another dictionary to
> make it easy to specify default values for all the parameters as well.

Here's a thought... instead of passing a class instance which you access
with obj.name, why not pass a dict which you access with dict[name]?

I don't understand the purpose of doing all this extra work to stuff
values stored in a dictionary into instance attributes, when it is so easy
to just use the dictionary. What advantage do you get?



-- 
Steven.

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


Re: Expanding Python as a macro language

2005-10-30 Thread has
Alex Martelli wrote:

> Don't neglect MacOSX -- it's quite secure, there are many open and free
> applications, AND it has a decent architecture for the kind of tasks you
> want to do (mostly intended for Apple's own Applescript language, but
> all the interfaces are open and easily available to Python, which is
> quite well supported on the Mac).

FWIW, there's a nice overview of the Open Scripting Architecture at:

http://www.cs.utexas.edu/users/wcook/papers/AppleScript/AppleScript95.pdf

It's not a perfect system and the quantity and quality of support
varies from application to application and language to language, but I
reckon the other *nixes could still learn a thing or two from it.

HTH

has
-- 
http://freespace.virgin.net/hamish.sanderson/

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


Re: Automatic binding of **kwargs to variables

2005-10-30 Thread Steven D'Aprano
On Fri, 28 Oct 2005 15:49:12 -0700, [EMAIL PROTECTED] wrote:

> I have a very long list of parameters coming from a web form to my
> method foo(self, **kwargs)
> 
> I would like to avoid manually binding the variables to the values
> coming through the **kwargs dictionary,

That's easy: Just Don't Do It.

Instead of some horribly contorted code converting key,item in kwargs into
self.key = item, just do the simple, obvious thing:

self.kwargs = kwargs

and then access the values with self.kwargs[key].

> just to keep the code cleaner,
> I'd like to bind them automatically

If you are worried about excessive use of quotation marks, you can always
define some constants to use:

NAME = 'name'
EMAIL = 'email'
NUM = 'num'

if self.kwargs[NAME]is None:
self.kwargs[NAME] = "Anonymous"
 

The benefit of this is that when you change your web form to use "number"
instead of "num", you only have to change your code in one place.


-- 
Steven

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


Re: Problem with exe from py2exe

2005-10-30 Thread Andrea Gavana
Hello Martin,

> My script runs fine with python, but the .exe produced with py2exe
> crashes out with:
>
> Traceback (most recent call last):
>   File "App1.py", line 4, in ?
>   File "wx\__init__.pyc", line 42, in ?
>   File "wx\_core.pyc", line 3163, in ?
> AttributeError: 'module' object has no attribute
> 'wxEVT_SCROLL_ENDSCROLL'

Noting that you use wxPython 2.6.1.0, are you still using the old
namespace/import commands:

from wxPython.wx import *

?

If this is the case, please switch to the (relatively) new namespace/import
command:

import wx

The new syntax does not have wxEVT_SCROLL_ENDSCROLL but it does have
wx.EVT_SCROLL_ENDSCROLL.

The syntax has changed about 1 year ago (if not more), so if you are still
using the old namespace, I think the wxPython mailing list and the wxPython
main page should report:

"Use the NEW namespace"

In red blinking characters.

If this is not the case, I'm sorry I can't help you a lot. I always use
py2exe but I never encountered such problems.

HTH.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77


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


Re: lambda functions within list comprehensions

2005-10-30 Thread Max Rybinsky
OK.
The thing i've got is an obscure semantic bug, occured because of my
unawareness of the following Python "features":
1. (In major)
http://mail.python.org/pipermail/python-dev/2005-September/056508.html
2. "late" bindings of the function's body

Got to know! :)

Thanks for your attention.

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


Re: lambda functions within list comprehensions

2005-10-30 Thread Max Rybinsky
Valid link in my previews message is

http://mail.python.org/pipermail/python-dev/2005-September/056669.html

Sorry.

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


Re: parsing a tuple in embedded python

2005-10-30 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> I am returning a tuple from my python method and am stuck trying to
> figure out how to read it into a C array using PyArg_Parse.
> My C Code:
> int array[3];
> PyArg_Parse(return, "(iii)", &array);
>
> My Python Code:
> mytuple = (1,2,3)
> return mytuple
>
> That gives me a segmentation fault. What am I doing wrong?

you're not providing enough arguments; "iii" means three pointers, not
one.  try:

PyArg_Parse(return, "(iii)", array, array+1, array+2)

instead.  or, if you prefer maximum clarity:

PyArg_Parse(return, "(iii)", &array[0], &array[1], &array[2])

(I assume you left out the error handling code; ignoring the return value
from PyArg_Parse is not a good idea)





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


Need Python Pro for Help!! Plzz

2005-10-30 Thread [EMAIL PROTECTED]
Need python Pro at [EMAIL PROTECTED] , if u wanna help, plz 
reply to that address. We are python beginners. need a real good 
Python Programmer for Help. TIA!!
-- 
 * Posted with NewsLeecher v3.0 Beta 7
 * http://www.newsleecher.com/?usenet
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: popen2

2005-10-30 Thread Piet van Oostrum
> Grant Edwards <[EMAIL PROTECTED]> (GE) wrote:

>GE> On 2005-10-29, Piet van Oostrum <[EMAIL PROTECTED]> wrote:
>GE> That would require that the application know about the named
>GE> pipe and open it.  I don't think there is any way to swap a
>GE> pipe in for stdin/stdout once a process is running.
>>> 
>>> Sure. 'myprogram' should be designed to communicate through a
>>> named pipe, or be called with named pipe(s) as stdin/stdout.

>GE> That's all well and good, but it's got nothing to do with the
>GE> OP's problem: he's got a program that's already running and he
>GE> wants to write a Python program that can "attach" pipes to that
>GE> already running program's stdin/stdout. 

He didn't state that he has no control over that program. I.e. if it is a
program that he has source code of, he could change its behaviour to use a
named pipe.
-- 
Piet van Oostrum <[EMAIL PROTECTED]>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-30 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote:
   ...
> > Don't ever catch and ``handle'' exceptions in such ways.  In particular,
> > each time you're thinking of writing a bare 'except:' clause, think
> > again, and you'll most likely find a much better approach.
> 
> What would you -- or anyone else -- recommend as a better approach?

That depends on your application, and what you're trying to accomplish
at this point.


> Is there a canonical list somewhere that states every possible exception
> from a file open or close?

No.  But if you get a totally unexpected exception, something that shows
the world has gone crazy and most likely any further action you perform
would run the risk of damaging the user's persistent data since the
macchine appears to be careening wildly out of control... WHY would you
want to perform any further action?  Crashing and burning (ideally
leaving as detailed a core-dump as feasible for later post-mortem)
appears to be preferable.  (Detailed information for post-mortem
purposes is best dumped in a sys.excepthook handler, since wild
unexpected exceptions may occur anywhere and it's impractical to pepper
your application code with bare except clauses for such purposes).

Obviously, if your program is so life-crucial that it cannot be missing
for a long period of time, you will have separately set up a "hot spare"
system, ready to take over at the behest of a separate monitor program
as soon as your program develops problems of such magnitude (a
"heartbeat" system helps with monitoring).  You do need redundant
hardware for that, since the root cause of unexpected problems may well
be in a hardware fault -- the disk has crashed, a memory chip just
melted, the CPU's on strike, locusts...!  Not stuff any program can do
much about in the short term, except by switching to a different
machine.


Alex

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


Re: Automatic binding of **kwargs to variables

2005-10-30 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> Don't know about this particular case but sometimes, I don't want to
> have a default argument value. That is, argument not there is different
> from argument = None. Though in general, I prefer the None as special
> meaning coding style. But even python's builtin function prefers to
> distinguish between "not there" and None.

the canonical idiom when you need such distinction is:

_not_there = object()
def foo(bar=_not_there, baz=_not_there, bap=_not_there):
if bar is _not_there: ...

Other unique objects can be substituted for the 'sentinel', but I prefer
an empty "object()" because it has no other possible meaning except that
of a distinguishable, identifiable sentinel.  IOW, you could set the
_not_there name to [] or {} or many other things, but that could be
slightly confusing for the reader (since the other things might have
other meanings and purposes) while 'object()' shouldn't be.

At any rate, the complicated code I was commenting on did treat absent
expected arguments exactly the same way as arguments passed as None,
which is also a frequent use case, so this further level of refinement
was apparently not warranted ("use only as much power as you need" is a
programming principle that tends to promote simplicity, and therefore,
in many cases, is well worth adhering to).


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


Re: Automatic binding of **kwargs to variables

2005-10-30 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> Alex Martelli wrote:
> 
> > I find this style of coding repulsive when compared to:
> >
> > def foo(arg1=None, arg2=None):
> > print dict(arg1=arg1, arg2=arg2)
> >
> > I don't understand what added value all of those extra, contorted lines
> > are supposed to bring to the party.
> 
> Hi Alex,
> 
> the thing is that I have about 30 parameters to pass to a method and I
> wanted to have a cleaner way to deal with it than just declaring them
> all as arguments: hence **kwargs and all that mess ;-)

Aha, I see -- yes, a nasty case; I understand why you don't want the
boilerplate of 30 'optional named arguments'.


> I was thinking to declare the a list of valid and expected parameters
> in a module and then import * and run my checks against it. Since this

A list seems inappropriate, because it implies an order you don't care
about; I would suggest a set instead.  Also, I don't see why "import *"
would be better than a normal import here -- the latter seems better.

> stuff is being posted by a web form I also need to deal with checkbox
> not being posted if not checked which lead me to:
> 
> for name in expected_form1_kwargs:
> if name not in kwargs:
> kwargs[name]=None

Set operations appear to be at a more appropriate level of abstraction
for what you're doing, it seems to me.  You want to ensure that kwargs,
seen as a set, is a subset of expected_kwargs, and then enrich kwargs
with None for all names that are in expected_kwargs but not yet in
kwargs; the most direct expression of this (assuming you want a detailed
diagnostics of wrongly-passed argument names), it appears to me, is to
keep in expected_args the dictionary mapping each expected arg to its
default value (dict.fromkeys(listofnames), if listofnames is a list of
the expected arguments' names a None the default value for each) and
then check in your method with code such as:

spurious_args = set(kwargs).difference(expected_args)
if spurious_args:
raise TypeError, 'spurious args %s' % spurious_args
kwargs = dict(expected_args, **kwargs)

If you didn't need the list of spurious argument names for diagnostic
purposes, the check might be more simply expressed as

if not set(kwargs).issubset(expected_args): ...

which is more readable (IMHO the difference isn't quite enough to
warrant checking in this way and then computing the spurious_args only
if needed, i.e., in the body of the if statement, but that's a debatable
issue of style).


Operating on sets, and more generally at higher abstraction levels, is
often a preferable Python alternative to "detailed looping" at a level
that's closer to the concrete data structures in your code.


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


process and spinning slash

2005-10-30 Thread benz
I am trying to fork and exec a child by python. Additionally, I am
attempting
to have a spinning slash while the child is running.

My code is as below:

import sys, os, time


def spin(delay):

  pattern=['-','\\','|','/','-','\\','|']

  while 1:
for i in pattern:
  sys.stdout.write(i + " ")
  sys.stdout.flush()
  sys.stdout.write("\b\b")
  time.sleep(delay)

pid = os.fork()

if pid == 0:
  os.execvp("du",("du","-shc","/"))
else:
  spin(0.05)


However, the spinner is kept spinning even the child process was ended.
Any idea ? Thanks!

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

Re: process and spinning slash

2005-10-30 Thread Alex Martelli
benz <[EMAIL PROTECTED]> wrote:
   ...
> def spin(delay):
> 
>   pattern=['-','\\','|','/','-','\\','|']
> 
>   while 1:
> for i in pattern:
>   sys.stdout.write(i + " ")
>   sys.stdout.flush()
>   sys.stdout.write("\b\b")
>   time.sleep(delay)
> 
> pid = os.fork()
> 
> if pid == 0:
>   os.execvp("du",("du","-shc","/"))
> else:
>   spin(0.05)
> 
> 
> However, the spinner is kept spinning even the child process was ended.

It would be astonishing if it were otherwise!  The loop in function spin
is deliberately coded to NEVER terminate, so of course it never does.

> Any idea ? Thanks!

Have the spin function accept the pid argument and exit the loop if said
pid has terminated; to check the latter, e.g., os.kill(pid, 0) -- this
will raise an OSError if no process with that pid exists, so you can use
a try/except OSError: to catch that and break as appropriate.


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


Re: process and spinning slash

2005-10-30 Thread Fredrik Lundh
Alex Martelli wrote:

> Have the spin function accept the pid argument and exit the loop if said
> pid has terminated; to check the latter, e.g., os.kill(pid, 0) -- this
> will raise an OSError if no process with that pid exists, so you can use
> a try/except OSError: to catch that and break as appropriate.

or use the subprocess module instead of fork/exec, pass the Popen instance
to spin, and use the poll() method to check if the process is still running.





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


Re: process and spinning slash

2005-10-30 Thread Alex Martelli
Fredrik Lundh <[EMAIL PROTECTED]> wrote:

> Alex Martelli wrote:
> 
> > Have the spin function accept the pid argument and exit the loop if said
> > pid has terminated; to check the latter, e.g., os.kill(pid, 0) -- this
> > will raise an OSError if no process with that pid exists, so you can use
> > a try/except OSError: to catch that and break as appropriate.
> 
> or use the subprocess module instead of fork/exec, pass the Popen instance
> to spin, and use the poll() method to check if the process is still running.

Much more elegant than the lower-level approach I was sketching, of
course, if one can use Python 2.4 (one cannot always sensibly do that;
e.g., Mac OS X Tiger [the latest release] includes Python 2.3.5, so if
you want to write Mac applications in Python 2.3 packaging and
distributing them is trivial, but if you want to use Python 2.4 you need
to distribute that as well, or package it with your app and thus make it
way bigger... so, limiting oneself to 2.3 is a reasonable choice here).


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


Re: process and spinning slash

2005-10-30 Thread Thomas Bellman
[EMAIL PROTECTED] (Alex Martelli) writes:

> Have the spin function accept the pid argument and exit the loop if said
> pid has terminated; to check the latter, e.g., os.kill(pid, 0) -- this
> will raise an OSError if no process with that pid exists, so you can use
> a try/except OSError: to catch that and break as appropriate.

Bad idea.

Even after the process has exited, it will exist as a zombie until
you os.wait() och os.waitpid() for it.  Sending signal 0 to it
will thus always succeed.

If you do wait for the child process, so the zombie disappears,
the process id can be reused by the OS.  Some Unixes reuse pids
very quickly (if I remember correctly, AIX do so), and others more
slowly, but there is *always* the risk of the pid being reused
immediately after the zombie was removed.  Of course, if you wait
for the process, you will know that it has died and don't need to
check by sending a signal to it if it is alive...

The best way to poll for the termination of a child process, is

dead_child, status = os.waitpid(pid, os.WNOHANG)
if dead_child == 0:
print "No child has died"
elif dead_child == pid:
print "The child process we waited for has died"
else:
print "Some other child process has died"

The last branch can't happen if you wait for a specific process.
(Process ids <= 0 given to waitpid have special meanings; read
the manual page for waitpid(2) for those.)

If the child process was started using subprocess.Popen, you
should usually use the poll() methods on the Popen object to
check if the process has terminated.


-- 
Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"You are in a twisty little passage of   !  bellman @ lysator.liu.se
 standards, all conflicting."!  Make Love -- Nicht Wahr!
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Need Python Pro for Help!! Plzz

2005-10-30 Thread Lasse Vågsæther Karlsen
[EMAIL PROTECTED] wrote:
> Need python Pro at [EMAIL PROTECTED] , if u wanna help, plz 
> reply to that address. We are python beginners. need a real good 
> Python Programmer for Help. TIA!!

If you need help, post questions to the newsgroups. That way you might 
get help from several people instead of just one.

-- 
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:[EMAIL PROTECTED]
PGP KeyID: 0x2A42A1C2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recursive generators and backtracking search

2005-10-30 Thread Talin

Alex Martelli wrote:

> for x in whatever_other_iterable: yield x
>
> into (say)
>
> yield from whatever_other_iterable
>
> is minute and not worth changing the syntax (even though something like
> 'yield from' would mean no keywords would need to be added).

I agree that the improvement is minor, but I'll take what I can get.
Although, I can think that perhaps there could be a potential
efficiency improvement as well - right now, each result has to crawl
its way up the stack of yields. For an 8 x 8 board, each final result
gets yielded 8 times. A 'yield from' might potentially be able to
splice the results directly into the output of the generator using some
kind of iterator chain logic. I'm not sure how feasible this is,
however.

A more compelling benefit would be some means of yielding a value
across multiple stack frames. Currently the generator semantics are
limited, in that they only allow co-routine behavior between a caller
and its directly called subroutine.

What I am more interested in, however, is the use of backtracking
search in Python, and its application to more complex search problems.
The main benefit of the generator approach is in the elimination of
callbacks, allowing the consumer of the search results to retain its
local state in a convenient way, (Anyone who's ever struggled with the
SAX API for XML parsing knows what I am talking about.)

One difference between these more complex problems and the simple
example I posted is that it isn't just simple recursion at work. Each
level of search may invoke a variety of different search strategies for
each sub-part of the problem, which themselves will do the same with
their own portion.

Here's a thought experiment: What would it take to make Python the
premier language for AI research? (Not that I am proposing that this
should be the Python community's goal, this is more in the way of a
brainstorm session.)

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


Re: popen2

2005-10-30 Thread Piet van Oostrum
> Piet van Oostrum <[EMAIL PROTECTED]> (PvO) wrote:

>PvO> He didn't state that he has no control over that program. I.e. if it is a
>PvO> program that he has source code of, he could change its behaviour to use a
>PvO> named pipe.

He could do the initial run of the program with stdin and stdout redirected
to named pipes.At least on Unix that works, I don't know about Windows (but
why not). Later you can write input to the stdin pipe and read the output
back from the other one.
-- 
Piet van Oostrum <[EMAIL PROTECTED]>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: learning emacs lisp

2005-10-30 Thread Xah Lee
well, in the past couple of days i started my own:
http://xahlee.org/emacs/notes.html

but i'm sure something like it exists.

Btw, the elisp intro by
Robert J Chassell. At:
http://www.gnu.org/software/emacs/emacs-lisp-intro/
is extremely well written.
(and so is the elisp reference)

Bravo to GNU & Freesoftware Foundation once again. Thank you.

PS Fuck unix and unix fuckheads. Fuck asshole Larry Wall. Fuck Python
documenation community and their fucking ass lying thru their teeth
ignorance fucking shit. (See:
 http://xahlee.org/UnixResource_dir/writ/gubni_papri.html)

Disclaimer: all mention of real person are opinion only.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

rgb wrote:
> > i'm looking for something example based... for senior professional
> > programers who may want to pickup some elisp for practical macro.
>
> Unfortunately the path from any given language to Elisp varies vastly.
> For example a Prolog programmer would need far fewer tips than a Cobol
> or even a C programmer.  It's unlikely you will find something
> tailored to your specific experience.
>
> I'd already written programs in well over 100 languages in the 20
> years before learning Elisp yet I didn't find the intro terribly
> tedious until around section 13 (Counting).  At that point it switches
> focus toward examples of creating functions rather than introducing
> syntax and available features.  Perhaps starting at section 12 would
> suit your learning style better.
>
> As you probably realize, the language itself is just syntax and the
> hard part is learning about all the facilities at your disposal once
> you decide to write something.  There are a lot of features available
> and, although daunting, I think the reference is the best resource
> for discovering them.
> 
> This group has also been indispensable to me.

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

Re: Understanding the arguments for SubElement factory in ElementTree

2005-10-30 Thread Giovanni Bajo
[EMAIL PROTECTED] wrote:

> 
>   xyz
> 
>
> rather than:
> root = Element('root')
> subroot = SubElement(root, 'subroot')
> subroot.text = 'xyz'
>
> Was wondering whether this code accomplish that
> root = Element('root')
> subroot = SubElement(root, 'subroot', text='xyz')


No, this creates:


  


I believe the text ought to be set in a separate statement.

Giovanni Bajo


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


MySQLdb and cgi

2005-10-30 Thread robert
 Dear group,

I have a problem with importing MySQLdb in a cgi-script.
The code fragment at the beginning:

  1 #!/usr/bin/env python
  2 
  3 print "Content-Type: text/html\n\n"
  4 
  5 import re
  6 import cgi
  7 import MySQLdb
8 print "something"

but the print statement in line 8 will never be executed. The Apache error log says:
"No module named MySQLdb, referer: http://localhost/sqlForm.html"

But when executing the import statement outside of an cgi-script, for example at the command line, the import works fine.
I assume, it has something to do with the apache. Why does the python interpreter not find the MySQLdb module when executed in 
apache context?

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

Re: XML Tree Discovery (script, tool, __?)

2005-10-30 Thread uche . ogbuji
"Neat, though non-trivial XSLT makes my head spin."

Well, you don't have to know XSLT at all to use the Examplotron
transform, although I can understand wanting to understand and hack
what you're using.

"Just for kicks, I
rewrote in python Michael Kay's DTDGenerator
(http://saxon.sourceforge.net/dtdgen.html), though as the original it
has several limitations on the accuracy of the inferred DTD. "

Ah.  Cool.  Got a link?

--
Uche Ogbuji   Fourthought, Inc.
http://uche.ogbuji.nethttp://fourthought.com
http://copia.ogbuji.net   http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

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


Problem with py2exe

2005-10-30 Thread DDany
HI!
I've a problem with py2exe: well, first of all I say that I use python
2.4.1 , py2exe 0.6.3 and distutils 1.0.2 on winxp.
To use py2exe I wrote a file setup.py like this:

from distutils.core import setup
import py2exe
setup(
version = "0.1",
description = "Py2Exe Script",
name = "App.exe",
windows = ["C:\app\\app.py"],
)

file that is launched in this way:
D:\programs\python24\python.exe setup.py py2exe

The command starts and at the beginning seems that all goes on with no
problem, but at a certain point there's an error:

copying D:\programs\python24\MSVCR71.dll -> D:\Applicazione\dist
error: could not delete 'C:\app\dist\MSVCR71.dll': Permission denied

However folders build and dist are created: in particular into the
folder dist i have the w9xpopen.exe file, some *.pyd files and some
dll's... among dll's I have the MSVCR71.dll too.
I don't understand what's the problem so I ask you a help!!!
See ya, bye!

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


Last.fm Python group

2005-10-30 Thread Jarek Zgoda
I was missing miscellaneous Python group on last.fm, so here it is:
http://www.last.fm/group/PythonCode. Enjoy.

-- 
Jarek Zgoda
http://jpa.berlios.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tk

2005-10-30 Thread Shi Mu
How can I make the main menu come first, and after clicking test/just
try, "how are you" interface shows.

Thanks a lot!
from Tkinter import *
from Tkinter import _cnfmerge

class Dialog(Widget):
  def __init__(self, master=None, cnf={}, **kw):
  cnf = _cnfmerge((cnf, kw))
  self.widgetName = '__dialog__'
  Widget._setup(self, master, cnf)
  self.num = self.tk.getint(
  apply(self.tk.call,
('tk_dialog', self._w,
 cnf['title'], cnf['text'],
 cnf['bitmap'], cnf['default'])
+ cnf['strings']))
  try: Widget.destroy(self)
  except TclError: pass
  def destroy(self): pass

if __name__ == '__main__':

  q = Button(None, {'text': 'How are you',
Pack: {}})
  b1 = Listbox()
  b1.pack()

  c1 = Checkbutton(text="Check")
  c1.pack()

  q.mainloop()

from Tkinter import *
root =Tk()
menu=Menu(root)
root.config(menu=menu)
filemenu=Menu(menu)
menu.add_cascade(label="Test", menu=filemenu)
filemenu.add_command(label="Just Try")
filemenu.add_separator()
mainloop()
-- 
http://mail.python.org/mailman/listinfo/python-list


Problem: Windows Command line Pipe to stdin

2005-10-30 Thread GerShar
Python 2.3 on Windows XP



The following works when run from the command line.



import sys

text = sys.stdin.read()
# do something on the text
# ...
sys.stdout.write(text)



But if the above code is used as a filter program that gets another programs 
output as shown below, it fails as shown.



C:\>textGen.py |  filter.py

Traceback (most recent call last):

  File "C:\filter.py", line 4, in ?

text = sys.stdin.read()

IOError: [Errno 9] Bad file descriptor



Any ideas on what is happening and how to fix it?


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


Re: Scanning a file

2005-10-30 Thread John J. Lee
[EMAIL PROTECTED] (Alex Martelli) writes:
[...]
> If you're trying to test your code to ensure it explicitly closes all
> files, you could (from within your tests) rebind built-ins 'file' and
> 'open' to be a class wrapping the real thing, and adding a flag to
> remember if the file is open; at __del__ time it would warn if the file
> had not been explicitly closed.  E.g. (untested code):
[...]

In general __del__ methods interfere with garbage collection, don't
they?  I guess in the case of file objects this is unlikely to be
problematic (because unlikely to be any reference cycles), but I
thought it might be worth warning people that in general this
debugging strategy might get rather confusing since the __del__ could
actually change the operation of the program by preventing garbage
collection of the objects whose lifetime you're trying to
investigate...


John

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


Using graphviz to visualize trace.py output, anybody?

2005-10-30 Thread svenn . are
Hi,

has anybody thought of / already used graphviz to convert the output of
trace.py into a graph? I looked at PyUMLGraph, but 1. PyUMLGraph does
not successfully create a dot file, and 2. I don't really want a UML
representation but a compact representation of program execution.

Maybe there is some other tool that I am not aware of which can create
this kind of trace. I use eclipse with pydev plugin on MacOS 10.3.9

Regards,
Svenn

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


Re: Using Python to add thumbnails to Explorer

2005-10-30 Thread John J. Lee
"Roger Upole" <[EMAIL PROTECTED]> writes:
> "c d saunter" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]
[...]
> > Turns out I need to use a .dll shell extension as per
> > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/
> > shellcc/platform/shell/programmersguide/shell_int/shell_int_extending/
> > extensionhandlers/shell_ext.asp
> >
> > Not so simple, and not (directly) a job for Python.
> 
> Sorry, I didn't realize you meant per-file.
> However, Pythoncom supports both the interfaces
> (IExtractIcon and IPersistFile) specified on the page
> you referenced, so you ought to be able to implement
> an icon handler with the Pywin32 extensions.

Or, if not, then you can do it with module ctypes.

http://starship.python.net/crew/theller/ctypes/


There's an O'Reilly book called something like "win32 shell
programming" that covers this stuff.


John

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


Re: Scanning a file

2005-10-30 Thread Steven D'Aprano
On Sun, 30 Oct 2005 08:35:12 -0700, Alex Martelli wrote:

> Steven D'Aprano <[EMAIL PROTECTED]> wrote:
>...
>> > Don't ever catch and ``handle'' exceptions in such ways.  In particular,
>> > each time you're thinking of writing a bare 'except:' clause, think
>> > again, and you'll most likely find a much better approach.
>> 
>> What would you -- or anyone else -- recommend as a better approach?
> 
> That depends on your application, and what you're trying to accomplish
> at this point.
> 
> 
>> Is there a canonical list somewhere that states every possible exception
>> from a file open or close?
> 
> No.  But if you get a totally unexpected exception, 

I'm more concerned about getting an expected exception -- or more
accurately, *missing* an expected exception. Matching on Exception is too
high. EOFError will probably need to be handled separately, since it often
isn't an error at all, just a change of state. IOError is the bad one.
What else can go wrong?


> something that shows
> the world has gone crazy and most likely any further action you perform
> would run the risk of damaging the user's persistent data since the
> macchine appears to be careening wildly out of control... WHY would you
> want to perform any further action?

In circumstances where, as you put it, the hard disk has crashed, the CPU
is on strike, or the memory has melted down, not only can you not recover
gracefully, but you probably can't even fail gracefully -- at least not
without a special purpose fail-safe operating system.

I'm not concerned with mylist.append(None) unexpectedly -- and
impossibly? -- raising an ImportError. I can't predict every way things
can explode, and even if they do, I can't safely recover from them. But I
can fail gracefully from *expected* errors: rather than the application
just crashing, I can at least try to put up a somewhat informative dialog
box, or at least print something to the console. If opening a preferences
file fails, I can fall back on default settings. If writing the
preferences file fails, I can tell the user that their settings won't be
saved, and continue. Just because the disk is full or their disk quota is
exceeded, doesn't necessarily mean the app can't continue with the job on
hand.


-- 
Steven.

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


Re: Using graphviz to visualize trace.py output, anybody?

2005-10-30 Thread Do Re Mi chel La Si Do
Hi!

Under Windows, I call graphwiz from Python via COM, with win32all (PyWin).
Sorry, I don't know the Mac.

@-salutations

Michel Claveau



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


Re: Examples of Python code compared to other languages

2005-10-30 Thread Nicolas Pernetty
On 29 Oct 2005 21:27:39 -0700, [EMAIL PROTECTED] wrote :

> http://pleac.sourceforge.net/ probably is what you're looking for. It
> shows how to to stuff from the perl cookbook in a plethora of other
> languages, including Python.
> 
> Kind regards Terji Petersen
> 

Hello,

As mentioned in the original message, this site is good but doesn't
include C or Fortran.
Of course I can try to contribute with C code...

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


Pickling and unpickling inherited attributes

2005-10-30 Thread Alex
I have a serious problem and I hope there is some solution. It is
easier to illustrate with a simple code:

>>> class Parent(object):
__slots__=['A', 'B']
def __init__(self, a, b):
self.A=a; self.B=b
def __getstate__(self):
return self.A, self.B
def __setstate__(self, tup):
self.A, self.B=tup


>>> class Child(Parent):
__slots__=['C',]
def __init__(self, c):
self.C=c
def __getstate__(self):
return self.C,
def __setstate__(self, tup):
self.C, =tup


>>> obj=Child(1)
>>> obj.A=2
>>> obj.B=3
>>> obj.A
2
>>> obj.B
3
>>> obj.C
1
>>> objct.Z=4

Traceback (most recent call last):
  File "", line 1, in -toplevel-
objct.Z=4
AttributeError: 'Child' object has no attribute 'Z'

So far so good.. Object obj inherited attributes (A and B) from the
parent class and refuses to take any new ones. But look what happens
when I try to pickle and unpickle it:

>>> import cPickle
>>> File=open('test', 'w')
>>> cPickle.dump(obj, File)
>>> File.close()
>>>
>>> file1=open('test', 'r')
>>> objct=cPickle.load(file1)
>>> file1.close()
>>> objct.A

Traceback (most recent call last):
  File "", line 1, in -toplevel-
objct.A
AttributeError: A
>>> objct.C
1

Its own attribute (C) value is restored but the value of an inherited
attribute (A) is not. I tried pickling protocol 2, and module pickle
instead of cPickle, all with the same result.

What can be done?! Or maybe nothing can be done?

I would greatly appreciate an advice. A lot of code is written, but now
we have a need for pickling and unpickling objects and discovered this
problem.

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


Re: Pickling and unpickling inherited attributes

2005-10-30 Thread Alex
Sorry, I copied and pasted a wrong piece from shell at one part of the
code:

objct.Z=4 was in fact obj.Z=4 and it did refuse to accept Z (because it
is not in __slots__).

But the question remains: why the value of attribute A is not preserved
during pickling and unpickling and what can be done about it, if
anything?

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


Re: parsing a tuple in embedded python

2005-10-30 Thread jenkins . justin
Thanks Fredrik. Yes, I see now how the function works. I'm new to
Python and the book I'm studying out of wasn't too explicit in how to
handle arrays. I've changed the code to what you suggested, but
strangely enough nothing got read into my array. If I return a single
integer from my Python method, I get the expected return using:
 PyArg_Parse(return, "i", &integer);
But,
 PyArg_Parse(return, "(iii)", array, array+1, array+2)
and
 PyArg_Parse(return, "(iii)", &array[0], &array[1], &array[2])
does not alter "array" at all. Any ideas? Thanks



Fredrik Lundh wrote:
> [EMAIL PROTECTED] wrote:
>
> > I am returning a tuple from my python method and am stuck trying to
> > figure out how to read it into a C array using PyArg_Parse.
> > My C Code:
> > int array[3];
> > PyArg_Parse(return, "(iii)", &array);
> >
> > My Python Code:
> > mytuple = (1,2,3)
> > return mytuple
> >
> > That gives me a segmentation fault. What am I doing wrong?
>
> you're not providing enough arguments; "iii" means three pointers, not
> one.  try:
>
> PyArg_Parse(return, "(iii)", array, array+1, array+2)
>
> instead.  or, if you prefer maximum clarity:
>
> PyArg_Parse(return, "(iii)", &array[0], &array[1], &array[2])
>
> (I assume you left out the error handling code; ignoring the return value
> from PyArg_Parse is not a good idea)
> 
> 

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


Re: How do I sort these?

2005-10-30 Thread Bengt Richter
On Sun, 30 Oct 2005 10:13:42 +0100, Peter Otten <[EMAIL PROTECTED]> wrote:

>Bengt Richter wrote:
>
[...]
>> Now select from the second list, by first-element position correspondence:
>>  >>> [second[t[1]] for t in sorted((f,i) for i,f in enumerate(first))]
>>  ['E', 'D', 'C', 'B', 'A', 'J', 'I', 'H', 'G', 'F']
>> 
>> Which did the OP really want? ;-)
>
>I don't know, but there certainly is no subtle requirement to not provide
>the key argument:
>
 import operator
 first = [2]*5 + [1]*5
 second = list(reversed("ABCDEFGHIJ"))
 [s for f, s in sorted(zip(first, second))]
>['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
 [s for f, s in sorted(zip(first, second), key=operator.itemgetter(0))]
>['E', 'D', 'C', 'B', 'A', 'J', 'I', 'H', 'G', 'F']
>
D'oh yeah, forgot about key ;-/ (and this kind of problem probably at least
partly motivated its introduction, so it should have jumped to mind).
Thanks, your version is much cleaner ;-)

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


Problem With Insert with MySQLdb

2005-10-30 Thread David Mitchell
Hello,

I am a complete beginner with Python. I've managed to get mod_python up and
running with Apache2 and I'm trying to a simple insert into a table in a
MySQL database. 

I'm using the MySQLdb library for connectivity. I can read from the database
no problem, but when I do an insert, the value never gets added to the
database, even though there is no error, and the SQL is fine (I print out
the SQL statement in the function). When I copy and paste the sql from my
browser and insert directly into MySQL, it works fine.

Here is the function in question:

def add(req):
db = MySQLdb.connect(host="intranet", user="root", passwd="",
db="intranet")
# create a cursor
cursor = db.cursor()
# execute SQL statement

sql = "INSERT INTO category (category_name) VALUES ('" +
req.form['category'] + "')"
cursor.execute(sql)
return sql


The SQL it is outputting is:

INSERT INTO category (category_name) VALUES ('Test')

Am I doing something obviously incorrect here?

Thanks,

Dave

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


Re: Problem With Insert with MySQLdb

2005-10-30 Thread Gerhard Häring
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

David Mitchell wrote:
> Hello,
> 
> I am a complete beginner with Python. I've managed to get mod_python up and
> running with Apache2 and I'm trying to a simple insert into a table in a
> MySQL database. 
> 
> I'm using the MySQLdb library for connectivity. I can read from the database
> no problem, but when I do an insert, the value never gets added to the
> database, even though there is no error, and the SQL is fine (I print out
> the SQL statement in the function). When I copy and paste the sql from my
> browser and insert directly into MySQL, it works fine.
> [...] Am I doing something obviously incorrect here?

It appears you forgot to call .commit() on the connection to commit your
transaction. From what you tell, it also looks like the MySQL
commandline tool defaults to autocommit (i. e. each statement is wrapped
in an implicit BEGIN ... COMMIT.

If you have no idea what a transaction is or what the heck the BEGIN,
COMMIT or ROLLBACK commands are for (corresponding to .commit() and
.rollback() methods of the DB-API2 connection object, BEGIN is issued
implicitly), then the MySQL documentation will probably answer that.

HTH,

- -- Gerhard
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDZWBndIO4ozGCH14RAuuKAJ9MiUn39dfd0FMclnYBFkXufN/wzwCdG7s4
6Bxj6HdyBbAz7u5O5tu0m7E=
=FXrV
-END PGP SIGNATURE-
-- 
http://mail.python.org/mailman/listinfo/python-list


Message about not deleted folders using Inno Setup

2005-10-30 Thread Martin
Hi
I would like to place a message in an uninstaller window which will
inform the user that some folders haven't been deleted. Is that possible 
using
Inno Setup?

--
Thanks in advance
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Automatically creating a HOME environ variable on Windows?

2005-10-30 Thread Jorgen Grahn
On Sat, 29 Oct 2005 18:43:44 +0200, Maciej Dziardziel <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
>
>> Cool, even better. So what's best, having code to add HOME
>> (=USERPROFILE) to os.environ, or change the various places that HOME is
>> used to check for USERPROFILE?
>
> Best solution would be to have portable function that returns
> user home directory and knows about all platfom quirks.

Why is that better than Python creating a HOME in os.environ, if it doesn't
already exist?  I can think of a few reasons it's better, and a few reasons
it's worse.

/Jorgen

-- 
  // Jorgen GrahnR'lyeh wgah'nagl fhtagn!
-- 
http://mail.python.org/mailman/listinfo/python-list


Controlling output using print with format string

2005-10-30 Thread Paul Watson
It is clear that just using 'print' with variable names is relatively 
uncontrollable.  However, I thought that using a format string would 
reign the problem in and give the desired output.

Must I resort to sys.stdout.write() to control output?

$ python
Python 2.4.1 (#1, Jul 19 2005, 14:16:43)
[GCC 4.0.0 20050519 (Red Hat 4.0.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> s = 'now is the time'
 >>> for c in s:
... print c,
...
n o w   i s   t h e   t i m e
 >>> for c in s:
... print "%c" % (c),
...
n o w   i s   t h e   t i m e
 >>> for c in s:
... print "%1c" % (c),
...
n o w   i s   t h e   t i m e
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickling and unpickling inherited attributes

2005-10-30 Thread Sam Pointon
The reason the state of obj.A and obj.B aren't preserved is because
your __getstate__ and __setstate__ don't preserve them - they only save
obj.C, and you don't make a call to the parent class's respective
methods. Here's what I mean:

>>> import pickle
>>> class Child(Parent):

__slots__=['C',]
def __init__(self, c):
self.C=c
def __getstate__(self):
return Parent.__getstate__(self) + (self.C)
def __setstate__(self, tup):
self.C = tup.pop()
Parent.__setstate__(self, tup)

>>> obj = Child('foo')
>>> obj.A = 'bar'
>>> obj.B = 'baz'
>>> objct = pickle.loads(pickle.dumps(obj))
>>> objct.A
'bar'
>>> objct.B
'baz'
>>> objct.C
'foo'

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


Re: Pickling and unpickling inherited attributes

2005-10-30 Thread Sam Pointon
Of course, in __setstate__, there should be a tup = list(tup) as well -
sheer forgetfulness and a confusing name in one.

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


Re: Scanning a file

2005-10-30 Thread Alex Martelli
John J. Lee <[EMAIL PROTECTED]> wrote:

> [EMAIL PROTECTED] (Alex Martelli) writes:
> [...]
> > If you're trying to test your code to ensure it explicitly closes all
> > files, you could (from within your tests) rebind built-ins 'file' and
> > 'open' to be a class wrapping the real thing, and adding a flag to
> > remember if the file is open; at __del__ time it would warn if the file
> > had not been explicitly closed.  E.g. (untested code):
> [...]
> 
> In general __del__ methods interfere with garbage collection, don't

Yes, cyclic gc only.

> they?  I guess in the case of file objects this is unlikely to be
> problematic (because unlikely to be any reference cycles), but I
> thought it might be worth warning people that in general this
> debugging strategy might get rather confusing since the __del__ could
> actually change the operation of the program by preventing garbage
> collection of the objects whose lifetime you're trying to
> investigate...

Yeah, but you'll find them all listed in gc.garbage for your perusal --
they DO get collected, but they get put in gc.garbage rather than FREED,
that's all.  E.g.:

>>> class a(object):
...   def __del__(self): print 'del', self.__class__.__name__
... 
>>> class b(a): pass
... 
>>> x=a(); y=b(); x.y=y; y.x=x
>>> del x, y
>>> gc.collect()
4
>>> gc.garbage
[<__main__.a object at 0x64cf0>, <__main__.b object at 0x58510>]

So, no big deal -- run a gc.collect() and parse through gc.garbage for
any instances of your "wrapper of file" class, and you'll find ones that
were forgotten as part of a cyclic garbage loop and you can check
whether they were explicitly closed or not.


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


Re: Scanning a file

2005-10-30 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote:
   ...
> > No.  But if you get a totally unexpected exception, 
> 
> I'm more concerned about getting an expected exception -- or more
> accurately, *missing* an expected exception. Matching on Exception is too
> high. EOFError will probably need to be handled separately, since it often
> isn't an error at all, just a change of state. IOError is the bad one.
> What else can go wrong?

Lots of things, but not ones you should WISH your application to
survive.

> > something that shows
> > the world has gone crazy and most likely any further action you perform
> > would run the risk of damaging the user's persistent data since the
> > macchine appears to be careening wildly out of control... WHY would you
> > want to perform any further action?
> 
> In circumstances where, as you put it, the hard disk has crashed, the CPU
> is on strike, or the memory has melted down, not only can you not recover
> gracefully, but you probably can't even fail gracefully -- at least not
> without a special purpose fail-safe operating system.

Right: as I explained, all you can do is switch operation over to a "hot
spare" server (a different machine), through the good services of a
"monitor" machine - and hope the redundancy you've built into your
storage system (presumably a database with good solid mirroring running
on other machines yet, if your app is as critical as that) has survived.


> I'm not concerned with mylist.append(None) unexpectedly -- and
> impossibly? -- raising an ImportError. I can't predict every way things
> can explode, and even if they do, I can't safely recover from them. But I

Exactly my point -- and yet if you use "except:", that's exactly what
you're TRYING to do, rather than let your app die.

> can fail gracefully from *expected* errors: rather than the application
> just crashing, I can at least try to put up a somewhat informative dialog
> box, or at least print something to the console.

You can do that from your sys.excepthook routine, if it's OK for your
app to die afterwards.  What we're discussing here are only, and
strictly, cases in which you wish your app to NOT die, but rather keep
processing (not just give nice error diagnostics and then terminate,
that's what sys.excepthook is for).  And what I'm saying is that you
should keep processing only for errors you *DO* expect, and should not
even try if the error is NOT expected.

>. If opening a preferences
> file fails, I can fall back on default settings. If writing the
> preferences file fails, I can tell the user that their settings won't be
> saved, and continue. Just because the disk is full or their disk quota is
> exceeded, doesn't necessarily mean the app can't continue with the job on
> hand.

Sure, that's why you catch IOError, which covers these _expected_ cases
(or, if you want to be a bit wider, maybe OSError) AND check its errno
attribute to ensure you haven't mistakenly caught something you did NOT
in fact expect (and thus can't really handle), to use a bare raise
statement to re-raise the "accidentally caught" exception if needed.

But if something goes wrong that you had NOT anticipated, just log as
much info as you can for the postmortem, give nice diagnostics to the
user if you wish, and do NOT keep processing -- and for these
diagnostic-only purposes, use sys.excepthook, not a slew of try/except
all over your application making it crufty and unmaintainable.


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


Re: Controlling output using print with format string

2005-10-30 Thread Alex Martelli
Paul Watson <[EMAIL PROTECTED]> wrote:

> It is clear that just using 'print' with variable names is relatively
> uncontrollable.  However, I thought that using a format string would 
> reign the problem in and give the desired output.
> 
> Must I resort to sys.stdout.write() to control output?

In general, yes, because print tries hard to separate with spaces the
things it is separately printing.  You can fight that by resetting
sys.stdout.softspace after each and every print, but that's harder than
just using sys.stdout.write.  So, you should use the right tool for the
job: print for simple output, often diagnostic in nature, where you
don't mind the spaces and/or newlines that implies; sys.stdout.write
when you do want to really control every aspect of the output.


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


Re: Pickling and unpickling inherited attributes

2005-10-30 Thread Alex
Thanks so much! It makes perfect sense and I am sure it will work now.

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


Re: Scanning a file

2005-10-30 Thread Bengt Richter
On Sat, 29 Oct 2005 21:10:11 +0100, Steve Holden <[EMAIL PROTECTED]> wrote:

>Peter Otten wrote:
>> Bengt Richter wrote:
>> 
>> 
>>>What struck me was
>>>
>>>
>> gen = byblocks(StringIO.StringIO('no'),1024,len('end?')-1)
>> [gen.next() for i in xrange(10)]
>>>
>>>['no', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'no']
>> 
>> 
>> Ouch. Seems like I spotted the subtle cornercase error and missed the big
>> one.
>> 
>
>No, you just realised subconsciously that we'd all spot the obvious one 
>and decided to point out the bug that would remain after the obvious one 
>had been fixed.
>
I still smelled a bug in the counting of substring in the overlap region,
and you motivated me to find it (obvious in hindsight, but aren't most ;-)

A substring can get over-counted if the "overlap" region joins infelicitously 
with the next
input. E.g., try counting 'xx' in 10*'xx' with a read chunk of 4 instead of 
1024*1024:

Assuming corrections so far posted as I understand them:

 >>> def byblocks(f, blocksize, overlap):
 ... block = f.read(blocksize)
 ... yield block
 ... if overlap>0:
 ... while True:
 ... next = f.read(blocksize-overlap)
 ... if not next: break
 ... block = block[-overlap:] + next
 ... yield block
 ... else:
 ... while True:
 ... next = f.read(blocksize)
 ... if not next: break
 ... yield next
 ...
 >>> def countsubst(f, subst, blksize=1024*1024):
 ... count = 0
 ... for block in byblocks(f, blksize, len(subst)-1):
 ... count += block.count(subst)
 ... f.close()
 ... return count
 ...

 >>> from StringIO import StringIO as S
 >>> countsubst(S('xx'*10), 'xx',  4)
 13
 >>> ('xx'*10).count('xx')
 10
 >>> list(byblocks(S('xx'*10), 4, len('xx')-1))
 ['', '', '', '', '', '', 'xx']

Of course, a large read chunk will make the problem either
go away

 >>> countsubst(S('xx'*10), 'xx',  1024)
 10

or might make it low probability depending on the data.

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


Re: Pickling and unpickling inherited attributes

2005-10-30 Thread Alex Martelli
Alex <[EMAIL PROTECTED]> wrote:

> I have a serious problem and I hope there is some solution. It is
> easier to illustrate with a simple code:
> 
> >>> class Parent(object):
>   __slots__=['A', 'B']
>   def __init__(self, a, b):
>   self.A=a; self.B=b
>   def __getstate__(self):
>   return self.A, self.B
>   def __setstate__(self, tup):
>   self.A, self.B=tup
> 
> 
> >>> class Child(Parent):
>   __slots__=['C',]
>   def __init__(self, c):
>   self.C=c
>   def __getstate__(self):
>   return self.C,
>   def __setstate__(self, tup):
>   self.C, =tup

Child.__getstate__ and __setstate__ need to cooperate with those of
Parent, not just override them as yours do.  __getstate__ is easy:

in Child:

   def __getstate__(self):
   return super(Child, self).__getstate__ + (self.C,)

i.e., each class will successively append its parts of state to the
resulting overall tuple.  It's harder to do for __setstate__ without
building into it a strong dependency on how many items of the tuple the
Parent is taking for its own use; you need to establish some protocol of
your own for that purpose, such as:

in Parent:

   def __setstate__(self, tup):
   self.A, self.B = tup[:2]
   self._tup = tup[2:]

in Child:

   def __setstate__(self, tup):
   super(Child, self).__setstate__(tup)
   self.C, = self._tup[:1]
   self._tup = self._tup[1:]

(the last statement is needed in case Child was further subclassed by a
Grandchild class following the same set-state protocol).

Working with lists rather than tuples might in fact be simpler.  But
simplest might be if __setstate__ was allowed to return something
different than None, so it could return "whatever's left of the tuple"
rather than relying on that nasty self._tup thingy (which is going to be
left there, empty, in the end -- though, each class could conditionally
delete it if it was empty).  However, while I believe it is not
currently enforced that __setstate__ might return a non-None value to be
ignored, I'm not sure it's _allowed_, either, so I wouldn't try (lest
this application should break in strange ways in future Python
versions).


Alex

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


Re: learning emacs lisp

2005-10-30 Thread Steve Holden
[To new readers:

please ignore the rantings of this unbalanced person, who is well known 
for posing inappropriate and inflammatory material on news groups and 
mailing lists of all kinds.]

Xah Lee wrote:
> well, in the past couple of days i started my own:
> http://xahlee.org/emacs/notes.html
> 
> but i'm sure something like it exists.
> 
> Btw, the elisp intro by
> Robert J Chassell. At:
> http://www.gnu.org/software/emacs/emacs-lisp-intro/
> is extremely well written.
> (and so is the elisp reference)
> 
> Bravo to GNU & Freesoftware Foundation once again. Thank you.
> 
> PS Fuck unix and unix fuckheads. Fuck asshole Larry Wall. Fuck Python
> documenation community and their fucking ass lying thru their teeth
> ignorance fucking shit. (See:
>  http://xahlee.org/UnixResource_dir/writ/gubni_papri.html)
> 
> Disclaimer: all mention of real person are opinion only.
> 
>  Xah
>  [EMAIL PROTECTED]
> ∑ http://xahlee.org/
> 
> rgb wrote:
> 
>>>i'm looking for something example based... for senior professional
>>>programers who may want to pickup some elisp for practical macro.
>>
>>Unfortunately the path from any given language to Elisp varies vastly.
>>For example a Prolog programmer would need far fewer tips than a Cobol
>>or even a C programmer.  It's unlikely you will find something
>>tailored to your specific experience.
>>
>>I'd already written programs in well over 100 languages in the 20
>>years before learning Elisp yet I didn't find the intro terribly
>>tedious until around section 13 (Counting).  At that point it switches
>>focus toward examples of creating functions rather than introducing
>>syntax and available features.  Perhaps starting at section 12 would
>>suit your learning style better.
>>
>>As you probably realize, the language itself is just syntax and the
>>hard part is learning about all the facilities at your disposal once
>>you decide to write something.  There are a lot of features available
>>and, although daunting, I think the reference is the best resource
>>for discovering them.
>>
>>This group has also been indispensable to me.
> 
> 


-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

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

Re: Controlling output using print with format string

2005-10-30 Thread Mike Meyer
Paul Watson <[EMAIL PROTECTED]> writes:

> It is clear that just using 'print' with variable names is relatively
> uncontrollable.  However, I thought that using a format string would
> reign the problem in and give the desired output.
>
> Must I resort to sys.stdout.write() to control output?

No. You can control the output of print to whatever degree you want -
just convert the printed objects to strings by hand before printing
them.

> $ python
> Python 2.4.1 (#1, Jul 19 2005, 14:16:43)
> [GCC 4.0.0 20050519 (Red Hat 4.0.0-8)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> s = 'now is the time'
>  >>> for c in s:
> ... print c,
> ...
> n o w   i s   t h e   t i m e
>  >>> for c in s:
> ... print "%c" % (c),
> ...
> n o w   i s   t h e   t i m e
>  >>> for c in s:
> ... print "%1c" % (c),
> ...
> n o w   i s   t h e   t i m e

On the other hand, you can't keep print from putting a space between
all the objects it prints. So you have to convert all the objects into
a single string before printing that string.

   http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-30 Thread Tony Nelson
In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

> Steve Holden wrote:
> > Indeed, but reading one byte at a time is about the slowest way to
> > process a file, in Python or any other language, because it fails to
> > amortize the overhead cost of function calls over many characters.
> >
> > Buffering wasn't invented because early programmers had nothing better
> > to occupy their minds, remember :-)
> 
> Buffer, and then read one byte at a time from the buffer.

Have you mesured it?

#!/usr/bin/python
'''Time some file scanning.
'''

import sys, time

f = open(sys.argv[1])
t = time.time()
while True:
b = f.read(256*1024)
if not b:
break
print 'initial read', time.time() - t
f.close()

f = open(sys.argv[1])
t = time.time()
while True:
b = f.read(256*1024)
if not b:
break
print 'second read', time.time() - t
f.close()

if 1:
f = open(sys.argv[1])
t = time.time()
while True:
b = f.read(256*1024)
if not b:
break
for c in b:
pass
print 'third chars', time.time() - t
f.close()

f = open(sys.argv[1])
t = time.time()
n = 0
srch = '\x00\x00\x01\x00'
laplen = len(srch)-1
lap = ''
while True:
b = f.read(256*1024)
if not b:
break
n += (lap+b[:laplen]).count(srch)
n += b.count(srch)
lap = b[-laplen:]
print 'fourth scan', time.time() - t, n
f.close()


On my (old) system, with a 512 MB file so it won't all buffer, the 
second time I get:

initial read 14.513395071
second read 14.8771388531
third chars 178.250257969
fourth scan 26.1602909565 1

TonyN.:'[EMAIL PROTECTED]
  '  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-30 Thread Paul Watson
Fredrik Lundh wrote:
> Paul Watson wrote:
> 
>>This is Cyngwin on Windows XP.
> 
> using cygwin to analyze performance characteristics of portable API:s
> is a really lousy idea.

Ok.  So, I agree.  That is just what I had at hand.  Here are some other 
numbers to which due diligence has also not been applied.  Source code 
is at the bottom for both file and mmap process.  I would be willing for 
someone to tell me what I could improve.

$ python -V
Python 2.4.1

$ uname -a
Linux ruth 2.6.13-1.1532_FC4 #1 Thu Oct 20 01:30:08 EDT 2005 i686

$ cat /proc/meminfo|head -2
MemTotal:   514232 kB
MemFree: 47080 kB

$ time ./scanfile.py
16384

real0m0.06s
user0m0.03s
sys 0m0.01s

$ time ./scanfilemmap.py
16384

real0m0.10s
user0m0.06s
sys 0m0.00s

Using a ~ 250 MB file, not even half of physical memory.

$ time ./scanfile.py
16777216

real0m11.19s
user0m10.98s
sys 0m0.17s

$ time ./scanfilemmap.py
16777216

real0m55.09s
user0m43.12s
sys 0m11.92s

==

$ cat scanfile.py
#!/usr/bin/env python

import sys

fn = 't.dat'
ss = '\x00\x00\x01\x00'
ss = 'time'

be = len(ss) - 1# length of overlap to check
blocksize = 64 * 1024# need to ensure that blocksize > overlap

fp = open(fn, 'rb')
b = fp.read(blocksize)
count = 0
while len(b) > be:
 count += b.count(ss)
 b = b[-be:] + fp.read(blocksize)
fp.close()

print count
sys.exit(0)

===

$ cat scanfilemmap.py
#!/usr/bin/env python

import sys
import os
import mmap

fn = 't.dat'
ss = '\x00\x00\x01\x00'
ss='time'

fp = open(fn, 'rb')
b = mmap.mmap(fp.fileno(), os.stat(fp.name).st_size, 
access=mmap.ACCESS_READ)

count = 0
foundpoint = b.find(ss, 0)
while foundpoint != -1 and (foundpoint + 1) < b.size():
 #print foundpoint
 count = count + 1
 foundpoint = b.find(ss, foundpoint + 1)
b.close()

print count

fp.close()
sys.exit(0)
-- 
http://mail.python.org/mailman/listinfo/python-list


need start point for getting html info from web

2005-10-30 Thread nephish
hey there,

i have a small app that i am going to need to get information from a
few tables on different websites. i have looked at urllib and httplib.
the sites i need to get data from mostly have this data in tables. So
that, i think would make it easier. Anyone suggest a good starting
point for me to find out how to do this, or know of a link to a good
how-to?
thanks,
sk

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


Re: Pickling and unpickling inherited attributes

2005-10-30 Thread Steve Holden
Alex wrote:
> I have a serious problem and I hope there is some solution. It is
> easier to illustrate with a simple code:
> 
> 
class Parent(object):
> 
>   __slots__=['A', 'B']
>   def __init__(self, a, b):
>   self.A=a; self.B=b
>   def __getstate__(self):
>   return self.A, self.B
>   def __setstate__(self, tup):
>   self.A, self.B=tup
> 
> 
> 
class Child(Parent):
> 
>   __slots__=['C',]
>   def __init__(self, c):
>   self.C=c
>   def __getstate__(self):
>   return self.C,
>   def __setstate__(self, tup):
>   self.C, =tup
> 
> 
> 
obj=Child(1)
obj.A=2
obj.B=3
obj.A
> 
> 2
> 
obj.B
> 
> 3
> 
obj.C
> 
> 1
> 
objct.Z=4
> 
> 
> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> objct.Z=4
> AttributeError: 'Child' object has no attribute 'Z'
> 
> So far so good.. Object obj inherited attributes (A and B) from the
> parent class and refuses to take any new ones. But look what happens
> when I try to pickle and unpickle it:
> 
> 
import cPickle
File=open('test', 'w')
cPickle.dump(obj, File)
File.close()

file1=open('test', 'r')
objct=cPickle.load(file1)
file1.close()
objct.A
> 
> 
> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> objct.A
> AttributeError: A
> 
objct.C
> 
> 1
> 
> Its own attribute (C) value is restored but the value of an inherited
> attribute (A) is not. I tried pickling protocol 2, and module pickle
> instead of cPickle, all with the same result.
> 
> What can be done?! Or maybe nothing can be done?
> 
> I would greatly appreciate an advice. A lot of code is written, but now
> we have a need for pickling and unpickling objects and discovered this
> problem.
> 
You have explicitly told the objects' class definition to only store the 
C attribute as a part of the object state.

If you change the definition of child to:

class Child(Parent):

__slots__=['C',]
def __init__(self, c):
self.C=c
def __getstate__(self):
return self.A, self.B, self.C,
def __setstate__(self, tup):
self.A, self.B, self.C, =tup

everything works as you expect. But I presume you want the subclass to 
require no knowledge of the superclass state?

In that case you might consider rewriting Child as

class Child(Parent):

 __slots__ = ['C']  # only tuples need trailing comma
 def __init__(self, c):
 self.C = c
 def __getstate__(self):
 return Parent.__getstate__(self) + (self.C, )
 def __setstate__(self, t):
 self.C = t[-1]
 Parent.__setstate__(self, t[:-1])

This would work adequately. May I ask your use case for __slots__? I 
presume there is a specific reason why you can't just code the classes as

class Parent(object):
 def __init__(self, a, b):
 self.A = a
 self.B = b

class Child(Parent):
 def __init__(self, c):
 self.C = c

since these definitions will pickle and unpickle perfectly.

A couple of other notes:

First, it's always safest to open pickle files in binary mode, as this 
will eliminate the chance of platform differences making a pickle 
written on one architecture or platform being unreadable by another.

Secondly, it's usual to have the subclass explicitly call the superclass 
to initialize that portion of the instance. So it would be more 
typically Pythonic to write

class Child(Parent):
 def __init__(self, c):
 Parent.__init__(self, a, b)
 self.C = c

This allows you to pass the a and b values into the creator call in the 
following way:

obj = Child(2, 3, 1)

rather than explicitly setting attributes of the Child instance in line. 
If your inheritance patterns are complex you may find it useful to 
investigate the super() function.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

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


Re: Controlling output using print with format string

2005-10-30 Thread Bengt Richter
On Sun, 30 Oct 2005 18:44:06 -0600, Paul Watson <[EMAIL PROTECTED]> wrote:

>It is clear that just using 'print' with variable names is relatively 
>uncontrollable.  However, I thought that using a format string would 
>reign the problem in and give the desired output.
>
>Must I resort to sys.stdout.write() to control output?
>
Maybe. But I wouldn't say "uncontrollable" -- print is pretty predictable.

>$ python
>Python 2.4.1 (#1, Jul 19 2005, 14:16:43)
>[GCC 4.0.0 20050519 (Red Hat 4.0.0-8)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
> >>> s = 'now is the time'
> >>> for c in s:
>... print c,
>...
>n o w   i s   t h e   t i m e
> >>> for c in s:
>... print "%c" % (c),
>...
>n o w   i s   t h e   t i m e
> >>> for c in s:
>... print "%1c" % (c),
>...
>n o w   i s   t h e   t i m e

If you like C, you can make something pretty close to printf:

 >>> import sys
 >>> def printf(fmt, *args):
 ... s = fmt%args
 ... sys.stdout.write(s)
 ... return len(s)
 ...
 >>> s = 'now is the time'
 >>> for c in s:
 ... printf('%s', c)
 ...
 n1
 o1
 w1
  1
 i1
 s1
  1
 t1
 h1
 e1
  1
 t1
 i1
 m1
 e1

Oops, interactively you probably  want to do something other
than implicity print the printf return value ;-)

 >>> s = 'now is the time'
 >>> for c in s:
 ... nc = printf('%s', c)
 ...
 now is the time>>>
 >>> for c in s:
 ... nc = printf('%c', c)
 ...
 now is the time>>>
 >>> for c in s:
 ... nc = printf('%1c', c)
 ...
 now is the time>>>
 
Just to show multiple args, you could pass all the characters
separately, but at once, e.g., (of course you need a format to match)

 >>> printf('%s'*len(s)+'\n', *s)
 now is the time
 16

 >>> printf('%s .. %s .. %s .. %s\n', *s.split())
 now .. is .. the .. time
 25

Or just don't return anything (None by default) from printf if you
just want to use it interactively. Whatever.

Your character-by-character output loop doesn't give much of a clue
to what obstacle you are really encountering ;-)

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


Re: Scanning a file

2005-10-30 Thread Steven D'Aprano
Alex Martelli wrote:

> Steven D'Aprano <[EMAIL PROTECTED]> wrote:
>...
> 
>>>No.  But if you get a totally unexpected exception, 
>>
>>I'm more concerned about getting an expected exception -- or more
>>accurately, *missing* an expected exception. Matching on Exception is too
>>high. EOFError will probably need to be handled separately, since it often
>>isn't an error at all, just a change of state. IOError is the bad one.
>>What else can go wrong?
> 
> 
> Lots of things, but not ones you should WISH your application to
> survive.

[snip]

> Sure, that's why you catch IOError, which covers these _expected_ cases
> (or, if you want to be a bit wider, maybe OSError) AND check its errno
> attribute to ensure you haven't mistakenly caught something you did NOT
> in fact expect (and thus can't really handle), to use a bare raise
> statement to re-raise the "accidentally caught" exception if needed.

Ah, that's precisely the answer I was looking for: 
IOError and OSError, and then check the errno. 
Excellent: thanks for that.

> But if something goes wrong that you had NOT anticipated, just log as
> much info as you can for the postmortem, give nice diagnostics to the
> user if you wish, and do NOT keep processing -- and for these
> diagnostic-only purposes, use sys.excepthook, not a slew of try/except
> all over your application making it crufty and unmaintainable.

Agreed -- I never intended people to draw the 
conclusion that every line, or even every logical block 
of lines, should be wrapped in try/except.


-- 
Steven.

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


putting a string in Mac Address form

2005-10-30 Thread kyle.tk
I came up with this. Is there a better (more pythonic) way to do it?

import string
def mac_to_norm(mac):
def bad_char(char):
if char not in string.hexdigits:
return False
return True
mac = filter(bad_char,mac)
if len(mac) is not 12: return None
new_mac = ''
c = 0
while len(new_mac) < 16:
new_mac += mac[c:c+2] + ':'
c=c+2
return new_mac[:-1].upper()
print mac_to_norm('0012.ab23.b2cd')
#shows: 00:12:AB:23:B2:CD

The part I think is bad is the while loop part. Could it be better?

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


Re: need start point for getting html info from web

2005-10-30 Thread Mike Meyer
[EMAIL PROTECTED] writes:
> i have a small app that i am going to need to get information from a
> few tables on different websites. i have looked at urllib and httplib.
> the sites i need to get data from mostly have this data in tables. So
> that, i think would make it easier. Anyone suggest a good starting
> point for me to find out how to do this, or know of a link to a good
> how-to?

Don't have a link to a howto. But you're halfway there. urllib (and
urllib2) will get HTML text from the websites. Pulling data from it
sort of depends on the nature of the HTML. If it's well-structured
XHTML, you can use your favorite xml library. if it's well structured
HTML, you can try htmllib, but it's pretty primitive. If it's not
well-structured, you can use BeautifulSoup. I've used it to pull data
from tables. The problem with any of this is that your code really
depends on the structure - or lack thereof - of the HTML you're
scraping. If they change it, your code breaks.

http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need start point for getting html info from web

2005-10-30 Thread nephish
yeah, i know i am going to have to write a bunch of stuff because the
values i want to get come from several different sites. ah-well, just
wanting to know the easiest way to learn how to get started. i will
check into beautiful soup, i think i have heard it referred to before.
thanks
shawn

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


Re: putting a string in Mac Address form

2005-10-30 Thread Alex Martelli
kyle.tk <[EMAIL PROTECTED]> wrote:
   ...
>   new_mac = ''
>   c = 0
>   while len(new_mac) < 16:
>   new_mac += mac[c:c+2] + ':'
>   c=c+2
>   return new_mac[:-1].upper()
   ...
> The part I think is bad is the while loop part. Could it be better?

What about replacing this whole block with, say...:

return ':'.join(mac[c:c+2] for c in range(0, 12, 2)).upper()

(as you did already check that len(mac) is 12...)


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


Re: Message about not deleted folders using Inno Setup

2005-10-30 Thread Peter Hansen
Martin wrote:
>I would like to place a message in an uninstaller window which will
> inform the user that some folders haven't been deleted. Is that possible 
> using
> Inno Setup?

Probably, but this newsgroup is for discussion of Python.  Unless you 
have some kind of Python angle to your question, a mailing list or 
something for InnoSetup would be a more appropriate place to ask.

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


Re: need start point for getting html info from web

2005-10-30 Thread Paul McGuire
<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> hey there,
>
> i have a small app that i am going to need to get information from a
> few tables on different websites. i have looked at urllib and httplib.
> the sites i need to get data from mostly have this data in tables. So
> that, i think would make it easier. Anyone suggest a good starting
> point for me to find out how to do this, or know of a link to a good
> how-to?
> thanks,
> sk
>
pyparsing comes with a simple HTML scraper example for extracting the NIST
NTP servers from an HTML table.  pyparsing is also fairly tolerant of
"unclean" HTML.  Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul


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


MacOS Extension Carbon.File.FSCatalogInfo file sizes should be UInt64?

2005-10-30 Thread donbro

If my read of the extension source (Mac/Modules/file/_Filemodule.c) is
correct, the parameter sizes specified for data and resource file sizes
are UInt32 where they should be UInt64.

In both OS9 and OSX Carbon, the MacOS File Manager (Files.h) uses
UInt64 values for data and resource filesizes.

Testing Python 2.3 and 2.4.2 on MacOS 10.3 returns 0L for the file size
values and correct values for other elements such as parentDirID:

>>> fsRef = FSRef('LICENSE')
>>> catinfo, d1, d2, d3  = fsRef.FSGetCatalogInfo(-1L)
>>> catinfo.dataPhysicalSize
0
>>> catinfo.parentDirID
2026177


in Files.h, both OS9 and OSX Carbon, the elements are UInt64:

struct FSCatalogInfo {
[...]
UInt64  dataLogicalSize;
UInt64  dataPhysicalSize;
UInt64  rsrcLogicalSize;
UInt64  rsrcPhysicalSize;
[...]
}

in _Filemodule.c the spec looks to be 32 bit:

static PyObject *FSCatalogInfo_get_dataLogicalSize(FSCatalogInfoObject
*self, void *closure)
{
return Py_BuildValue("l", self->ob_itself.dataLogicalSize);
}

I have, perhaps naively, just changed my local copy to use a BuildValue
parameter of "L" instead of "l" for each of these get and set methods
and this has survived my initial testing:

static PyObject *FSCatalogInfo_get_dataLogicalSize(FSCatalogInfoObject
*self, void *closure)
{
return Py_BuildValue("L", self->ob_itself.dataLogicalSize);
}

But my questions are:

Has anyone else seen this?

Does this seem like the right fix?

There is another routine in _Filemodule.c: FSCatalogInfo_tp_init() that
also seems to hold information relevant to parameter size.  Should this
be changed as well?

If this is a bug, what's the best procedure to report this?
Sourceforge?  This mailing list?

I'm porting a MacOS9 application from C++ to python and I expect to be
seeing a lot of these python macintosh filesystem extensions in the
near future!

Thanks for any help

Don

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


Rich __repr__

2005-10-30 Thread Ben Finney
Howdy all,

The builtin types have __repr__ attributes that return something nice,
that looks like the syntax one would use to create that particular
instance.

The default __repr__ for custom classes show the fully-qualified class
name, and the memory address of the instance.

If I want to implement a __repr__ that's reasonably "nice" to the
programmer, what's the Right Way? Are there recipes I should look at?

-- 
 \ "For certain people, after fifty, litigation takes the place of |
  `\  sex."  -- Gore Vidal |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-30 Thread Peter Otten
Bengt Richter wrote:

> I still smelled a bug in the counting of substring in the overlap region,
> and you motivated me to find it (obvious in hindsight, but aren't most ;-)
> 
> A substring can get over-counted if the "overlap" region joins
> infelicitously with the next input. E.g., try counting 'xx' in 10*'xx'
> with a read chunk of 4 instead of 1024*1024:
> 
> Assuming corrections so far posted as I understand them:
> 
>  >>> def byblocks(f, blocksize, overlap):
>  ... block = f.read(blocksize)
>  ... yield block
>  ... if overlap>0:
>  ... while True:
>  ... next = f.read(blocksize-overlap)
>  ... if not next: break
>  ... block = block[-overlap:] + next
>  ... yield block
>  ... else:
>  ... while True:
>  ... next = f.read(blocksize)
>  ... if not next: break
>  ... yield next
>  ...
>  >>> def countsubst(f, subst, blksize=1024*1024):
>  ... count = 0
>  ... for block in byblocks(f, blksize, len(subst)-1):
>  ... count += block.count(subst)
>  ... f.close()
>  ... return count
>  ...
> 
>  >>> from StringIO import StringIO as S
>  >>> countsubst(S('xx'*10), 'xx',  4)
>  13
>  >>> ('xx'*10).count('xx')
>  10
>  >>> list(byblocks(S('xx'*10), 4, len('xx')-1))
>  ['', '', '', '', '', '', 'xx']
> 
> Of course, a large read chunk will make the problem either
> go away
> 
>  >>> countsubst(S('xx'*10), 'xx',  1024)
>  10
> 
> or might make it low probability depending on the data.

[David Rasmussen]

> First of all, this isn't a text file, it is a binary file. Secondly,
> substrings can overlap. In the sequence 0010010 the substring 0010
> occurs twice.

Coincidentally the "always overlap" case seems the easiest to fix. It
suffices to replace the count() method with

def count_overlap(s, token):
pos = -1
n = 0
while 1:
try:
pos = s.index(token, pos+1)
except ValueError:
break
n += 1
return n

Or so I hope, without the thorough tests that are indispensable as we should
have learned by now...

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


Re: Problem with py2exe

2005-10-30 Thread Miki Tebeka
Hello DDany,


> copying D:\programs\python24\MSVCR71.dll -> D:\Applicazione\dist
> error: could not delete 'C:\app\dist\MSVCR71.dll': Permission denied
> 
> However folders build and dist are created: in particular into the
> folder dist i have the w9xpopen.exe file, some *.pyd files and some
> dll's... among dll's I have the MSVCR71.dll too.
>
My guess in the MSVCR71.dll is a read-only file in system32 and when py2exe
tries to copy it the 2'nd time it fails.

Either change MSVCR71.dll to read-write in system32 or delete "dist" before
calling to py2exe.

One final note: 
You subject line could have been better, see
http://www.catb.org/~esr/faqs/smart-questions.html.

Bye.
--

Miki Tebeka <[EMAIL PROTECTED]>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys


pgpvjhxpgaBxm.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Scanning a file

2005-10-30 Thread David Rasmussen
No comments to this post?

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


Re: Rich __repr__

2005-10-30 Thread Ben Finney
Ben Finney <[EMAIL PROTECTED]> wrote:
> If I want to implement a __repr__ that's reasonably "nice" to the
> programmer, what's the Right Way? Are there recipes I should look
> at?

As a (carefully selected) example from the code I'm writing:


>>> import lib.attribute
>>> m = lib.attribute.Human_Sex("male")
>>> m
Human_Sex(name='male')
>>> isinstance(m, basestring)
True
>>> print m
male

Thus, a Human_Sex instance is a string, and that's how it prints
(because of the __str__ attribute of the 'str' type). But since there
are potentially other things to know about a Human_Sex instance, the
__repr__ is overridden to give an idealised constructor call for the
instance.

class Human_Sex(str):
def __repr__(self):
repr_str = "%s(name=%s)" % (
self.__class__.__name__,
str.__repr__(self)
)
return repr_str

def __init__(self, name):
str.__init__(name)
# [... other initialisation for this class ...]

I've simplified somewhat; Human_Sex is actually just one possible
attribute being implemented, and the above methods actually come from
a superclass. I'm looking for how to do this in general, and this is a
simple enough example of what I want.

Is this __repr__ implementation too complex? Too simple? Limited in
some way? Confusingly non-standard?

-- 
 \"Laurie got offended that I used the word 'puke.' But to me, |
  `\  that's what her dinner tasted like."  -- Jack Handey |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list