Re: swig & Python question

2004-12-11 Thread Keith Dart
It's me wrote:
"It's me" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
I am playing around with SWING building a Python module using the no
brainer
example in http://www.swig.org/tutorial.html.   With that first example,

Oops!  Soapy fingers.   "SWIG" - not "SWING".
--
It's me.
I have used SWIG before, and it's not always such a "no-brainer". In 
fact, it rarely is except for trivial examples. But it can work. I think 
it is best suited for wrapping large libraries. For small stuff, it 
would be better to just do it "manually" using the Python C API.

Good luck.
--
It's not me.

--
   \/ \/
   (O O)
-- ----oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4

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


Re: subprocess.Popen

2004-12-12 Thread Keith Dart
Michele Simionato wrote:
I was looking at Python 2.4 subprocess.Popen. Quite nice and handy, but I
wonder why a "kill" method is missing. I am just adding it via subclassing,
class Popen(subprocess.Popen):
def kill(self, signal = SIGTERM):
os.kill(self.pid, signal)
but I would prefer to have it in the standard Popen class. I am surprised
it is not there. Any comments?
Probably because it is not entirely portable. If you want a more 
complete, but Posix-only (at least Linux and FreeBSD), process 
management and spawning then you can use the proctools module in pyNMS.

http://sourceforge.net/projects/pynms/


--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo--------
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: Persistent objects

2004-12-12 Thread Keith Dart
Paul Rubin wrote:
I've had this recurring half-baked desire for long enough that I
thought I'd post about it, even though I don't have any concrete
proposals and the whole idea is fraught with hazards.
Basically I wish there was a way to have persistent in-memory objects
in a Python app, maybe a multi-process one.  So you could have a
persistent dictionary d, and if you say 
   d[x] = Frob(foo=9, bar=23)
that creates a Frob instance and stores it in d[x].  Then if you
exit the app and restart it later, there'd be a way to bring d back
into the process and have that Frob instance be there.
Check out the Durus project.
http://www.mems-exchange.org/software/durus/

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo----
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: Persistent objects

2004-12-12 Thread Keith Dart
Paul Rubin wrote:
I've had this recurring half-baked desire for long enough that I
thought I'd post about it, even though I don't have any concrete
proposals and the whole idea is fraught with hazards.
Basically I wish there was a way to have persistent in-memory objects
in a Python app, maybe a multi-process one.  So you could have a
persistent dictionary d, and if you say 
   d[x] = Frob(foo=9, bar=23)
that creates a Frob instance and stores it in d[x].  Then if you
exit the app and restart it later, there'd be a way to bring d back
into the process and have that Frob instance be there.
Check out the Durus project.
http://www.mems-exchange.org/software/durus/

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo----
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: Persistent objects

2004-12-12 Thread Keith Dart
Paul Rubin wrote:
I've had this recurring half-baked desire for long enough that I
thought I'd post about it, even though I don't have any concrete
proposals and the whole idea is fraught with hazards.
Basically I wish there was a way to have persistent in-memory objects
in a Python app, maybe a multi-process one.  So you could have a
persistent dictionary d, and if you say 
   d[x] = Frob(foo=9, bar=23)
that creates a Frob instance and stores it in d[x].  Then if you
exit the app and restart it later, there'd be a way to bring d back
into the process and have that Frob instance be there.
Check out the Durus project.
http://www.mems-exchange.org/software/durus/

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo----
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: subprocess.Popen

2004-12-12 Thread Keith Dart
Keith Dart wrote:
Michele Simionato wrote:
I was looking at Python 2.4 subprocess.Popen. Quite nice and handy, but I
wonder why a "kill" method is missing. I am just adding it via 
subclassing,

class Popen(subprocess.Popen):
def kill(self, signal = SIGTERM):
os.kill(self.pid, signal)
but I would prefer to have it in the standard Popen class. I am surprised
it is not there. Any comments?

Probably because it is not entirely portable. If you want a more 
complete, but Posix-only (at least Linux and FreeBSD), process 
management and spawning then you can use the proctools module in pyNMS.

http://sourceforge.net/projects/pynms/
I forgot to mention that the pyNMS package also has a module called 
"expect" that works like the Expect language. You can interact and 
control interactive processes and external CLIs with it.


--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo----
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: Python vs. Perl

2004-12-13 Thread Keith Dart
Keith Dart wrote:
Ian Bicking wrote:
Jon Perez wrote:
Michael McGarry wrote:
I intend to use a scripting language for GUI development and front 
end code for my simulations in C. I want a language that can support 
SQL, Sockets, File I/O, and shell interaction.


In my experience, Python is definitely much more suitable than Perl
for the first four areas mentioned in the last sentence.  For the
last area, I'm not sure, but Python's capabilities in this area are
also quite good.

Shell interaction (or rather, external process interaction) is a lot 
better with Python 2.4's subprocess module.  Better or worse than 
Perl?  I'm not sure; generally I'd guess better, as it avoids the 
shell with all the shell's issues, and provides a more controlled 
programmatic way of interacting with subprocesses.  OTOH, Perl might 
have perfectly good modules for doing the same thing.  I can only say 
it's been missing for a while in Python, and it's good to see this 
done right.

Yow, I must not get picked up in Google enough. ;-) The "proctools" 
module in the pyNMS package <http://sourceforge.net/projects/pynms/> has 
been around for years. I use it all the time for shell-like stuff. There 
is also an "expect" module, and the "termtools" module. If you need a 
more complete process spawning and controlling framework then use pyNMS. 
 It can "juggle" multiple processes, reaps child status (no 
zombies), operates asynchronously (The ProcManager object is a SIGCHLD 
handler), and works with pty's and pipes. It also offers a "thread-like" 
interface  for Python subprocesses (uses fork). Can leave some fd's open 
that you specify, can run the subprocess as a different user, and more...

Check it out.
Oh, I forgot to mention that it also has a more user- and 
programmer-friendly ExitStatus object that processess can return. This 
is directly testable in Python:

proc = proctools.spawn("somecommand")
exitstatus = proc.wait()
if exitstatus:
print "good result (errorlevel of zero)"
else:
print exitstatus # prints message with exit value


--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: [dictionary] how to get key by item

2004-12-13 Thread Keith Dart
Skip Montanaro wrote:
Egor> i know how to get item by key
...
Egor> but i wonder how to get key by item
Assuming your dictionary defines a one-to-one mapping, just invert it:
>>> forward = {10 : 50, 2 : 12, 4 : 43}
>>> reverse = dict([(v,k) for (k,v) in forward.iteritems()])
>>> print forward
{10: 50, 4: 43, 2: 12}
>>> print reverse
{50: 10, 43: 4, 12: 2}
That doubles your storage, so you'll have to trade that off against the
speed gain of not having to loop over the entire dictionary.
Skip
But beware that all the items in the original dictionary must be 
hashable. The example shows just integers, so I assume they are in this 
case. But generally, this may not work.


--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: gather information from various files efficiently

2004-12-13 Thread Keith Dart
Klaus Neuner wrote:
Hello,
I need to gather information that is contained in various files.
Like so:
file1:
=
foo : 1 2
bar : 2 4
baz : 3
=
file2:
=
foo : 5
bar : 6
baz : 7
=
file3:
=
foo : 4 18
bar : 8
=
The straightforward way to solve this problem is to create a
dictionary. Like so:
[...]
a, b = get_information(line)
if a in dict.keys():
dict[a].append(b)
else:
dict[a] = [b]
Aye...
the dict.keys() line creates a temporary list, and then the 'in' does a 
linear search of the list. Better would be:

try:
dict[a].append(b)
except KeyError:
dict[a] = [b]
since you expect the key to be there most of the time, this method is 
most efficient. You optomistically get the dictionary entry, and on the 
exceptional case where it doesn't yet exist you add it.



--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo----
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4

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


Re: gather information from various files efficiently

2004-12-13 Thread Keith Dart
Kent Johnson wrote:
Keith Dart wrote:
try:
dict[a].append(b)
except KeyError:
dict[a] = [b]

or my favorite Python shortcut:
dict.setdefault(a, []).append(b)
Kent
Hey, when did THAT get in there? ;-) That's nice. However, the 
try..except block is a useful pattern for many similiar situations that 
the OP might want to keep in mind. It is usually better than the 
following, also:

if dct.has_key(a):
dct[a].append(b)
else:
dct[a] = [b]
Which is a pattern I have seen often.

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: subprocess vs. proctools

2004-12-13 Thread Keith Dart
Nick Craig-Wood wrote:
Keith Dart <[EMAIL PROTECTED]> wrote:
Oh, I forgot to mention that it also has a more user- and 
programmer-friendly ExitStatus object that processess can return. This 
is directly testable in Python:

proc = proctools.spawn("somecommand")
exitstatus = proc.wait()
if exitstatus:
print "good result (errorlevel of zero)"
else:
 print exitstatus # prints message with exit value

This sounds rather like the new subprocess module...

import subprocess
rc = subprocess.call(["ls", "-l"])
total 381896
-rw-r--r--1 ncw ncw  1542 Oct 12 17:55 1
[snip]
-rw-r--r--1 ncw ncw   713 Nov 16 08:18 z~
print rc
0
But this evaluates to False in Python, but True in a shell. It also 
requires an extra check for normal exit, or exit by a signal. The 
proctools ExitStatus object avaluates to True only on a normal exit, 
period. Thus it follows a shell semantics for clarity. You cannot do 
this with the subprocess module:

if rc:
print "exited normally"
But in proctools, the exitstatus is an object that evaluates True only 
for normal exit.

import proctools
proc = proctools.spawnpipe("ls -l")
print proc.read()
 
print proc.exitstatus
ls: Exited normally.
proc = proctools.spawnpipe("ls -l xx")
print proc.read()
'ls: xx: No such file or directory\n'
print proc.exitstatus
ls: Exited abnormally with status 1.
if proc.exitstatus:
print "returned normally"
But you can get the integer return value, if you want it, like this:
int(proc.exitstatus)
or query it with methods returning booleans:
exitstatus.exited()
exitstatus.signalled()
exitstatus.stopped()
Also, proctools lets you use a pty, if you choose. Not every program 
works well from a pipe.

IMHO the new subprocess module is a very well thought out interface...
The proctools Process object presents a file-like object to the rest of 
Python, which makes a process polymorhic with any other file, pipe or 
socket object. It has the usual read, write, readline, and readlines 
methods. It can also be made non-blocking, and you can have many open at 
once. In addition, there are special methods for controlling the 
sub-process: You can kill it, stop it, re-start it, clone it, wait on 
it, and get stats from it. The stat() method returns a ProcStat object, 
which has attributes like what you get from the 'ps' program. Need to 
know the process's RSS? No problem. It also supports logging to a log 
file, and on-exit callback for persistent process requirements.

You always invoke the spawn* functions with a string. This is parsed by 
a shell-like parser (the shparser module that comes with it), but no 
/bin/sh is invoked. The parser can handle single and double quotes, and 
backslash escapes.

Alas, one thing the proctools module does not do well yet is create a 
pipeline. I have plans to fix that.

It does not work with MS Windows, but can work with cygwin on Windows.
Whew... and I have not even covered the ProcessManager object...

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: gather information from various files efficiently

2004-12-13 Thread Keith Dart
Fredrik Lundh wrote:
...
if dct.has_key(a):
   dct[a].append(b)
else:
   dct[a] = [b]

the drawback here is that if the number of collisions are high, you end
up doing lots of extra dictionary lookups.  in that case, there are better
ways to do this.
Sigh, this reminds me of a discussion I had at my work once... It seems 
to write optimal Python code one must understand various probabilites of 
your data, and code according to the likely scenario. 8-) Now, perhaps 
we could write an adaptive data analyzer-code-generator... ;-)




--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4

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


Re: subprocess vs. proctools

2004-12-14 Thread Keith Dart
Donn Cave wrote:
Keith Dart <[EMAIL PROTECTED]> wrote:
|>> if exitstatus:
|>>   print "good result (errorlevel of zero)"
|>> else:
|>>  print exitstatus # prints message with exit value
This is indeed how the shell works, though the actual failure value
is rarely of any interest.  It's also in a more general sense how
C works - whether errors turn out to be "true" or "false", in either
case you test for that status (or you don't.)
Python doesn't work that way, there is normally no such thing as
an error return.  An idiomatic Python interface would be
try:
proc = proctools.spawn(command)
proc.wait()
print 'good result'
except proctools.error, ev:
print >> sys.stderr, '%s: %s' % (proc.name, ev.text)

Your first statement is exactly right. One does not always care about 
the return value of an external process. And some programs still return 
an undefined value, even when successful. Therefore, I don't want to 
always have to wrap a call to an external program in a try..except 
block. Thus, it returns an ExitStatus object that you can easily test 
true-false with as in a shell, or get the actual value if you need it. 
Otherwise, just ignore it.


[... list of features ...]
| You always invoke the spawn* functions with a string. This is parsed by 
| a shell-like parser (the shparser module that comes with it), but no 
| /bin/sh is invoked. The parser can handle single and double quotes, and 
| backslash escapes.

It was sounding good up to here.  A lot depends on the quality of
the parser, but it's so easy to support a list of arguments that
gets passed unmodified to execve(), and such an obvious win in the
common case where the command parameters are already separate values,
that an interface where you "always" have to encode them in a string
to be submitted to your parser seems to be ignoring the progress that
os.spawnv and popen2.Popen3 made on this.  Of course you don't need
to repeat their blunders either and accept either string or list of
strings in the same parameter, which makes for kind of a shabby API,
but maybe a keyword parameter or a separate function would make sense.
Actually, an earlier version of proctools did take a list. However, 
after much usage I realized that in most cases what I got was a string 
to begin with. Either from user input or read from a file. I also found 
it easier to construct command-lines using the string-mod operator, 
substituting various attributes into option-value pairs in arbitrary 
ways. I was having to split/parse a string so often I decided to just 
make the Process object parse it itself. The shparser module has been 
perfectly adequate for this, and you can pass to the Process object 
pretty much the same string as you would to a real shell (thus making it 
easier to use for *nix people). I could add a list-input check, but 
likely I would never use it.


--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4

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


Re: Regular Expression

2004-12-14 Thread Keith Dart
Michael McGarry wrote:
Hi,
I am horrible with Regular Expressions, can anyone recommend a book on it?
Also I am trying to parse the following string to extract the number 
after load average.

" load average: 0.04, 0.02, 0.01"
how can I extract this number with RE or otherwise?
This particular example might be parsed more quickly and easily just by 
chopping it up:

s = " load average: 0.04, 0.02, 0.01"
[left, right] = s.split(":")
[av1, av2, av3] = map(float, map(str.strip, right.split(",")))

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo----
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: ".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)

2004-12-16 Thread Keith Dart
Fernando Perez wrote:

You might want to look at ipython:
http://ipython.scipy.org,

I did just recently install that. It looks very nice. Would make a great 
interactive prompt for an IDE, as well.


--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


pywhich script - where is that module?

2004-12-17 Thread Keith Dart
Have you ever wondered where your python modules get imported from?
Here is a little script, called "pywhich", that will tell you.


--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo--------
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4 

#!/usr/bin/env python

"""pywhich 
Tell which Python module is imported.
"""

import sys

def main(argv):
if len(argv) < 2:
print __doc__
return
for modname in argv[1:]:
try:
mod = __import__(modname)
except:
print "No module or package by named '%s' found." % modname
else:
print "%15s : %s" % (modname, mod.__file__)

main(sys.argv)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: High level SNMP

2004-12-11 Thread Keith Dart
Jeremy Sanders wrote:
Hi -
I'd like to write a program which basically does a few snmpgets. I haven't
been able to find a python package which gives you a nice high-level and
simple way of doing this (like PHP has). Everything appears to be
extremely low level. All I need is SNMPv1.
Does anyone know of a simple python package for doing this? I'd rather
have something written in pure python, so that it is easily cross-platform.
Jeremy
The pyNMS package at sourceforge has a complete SNMP (v1, v2c) 
implementation. In pure Python, and fairly self contained. See

http://sourceforge.net/projects/pynms
There are few docs, sorry. If you want to use it and have any questions 
then please let me know (I wrote it).

BTW, you can also read MIB files if you have libsmi installed. But the 
pyNMS package contains a utility called mib2py that converts MIB objects 
to Python, and the pyNMS package has most standard MIBS pre-compiled. 
So, you don't really need libsmi to use the standard MIBs.

The name means Python Network Management System, and will become a 
complete network management system with GUI and scriptability soon. ;-)

There is some support for creating XHTML reports, NMS web interface, 
SNMP get/set, SNMP trap receiver, Ping/ICMP module, process management, 
MIB browser, CLI construction kit, web protocols, easy email interface, 
and asyncio framework. Works well with Linux or FreeBSD.

(PS. It can also answer your phone and take a message)

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo----
Keith Dart <[EMAIL PROTECTED]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4   URL: <http://www.kdart.com/~kdart/public.key>

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


Re: Python vs. Perl

2004-12-14 Thread Keith Dart
Ian Bicking wrote:
Jon Perez wrote:
Michael McGarry wrote:
I intend to use a scripting language for GUI development and front 
end code for my simulations in C. I want a language that can support 
SQL, Sockets, File I/O, and shell interaction.

In my experience, Python is definitely much more suitable than Perl
for the first four areas mentioned in the last sentence.  For the
last area, I'm not sure, but Python's capabilities in this area are
also quite good.

Shell interaction (or rather, external process interaction) is a lot 
better with Python 2.4's subprocess module.  Better or worse than Perl? 
 I'm not sure; generally I'd guess better, as it avoids the shell with 
all the shell's issues, and provides a more controlled programmatic way 
of interacting with subprocesses.  OTOH, Perl might have perfectly good 
modules for doing the same thing.  I can only say it's been missing for 
a while in Python, and it's good to see this done right.

Yow, I must not get picked up in Google enough. ;-) The "proctools" 
module in the pyNMS package <http://sourceforge.net/projects/pynms/> has 
been around for years. I use it all the time for shell-like stuff. There 
is also an "expect" module, and the "termtools" module. If you need a 
more complete process spawning and controlling framework then use pyNMS. 
 It can "juggle" multiple processes, reaps child status (no 
zombies), operates asynchronously (The ProcManager object is a SIGCHLD 
handler), and works with pty's and pipes. It also offers a "thread-like" 
interface  for Python subprocesses (uses fork). Can leave some fd's open 
that you specify, can run the subprocess as a different user, and more...

Check it out.

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4 

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


Re: Python mascot proposal

2004-12-14 Thread Keith Dart
Dimitri Tcaciuc wrote:
Yup, I was aware of the fact of Monty Python roots of the language name. 
However, you will probably agree that a snake is more associative.

Plus, if to use some characteristic MP feature like a giant foot, I'm 
not positive that it won't trigger any copyright issues.
I prefer an alternate meaning:
   2. A diviner by spirits. ``[Manasses] observed omens, and
  appointed pythons.'' --4 Kings xxi. 6 (Douay version).
Since Python is a divine language, and conjures up quick solutions to 
ghastly problems.  And, in the spirit of oracles, reflects the wisdom of 
the languages design. 8-)

Now, how about an icon that conveys something like that? hm... smoke 
curled around wizard perhaps?

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo----
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4

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


Re: subprocess vs. proctools

2004-12-15 Thread Keith Dart
Nick Craig-Wood wrote:
There are many ways for a program to fail (non-zero exit codes) but
only one way for it to succeed (zero exit code).  Therefore rc should
be 0 for success.
Exactly. And as a convenience the ExitStatus object of proctools handles 
that for you.

As a general rule, I believe Python interfaces should be more abstract 
and object-oriented. I don't think the users of my framework (myself 
included) should have to know or deal with the fact that "zero means 
good". You only need to know that if the ExitStatus object you get 
avaluates to True, it is a "good" exit.

IMHO Shell semantics are nuts (0 is True - yeah!) - they hurt my head
every time I have to use them ;-)
Exactly, and that is why the proctools framework hides that from the 
user-programmer.


import subprocess
subprocess.call(["sleep", "60"])
-11
# I killed the sleep process with a SEGV here from another xterm
Python> import proctools
Python> print proctools.call("sleep 60")
sleep exited by signal 11.
# same here... sent SEGV. which is easier to understand what is going on?
 (BTW, I just added a "call" function to proctools with similiar 
functionality)



subprocess.call(["sleep", "asdfasdf"])
sleep: invalid time interval `asdfasdf'
Try `sleep --help' for more information.
1
Python> print proctools.call("sleep asdf")
sleep: Exited abnormally with status 1.

Signals are -ve, exit codes are +ve which seems perfect.  Exit codes
can only be from 0..255 under linux.  Signals go from -1 to -64.
And why should my API user-programmers need to know that? That is just 
too... low-level.


if rc:
 print "exited normally"

Actually I think
if rc == 0:
 print "exited normally"
is exactly equivalent!
Yes, but requires the programmer to know that zero is good, and signals 
are negative. Again, the proctool's ExitStatus makes that more abstract 
for you (as Python interfaces should be) and provides test methods if 
you need them.

Python> rc = proctools.call("sleep asdf")
Python> rc.exited()
True
Python> rc.signalled()
False
Python> int(rc)
1
# Note that you can still get the exit value for those programs that
# return something meaningful.
Python> rc = proctools.call("sleep 60")
# send SEGV
Python> rc.signalled()
True
I like python because I can write stuff on linux and it works on
windows without too much effort, and in general I try not to use
modules which don't work on both platforms.
Sorry for you. I like Python becuase it allows me to write good, solid 
programs easily and quickly. I try to make my libraries facilitate that, 
and also be easy to use for beginning Python programmers. Python on 
Linux is a powerful combination, and I cannot fathom why someone would 
choose anything less. (I would concede that Python on Darwin is also good)


--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4 

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


Re: spawn* or exec* and fork, what should I use and how ?

2004-12-16 Thread Keith Dart
Lingyun Yang wrote:
Hi,
  I want to use python as a "shell like" program,
and execute an external program in it( such as mv, cp, tar, gnuplot)
I tried:
Since you appear to be on a *nix system, a good choice is the proctools 
module in the pyNMS package.

http://sourceforge.net/projects/pynms

os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot < plot.tmp"'))
You could do this:
import proctools
proctools.spawnpipe("gnuplot plot.tmp")
You can keep your existing Python 2.3 installation, as well.
1. why my exec(..) command doesn't work?
It replaces your current process.
2. exec* must be with fork ?
in this case, yes. but proctools does that for you.
3. In what situation, we choose one over another ?
The fork-and-exec is a common pattern in *nix for spawning another 
process. However, there are libraries in Python that do that for you. 
See above.

--
   \/ \/
   (O O)
-- ----oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4 

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


Re: ".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)

2004-12-16 Thread Keith Dart
Steve Holden wrote:
Nick Coghlan wrote:
Roel Schroeven wrote:
Stefan Behnel wrote:
This is the first time I see that and I totally like the idea of 
writing ".>>>" instead of ">>>" at the beginning of a line. Thank 
you Dr. Dobb! It's unfortunate for c.l.py that Python uses ">>>" as 
the default prompt as it messes up the display on mail/news readers 
that provide "syntax highlighting" for quotes.

I use Thunderbird, and started doing it so I could read my own posts. 
I did copy it from someone, though (but I can't recall who).

The trick can also be useful for web tools that strip leading whitespace.
Prepending every line with . is not an ideal solution though... I 
think it gets tiresome very quickly.

Aye, can't argue with that. It does have the virtues of reliability 
and portability, though :)

Cheers,
Nick.
$ python
Python 2.4 (#1, Dec  4 2004, 20:10:33)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import sys
 >>> sys.ps1 = ".>>> "; sys.ps2 = " "
.>>> print """\
 It isn't that hard"""
It isn't that hard
.>>>
Would it work, I wonder, with a leading space on the prompt? That might 
be a valid change to the standard prompt. Let's see

 >>> This line isn't really quoted three times.
What I do is set Python's sys.ps1 variable to something else. I have a 
module called "interactive" that I import implicitly by shell alias:

py='python -i -c '\''import interactive'\'
Which, among other things, sets the prompt to "Python> "
433 $ py
Python> print "This has no leader that screws up email programs."
This has no leader that screws up email programs.
Python>

--
   \/ \/
   (O O)
-- oOOo~(_)~oOOo
Keith Dart <[EMAIL PROTECTED]>
public key: ID: F3D288E4 

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


Re: Plugin system; imp module head scratch

2004-12-18 Thread Keith Dart
Dave wrote:
Hi, all,
I'm trying to implement a simple plugin framework, with some
unexpected results. I'm using Python 2.3.4 on Windows 2000.
What would be the difference between a "plugin" and a regular Python module?

-- ~~~~~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: Language fluency (was Re: BASIC vs Python)

2004-12-19 Thread Keith Dart
Hello, Aahz,
Aahz wrote:
myself to have "complete and utter" fluency.  In fact, in some ways my
fluency has degenerated now that I'm focusing on writing code for
production.
I'm curious about that last statement. Are you saying that if you write, 
full time, code for "production", that fluency will decrease? Or that 
the nifty recent features of Python (generators, etc.) are not useful in 
"production" code?

-- ~~~~~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: Easy "here documents" ??

2004-12-19 Thread Keith Dart
Jim Hill wrote:
I've done some Googling around on this and it seems like creating a here
document is a bit tricky with Python.  Trivial via triple-quoted strings
if there's no need for variable interpolation but requiring a long, long
formatted arglist via (%s,%s,%s,ad infinitum) if there is.  So my
question is:
Is there a way to produce a very long multiline string of output with
variables' values inserted without having to resort to this wacky
I was thinking about this. But I can't think of any reason why you would 
want to do this in Python. What's wrong with a regular parameterized 
function?

--
-- ~~~~~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question

2004-12-19 Thread Keith Dart
David Wurmfeld wrote:
I am new to python; any insight on the following would be appreciated, even 
if it is the admonition to RTFM (as long as you can direct me to a relevant 
FM)

Is there a standard approach to enumerated types? I could create a 
dictionary with a linear set of keys, but isn't this overkill? There is 
afterall a "True" and "False" enumeration for Boolean.
Not a standard one, but here's what I use:
class Enum(int):
__slots__ = ("_name")
def __new__(cls, val, name):
v = int.__new__(cls, val)
v._name = str(name)
return v
def __str__(self):
return self._name
def __repr__(self):
return "%s(%d, %r)" % (self.__class__.__name__, self, 
self._name)
def __cmp__(self, other):
if isinstance(other, int):
return int.__cmp__(self, other)
if type(other) is str:
return cmp(self._name, other)
raise ValueError, "Enum comparison with bad type"
class Enums(list):
def __init__(self, *init):
for i, val in enumerate(init):
if issubclass(type(val), list):
for j, subval in enumerate(val):
self.append(Enum(i+j, str(subval)))
elif isinstance(val, Enum):
self.append(val)
else:
self.append(Enum(i, str(val)))
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, list.__repr__(self))

-- ~~~~~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: input record seperator (equivalent of "$|" of perl)

2004-12-19 Thread Keith Dart
[EMAIL PROTECTED] wrote:
Hi,
I know that i can do readline() from a file object.
However, how can I read till a specific seperator?
for exmple,
if my files are
name
profession
id
#
name2
profession3
id2
I would like to read this file as a record.
I can do this in perl by defining a record seperator;
is there an equivalent in python? 
thanks

I don't think so. But in the pyNMS package 
(http://sourceforge/net/projects/pynms) there is a module called 
"expect", and a class "Expect". With that you can wrap a file object and 
use the Expect.read_until() method to do what you want.


--
-- ~~~~~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: A completely silly question

2004-12-19 Thread Keith Dart
Mike Meyer wrote:
Craig Ringer <[EMAIL PROTECTED]> writes:

On Sat, 2004-12-18 at 00:40, Amir Dekel wrote:
This must be the silliest question ever:
What about user input in Python? (like stdin)
Where can I find it? I can't find any references to it in the documentation.
Under UNIX, I generally either use curses, or just put the terminal into
raw mode:
.>>> def sane():
os.system("stty sane")

.>>> def raw():
os.system("stty raw")

The termios gives module gives you the tools to manipulate the tty
directly, without invoking stty. The tty module gives you an easier
interface to those routines. However, it's missing a setsane
functions. Hmm. I think it's time for another PEP.
   In the pyNMS package (http://sourceforge.net/projects/pynms/) there is a 
module called "termtools". This module can be used in place of the "tty" 
module. It has many improvements, including a "sane" function, a "raw" 
function, and an "stty" function. This module also replaces the 
"getpass" module, as it has the same functions found there. The PagedIO 
object is used by the CLI framework in pyNMS.



--
-- ~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: A completely silly question

2004-12-19 Thread Keith Dart
Jp Calderone wrote:
On Sun, 19 Dec 2004 23:15:40 GMT, Keith Dart <[EMAIL PROTECTED]> wrote:
Mike Meyer wrote:
The termios gives module gives you the tools to manipulate the tty
directly, without invoking stty. The tty module gives you an easier
interface to those routines. However, it's missing a setsane
functions. Hmm. I think it's time for another PEP.
  In the pyNMS package (http://sourceforge.net/projects/pynms/) there is a 
module called "termtools". This module can be used in place of the "tty" 
module. It has many improvements, including a "sane" function, a "raw" 
function, and an "stty" function. This module also replaces the 
"getpass" module, as it has the same functions found there. The PagedIO 
object is used by the CLI framework in pyNMS.


I found this:
http://cvs.sourceforge.net/viewcvs.py/*checkout*/pynms/pyNMS/lib/termtools.py?content-type=text%2Fplain&rev=1.4
But I don't see a function named "sane".  Is sf cvs out of date?  If so, where 
should I be looking?
  Jp
Yes, sorry, the CVS is very out of date. Please get the tarball (I just 
today put up a new one with the "sane" function in termtools).

I am coverting it all to subversion, and will make pyNMS publicly 
available through that means when it's ready.

Thanks for looking,
Keith

--
-- ~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python To Send Emails Via Outlook Express

2004-12-19 Thread Keith Dart
[EMAIL PROTECTED] wrote:
Hi Ganesan
I tried changing s.Send to s.Send(). It now comes up with an exception
error..
The details are below.
Looks like the COM part works, but sending mail has an error from the 
SMTP host. But, slightly off topic, FYI, Python can send email directly 
with the email and snmplib modules.

--
-- ~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: Easy "here documents" ??

2004-12-19 Thread Keith Dart
Fredrik Lundh wrote:
Jim Hill wrote:

I'm trying to write a script that writes a script for a rather specialized
task.  I know that seems weird, but the original version was written in
Korn shell and most of my team are familiar with the way it does things
even though they don't read Korn.

so why didn't you tell us? ;-)
if you want $-style interpolation, you can use the new string.Template
class (mentioned in passing by Nick above); useful examples here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304004
if you don't have 2.4, you can use the RE machinery for the same purpose;
see e.g.
http://effbot.org/zone/re-sub.htm#simple-templating
You might also try the following:
-python--
# a self-substituting string object. Just set attribute names to mapping 
names
# that are given in the initializer string.
class mapstr(str):
	def __new__(cls, initstr, **kwargs):
		s = str.__new__(cls, initstr)
		return s
	def __init__(self, initstr, **kwargs):
		d = {}
		for name in _findkeys(self):
			d[name] = kwargs.get(name, None)
		self.__dict__["_attribs"] = d
	def __setattr__(self, name, val):
		if name not in self.__dict__["_attribs"].keys():
			raise AttributeError, "invalid attribute name %r" % (name,)
		self.__dict__["_attribs"][name] = val
	def __getattr__(self, name):
		try:
			return self.__dict__["_attribs"][name]
		except KeyError:
			raise AttributeError, "Invalid attribute %r" % (name,)
	def __str__(self):
		if None in self._attribs.values():
			raise ValueError, "one of the attributes %r is not set" % 
(self._attribs.keys(),)
		return self % self._attribs
	def __call__(self, **kwargs):
		for name, value in kwargs.items():
			setattr(self, name, value)
		return self % self._attribs
	def __repr__(self):
		return "%s(%s)" % (self.__class__.__name__, str.__repr__(self))
	def attributes(self):
		return self._attribs.keys()

import re
_findkeys = re.compile(r"%\((\w+)\)").findall
del re
---
You use it like this:
TEST = mapstr("some%(one)s one\nsome%(two)s three\nsome%(three)s four")
print TEST.attributes()
TEST.one = "one"
TEST.two = "thing"
TEST.three = "where"
print TEST
s = str(TEST) # makes new, substituted, string
assert s == "someone one\nsomething three\nsomewhere four"
This allows you to use mapping-substitution syntax on a special string 
object. But the substituted variables are attributes of the object. 
String-ifying it gets the new string with the substitutions made.


--
-- ~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ack! Zombie processes won't die!

2004-12-22 Thread Keith Dart
Brian wrote:
From one script, I'm spawnv'ing another that will launch mpg123 to play a 
specified mp3.  Problem is that After the second script has launched 
mpg123, it'll turn into a zombie process.  It doesn't happen when I launch 
it from the command line, so there's something wrong with the way I'm 
calling it (I believe).

mp3pid = os.spawnv(os.P_NOWAIT, "/oter/playfile.py", ["playfile", filename, 
"0"])

Shouldn't this launch the script without waiting for it to finish?
It does, but the OS keeps the process information around until you 
"wait" on it, with "reaps", or collects the exit status then. You can do 
this asyncronously with a SIGCHLD handler. However, this has already 
been done.

In the the pyNMS package on sourceforge 
(http://sourceforge.net/projects/pynms) there is a module called 
"proctools". It has a process manager that does this for you.

--
-- ~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: win32 process name

2004-12-22 Thread Keith Dart
Fredrik Lundh wrote:
"phil" <[EMAIL PROTECTED]> wrote:

from win32all
EnumProcesses gives me the pids, then
OpenProcess(pid) gives me a handle.
Then what?
GetModuleFileNameEX?  It requires two handles as args
and I can't figure out which one is the handle from OpenProcess
and what it wants for the other one and I can't find any
Win32 SDK docs that help.

http://msdn.microsoft.com/library/en-us/perfmon/base/getmodulefilenameex.asp
describes a function with two [in] arguments, and one [out] argument.
the first argument is the process handle, the second a module handle;
the second argument can be NULL.

This ought to be a nobrainer. But Nooo. Its Windows.

it can be pretty tricky on other platforms too; that's why Unix programs usually
solve this by writing their PID to a file in a known location, so that other 
programs
can find them without having to resort to more or less stupid tricks.
 
On Linux, you can just scan the /proc directory. This is what the procps 
package does. From the command line:

310 $ ps -o cmd --no-heading 867
metalog [MASTER]
In the pyNMS package on sourceforge 
(http://sourceforge.net/projects/pynms) there is a module called 
Linux.procfs, and in it there is an object called "ProcStat" that lets 
you get the process info easily (if running on Linux).

Python> import procfs
Python> procfs.ProcStat(1)
Python> pid1 = procfs.ProcStat(1)
Python> print pid1
cmdline: init [3]
   cmaj_flt: 63690958
   cmin_flt: 186761321
 cnswap: 0
command: (init)
eip: 35
esp: 43
exit_signal: 0
  flags: 256
  it_real_value: 0
maj_flt: 224
min_flt: 1963
mm_end_code: 134538444
  mm_start_code: 134512640
 mm_start_stack: 3221225232
   nice: 0
  nswap: 0
   pgrp: 0
pid: 1
   ppid: 0
   priority: 15
  processor: 0
   rlim_cur: 4294967295
rss: 117
session: 0
sig_blocked: 0
  sig_catch: 671819267
 sig_ignore: 1475401980
sig_pending: 0
 start_time: 42
  state: S
 tms_cstime: 731277
 tms_cutime: 9593767
  tms_stime: 237
  tms_utime: 75
 tty_nr: 0
   tty_pgrp: -1
  vsize: 1429504
  wchan: 3222957162
--
-- ~~~~~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to start a new process while the other ist running on

2004-12-22 Thread Keith Dart
On 2004-12-22, Erik Geiger <[EMAIL PROTECTED]> wrote:
> Donn Cave schrieb:
>
>> In article <[EMAIL PROTECTED]>, Erik Geiger <[EMAIL PROTECTED]>
>> wrote:
> [...]
>> > Thats what I've tried, but it did not work. Maybe it's because I want to
>> > start something like su -c '/path/to/skript $parameter1 $parameter2' 
>> > user
>
>> Unfortunately this particular case kind of dilutes the advantages
>> of spawnv.  In the common case, parameter1 et al. would be submitted
>> directly as the parameter list.  I believe it may be clearer to start
>> with to think about the spawnv() function -
>>os.spawnv(os.P_NOWAIT, path, [cmdname, parameter1, parameter2])
>> 
>> If one of the parameters is itself another command, then of course
>> it has to be rendered as a string
>>os.spawnv(os.P_NOWAIT, '/bin/su', ['su', '-c', '%s %s %s' % (cmd,
>>   parameter1, parameter2)])
>> so you have almost as much work to scan the parameters for shell
>> metacharacters as you would have with system().
>> 
>>Donn Cave, [EMAIL PROTECTED]
> OK, thats too high for me ;-)
>
> Is the %s the variable for the following commands/parameters? If I have
> three parameters, I'll need one more %s? Where to write the user for the su
> command?
>
> Well, I've given up ;-)
>

Hey, don't give up. There is help. ;-) guess...

There is a package called pyNMS (http://sourceforge.net/projects/pynms)
That has a module called "proctools", and also one called "sudo". Once
you set up sudo correctly, you can do what you want easily. 

However, be aware that proctools spawns a process with the subprocess
stdio connected to _your_ parent process, and does not inherit the stdio
of the parent. So, if the subprocess writes a lot of stuff you must read
it, or the subprocess will block. However, there is some (perhaps buggy)
support for asynchronous operation where those reads happen
automatically (on Linux). 




-- 
-- ~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda going out of fashion

2004-12-22 Thread Keith Dart
On 2004-12-23, Stephen Thorne <[EMAIL PROTECTED]> wrote:
> Hi guys,
>
> I'm a little worried about the expected disappearance of lambda in
> python3000. I've had my brain badly broken by functional programming
> in the past, and I would hate to see things suddenly become harder
> than they need to be.

I use Python lambda quite a bit, and I don't understand the recent noise
about problems with it, and its removal. I don't have a problem with
lambdas.

My personal gripe is this. I think the core language, as of 2.3 or 2.4
is very good, has more features than most people will ever use, and they
(Guido, et al.) can stop tinkering with it now and concentrate more on
the standard libraries. 


-- 
-- ~~~~~
   Keith Dart <[EMAIL PROTECTED]>
   public key: ID: F3D288E4
   =
-- 
http://mail.python.org/mailman/listinfo/python-list