Re: Counting Threads

2005-10-29 Thread David Poundall
After much optimisation it turns out the following code does the job
for me.  In the end using count didn't give me the flexibility I
needed.  Instead I now name each thread and track them accordingly.
It's arguable if I need the thread locking now though, however I have
left it in to remind me of the syntax.

Thank you for posting back Dennis.  Much appreciated.

# 
class ServerThreads:
"""
Wrapper for thread handling.  These threads are not
dynamic, that is to say once the  application is fully
loaded the number of threads running is determined
by the size of  the application - and is fixed.
"""
# - Class Attributes
# Dict holds object references to all threads
# created in the server (running or not)
thr_objects = {}
# Dict holds status of all running threads
# 0 = stopped, 1 = running
thr_running = {}
# Lock object
lck = Lock()

def launch(self, ThrName, SubToLaunch,
   SubsArgs=(), SubsKwargs={}, AsDaemon=True):

""" Kickoff a thread.
thr_objects: Dictionary using ThreadTitle
as the key which holds
references to the thread object
thr_running: Dictionary holding the status
of each thread
"""
s = ServerThreads
# -
s.lck.acquire()
try:
t = Thread(name = ThrName, target=SubToLaunch,
args = SubsArgs, kwargs = SubsKwargs)
t.setDaemon(AsDaemon)# Must be set before start
s.thr_objects[ThrName] = t
s.thr_running[ThrName] = 1
if ag.gb_Appdebug: print 'Thread Started -  ',
ThrName
t.start()
finally:
s.lck.release()
# -

def stoprequest(self,thr_name):
""" Thread stop request - stop is pending.
Join is not needed because the main code body
drops out of the thread loops once the
thr_running = true condition has been removed."""
s = ServerThreads
# -
s.lck.acquire()
s.thr_running[thr_name] = 0# Flag to tell running
thread to please terminate
if ag.gb_Appdebug: print 'Thread Stopping --- ' +
thr_name
s.lck.release()
# -

def allOK(self):
""" Returns a list of all threads that are down
when they shouldn't be (if any) """
s = ServerThreads
ThreadsDown = []
for thr_name in s.thr_objects:
if not s.thr_objects[thr_name].isAlive() and
s.thr_running[thr_name]:
# If a thread has an unscheduled stop indicate this by
returning the
# threads name otherwise return None.
ThreadsDown.append(thr_name)
return ThreadsDown

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


Re: Scanning a file

2005-10-29 Thread Bengt Richter
On 28 Oct 2005 06:51:36 -0700, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

>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.
>
ISTM you better let others know exactly what you mean by this, before
you use the various solutions suggested or your own ;-)

a) Are you talking about bit strings or byte strings?
b) Do you want to _count_ overlapping substrings??!!
Note result of s.count on your example:

 >>> s = '0010010'
 >>> s.count('0010')
 1

vs. brute force counting overlapped substrings (not tested beyond what you see 
;-)

 >>> def ovcount(s, sub):
 ... start = count = 0
 ... while True:
 ... start = s.find(sub, start) + 1
 ... if start==0: break
 ... count += 1
 ... return count
 ...
 >>> ovcount(s, '0010')
 2

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


Re: Scanning a file

2005-10-29 Thread Bengt Richter
On Fri, 28 Oct 2005 20:03:17 -0700, [EMAIL PROTECTED] (Alex Martelli) wrote:

>Mike Meyer <[EMAIL PROTECTED]> wrote:
>   ...
>> Except if you can't read the file into memory because it's to large,
>> there's a pretty good chance you won't be able to mmap it either.  To
>> deal with huge files, the only option is to read the file in in
>> chunks, count the occurences in each chunk, and then do some fiddling
>> to deal with the pattern landing on a boundary.
>
>That's the kind of things generators are for...:
>
>def byblocks(f, blocksize, overlap):
>block = f.read(blocksize)
>yield block
>while block:
>block = block[-overlap:] + f.read(blocksize-overlap)
>if block: yield block
>
>Now, to look for a substring of length N in an open binary file f:
>
>f = open(whatever, 'b')
>count = 0
>for block in byblocks(f, 1024*1024, len(subst)-1):
>count += block.count(subst)
>f.close()
>
>not much "fiddling" needed, as you can see, and what little "fiddling"
>is needed is entirely encompassed by the generator...
>
Do I get a job at google if I find something wrong with the above? ;-)

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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread Peter Otten
[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, just to keep the code cleaner,
> I'd like to bind them automatically

Why don't you just change the method signature to foo(self, x, y, z,
whatever, **kwargs)?

Peter

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


Re: Scanning a file

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

> On Fri, 28 Oct 2005 20:03:17 -0700, [EMAIL PROTECTED] (Alex Martelli)
> wrote:
> 
>>Mike Meyer <[EMAIL PROTECTED]> wrote:
>>   ...
>>> Except if you can't read the file into memory because it's to large,
>>> there's a pretty good chance you won't be able to mmap it either.  To
>>> deal with huge files, the only option is to read the file in in
>>> chunks, count the occurences in each chunk, and then do some fiddling
>>> to deal with the pattern landing on a boundary.
>>
>>That's the kind of things generators are for...:
>>
>>def byblocks(f, blocksize, overlap):
>>block = f.read(blocksize)
>>yield block
>>while block:
>>block = block[-overlap:] + f.read(blocksize-overlap)
>>if block: yield block
>>
>>Now, to look for a substring of length N in an open binary file f:
>>
>>f = open(whatever, 'b')
>>count = 0
>>for block in byblocks(f, 1024*1024, len(subst)-1):
>>count += block.count(subst)
>>f.close()
>>
>>not much "fiddling" needed, as you can see, and what little "fiddling"
>>is needed is entirely encompassed by the generator...
>>
> Do I get a job at google if I find something wrong with the above? ;-)

Try it with a subst of length 1. Seems like you missed an opportunity :-)

Peter

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


Re: Automatically creating a HOME environ variable on Windows?

2005-10-29 Thread Jarek Zgoda
[EMAIL PROTECTED] napisał(a):

> Windows doesn't have a HOME environment variable, but it does have
> HOMEDRIVE and HOMEPATH. Could Windows versions of Python automatically
> populate os.environ with HOME, where HOME =
> os.path.join(os.environ['HOMEDRIVE'], os.environ['HOMEPATH'])?

MS recommends using %USERPROFILE%, as the above in many cases returns
"C:\", which is wrong.

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


Re: Expanding Python as a macro language

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


Not the good answer, but, for information :

AutoIt is very better in beta-release (many versions beta exist). AutoIt 
beta can use COM call, and can use COM-server writed in Python with win32all 
(PyWin).

And, also, AutoIt exist like Active-X (in reality a COM server), who can to 
do used from Python (+ PyWin).


@-salutations

Michel Claveau



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


Re: How do I sort these?

2005-10-29 Thread Diez B. Roggisch
KraftDiner wrote:
> unzip doesn't seem to work for me...

Mrmpf, my bad. The operation is called unzip, but implpemented by using 
zip - so


unzip(l) == zip(*l)


So the exchange unzip with zip in my example.

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


Re: Oh what a twisted thread we weave....

2005-10-29 Thread Tom Anderson
On Sat, 28 Oct 2005, GregM wrote:

> ST_zeroMatch = 'You found 0 products'
> ST_zeroMatch2 = 'There are no products matching your selection'
>
> # why does this always drop through even though the If should be true.
>   if (ST_zeroMatch or ST_zeroMatch2) in self.webpg:

This code - i do not think it means what you think it means. Specifically, 
it doesn't mean "is either of ST_zeroMatch or ST_zeroMatch2 in 
self.webpg"; what it means is "apply the 'or' opereator to ST_zeroMatch 
and ST_zeroMatch2, then check if the result is in self.webpg". The result 
of applying the or operator to two nonempty strings is the left-hand 
string; your code is thus equivalent to

if ST_zeroMatch in self.webpg:

Which will work in cases where your page says 'You found 0 products', but 
not in cases where it says 'There are no products matching your 
selection'.

What you want is:

if (ST_zeroMatch in self.webpg) or (ST_zeroMatch2 in self.webpg):

Or something like that.

You say that you have a single-threaded version of this that works; 
presumably, you have a working version of this logic in there. Did you 
write the threaded version from scratch? Often a bad move!

tom

-- 
It's the 21st century, man - we rue _minutes_. -- Benjamin Rosenbaum
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to associate files with application

2005-10-29 Thread Lasse Vågsæther Karlsen
Dennis Lee Bieber wrote:
> On Fri, 28 Oct 2005 09:48:27 -0400, "Colin J. Williams"
> <[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
> 
> 
> 
>>I'm no Windows expert but I think that, using Windows Explorer, one can, 
>>with a right mouse click, select "Open With".


There are several ways to do this using Windows Explorer. I was under 
the assumption the OP wanted to know how he could automate it since that 
is what you typically want to do with applications you write.

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


Re: popen2

2005-10-29 Thread Piet van Oostrum
> "g.franzkowiak" <[EMAIL PROTECTED]> (gf) wrote:

>gf> If starts a process with popen2.popen3('myprogram') and myprogram.exe is
>gf>  running before, I've a connection to the second process, not to the first.
>gf> I can find the process by name before I start a process with popen2...,
>gf> but how bcan I connect t this process with a pipe ?

You have 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: Automatically creating a HOME environ variable on Windows?

2005-10-29 Thread jim . eggleston
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?

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


Re: tool for syntax coloring in html

2005-10-29 Thread vishnuvyas
I use htmlize for that. you can try that.
http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el

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


Re: Debugging with SciTE

2005-10-29 Thread jim . eggleston
Based on information from  Jarek Zgoda in another thread on the Windows
USERPROFILE environment variable, debug.py should be:
import sys
from pdb import pm, set_trace
from inspect import getmembers
if sys.platform == 'win32':
import os
os.environ['HOME'] = os.environ['USERPROFILE']
del sys.argv[0]
execfile(sys.argv[0])

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


[help]problem with python thread

2005-10-29 Thread BaoYongjun
Hi,all:
Could someone explan the python thread to me?
When I  use the python thread,I find some problems.If I run many
(such as 100) python threads ,then all thread are blocked.Below is the code:
import thread,time
def f():
print 'f()'
 
 def loop():
  while True:
   f()

for i in xrange(100):
t=threading.Thread(target=loop)
  t.start()

time.sleep(1)

I run this program on red hat linux.The  intersting thing is that
the program sometimes works,but somtimes blocks(no output at all). 

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


Re: popen2

2005-10-29 Thread Grant Edwards
On 2005-10-29, Piet van Oostrum <[EMAIL PROTECTED]> wrote:
>> "g.franzkowiak" <[EMAIL PROTECTED]> (gf) wrote:
>
>>gf> If starts a process with popen2.popen3('myprogram') and myprogram.exe is
>>gf>  running before, I've a connection to the second process, not to the 
>>first.
>>gf> I can find the process by name before I start a process with popen2...,
>>gf> but how bcan I connect t this process with a pipe ?
>
> You have to use a named pipe.

That would require that the application know about the named
pipe and open it.  I don't think there is any way to swap a
pipe in for stdin/stdout once a process is running.

-- 
Grant Edwards   grante Yow!  NOW, I'm supposed
  at   to SCRAMBLE two, and HOLD
   visi.comth' MAYO!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Expanding Python as a macro language

2005-10-29 Thread qwweeeit
Thank you for your replays (both for WMI and AutoIt beta-release)
but I would be satisfied if I had in Linux something similar to
the standard version of AutoIt!
Concerning WMI, a part the fact that it works only under Windows,
from the examples I have seen, IMHO it is much less powerful than
AutoIt.
In Linux you can easily obtain almost the same results by using shell
programming or by emulation of the shell by means of os.system("shell
command string") in Python.
But the problem is that in Linux you can't even send a keystroke to
a running GUI application!
I want to find a solution in Linux, with the help of experts
(if they don't use only Windows...)  for two reasons:
- the reduced availability in Windows of "free" or "open" applications
- the more severe security problems in Windows.
Concerning the second point, you can correctly argue that this is,
at least partly, due to the wider market share of Windows but IMHO
Linux is more robust in this field, and ...at the present times the
situation is like that!
As I want to use automation mainly in Internet, I am worried of
security and I don't want to spend time and money in anti-virus
and firewalls...
I want to give you an idea of possible applications of automation.
Imagine you want to access a big database from a html server which
only allows you to access such data in chunks pressing a "next"
button on an ASP file. It will take several hours to click the button
and then save the new data supplied by the server!
That is a typical task of AutoIt, very easily programmable,
a part that, during the script execution, you are under virus or
other security treat!
Another example of application is to emulate a spider program to make
searches or downloads in Internet. By emulating a user you can bypass
the restrictions imposed by search engines or html servers on
automatic operations (like wget or urlib).
A last example is for protecting the privacy using several
"anonymizers". That can only be possible with a macro language for
automatic connection to the web anonymizers. Also the subsequent
web operations must be carried out (IMHO!) in an automatic manner.
There are of course plenty of possible application of a macro language
and above all it can save me from be obliged to learn C++ (for Qt).
For example, to make a graph you are not obliged to make a program in
C++ or PyQt to draw and possibly print the graph of your data, but you
need only to program the data preparation then send the data to a
drawing application...
Bye.

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


Re: Expanding Python as a macro language

2005-10-29 Thread qwweeeit
Thank you for your replays (both for WMI and AutoIt beta-release)
but I would be satisfied if I had in Linux something similar to
the standard version of AutoIt!
Concerning WMI, a part the fact that it works only under Windows,
from the examples I have seen, IMHO it is much less powerful than
AutoIt.
In Linux you can easily obtain almost the same results by using shell
programming or by emulation of the shell by means of os.system("shell
command string") in Python.
But the problem is that in Linux you can't even send a keystroke to
a running GUI application!
I want to find a solution in Linux, with the help of experts
(if they don't use only Windows...)  for two reasons:
- the reduced availability in Windows of "free" or "open" applications
- the more severe security problems in Windows.
Concerning the second point, you can correctly argue that this is,
at least partly, due to the wider market share of Windows but IMHO
Linux is more robust in this field, and ...at the present times the
situation is like that!
As I want to use automation mainly in Internet, I am worried of
security and I don't want to spend time and money in anti-virus
and firewalls...
I want to give you an idea of possible applications of automation.
Imagine you want to access a big database from a html server which
only allows you to access such data in chunks pressing a "next"
button on an ASP file. It will take several hours to click the button
and then save the new data supplied by the server!
That is a typical task of AutoIt, very easily programmable,
a part that, during the script execution, you are under virus or
other security treat!
Another example of application is to emulate a spider program to make
searches or downloads in Internet. By emulating a user you can bypass
the restrictions imposed by search engines or html servers on
automatic operations (like wget or urlib).
A last example is for protecting the privacy using several
"anonymizers". That can only be possible with a macro language for
automatic connection to the web anonymizers. Also the subsequent
web operations must be carried out (IMHO!) in an automatic manner.
There are of course plenty of possible application of a macro language
and above all it can save me from be obliged to learn C++ (for Qt).
For example, to make a graph you are not obliged to make a program in
C++ or PyQt to draw and possibly print the graph of your data, but you
need only to program the data preparation then send the data to a
drawing application...
Bye.

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


Re: Generic utility class for passing data

2005-10-29 Thread Chris Smith
> "Gordon" == Gordon Airporte <[EMAIL PROTECTED]> writes:

Gordon> I'm wondering if this is might be bad practice. Sometimes
Gordon> when I need to pass around several pieces of datum I will
Gordon> put them in a tuple, then when I need to use them in a
Gordon> receiving function I get them out with subscripts. The
Gordon> problem is that the subscript number is completely
Gordon> meaningless and I have to remember the order I used.  As
Gordon> an alternative I was considering using a dummy class like
Gordon> this:

Gordon> class Dummy: pass

Gordon> Then when I need to pass some related data, Python lets me
Gordon> do this:

Gordon> prefill = Dummy() prefill.foreground = 'blue'
Gordon> #"foreground" is made up on the fly prefill.background =
Gordon> 'red' prefill.pattern = mypattern return prefill

Gordon> Now I can access the data later using meaningful names.
Gordon> Is this going to cause problems somehow? Should I rather
Gordon> go to the trouble of creating more substantial individual
Gordon> classes for every grouping of data I might need to pass
Gordon> (with __init__'s and default values and so on)? Should I
Gordon> just stick with subscripted groupings because of the
Gordon> overhead?

May fortune smile on Zoran Isailovski, whose Enum class is exactly
what you want:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486
-Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: popen2

2005-10-29 Thread Pierre Hanser
Grant Edwards a écrit :
> On 2005-10-29, Piet van Oostrum <[EMAIL PROTECTED]> wrote:
> 
>>>"g.franzkowiak" <[EMAIL PROTECTED]> (gf) wrote:
>>
>>>gf> If starts a process with popen2.popen3('myprogram') and myprogram.exe is
>>>gf>  running before, I've a connection to the second process, not to the 
>>>first.
>>>gf> I can find the process by name before I start a process with popen2...,
>>>gf> but how bcan I connect t this process with a pipe ?
>>
>>You have to use a named pipe.
> 
> 
> That would require that the application know about the named
> pipe and open it.  I don't think there is any way to swap a
> pipe in for stdin/stdout once a process is running.
> 
in C: freopen
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-10-29 Thread George Sakkis
<[EMAIL PROTECTED]> wrote:

> """
> I was looking for something similar (XML to DTD inference) but I didn't
> find anything related in python. Trang
> (http://www.thaiopensource.com/relaxng/trang-manual.html#introduction),
> on the other hand seems impressive after a few non-trivial tests. It
> would be neat to have it ported in python, at least the inference part.
>
> """
>
> If you're OK with RELAX NG rather than DTD as the schema output
> (probably a good idea if you're using namespaces), consider
> Examplotron, which I've used on many such production tasks.
>
> http://www-128.ibm.com/developerworks/xml/library/x-xmptron/
>
> It's XSLT rather than Python, but the good news is that XSLT is easy to
> invoke from Python using tools such as 4Suite.
>
> http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/python-xslt

Neat, though non-trivial XSLT makes my head spin. 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.

George

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


Re: Expanding Python as a macro language

2005-10-29 Thread Michael
[EMAIL PROTECTED] wrote:

>  But then I changed idea... Also if it is already one year that I try
>  to find a solution in Linux (mainly with Python or DCOP and KDE),

This doesn't express the question you have anywhere clearly enough. Linux
can run perfectly happily without any form of windowing environment. Many
people run using Gnome. Many others using KDE. And the number of people who
use *neither* of those but some window manager is still high.

Do you want to replay against qt applications? KDE? wx? GTK? Or send raw X11
events? The answers to these questions aren't linux specific either...


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


Re: popen2

2005-10-29 Thread Donn Cave
Quoth Pierre Hanser <[EMAIL PROTECTED]>:
| Grant Edwards a écrit :
|> On 2005-10-29, Piet van Oostrum <[EMAIL PROTECTED]> wrote:
|>>>"g.franzkowiak" <[EMAIL PROTECTED]> (gf) wrote:
|>>
|>>>gf> If starts a process with popen2.popen3('myprogram') and myprogram.exe is
|>>>gf> running before, I've a connection to the second process, not to the 
first.
|>>>gf> I can find the process by name before I start a process with popen2...,
|>>>gf> but how bcan I connect t this process with a pipe ?
|>>
|>>You have to use a named pipe.
|> 
|> 
|> That would require that the application know about the named
|> pipe and open it.  I don't think there is any way to swap a
|> pipe in for stdin/stdout once a process is running.
|> 
| in C: freopen

Hello, it seems fairly clear that the stdin/stdout in question belongs
to another process, which cannot be instructed at this point to execute
freopen().  If there's a way to do this, it will be peculiar to the
platform and almost certainly not worth the effort.

Donn Cave, [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Expanding Python as a macro language

2005-10-29 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote:
   ...
> But the problem is that in Linux you can't even send a keystroke to
> a running GUI application!

Actually, if the app is running under X11 you may try to fake out a
keystroke event (with low level calls, but ctypes might let you use it
from Python).  Of course, the app WILL be told that the keystroke is
fake, through a special flag if it cares to check for it, for security
reasons; but if the app doesn't specifically defend itself in this way.

See, for example, http://xmacro.sourceforge.net/ -- I guess that
xmacroplay could pretty easily be adapted, or maybe even used as is with
an os.popen.


> I want to find a solution in Linux, with the help of experts
> (if they don't use only Windows...)  for two reasons:
> - the reduced availability in Windows of "free" or "open" applications
> - the more severe security problems in Windows.
> Concerning the second point, you can correctly argue that this is,
> at least partly, due to the wider market share of Windows but IMHO
> Linux is more robust in this field, and ...at the present times the
> situation is like that!

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).

It also has an excellent italian newsgroup, it.comp.macintosh -- quite
high volume (as it discusses ANYthing Apple, from iPod shuffles to
golden oldies to rumors about new servers &c, with a lot of volume on
the audio and video applications that macs do so well) but worth it.


However, all the specific use cases you describe are best handled by
either fully emulating or directly integrating with a browser; faking
keystrokes is definitely too low-level an approach.  Python is very good
at dealing with the web (it IS, apparently, the favourite language of
Tim Berners Lee --- he came give a keynote at a Python conference),
including recording and replaying cookies and anything else you may need
to make a "special purpose browser" for automation purposes. Twisted is
an asynchronous framework for very well-performing, lightweight clients
and servers -- or, Python's standard library can suffice if you're not
in a terrible hurry;-).

Alternatively, Firefox and other Mozilla Foundation apps are designed to
be automated via XPCOM, essentially a cross-platform equivalent of
Microsoft's good old COM, and there are Python interfaces to it (some
future Firefox version might perhaps integrate a Python engine, just
like it integrates a Javascript engine today, but I wouldn't hold my
breath waiting;-).


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


Building 2.4.2 on OpenBSD 3.8

2005-10-29 Thread Dan M
I've been working with python 2.2.3 for the last couple of weeks, but
would like to move up to 2.4. I grabbed the 2.4.2 tarball and attempted to
install, but I get:

gcc -pthread -c -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes 
-I. -I./Include  -DPy_BUILD_CORE -o Modules/python.o Modules/python.c
In file included from /usr/include/sys/select.h:38,
 from Include/pyport.h:116,
 from Include/Python.h:55,
 from Modules/python.c:3:
/usr/include/sys/event.h:53: error: syntax error before "u_int"
/usr/include/sys/event.h:55: error: syntax error before "u_short"
*** Error code 1

Stop in /usr/local/src/Python-2.4.2.

immediately upon running "make". I took at look at event.h to see if I
could find and fix the problem, but the error isn't immediately apparent
to me.

Has anyone successfully built 2.4.2 on OpenBSD? If so, how did you do it?

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


Re: Automatically creating a HOME environ variable on Windows?

2005-10-29 Thread Maciej Dziardziel
[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.

-- 
Maciej "Fiedzia" Dziardziel (fiedzia (at) fiedzia (dot) prv (dot) pl)
www.fiedzia.prv.pl

If you lost your left arm, your right arm would be left.

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


Re: Building 2.4.2 on OpenBSD 3.8

2005-10-29 Thread Martin v. Löwis
Dan M wrote:
> /usr/include/sys/event.h:53: error: syntax error before "u_int"
> /usr/include/sys/event.h:55: error: syntax error before "u_short"
> *** Error code 1
> 
> Stop in /usr/local/src/Python-2.4.2.
> 
> immediately upon running "make". I took at look at event.h to see if I
> could find and fix the problem, but the error isn't immediately apparent
> to me.

In that case, it would help if you would report what line 53 of event.h
precisely is. Typically, if it says "syntax error before", it means
"immediately, right away before", or "the error actually is *in*
u_int" (the latter indicating that u_int is not defined at that point).

Generating a preprocessor output then helps, seeing whether something
got #define'd in some strange sense, or whether u_int was defined before
use.

Most likely, it is the old BSD bug of not supporting POSIX correctly.
Somebody should really work with the system developers to get this
resolved; it's been several years now that the bug is present.

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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread Steve Holden
Peter Otten wrote:
> [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, just to keep the code cleaner,
>>I'd like to bind them automatically
> 
> 
> Why don't you just change the method signature to foo(self, x, y, z,
> whatever, **kwargs)?
> 
> Peter
> 
Probably because values are then required for those arguments. Plus it's 
a lot of work to specify "a very long list", and the list will also need 
maintaining.

I must, however, agree with Mike's advice: it's unwise to try and 
pollute a function's namespace with arbitrary variables. Some kind of 
bunch-like object would seem to be the most satisfactory way to go.

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


py.log using decorators for DRY

2005-10-29 Thread yoda
I'm using py.log for logging and I find that I end up having the
following pattern emerge within my code (influenced by
http://agiletesting.blogspot.com/2005/06/keyword-based-logging-with-py-library.html):


def foo(**kwargs):
log.foo(kwargs)
#body form

This led me to believe that I could simplify that pattern with the
following idiom :


def logit (fn):
'''
decorator to enable logging of all tagged methods
'''
def decorator (**kwargs):
# call a method named fn.func_name on log with kwargs
#should be something like: log.func_name (kwargs)

return decorator


I can then do add @logit to all my existing methods via a script
(there's a truck load of methods to tag):


@logit
def oldfoo () : pass


My question is in regards to the body form in the decorator.  How do I
call that method on the log object at runtime?

(ps.  I hope my question is clear $)

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


Re: Expanding Python as a macro language

2005-10-29 Thread Mike Meyer
[EMAIL PROTECTED] (Alex Martelli) writes:

>...
>> But the problem is that in Linux you can't even send a keystroke to
>> a running GUI application!
> Actually, if the app is running under X11 you may try to fake out a
> keystroke event (with low level calls, but ctypes might let you use it
> from Python).  Of course, the app WILL be told that the keystroke is
> fake, through a special flag if it cares to check for it, for security
> reasons; but if the app doesn't specifically defend itself in this way.

I'm pretty sure that python-xlib can do this without having to go to
low level calls.

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


Any Pythonic GTK Undo library?

2005-10-29 Thread Tony Nelson
I'm looking for a "pythonic" GTK Undo library/class.  It would have a 
framework for Undo/Redo, and would provide Undo/Redo for TextView, 
Entry, and containers and other classes.  In a "batteries included" 
fashion, just instantiating a "UndoableTextView" or "UndoableEntry" or 
"UndoableContainer" would provide Undo and Redo in the right-click menu; 
simply connecting such an object to an "UndoableUIManager" would take 
care of the stock items in the menus and toolbar; and there would be a 
simple connection to some sort of "UndoableDocument" interface or mix-in 
for more global sequencing of Undo/Redo.  Does something like this exist 
for Python or GTK?  Googling didn't turn up anything useful.

I'm disappointed that GTK doesn't do this already.  Making my own seems 
doable, but a fair amount of work.  If there isn't some such thing 
already, is there interest in using one that I make?

I know about GUndo, which doesn't implement any actual undo operations; 
the actual operations would need to be coded for TextView and Entry and 
anything else.

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


extracting numbers from a file, excluding fixed words

2005-10-29 Thread dawenliu
Hi, I have a file with this content:
xxx xx x xxx
1
0
0
0
1
1
0
(many more 1's and 0's to follow)
y yy yyy yy y yyy

The x's and y's are FIXED and known words which I will ignore, such as
"This is the start of the file" and "This is the end of the file".  The
digits 1 and 0 have UNKNOWN length.  I want to extract the digits and
store them in a file.  Any suggestions will be appreciated.

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


Re: py.log using decorators for DRY

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

> I'm using py.log for logging and I find that I end up having the following
> pattern emerge within my code (influenced by
> http://agiletesting.blogspot.com/2005/06/keyword-based-logging-with-py-lib
> rary.html):
> 
> def foo(**kwargs):
> log.foo(kwargs)
> #body form
> 
> This led me to believe that I could simplify that pattern with the
> following idiom :
> 
> 
> def logit (fn):
> '''
> decorator to enable logging of all tagged methods
> '''
> def decorator (**kwargs):
> # call a method named fn.func_name on log with kwargs
> #should be something like: log.func_name (kwargs)
> 
> return decorator

Assuming the attributes of object 'log' don't change at runtime (i.e.,
you're OK with early binding), I'd code:

def logit(fn):
method = getattr(log, fn.func_name)
def callit(**kwargs): return method(kwargs)
return callit

If you need to do late binding instead, you can move the getattr to
inside the body of callit.


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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread [EMAIL PROTECTED]
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.

Cheers,
Chris

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


Re: Understanding the arguments for SubElement factory in ElementTree

2005-10-29 Thread B Mahoney
Your SubElement call is lacking the attrib argument, but you can't set
text, anyway.

The elementtree source makes it clear, you can only set element attrib
attributes
with SubElement

def SubElement(parent, tag, attrib={}, **extra):
attrib = attrib.copy()
attrib.update(extra)
element = parent.makeelement(tag, attrib)
parent.append(element)
return element

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


Re: Scanning a file

2005-10-29 Thread Bengt Richter
On Sat, 29 Oct 2005 10:34:24 +0200, Peter Otten <[EMAIL PROTECTED]> wrote:

>Bengt Richter wrote:
>
>> On Fri, 28 Oct 2005 20:03:17 -0700, [EMAIL PROTECTED] (Alex Martelli)
>> wrote:
>> 
>>>Mike Meyer <[EMAIL PROTECTED]> wrote:
>>>   ...
 Except if you can't read the file into memory because it's to large,
 there's a pretty good chance you won't be able to mmap it either.  To
 deal with huge files, the only option is to read the file in in
 chunks, count the occurences in each chunk, and then do some fiddling
 to deal with the pattern landing on a boundary.
>>>
>>>That's the kind of things generators are for...:
>>>
>>>def byblocks(f, blocksize, overlap):
>>>block = f.read(blocksize)
>>>yield block
>>>while block:
>>>block = block[-overlap:] + f.read(blocksize-overlap)
>>>if block: yield block
>>>
>>>Now, to look for a substring of length N in an open binary file f:
>>>
>>>f = open(whatever, 'b')
>>>count = 0
>>>for block in byblocks(f, 1024*1024, len(subst)-1):
>>>count += block.count(subst)
>>>f.close()
>>>
>>>not much "fiddling" needed, as you can see, and what little "fiddling"
>>>is needed is entirely encompassed by the generator...
>>>
>> Do I get a job at google if I find something wrong with the above? ;-)
>
>Try it with a subst of length 1. Seems like you missed an opportunity :-)
>
I was thinking this was an example a la Alex's previous discussion
of interviewee code challenges ;-)

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']

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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread Peter Otten
Steve Holden wrote:

>> Why don't you just change the method signature to foo(self, x, y, z,
>> whatever, **kwargs)?

> Probably because values are then required for those arguments. Plus it's
> a lot of work to specify "a very long list", and the list will also need
> maintaining.
 
Note that I kept the **kwargs argument. He would only have to specify
variables that are actually used in foo(), and these need maintaining
anyway. As to optional arguments, you can provide default values. Checking
for those is easier and safer than using local variables that may not be
bound or accidentally refer to a global.

# e. g.
missing = object() # if None is a valid state of whatever
def foo(self, whatever=missing, **kwargs):
if whatever is not missing:
# use whatever

> I must, however, agree with Mike's advice: it's unwise to try and
> pollute a function's namespace with arbitrary variables. Some kind of
> bunch-like object would seem to be the most satisfactory way to go.  

Using a bunch doesn't remove the necessity of an existence-test either (for
optional attributes).

Peter


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


Re: popen2

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

>GE> On 2005-10-29, Piet van Oostrum <[EMAIL PROTECTED]> wrote:
 "g.franzkowiak" <[EMAIL PROTECTED]> (gf) wrote:
>>> 
>gf> If starts a process with popen2.popen3('myprogram') and myprogram.exe is
>gf> running before, I've a connection to the second process, not to the first.
>gf> I can find the process by name before I start a process with popen2...,
>gf> but how bcan I connect t this process with a pipe ?
>>> 
>>> You have to use a named pipe.

>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.
-- 
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-29 Thread Alex Martelli
Bengt Richter <[EMAIL PROTECTED]> wrote:
   ...
> >>>while block:
> >>>block = block[-overlap:] + f.read(blocksize-overlap)
> >>>if block: yield block
   ...
> I was thinking this was an example a la Alex's previous discussion
> of interviewee code challenges ;-)
> 
> 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']

Heh, OK, I should get back into the habit of adding a "warning: untested
code" when I post code (particularly when it's late and I'm
jetlagged;-).  The code I posted will never exit, since block always
keeps the last overlap bytes; it needs to be changed into something like
(warning -- untested code!-)

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

(the if/else is needed to handle requests for overlaps <= 0, if desired;
I think it's clearer to split the cases rather than to test inside the
loop's body).


Alex

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


Re: Microsoft Hatred FAQ

2005-10-29 Thread Tim Roberts
"David Schwartz" <[EMAIL PROTECTED]> wrote:

>Paul Rubin wrote:
>
>> "David Schwartz" <[EMAIL PROTECTED]> writes:
>
>>> To call it an "established legal fact" is to grossly distort the
>>> circumstances under which it was determined and upheld.
>
>> Who is paying you to post such nonsense?
>
>That's basically slander.

Slander is spoken.  When it's written down like this, it's libel.
Slander/spoken start with S, libel/literature start with L.

Normally, I would never post such a trivial correction, but I thought it
was quite appropriate to throw yet another completely useless fact into
this completely useless thread, which was started by a completely useless
troll.
-- 
- Tim Roberts, [EMAIL PROTECTED]
  Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-29 Thread Peter Otten
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.

Peter

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


Re: Any Pythonic GTK Undo library?

2005-10-29 Thread Dave Cook
On 2005-10-29, Tony Nelson <[EMAIL PROTECTED]> wrote:

> I'm looking for a "pythonic" GTK Undo library/class.  It would have a 

You might ask the authors of Kiwi if they plan to add undo/redo.  Or help
them add it if you can.

http://www.async.com.br/projects/kiwi/

It would be great to have this feature in the Gtk C API, though.  I do see
some relevant bugzilla entries:

http://bugzilla.gnome.org/show_bug.cgi?id=316551

You might want to make a new request for a general undo/redo interface.

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


Re: Hi All - Newby

2005-10-29 Thread Tim Roberts
"Ask" <[EMAIL PROTECTED]> wrote:
>
>Hi TIm,
>
>Ahh I see.. (Told you I was a newby!)  ;-)
>
>Tkinter is what I'm using as that was loaded by default with the 
>installation of Python I am using.

Now your question makes good sense, especially if you were coming from
something like the Win32 API.

In Tkinter, as in MOST of the GUI toolkits for Python, you dont just place
a widget at a specific X,Y with a specific width and height.  The problems
with that kind of approach are well known; such layouts don't scale with
different fonts, they look bad when the owning window is stretched, they
don't compensate for screen sizes, etc.

The various toolkits have different methods for managing this.  In Tk, the
concept is called a "geometry manager".  You use a geometry manager to
define the RELATIONSHIP between widgets, and between the widgets and the
owning window.  The geometry manager, then, worries about the exact
placement and size.

Googling for "tkinter geometry manager" should prove fruitful.  Here is an
excellent introduction:

http://effbot.org/zone/tkinter-geometry.htm

There is a fabulous book called "Python and Tkinter Programming" by John
Grayson, ISBN 1884777813.  If you will be getting serious with Tkinter, I
strongly recommend it.

Personally, I started with Tkinter, but I couldn't wrap my brain around the
details.  I switched to wxPython, and never looked back.
-- 
- Tim Roberts, [EMAIL PROTECTED]
  Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extracting numbers from a file, excluding fixed words

2005-10-29 Thread Kent Johnson
dawenliu wrote:
> Hi, I have a file with this content:
> xxx xx x xxx
> 1
> 0
> 0
> 0
> 1
> 1
> 0
> (many more 1's and 0's to follow)
> y yy yyy yy y yyy
> 
> The x's and y's are FIXED and known words which I will ignore, such as
> "This is the start of the file" and "This is the end of the file".  The
> digits 1 and 0 have UNKNOWN length.  I want to extract the digits and
> store them in a file.  Any suggestions will be appreciated.
> 

Off the top of my head (not tested):

inf = open('input.txt')
out = open('output.txt', 'w')

skips = [
  'xxx xx x xxx',
  'y yy yyy yy y yyy',
]

for line in inf:
  for skip in skips:
if skip in line:
  continue
  out.write(line)

inf.close()
out.close()

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


Re: extracting numbers from a file, excluding fixed words

2005-10-29 Thread Daniel Bowett
dawenliu wrote:
> Hi, I have a file with this content:
> xxx xx x xxx
> 1
> 0
> 0
> 0
> 1
> 1
> 0
> (many more 1's and 0's to follow)
> y yy yyy yy y yyy
> 
> The x's and y's are FIXED and known words which I will ignore, such as
> "This is the start of the file" and "This is the end of the file".  The
> digits 1 and 0 have UNKNOWN length.  I want to extract the digits and
> store them in a file.  Any suggestions will be appreciated.
> 

Open the file and read each line 1 at a time. If the line doesn't equal 
x or y then add the line to a list

f = open("file.txt")

numbers = []

for eachline in f.xreadlines():
if (eachline <> x) or (eachline <> y):
numbers.append(eachline)

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


Re: Scanning a file

2005-10-29 Thread Paul Watson
"Mike Meyer" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "Paul Watson" <[EMAIL PROTECTED]> writes:
...
> Did you do timings on it vs. mmap? Having to copy the data multiple
> times to deal with the overlap - thanks to strings being immutable -
> would seem to be a lose, and makes me wonder how it could be faster
> than mmap in general.

The only thing copied is a string one byte less than the search string for 
each block.

I did not do due dilligence with respect to timings.  Here is a small 
dataset read sequentially and using mmap.

$ ls -lgG t.dat
-rw-r--r--  1 16777216 Oct 28 16:32 t.dat
$ time  ./scanfile.py
1048576
0.80s real 0.64s user 0.15s system
$ time  ./scanfilemmap.py
1048576
   20.33s real 6.09s user14.24s system

With a larger file, the system time skyrockets. I assume that to be the 
paging mechanism in the OS.  This is Cyngwin on Windows XP.

$ ls -lgG t2.dat
-rw-r--r--  1 268435456 Oct 28 16:33 t2.dat
$ time  ./scanfile.py
16777216
   28.85s real16.37s user 0.93s system
$ time  ./scanfilemmap.py
16777216
  323.45s real94.45s user   227.74s system


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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread Steve Holden
Peter Otten wrote:
> Steve Holden wrote:
> 
> 
>>>Why don't you just change the method signature to foo(self, x, y, z,
>>>whatever, **kwargs)?
> 
> 
>>Probably because values are then required for those arguments. Plus it's
>>a lot of work to specify "a very long list", and the list will also need
>>maintaining.
> 
>  
> Note that I kept the **kwargs argument. He would only have to specify
> variables that are actually used in foo(), and these need maintaining
> anyway. As to optional arguments, you can provide default values. Checking
> for those is easier and safer than using local variables that may not be
> bound or accidentally refer to a global.
> 
Now I think I understand your intention better. Further discussion would 
be flogging horse already dead for several days.

> # e. g.
> missing = object() # if None is a valid state of whatever
> def foo(self, whatever=missing, **kwargs):
> if whatever is not missing:
> # use whatever
> 
> 
>>I must, however, agree with Mike's advice: it's unwise to try and
>>pollute a function's namespace with arbitrary variables. Some kind of
>>bunch-like object would seem to be the most satisfactory way to go.  
> 
> 
> Using a bunch doesn't remove the necessity of an existence-test either (for
> optional attributes).
> 
No more it does.

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: Scanning a file

2005-10-29 Thread netvaibhav
I think implementing a finite state automaton would be a good (best?)
solution. I have drawn a FSM for you (try viewing the following in
fixed width font). Just increment the count when you reach state 5.

<---|
   ||
0  0   |  1  0  |0
 -->[1]--->[2]--->[3]--->[4]--->[5]-|
 ^ ||  ^ | |  |
1| |<---|  | | |1 |1
 |_|1  |_| |  |
 ^ 0   |  |
 |-|<-|

If you don't understand FSM's, try getting a book on computational
theory (the book by Hopcroft & Ullman is great.)

Here you don't have special cases whether reading in blocks or reading
whole at once (as you only need one byte at a time).

Vaibhav

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


Re: py.log using decorators for DRY

2005-10-29 Thread yoda
I feel so stupid... lol... now why didn't I think of that?

Thanks Alex.

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


Re: extracting numbers from a file, excluding fixed words

2005-10-29 Thread dawenliu
Thanks Kent.  The code looks reasonable, but the result is that, the
output file comes out identical as the input file, with all the 
and  remaining inside.

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


Recursive generators and backtracking search

2005-10-29 Thread Talin
I've been using generators to implement backtracking search for a while
now. Unfortunately, my code is large and complex enough (doing
unification on math expressions) that its hard to post a simple
example. So I decided to look for a simpler problem that could be used
to demonstrate the technique that I am talking about.

I noticed that PEP 255 (Simple Generators) refers to an implementation
of the "8 Queens" problem in the lib/test directory. Looking at the
code, I see that while it does use generators, it doesn't use them
recursively.

As an alternative, I'd like to present the following implementation. If
you compare this one with the one in lib/test/test_generator.py you
will agree (I hope) that by using recursive generators to implement
backtracking, the resulting code is a little more straightforward and
intuitive:

# Example of using generators to implement backtracking search.
# The example below is an implementation of the classic "N queens"
# problem (place N queens on an N x N chessboard so that none are
# threatened by the others.)
#
# Board representation: Since no two queens can be one the same
# row, the board is represented as a tuple of N length, where
# each element is the column occupied by the queen on that row.

def queens( bsize ):

# Function to test if a queen is threatened by any previously
# placed queen.
def threaten( qarray, newpos ):
# Now check the diagonals
dist = len( qarray )# Distance between rows
for q in qarray:
if q == newpos: return True # Same column
if q + dist == newpos: return True  # diagonal
if q - dist == newpos: return True  # diagonal
dist -= 1
return False

def qsearch( qarray = () ):
for q in range( 0, bsize ):# Try each position
if not threaten( qarray, q ):  # If not threatened
pos = qarray + ( q, ); # Append to the pos array

if len( pos ) >= bsize:# Are we done?
yield pos  # Yield the answer
else:# recursively call new generator
for pos in qsearch( pos ):
yield pos

print "Queens problem for", bsize, "x", bsize, "board."
for ans in qsearch():
`   # Print out the board
print "+" + "---+" * bsize;
for q in ans:
print "|" + "   |" * q + " Q |" + "   |" * (bsize - q - 1)
print "+" + "---+" * bsize;
print

queens( 8 )

Now, you may be wondering what is my point? Well, first, I want to
encourage people to think about using Python as a language for complex
heuristic search problems. Traditionally, LISP and Prolog have been the
language of choices for "AI" type programming, however there is a clear
advantage to the readability and maintainability of Python, as well as
being much more integrated into modern computing environments (in terms
of available interpreters, IDEs, libraries, etc.)

Secondly, I want to lobby for additional support in the language and
standard libraries for handling such problems. There are a number of
fairly obvious language enhancements which would make the above example
even simpler - for examle, the idea of being able to return the output
of one generator directly from another instead of having to iterate
through all of the results and then re-yield them has already been
discussed in this forum.

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


Re: Scanning a file

2005-10-29 Thread Steve Holden
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.

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: extracting numbers from a file, excluding fixed words

2005-10-29 Thread dawenliu
I've changed the code a little bit and works fine now:
inf = open('input.txt')
out = open('output.txt', 'w')

skips = [
'xxx xx x xxx',
  'y yy yyy yy y yyy']

for line in inf:
  flag = 0
  for skip in skips:
if skip in line:
  flag = 1
  continue
  if flag == 0:
out.write(line)

inf.close()
out.close()

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


how to discard a line if it's not a number?

2005-10-29 Thread dawenliu
Hi, I'm reading a file line by line, and whenever a line is not
consisted of a single number (such as 0.315), I want to discard that
line (and store only the single numbers).

For example,

0.315
discarded this line of text
3.8
-1.44
forget about me also
2.6


Then I want to store only the four numbers into another file, without
the two sentences.  
Suggestions are appreciated!

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


Re: popen2

2005-10-29 Thread Grant Edwards
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.

That's all well and good, but it's got nothing to do with the
OP's problem: he's got a program that's already running and he
wants to write a Python program that can "attach" pipes to that
already running program's stdin/stdout.  I know there's no way
to do that under Unix.  IIRC, the OP is running Win32, and I'm
not quite as confident that it can't be dont under Win32, but I
don't think it can.

-- 
Grant Edwards   grante Yow!  With YOU, I can be
  at   MYSELF... We don't NEED
   visi.comDan Rather...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to discard a line if it's not a number?

2005-10-29 Thread bearophileHUGS
This is a possible solution, using exceptions:

fileName = "data"
out = file(fileName + "_filt.txt", "w")
for line in file(fileName + ".txt"):
try:
nline = float(line)
except ValueError:
pass
else:
out.write(str(nline) + "\n")
out.close()

If the file is small enough this can be a little faster:

fileName = "data"
filtered = []
data = file(fileName + ".txt").readlines()
for line in data:
try:
filtered.append( str(float(line)) )
except ValueError:
pass
out = open(fileName + "_filt.txt", "w")
out.write( "\n".join(filtered) )
out.close()

Bye,
bearophile

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


Re: Scanning a file

2005-10-29 Thread Tim Roberts
Steven D'Aprano <[EMAIL PROTECTED]> wrote:
>
>On Fri, 28 Oct 2005 15:29:46 +0200, Björn Lindström wrote:
>
>> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
>> 
>>> f = open("filename", "rb")
>>> s = f.read()
>>> sub = "\x00\x00\x01\x00"
>>> count = s.count(sub)
>>> print count
>> 
>> That's a lot of lines. This is a bit off topic, but I just can't stand
>> unnecessary local variables.
>> 
>> print file("filename", "rb").read().count("\x00\x00\x01\x00")
>
>Funny you should say that, because I can't stand unnecessary one-liners.
>
>In any case, you are assuming that Python will automagically close the
>file when you are done.

Nonsense.  This behavior is deterministic.  At the end of that line, the
anonymous file object out of scope, the object is deleted, and the file is
closed.
-- 
- Tim Roberts, [EMAIL PROTECTED]
  Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-29 Thread Scott David Daniels
Paul Watson wrote:

> Here is a better one that counts, and not just detects, the substring.  This 
> is -much- faster than using mmap; especially for a large file that may cause 
> paging to start.  Using mmap can be -very- slow.
> 
 > 
> ...
> b = fp.read(blocksize)
> count = 0
> while len(b) > be:
> count += b.count(ss)
> b = b[-be:] + fp.read(blocksize)
> ...
In cases where that one wins and blocksize is big,
this should do even better:
 ...
 block = fp.read(blocksize)
 count = 0
 while len(block) > be:
 count += block.count(ss)
 lead = block[-be :]
 block = fp.read(blocksize)
 count += (lead + block[: be]).count(ss)
 ...
-- 
-Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-29 Thread Alex Martelli
Tim Roberts <[EMAIL PROTECTED]> wrote:
   ...
> >> print file("filename", "rb").read().count("\x00\x00\x01\x00")
> >
> >Funny you should say that, because I can't stand unnecessary one-liners.
> >
> >In any case, you are assuming that Python will automagically close the
> >file when you are done.
> 
> Nonsense.  This behavior is deterministic.  At the end of that line, the
> anonymous file object out of scope, the object is deleted, and the file is
> closed.

In today's implementations of Classic Python, yes.  In other equally
valid implementations of the language, such as Jython, IronPython, or,
for all we know, some future implementation of Classic, that may well
not be the case.  Many, quite reasonably, dislike relying on a specific
implementation's peculiarities, and prefer to write code that relies
only on what the _language_ specs guarantee.


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


Re: extracting numbers from a file, excluding fixed words

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

> Hi, I have a file with this content:
> xxx xx x xxx
> 1
> 0
> 0
> 0
> 1
> 1
> 0
> (many more 1's and 0's to follow)
> y yy yyy yy y yyy
> 
> The x's and y's are FIXED and known words which I will ignore, such as
> "This is the start of the file" and "This is the end of the file".  The
> digits 1 and 0 have UNKNOWN length.  I want to extract the digits and
> store them in a file.  Any suggestions will be appreciated.

[[warning, untested code...]]

infile = open('infile.txt')
oufile = open('oufile.txt', 'w')
for line in infile:
if line.strip().isdigit(): oufile.write(line)
oufile.close()
infile.close()


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


Re: Recursive generators and backtracking search

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

> even simpler - for examle, the idea of being able to return the output
> of one generator directly from another instead of having to iterate
> through all of the results and then re-yield them has already been
> discussed in this forum.

I missed those discussions, having been away from the group for awhile.
To me, the simplification of changing, e.g.,

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).


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


lambda functions within list comprehensions

2005-10-29 Thread Max Rybinsky
Hello!

Please take a look at the example.

>>> a = [(x, y) for x, y in map(None, range(10), range(10))] # Just a list of 
>>> tuples
>>> a
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8,
8), (9, 9)]

Now i want to get a list of functions x*y/n, for each (x, y) in a:

>>> funcs = [lambda n: x * y / n for x, y in a]

It looks consistent!

>>> funcs
[ at 0x010F3DF0>,  at 0x010F7CF0>,
 at 0x010F7730>,  at 0x010FD270>,
 at 0x010FD0B0>,  at 0x010FD5B0>,
 at 0x010FD570>,  at 0x010FD630>,
 at 0x01100270>,  at 0x011002B0>]

...and functions are likely to be different.

>>> funcs[0](1)
81

But they aren't!

>>> for func in funcs:
... print func(1)
...
81
81
81
81
81
81
81
81
81
81

It seems, all functions have x and y set to 9.
What's wrong with it? Is it a bug?

On the other hand, this problem has a solution:

>>> def buldFunc(x, y):
... return lambda n: x * y / n
...
>>> funcs = [buldFunc(x, y) for x, y in a]

... and it does work!

But why not to save several lines of code? ;)

Thanks in advance.

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


Re: lambda functions within list comprehensions

2005-10-29 Thread Alex Martelli
Max Rybinsky <[EMAIL PROTECTED]> wrote:
   ...
> >>> funcs = [lambda n: x * y / n for x, y in a]
   ...
> It seems, all functions have x and y set to 9.
> What's wrong with it? Is it a bug?

It's known as *late binding*: names x and y are looked up when the
lambda's body is executing, and at that time they're both set to the
value 9.  You appear to have expected *early binding*, with the names
being somehow looked up at the time the lambda keyword executed, but
that's just not Python semantics (and would interfere with many other
cases where late binding is exactly what one wants).

You've already indicated what's probably the best solution -- a factory
function instead of the lambda.  There are other ways to request early
binding, and since you appear to value compactness over clarity the most
compact way is probably:

funcs = [lambda n, x=x, y=y: x*y/n for x, y in a]

it's not perfect, because the resulting functions can take up to 3
arguments, so that if you called funcs[1](2,3) you'd get an unwanted
result rather than a TypeError exception.  If you're keen on getting the
exception in such cases, you can use a lambda factory in the same role
as the much clearer and more readable factory function you had (which I
keep thinking is the _sensible_ solution)...:

funcs = [ (lambda x,y: lambda n: x*y/n)(x,y) for x,y in a ]


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


Re: Any Pythonic GTK Undo library?

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

> On 2005-10-29, Tony Nelson <[EMAIL PROTECTED]> wrote:
> 
> > I'm looking for a "pythonic" GTK Undo library/class.  It would have a 
> 
> You might ask the authors of Kiwi if they plan to add undo/redo.  Or help
> them add it if you can.
> 
> http://www.async.com.br/projects/kiwi/
> 

Well, after I implement it myself, if I do, I could give them the code 
to port to kiwi.


> It would be great to have this feature in the Gtk C API, though.  I do see
> some relevant bugzilla entries:
> 
> http://bugzilla.gnome.org/show_bug.cgi?id=316551
> 
> You might want to make a new request for a general undo/redo interface.

Well, after I implement it myself, if I do, I could give them the code 
to translate to C.

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


Re: Scanning a file

2005-10-29 Thread Steve Holden
[EMAIL PROTECTED] wrote:
> I think implementing a finite state automaton would be a good (best?)
> solution. I have drawn a FSM for you (try viewing the following in
> fixed width font). Just increment the count when you reach state 5.
> 
> <---|
>||
> 0  0   |  1  0  |0
>  -->[1]--->[2]--->[3]--->[4]--->[5]-|
>  ^ ||  ^ | |  |
> 1| |<---|  | | |1 |1
>  |_|1  |_| |  |
>  ^ 0   |  |
>  |-|<-|
> 
> If you don't understand FSM's, try getting a book on computational
> theory (the book by Hopcroft & Ullman is great.)
> 
> Here you don't have special cases whether reading in blocks or reading
> whole at once (as you only need one byte at a time).
> 
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 :-)

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: lambda functions within list comprehensions

2005-10-29 Thread Jean-Paul Calderone
On 29 Oct 2005 14:25:24 -0700, Max Rybinsky <[EMAIL PROTECTED]> wrote:
>Hello!
>
>Please take a look at the example.
>
 a = [(x, y) for x, y in map(None, range(10), range(10))] # Just a list of 
 tuples
 a
>[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8,
>8), (9, 9)]
>
>Now i want to get a list of functions x*y/n, for each (x, y) in a:
>
 funcs = [lambda n: x * y / n for x, y in a]
>
>It looks consistent!
>
 funcs
>[ at 0x010F3DF0>,  at 0x010F7CF0>,
> at 0x010F7730>,  at 0x010FD270>,
> at 0x010FD0B0>,  at 0x010FD5B0>,
> at 0x010FD570>,  at 0x010FD630>,
> at 0x01100270>,  at 0x011002B0>]
>
>...and functions are likely to be different.

A search of this group would reveal one or two instances in the past in which 
this question has been asked and answered:

  http://article.gmane.org/gmane.comp.python.general/427200
  http://article.gmane.org/gmane.comp.python.general/424389
  http://article.gmane.org/gmane.comp.python.general/399224
  http://article.gmane.org/gmane.comp.python.general/390097
  http://article.gmane.org/gmane.comp.python.general/389011
  http://article.gmane.org/gmane.comp.python.general/334625

I could go on for a while longer, but hopefully some of the linked material 
will answer your question.

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


Re: Scanning a file

2005-10-29 Thread Paul Watson
"Alex Martelli" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

> In today's implementations of Classic Python, yes.  In other equally
> valid implementations of the language, such as Jython, IronPython, or,
> for all we know, some future implementation of Classic, that may well
> not be the case.  Many, quite reasonably, dislike relying on a specific
> implementation's peculiarities, and prefer to write code that relies
> only on what the _language_ specs guarantee.

How could I identify when Python code does not close files and depends on 
the runtime to take care of this?  I want to know that the code will work 
well under other Python implementations and future implementations which may 
not have this provided. 


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


Re: lambda functions within list comprehensions

2005-10-29 Thread Max Rybinsky
Thank you for explanation, Alex.
It appears that almost every beginner to Python gets in trouble with
this ...feature. :)

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


Re: Scanning a file

2005-10-29 Thread Paul Rubin
"Paul Watson" <[EMAIL PROTECTED]> writes:
> How could I identify when Python code does not close files and depends on 
> the runtime to take care of this?  I want to know that the code will work 
> well under other Python implementations and future implementations which may 
> not have this provided. 

There is nothing in the Python language reference that guarantees the
files will be closed when the references go out of scope.  That
CPython does it is simply an implementation artifact.  If you want to
make sure they get closed, you have to close them explicitly.  There
are some Python language extensions in the works to make this more
convenient (PEP 343) but for now you have to do it by hand.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread Mike Meyer
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:

> 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

You clipped my description of what's wrong with setattr: it doesn't
bind the names in the local namespace, it binds them to an
object. This may be acceptable, but wasn't what the OP asked for.

> 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.

If you're going to do that, why not just specify them explicitly in
the function definition instead of using **kwargs? That takes care of
everything - binding, checking, and providing a default value - in one
swoop.

  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-29 Thread David Rasmussen
[EMAIL PROTECTED] wrote:
> I think implementing a finite state automaton would be a good (best?)
> solution. I have drawn a FSM for you (try viewing the following in
> fixed width font). Just increment the count when you reach state 5.
> 
> <---|
>||
> 0  0   |  1  0  |0
>  -->[1]--->[2]--->[3]--->[4]--->[5]-|
>  ^ ||  ^ | |  |
> 1| |<---|  | | |1 |1
>  |_|1  |_| |  |
>  ^ 0   |  |
>  |-|<-|
> 
> If you don't understand FSM's, try getting a book on computational
> theory (the book by Hopcroft & Ullman is great.)
> 

I already have that book. The above solution very slow in practice. None 
of the solutions presented in this thread is nearly as fast as the

print file("filename", "rb").read().count("\x00\x00\x01\x00")

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


Examples of Python code compared to other languages

2005-10-29 Thread Nicolas Pernetty
Hello,

I'm looking (without success so far) for code sources for common
problems in different languages, but especially Python, C, Fortran, Ada
and Matlab.

It would help people coming from other languages to understand the
'python way of thinking'.

Priority is clean code, performance is not an issue here because it
often leads to obscure code.

Ideally code for Python would extensively use the standard library
("don't reinvent the wheel").

I've already checked the following sites, but they were not suited for
what I'm looking for :

These ones are too focused on performance :
http://shootout.alioth.debian.org/
http://dada.perl.it/shootout/

This one don't include C or Fortran :
http://pleac.sourceforge.net/

This one is ok but rather limited :
http://www.kochandreas.com/home/language/lang.htm

If there is a site which regroup very good ('Pythonic') code examples
which make extensively use of standard library to resolve common
problems, I would be happy to help and try to contribute by giving the
equivalent in C code.

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


dreaming in Python

2005-10-29 Thread The Eternal Squire
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

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


Re: Scanning a file

2005-10-29 Thread Steven D'Aprano
On Sat, 29 Oct 2005 21:08:09 +, Tim Roberts wrote:

>>In any case, you are assuming that Python will automagically close the
>>file when you are done.
> 
> Nonsense.  This behavior is deterministic.  At the end of that line, the
> anonymous file object out of scope, the object is deleted, and the file is
> closed.

That is an implementation detail. CPython may do that, but JPython does
not -- or at least it did not last time I looked. JPython doesn't
guarantee that the file will be closed at any particular time, just that
it will be closed eventually.

If all goes well. What if you have a circular dependence and the file
reference never gets garbage-collected? What happens if the JPython
process gets killed before the file is closed? You might not care about
one file not being released, but what if it is hundreds of files?

In general, it is best practice to release external resources as soon as
you're done with them, and not rely on a garbage collector which may or
may not release them in a timely manner.

There are circumstances where things do not go well and the file never
gets closed cleanly -- for example, when your disk is full, and the
buffer is only written to disk when you close the file. Would you
prefer that error to raise an exception, or to pass silently? If you want
close exceptions to pass silently, then by all means rely on the garbage
collector to close the file.

You might not care about these details in a short script -- when I'm
writing a use-once-and-throw-away script, that's what I do. But it isn't
best practice: explicit is better than implicit.

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."


-- 
Steven.

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


Re: Scanning a file

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

> "Mike Meyer" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
>> "Paul Watson" <[EMAIL PROTECTED]> writes:
> ...
>> Did you do timings on it vs. mmap? Having to copy the data multiple
>> times to deal with the overlap - thanks to strings being immutable -
>> would seem to be a lose, and makes me wonder how it could be faster
>> than mmap in general.
>
> The only thing copied is a string one byte less than the search string for 
> each block.

Um - you removed the code, but I could have *sworn* that it did
something like:

  buf = buf[testlen:] + f.read(bufsize - testlen)

which should cause the the creation of three strings: the last few
bytes of the old buffer, a new bufferfull from the read, then the sum
of those two - created by copying the first two into a new string. So
you wind up copying all the data.
  
Which, as you showed, doesn't take nearly as much time as using mmap.

   Thanks,
 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: Recursive generators and backtracking search

2005-10-29 Thread Mike Meyer
"Talin" <[EMAIL PROTECTED]> writes:
> As an alternative, I'd like to present the following implementation. If
> you compare this one with the one in lib/test/test_generator.py you
> will agree (I hope) that by using recursive generators to implement
> backtracking, the resulting code is a little more straightforward and
> intuitive:

I'd propose one change to that...
> def qsearch( qarray = () ):
> for q in range( 0, bsize ):# Try each position
> if not threaten( qarray, q ):  # If not threatened
> pos = qarray + ( q, ); # Append to the pos array
>
> if len( pos ) >= bsize:# Are we done?
> yield pos  # Yield the answer
> else:# recursively call new generator
> 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.

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-29 Thread David Rasmussen
Steven D'Aprano wrote:
> On Fri, 28 Oct 2005 06:22:11 -0700, [EMAIL PROTECTED] wrote:
> 
>>Which is quite fast. The only problems is that the file might be huge.
> 
> What *you* call huge and what *Python* calls huge may be very different
> indeed. What are you calling huge?
> 

I'm not saying that it is too big for Python. I am saying that it is too 
big for the systems it is going to run on. These files can be 22 MB or 5 
GB or ..., depending on the situation. It might not be okay to run a 
tool that claims that much memory, even if it is available.

> 
>>I really have no need for reading the entire file into a string as I am
>>doing here. All I want is to count occurences this substring. Can I
>>somehow count occurences in a file without reading it into a string
>>first?
> 
> Magic?
> 

That would be nice :)

But you misunderstand me...

> You have to read the file into memory at some stage, otherwise how can you
> see what value the bytes are? 

I haven't said that I would like to scan the file without reading it. I 
am just saying that the .count() functionality implemented into strings 
could just as well be applied to some abstraction such as a stream (I 
come from C++). In C++, the count() functionality would be separated as 
much as possible from any concrete datatype (such as a string), 
precisely because it is a concept that is applicable at a more abstract 
level. I should be able to say "count the substring occurences of this 
stream" or "using this iterator" or something to that effect. If I could say

print file("filename", "rb").count("\x00\x00\x01\x00")

(or something like that)

instead of the original

print file("filename", "rb").read().count("\x00\x00\x01\x00")

it would be exactly what I am after. What is the conceptual difference? 
The first solution should be at least as fast as the second. I have to 
read and compare the characters anyway. I just don't need to store them 
in a string. In essence, I should be able to use the "count occurences" 
functionality on more things, such as a file, or even better, a file 
read through a buffer with a size specified by me.

> 
> Here is another thought. What are you going to do with the count when you
> are done? That sounds to me like a pretty pointless result: "Hi user, the
> file XYZ has 27 occurrences of bitpattern \x00\x00\x01\x00. Would you like
> to do another file?"
> 

It might sound pointless to you, but it is not pointless for my purposes :)

If you must know, the above one-liner actually counts the number of 
frames in an MPEG2 file. I want to know this number for a number of 
files for various reasons. I don't want it to take forever.

> If you are planning to use this count to do something, perhaps there is a
> more efficient way to combine the two steps into one -- especially
> valuable if your files really are huge.
> 

Of course, but I don't need to do anything else in this case.

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


Re: How to replace all None values with the string "Null" in a dictionary

2005-10-29 Thread Bruno Desthuilliers
dcrespo a écrit :
>>I think it would be time for you to read the Fine Manual...
> 
> 
> hi, thanks for your answer... I really did it the same way you
> suggested, but I forgot to tell you that I wanted to get a better way
> for doing it.

Let us know if you find one...

> 
> By the way, knowing your wisdom, 
> what do I have to install to get the
> following code work (Win XP, Python 2.4.2)


Being wise, I don't use Windows !-)

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


Re: lambda functions within list comprehensions

2005-10-29 Thread Alex Martelli
Max Rybinsky <[EMAIL PROTECTED]> wrote:

> Thank you for explanation, Alex.
> It appears that almost every beginner to Python gets in trouble with
> this ...feature. :)

Almost every beginner to Python gets in trouble by expecting "do what
I'm thinking of RIGHT NOW"-binding, which no language offers: in other
words, such beginners sometimes expect late binding where Python binds
early, and, vice versa, they at other times expect early binding where
Python binds late.  Not ALWAYS, mind you -- what they expect depends on
what would appear to them to be most convenient for their immediate
needs on each separate occasion.  Some other languages try to follow
beginners and offer "do what I mean" semantics -- when using such
languages, one ends up in a battle of wit against the compiler's guesses
about one's intentions.  Python instead offers extremely simple rules,
such as: any name is looked up each and every time it's evaluated (and
at no other times); evaluation of function headers happens completely at
the time the 'def' or 'lambda' evaluates, while evaluation of function
bodies happens completely at the time the function is _called_.  By
learning and applying such simple rules there can be no surprise about
what is evaluated (and, in particular, looked up) when.  E.g., consider
the difference between the following two functions:

def early(x=whatever()):
   ...

def late():
   x=whatever()
   ...

In 'early', the call to whatever() is part of the function's header, and
therefore happens at the time the 'def' statement executes -- and thus
name 'whatever' means whatever it means at THAT time (if at that time
it's not bound to anything, the 'def' statement fails with an
exception).

In 'late', the call to whatever() is part of the function's body, and
therefore happens each time the function is called -- and thus name
'whatever' means whatever it means at THAT time (if at that time it's
not bound to anything, the call fails with an exception).


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


Re: Scanning a file

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

> "Alex Martelli" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
> 
> > In today's implementations of Classic Python, yes.  In other equally
> > valid implementations of the language, such as Jython, IronPython, or,
> > for all we know, some future implementation of Classic, that may well
> > not be the case.  Many, quite reasonably, dislike relying on a specific
> > implementation's peculiarities, and prefer to write code that relies
> > only on what the _language_ specs guarantee.
> 
> How could I identify when Python code does not close files and depends on
> the runtime to take care of this?  I want to know that the code will work
> well under other Python implementations and future implementations which may
> not have this provided. 

Then you should use try/finally (to have your code run correctly in all
of today's implementations; Python 2.5 will have a 'with' statement to
offer nicer syntax sugar for that, but it will be a while before all the
implementations get around to adding it).

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):

import __builtin__
import warnings

_f = __builtin__.file
class testing_file(_f):
  def __init__(self, *a, **k):
 _f.__init__(self, *a, **k)
self._opened = True
  def close(self):
_f.close(self)
self._opened = False
  def __del__(self):
if self._opened:
   warnings.warn(...)
   self.close()

__builtin__.file = __builtin__.open = testing_file



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


Re: Scanning a file

2005-10-29 Thread Alex Martelli
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.

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.


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


Re: Expanding Python as a macro language

2005-10-29 Thread qwweeeit
At first you must forgive my double posting (4 & 5 in terms of date and
4 & 7 in terms of answers). I must then thank the new comers:
Michael, Alex Martelli and Mike Meyer.

Michel wrote:
>> [EMAIL PROTECTED] wrote:
>> But then I changed idea... Also if it is already one year that I try
>> to find a solution in Linux (mainly with Python or DCOP and KDE),

 > This doesn't express the question you have anywhere clearly enough.

My English is so poor that I don't understand...

 > Linux can run perfectly happily without any form of windowing
 > environment.

I know, but nowadays almost any relevant application has a GUI.

 > Do you want to replay against qt applications? KDE? wx? GTK?
 > Or send raw X11 events? The answers to these questions aren't
 > linux specific either...

I have nothing against GUI and in fact I chose Qt also if I made
only limited tests using Qt Designer. However I should prefer not
to program at low level (sending raw X11 events...).
Being a newbye I was confident that someone had already developed
a macro language for Linux...
I hoped to have found it in Python, but I was wrong.

Alex Martelli wrote:

 > Actually, if the app is running under X11 you may try to fake out a
 > keystroke event (with low level calls, but ctypes might let you use
 > it from Python). Of course, the app WILL be told that the keystroke
 > is fake, through a special flag if it cares to check for it,
 > for security reasons; but if the app doesn't specifically defend
 > itself in this way.

 > See, for example, http://xmacro.sourceforge.net/ -- I guess that
 > xmacroplay could pretty easily be adapted, or maybe even used as is
 > with an os.popen.

My answer is the same as that given to Michael about low level
programming.
But I must thank anyway Alex for giving informations to such a level
(I didn't know that under Linux there was such a level of
sophistication with the possibility for an application to discover fake
keystrokes...).

If this fake keystroke is used to fire an event (like pressing a
button), the fake flag is propagated to the action taken by the button?

For the other Alex observations (about Mac OsX and my examples of
automation centered on web automation) I have a PC, and the fact that
Python is very good at dealing with the web, doesn't help too much
in this case...
In any case a macro language like AutoIt is a general purpose
application.

At last I must thank  Mike Meyer for his suggestion to use python-xlib
to avoid low level programming...

Bye.

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


Re: Recursive generators and backtracking search

2005-10-29 Thread George Sakkis
"Talin" <[EMAIL PROTECTED]> wrote:

> I've been using generators to implement backtracking search for a while
> now. Unfortunately, my code is large and complex enough (doing
> unification on math expressions) that its hard to post a simple
> example. So I decided to look for a simpler problem that could be used
> to demonstrate the technique that I am talking about.

Here are two even simpler problems that are solved elegantly (though
not necessarily efficiently) with recursive generators:

def cartesian_product(*sequences):
'''Iterate over the elements of the cartesian product of zero or
more sequences.

>>> for x in cartesian_product(range(3), 'a', (3.25,-1.2)):
... print x
(0, 'a', 3.25)
(0, 'a', -1.2)
(1, 'a', 3.25)
(1, 'a', -1.2)
(2, 'a', 3.25)
(2, 'a', -1.2)
'''
if not sequences:
yield ()
else:
for item in sequences[0]:
head = (item,)
for tail in cartesian_product(*sequences[1:]):
yield head + tail


def powerset(iterable):
'''Iterate over all subsets of an iterable.

>>> for s in powerset('abc'):
... print s
frozenset([])
frozenset(['a'])
frozenset(['b'])
frozenset(['a', 'b'])
frozenset(['c'])
frozenset(['a', 'c'])
frozenset(['c', 'b'])
frozenset(['a', 'c', 'b'])
'''
yield frozenset()
for s in _powerset(iter(iterable)):
yield s

def _powerset(iterator):
first = frozenset([iterator.next()])
yield first
for s in _powerset(iterator):
yield s
yield s | first


George

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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread [EMAIL PROTECTED]
Thanks everybody for their reply. I'll see what solution is best for my
case and maybe follow up here.

Thanks again,
Lorenzo

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


Re: Expanding Python as a macro language

2005-10-29 Thread Mike Meyer
[EMAIL PROTECTED] writes:
> My answer is the same as that given to Michael about low level
> programming.
> But I must thank anyway Alex for giving informations to such a level
> (I didn't know that under Linux there was such a level of
> sophistication with the possibility for an application to discover fake
> keystrokes...).
>
> If this fake keystroke is used to fire an event (like pressing a
> button), the fake flag is propagated to the action taken by the button?

The action is internal to the application, so whether or not it
propogates the "fake" flag is up to the application. In between the X
event and the action is the GUI library, which may also choose to not
propogate the flag.

> For the other Alex observations (about Mac OsX and my examples of
> automation centered on web automation) I have a PC, and the fact that
> Python is very good at dealing with the web, doesn't help too much
> in this case...
> In any case a macro language like AutoIt is a general purpose
> application.
>
> At last I must thank  Mike Meyer for his suggestion to use python-xlib
> to avoid low level programming...

Just to clarify - python-xlib will let you avoid the need for C
functions to do things like send X events. In that sense, it helps you
avoid low-level programming. On the other hand, you're still sending X
events, so in *that* sense, you're not avoiding low-level programming.

In general, application scripting facilities are one thing that Unix
hasn't dealt with well. Windows and OS X both do better (but neither
do as well later versions of AmigaDOS). Whereas those systems come
with a (or a selection) mechanism that applications can use to allow
macro programming, Unix doesn't, so each application is left up to
it's own devices. The most popular solution is to embed a custom
language for scripting - which is where something like Python comes
in. Some of them now export APIs that can be hooked up to a variety of
languages.  IIRC, Gnome has embraced CORBA as an interface for such,
but it didn't seem well supported the last time I looked. The bottom
line is that when it comes to scripting applications on Unix, your
solution is going to be tailored to the application.

The Unix variant with the best system-provided scripting facility is
probably Plan 9. Some Linux variants are moving in that direction, but
I don't know of any Linux applications that provide support for it.

   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-29 Thread netvaibhav

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.

Vaibhav

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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread [EMAIL PROTECTED]
What do u think of the following? I could keep the form schema as
expected_form1_kwargs in a separate module and import * and wrap the
kwargs check done in the for loop in a function for use in the whole
site.

The elif part is useful for checkboxes which are not passed by the
browser if they're not checked.

def foo(self, **kwargs)
expected_form1_kwargs = [arg1, arg2]

for name in kwargs:
if name not in expected_form1_kwargs:
raise ValueError, "Unrecognized keyword" + name
elif name in expected_form1_kwargs not in kwargs.keys():
kwargs.update(name=None)


Thanks again everybody,
Lorenzo

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


Re: How do I sort these?

2005-10-29 Thread Bengt Richter
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? ;-)

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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread Peter Hansen
[EMAIL PROTECTED] wrote:
> if name not in expected_form1_kwargs:
> raise ValueError, "Unrecognized keyword" + name
> elif name in expected_form1_kwargs not in kwargs.keys():
> kwargs.update(name=None)

Do you mean this instead?

  elif name in expected_form1_kwargs and name not in kwargs:

What you wrote doesn't do what you think it does...  it actually tests 
for whether True or False is a key in kwargs, depending on whether "name 
in expected_form1_kwargs" returns True or False.

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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread [EMAIL PROTECTED]
FormEncode.

[EMAIL PROTECTED] wrote:
> What do u think of the following? I could keep the form schema as
> expected_form1_kwargs in a separate module and import * and wrap the
> kwargs check done in the for loop in a function for use in the whole
> site.
>
> The elif part is useful for checkboxes which are not passed by the
> browser if they're not checked.
>
> def foo(self, **kwargs)
> expected_form1_kwargs = [arg1, arg2]
>
> for name in kwargs:
> if name not in expected_form1_kwargs:
> raise ValueError, "Unrecognized keyword" + name
> elif name in expected_form1_kwargs not in kwargs.keys():
> kwargs.update(name=None)
> 
> 
> Thanks again everybody,
> Lorenzo

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


Re: How to translate python into C

2005-10-29 Thread Johnny Lee
Thanks Szabolcs and Laurence, it's not the crash of python but the
crash of cygwin. We can locate the line number but when we submit the
crash to cygwin's mail list, they told us they don't speak python. So
I'm just trying to re-produce the crash in C. 

Regards, 
Johnny

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


Re: Expanding Python as a macro language

2005-10-29 Thread SPE - Stani's Python Editor
> some future Firefox version might perhaps integrate a Python engine

For those who never heard about Firefox 1.9, check the following urls.
It looks very promising:

1) http://wiki.mozilla.org/Roadmap_Scratchpad#Python_for_XUL

Python for XUL

Significant potential contributors in both the Python and XUL
application development communities have long wanted access to Python's
set of libraries and its capabilities as an application development
language. So in addition to JavaScript, which is the default web and
XUL scripting language, we plan to extend the reach of Gecko and XUL to
the Python world.

Mark Hammond's work on PyXPCOM and a language-neutral DOM is well under
way as of October 2005, and we believe that the glue code and bindings
will be slim enough to be part of a default XULRunner or Firefox
distribution when the 1.9 cycle is complete.

(N.B.: Mozilla does not intend to distribute the C-Python runtime with
its applications or frameworks, and application developers who wish to
take advantage of these capabilities will need to provide for this
dependency in their installers or packaging. Stub or streaming
installer capabilities in the Firefox/XULRunner based on Gecko 1.9 will
probably help a great deal to ease the extra download for Python-less
users.)

2) http://weblogs.mozillazine.org/roadmap/archives/008865.html

Given evolving JS, why Python? Simple: different programming languages,
of different ages, have their strengths, and one of the greatest
strength of any solid language is its community. We want to support
Python as well as JS for XUL scripting in order to engage a large,
creative community of hackers that is mostly disjoint from the
web-standards-savvy XUL hackers who are already engaged with Mozilla
and Firefox.

*
Stani
--
http://pythonide.stani.be
http://pythonide.stani.be/manual/html/manual.html

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


Re: dreaming in Python

2005-10-29 Thread [EMAIL PROTECTED]
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?

Yeah, although I'm not sure how much was dream and how
much was conscience invention while waking up. Anyway...

In this future, every computer in the world is networked
together and all I/O devices have been replaced by the
MOUSE (Micro Optical Universal Sensing Element) by which
you control the computer just by looking at it.

I find myself in a control room overlooking a vast sea of
CRT screens (something like a NASA control center) when a
crisis occurs: somehow, every bit of every memory chip in
every computer in the entire world has become a 0
simultaneously locking up every computer with no way to
reset them.

I thought "if only I had a keyboard" but keyboards had
been obsoleted by the MOUSE. Suddenly, I get a thought,
open a drawer, and pull out the original MOUSE engineering
prototype wired to a pocket calculator. With the calculator
display showing 0, I press the 1/x key, which causes ERROR
to be displayed. But the prototype MOUSE is networked to
every other computer in the world, and I look out over the
room to see all the CRT screens light up as 1 bits propagate
across the world.

> 
> The Eternal Squire

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


query a port

2005-10-29 Thread eight02645999
hi
in python, how do one query a port to see whether it's up or not?
thanks

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


Online Poker Pays! Learn All The Secrets Now!NBP Unregistered.

2005-10-29 Thread Poker Paid Us
Did you know that online Poker pays huge money? In the last three days my wife and I have made over 343.00 USD with online poker while we slept
Want to know our secret? Click HERE
and visit a link of your choice. 
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Examples of Python code compared to other languages

2005-10-29 Thread Terji78
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

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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread [EMAIL PROTECTED]

Peter Hansen wrote:
> Do you mean this instead?
>
>   elif name in expected_form1_kwargs and name not in kwargs:
>
> What you wrote doesn't do what you think it does...  it actually tests
> for whether True or False is a key in kwargs, depending on whether "name
> in expected_form1_kwargs" returns True or False.

Hi Peter,

you're right... the code needed also other fixes and I didn't find a
way to do it other than this:

def foo(**kwargs):
expected_form1_kwargs = ["arg1", "arg2"]

for name in expected_form1_kwargs:
if name not in kwargs:
kwargs[name]=None

for name in kwargs:
if name in kwargs and name not in expected_form1_kwargs:
raise ValueError, "Unrecognized keyword: " + name

print kwargs


if __name__ == "__main__":
foo(arg1=8)


Lorenzo

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


Re: query a port

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

> hi
> in python, how do one query a port to see whether it's up or not?
> thanks

I'm an absolute beginner, but let's see if I can help. Assuming you want
to check a port on another machine,

import socket
port=25 # Port we want to test
host=machine.to.test# Either IP address or FQDN
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
try:
s.connect((host, port)) 
print "We made the connection"
except socket.error:
print "Sorry, that port is not open"
s.close

Does that help?

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


Re: Expanding Python as a macro language

2005-10-29 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote:
...
> For the other Alex observations (about Mac OsX and my examples of
> automation centered on web automation) I have a PC, and the fact that
> Python is very good at dealing with the web, doesn't help too much
> in this case...

All of your sensible use cases were about the Web; the fact that you
have a PC is irrelevant to this observation.

> In any case a macro language like AutoIt is a general purpose
> application.

"General purpose" is, I believe, an overbid here -- if what you're doing
is simulating keystrokes, you're limited to the purposes that can in
fact be achieved through keystroke-simulation.  And I've pointed you to
an application that can run under Linux to simulate keystrokes (which,
of course, applications are able to detect as being simulated, if they
want to implement very strong security on this plane).  What, exactly,
is stopping you from using Python to drive that application, if the
simulation of keystrokes is the pinnacle of your heart's desire?

If what you're whining about is that Linux applications can be built to
be secure (rejecting fake keystrokes, among other things), then stick
with Windows and its endless flood of malware.  If you want security,
don't complain about the existence of security.


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


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
   ...
> def foo(**kwargs):
> expected_form1_kwargs = ["arg1", "arg2"]
> 
> for name in expected_form1_kwargs:
> if name not in kwargs:
> kwargs[name]=None
> 
> for name in kwargs:
> if name in kwargs and name not in expected_form1_kwargs:
> raise ValueError, "Unrecognized keyword: " + name
> 
> print kwargs

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.


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


  1   2   >