Re: Controlling newlines when writing to stdout (no \r\n).

2005-01-03 Thread Jeff Epler
Well, here's the first page turned up by google for the terms 'python
binary stdout':
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65443


Code from that page:
import sys

if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

Thanks, Google & the Python Cookbook website!

Jeff


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

Re: Octal notation: severe deprecation

2005-01-13 Thread Jeff Epler
On Thu, Jan 13, 2005 at 11:04:21PM +, Bengt Richter wrote:
> One way to do it consistently is to have a sign digit as the first
> digit after the x, which is either 0 or base-1 -- e.g., +3 and -3 would be
> 
> 2x011 2x101
> 8x03  8x75
> 16x03 16xfd
> 10x03 10x97

... so that 0x8 and 16x8 would be different?  So that 2x1 and 2x01 would
be different?

Jeff


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

Re: directory bug on linux; workaround?

2005-01-13 Thread Jeff Epler
Python is at the whim of the services the OS provides.  Maybe you should
ask in a linux-related newsgroup or mailing list, they might know more
about the specifics of both detecting and working around "weird"
filesystems like "fat".

To find the type of a filesystem, Linux provides the statfs(2) function,
but no built-in Python module includes access to that function.  Writing
the wrapper would be an easy first exercise in extending Python, or using
Pyrex or ctypes.  The filesystem "magic numbers" also aren't available
in Python.  A partial list can be found by "grep _SUPER_MAGIC
/usr/include/linux/*"

Jeff


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

Re: Classical FP problem in python : Hamming problem

2005-01-23 Thread Jeff Epler
Your formulation in Python is recursive (hamming calls hamming()) and I
think that's why your program gives up fairly early.

Instead, use itertools.tee() [new in Python 2.4, or search the internet
for an implementation that works in 2.3] to give a copy of the output
sequence to each "multiply by N" function as well as one to be the
return value.

Here's my implementation, which matched your list early on but
effortlessly reached larger values.  One large value it printed was
6412351813189632 (a Python long) which indeed has only the distinct
prime factors 2 and 3. (2**43 * 3**6)

Jeff

from itertools import tee
import sys

def imerge(xs, ys):
x = xs.next()
y = ys.next()
while True:
if x == y:
yield x
x = xs.next()
y = ys.next()
elif x < y:
yield x
x = xs.next()
else:
yield y
y = ys.next()

def hamming():
def _hamming(j, k):
yield 1
hamming = generators[j]
for i in hamming:
yield i * k
generators = []
generator = imerge(imerge(_hamming(0, 2), _hamming(1, 3)), _hamming(2, 5))
generators[:] = tee(generator, 4)
return generators[3]

for i in hamming():
print i,
sys.stdout.flush()


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

Re: Tuple slices

2005-01-24 Thread Jeff Epler
The cpython implementation stores tuples in memory like this:
[common fields for all Python objects]
[common fields for all variable-size python objects, including tuple size]
[fields specific to tuple objects, if any]
[array of PyObject*, one for each item in the tuple]
This way of storing variable-size Python objects was chosen in part
because it reuqires only one allocation for an object, not two.
However, there is no way for one tuple to point to a slice of another
tuple.

there's no reason that some other python implementation couldn't make a
different choice.

Jeff


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

Re: tkinter: Can You Underline More Than 1 Char In A Menu Title

2005-01-27 Thread Jeff Epler
On Thu, Jan 27, 2005 at 06:38:22AM -0500, Tim Daneliuk wrote:
> Is it possible to underline more than a single character as I am doing
> with the 'underline=0' above. I tried 'underline=(0,2)' but that didn't
> work.

No.

Jeff


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

Re: [Tkinter] problem

2005-01-29 Thread Jeff Epler
These lines
> if __name__ == '__main__':
> OptionsWindow()
mean "if this source code is the main program (not an imported module),
call OptionsWindow()".  So the behavior should be different when the
source code is the main program ('python opt_newlogin.py') and when it's
imported ('python -c "import opt_newlogin"')

Jeff


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

Re: Embedding Python - Deleting a class instance

2005-06-21 Thread Jeff Epler
I wrote the following module to test the behavior of PyInstance_New.  I
called it something like this:
import vedel

class k:
def __del__(self): print "deleted"

vedel.g(k)

I get output like:
after creation, x->refcnt = 1
doing decref
deleted
after decref

Unless there's a cycle and GC gets involved, all there is to deleting
*anything* in Python is correctly managing the refcount.  On the other
hand, you can never free an object while it is still reachable.  Some
local name "x" may never spontaneously lose the thing it refers to.

/**/
#include 

static PyObject *g(PyObject *s, PyObject *o) {
PyObject *x = PyInstance_New( o, NULL, NULL);
if(x) {
printf("after creation, x->refcnt = %d\n", x->ob_refcnt);
printf("doing decref\n");
Py_DECREF(x);
printf("after decref\n");
} else {
printf("x == NULL\n");
}
Py_INCREF(Py_None);
return Py_None;
}

static PyMethodDef methods[] = {
{"g", (PyCFunction)g, METH_O, NULL},
{NULL},
};

void initvedel(void) {
Py_InitModule("vedel", methods);
}
/**/


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

Re: eval() in python

2005-06-21 Thread Jeff Epler
On Tue, Jun 21, 2005 at 08:13:47AM -0400, Peter Hansen wrote:
> Xah Lee wrote:
> > the doc seems to suggest that eval is only for expressions... it says
> > uses exec for statements, but i don't seem to see a exec function?
> 
> Because it's a statement: http://docs.python.org/ref/exec.html#l2h-563

but the documentation is sooo baaad that it makes babies cry and
maybe spreads herpes too.


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

Re: utf8 silly question

2005-06-21 Thread Jeff Epler
If you want to work with unicode, then write
us = u"\N{COPYRIGHT SIGN} some text"
You can also write this as
us = unichr(169) + u" some text"


When you have a Unicode string, you can convert it to a particular
encoding stored in a byte string with
bs = us.encode("utf-8")


It's generally a mistake to use the .encode() method on a byte string,
but that's what code like
bs = "\xa9 some text"
bs = bs.encode("utf-8")
does.  It can lull you into believing it works, if the test data only
has US ASCII contents, then break when you go into production and have
non-ASCII strings.

Jeff


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

Re: PEP ? os.listdir enhancement

2005-06-22 Thread Jeff Epler
Why not just define the function yourself?  Not every 3-line function
needs to be built in.

def listdir_joined(path):
return [os.path.join(path, entry) for entry in os.listdir(path)]

dirs = [x for x in listdir_joined(path) if os.path.isdir(x)]

path_size = [(x, getsize(x)) for x in listdir_joined(path) if os.path.isfile(x)]


Jeff


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

Re: Loop until condition is true

2005-06-22 Thread Jeff Epler
def until(pred):
yield None
while True:
if pred(): break
yield None

def example():
i = 0
for _ in until(lambda: x==0):
x = 10 - i
i += 1
print x, i

example()


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

Re: Is this a bug? I don't know where to start

2005-06-22 Thread Jeff Epler
Your list "targets" contains some values twice.

>>> targets=[97,101,139,41,37,31,29,89,23,19,8,13,131,19,73,97,19,139,79,67,61,17,113,127]
>>> for t in set(targets):
... if targets.count(t) > 1: print t
... 
97
139
19

It looks like the "duplicated" items in the output contain one of the
duplicated items from the input.

Jeff


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

Re: Frame widget (title and geometry)

2005-06-24 Thread Jeff Epler
It would help if you posted your code, as we're in the dark about
exactly what you tried to do and the error you received.

It sounds like you may be using the wrong type of widget for what you
want.  The terms used in Tk are different than in some other systems.

If you want a separate window with title bar etc, you want to create a
new instance of Tkinter.Toplevel.  It will have methods like wm_title
and wm_geometry.

Newer versions of Tk (8.4 and maybe 8.3) have a widget called
"labelframe" (called Tkinter.LabelFrame in python2.3 and newer) which is the
grooved-border-and-label container used to semantically group related
widgets.

"Frame" widgets are simply containers which are often useful for making
the screen layout work the way you want with pack and grid.

Jeff


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

Re: Frame widget (title and geometry)

2005-06-24 Thread Jeff Epler
Tkinter.Frame instances are not created with "geometry" or "title"
attributes.  Whatever 'classtitle' and 'classtitle2' are, they are not
written to work with Tkinter.Frame instances.

Jeff


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

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread Jeff Epler
On Mon, Jun 27, 2005 at 08:21:41AM -0600, John Roth wrote:
> Unfortunately, I've seen that behavior a number of times:
> no output is None, one output is the object, more than one
> is a list of objects. That forces you to have checks for None
> and list types all over the place.

maybe you can at least push this into a single convenience function...

def destupid(x, constructor=tuple, sequencetypes=(tuple, list)):
if x is None: return constructor()
if isinstance(x, sequencetypes): return x
return constructor((x,))

Jeff


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

Re: Dictionary to tuple

2005-06-28 Thread Jeff Epler
It looks like you want tuple(d.iteritems())

>>> d = {1: 'one', 2: 'two', 3: 'three'}
>>> tuple(d.iteritems())
((1, 'one'), (2, 'two'), (3, 'three'))

You could also use tuple(d.items()).  The result is essentially the
same.  Only if the dictionary is extremely large does the difference
matter. (or if you're using an older version of Python without the
iteritems method)

Jeff


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

Re: It seems that ZipFile().write() can only write files, how can empty directories be put into it?

2005-07-01 Thread Jeff Epler
This has been discussed before.  One thread I found was
http://mail.python.org/pipermail/python-list/2003-June/170526.html
The advice in that message might work for you.

Jeff


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

Re: Trapping user logins in python ( post #1)

2005-07-04 Thread Jeff Epler
I don't know of a portable way for an inetd-style daemon to "listen" for
user logins.

On some systems (including RedHat/Fedora and debian), you may be able to
use PAM to do this.  (pam modules don't just perform authentication,
they can take other actions.  As an example, pam_lastlog "prints the
last login on successful login".  I'm not sure what priviledge a pam
module has when it executes.

A more standard way to do this would be to place lines in /etc/profile
/etc/csh.login and so forth for any other shells used on your system.
RedHat-style systems have an /etc/profile.d where you can drop a file
that will be executed at login, too.  This will, of course, be executed
with the user's privilege level.  Another problem with this approach is
that /etc/profile is executed for a "login shell", but a graphical login
is not a login shell.

Jeff


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

Re: importing pyc from memory?

2005-07-04 Thread Jeff Epler
This stupid code works for modules, but not for packages.  It probably has bugs.


import marshal, types

class StringImporter:
def __init__(self, old_import, modules):
self._import = old_import
self._modules = modules

def __call__(self, name, *args):
module = self._modules.get(name, None)
if module is None:
return self._import(name, *args)
code = marshal.loads(module)
mod = types.ModuleType(name)
exec code in mod.__dict__
return mod

def test():
import __builtin__
__builtin__.__import__ = StringImporter(__builtin__.__import__,
{ 'test_importer': open("/usr/lib/python2.3/os.pyc").read()[8:] })

import test_importer
print test_importer.path.join("a", "b")
print test_importer.__doc__

if __name__ == '__main__': test()


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

Re: Tkinter + Tcl help

2005-07-05 Thread Jeff Epler
I think you need to write
root.tk.eval('load', '...\\libtcldot.so.0')

When you write
root.tk.eval("x y z")
it's like doing this at the wish/tclsh prompt:
# {x y z}
Not like this:
# x y z
Now, how useful it is to have a command called "x y z", I can't
guess... but tcl would let you do it.

I've also doubled the backslash in your library filename.  When using
windows-style paths in string literals, you must either double the
backslashes, or use r'' strings.  When you don't, it will often work,
but if the character after the un-doubled backslash is one with special
meaning (including the commonly-seen \r and \t, but also other letters)
then you'll get a pathname and an error that leave you scratching your
head.

Jeff


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

Re: resume upload

2005-07-05 Thread Jeff Epler
probably by using REST.  This stupid program puts a 200 line file by
sending 100 lines, then using REST to set a resume position and sending
the next 100 lines.

import getpass, StringIO, ftplib

lines = ["Line %d\n" % i for i in range(200)]
part1 = "".join(lines[:100])
part2 = "".join(lines[:100])

f = ftplib.FTP('server', 'username', 'password')
f.debugging = True
f.sendcmd('CWD /tmp')
f.storbinary("STOR example", StringIO.StringIO(part1))
f.putcmd('REST %d' % len(part1))
resp = f.getresp();
if resp[0] != '3': raise ftplib.error_reply, resp
f.storbinary("STOR example", StringIO.StringIO(part2))
f.quit()


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

Re: math.nroot [was Re: A brief question.]

2005-07-06 Thread Jeff Epler
On Tue, Jul 05, 2005 at 09:49:33PM +0100, Tom Anderson wrote:
> Are there any uses for NaN that aren't met by exceptions?

Sure.  If you can naturally calculate two things at once, but one might
turn out to be a NaN under current rules.

x, y = calculate_two_things()
if isnan(x):
perform_next_step_with_only_y(y)
else:
perform_next_step_with_both(x, y)

Under your scheme, you'd have to write
try:
x, y = calculate_two_things()
except NaNException:
y = calculate_one_thing()
perform_next_step_with_only_y(y)
else:
perform_next_step_with_both(x, y)
and at the very least duplicate the code for calculating 'y', possibly
re-doing a lot of work at runtime too.

Jeff


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

Re: Strange os.path.exists() behaviour

2005-07-06 Thread Jeff Epler
Pierre wrote:
> Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
   ^^^
Here's the bug.  You're using Windows.  It's a filesystem, but not as we know 
it...

Anyway, You are getting exactly what the low-level Windows APIs return.

Here's a small "C" program.  It prints "0" next to the filename if the
file exists, -1 otherwise, as described at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__access.2c_._waccess.asp

int main(int argc, char **argv) {
int i;
for(i=1; idir
 Volume in drive C has no label.
 Volume Serial Number is 171D-4D2A

 Directory of C:\TMP\example

07/06/05  03:04p  .
07/06/05  03:04p  ..
07/06/05  03:05p 3 exist
   3 File(s)  3 bytes

C:\TMP\example>x:a.exe exist exist. exist nonexist nonexist. nonexist...
   exist: 0
  exist.: 0
   exist: 0
nonexist: -1
   nonexist.: -1
 nonexist...: -1

C:\TMP\example>type nonexist
The system cannot find the file specified.

C:\TMP\example>type exist


C:\TMP\example>

As you can see, not only does Windows think that "exist" exists, but it can
successfully "type" its contents too!

Jeff


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

Re: Query

2005-07-08 Thread Jeff Epler
python-xlib includes an implementation of the xtest extension, which is
enabled on most users' X servers, and can be used to send arbitrary
keyboard or mouse events.

jeff


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

Re: About undisclosed recipient

2005-07-09 Thread Jeff Epler
You provided far too little information for us to be able to help.

If you are using smtplib, it doesn't even look at message's headers to
find the recipient list; you must use the rcpt() method to specify each
one.  If you are using the sendmail method, the "to_addrs" list has no
relationship to the headers of the actual message---it simply calls
rcpt() once for each address in to_addrs.  The example in the docstring
doesn't even *have* a To: header in the message!

Jeff


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

Re: cursor positioning

2005-07-11 Thread Jeff Epler
Here's a simple module for doing progress reporting.  On systems without
curses, it simply uses "\r" to return the cursor to the first column.
On systems with curses, it also clears to the end of the line.  This
means that when the progress message gets shorter, there aren't droppings
left from the longer ones.

You have to take care that the progress message isn't wider than the
screen, but I don't know a nice way to *find* the width of the screen
that will work on windows and unix.  Hardcoding 80 ain't it either.

import sys

def progress(s):
sys.stderr.write(s + CLEAR_EOL + "\r"); sys.stderr.flush()

try:
import curses
except ImportError:
CLEAR_EOL = ''
else:
curses.setupterm()
CLEAR_EOL = curses.tigetstr("el") or ''

def test():
import time
for j in range(2):
for i in range(100):
progress("Doing item %d, %d%%" % (i * 100, i))
time.sleep(.01)
if __name__ == '__main__': test()


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

Re: Parsing Data, Storing into an array, Infinite Backslashes

2005-07-11 Thread Jeff Epler
Your code is needlessly complicated.

Instead of this business
while 1:
try:
i = fetch.next()
except stopIteration:
break
simply write:
for i in fetch:
(if there's an explicit 'fetch = iter(somethingelse)' in code you did
not show, then get rid of that and just loop 'for i in somethingelse')

i[1] will never compare equal to count, because i[1] is always a string
and count is always an integer.  Integers and strings are never equal to
each other.

Wring code like
x = "a string " + 3
does not work in Python.  You can either convert to a string and then
use the + operator to concatenate:
x = "a string " + str(3)
or you can use %-formatting:
x = "a string %s" % 3
("%s" accepts any sort of object, not just strings)

Using repr(...) (`...` is just a shorthand for this) is what is really
introducing the backslashes.  When it outputs a string, it quotes the
string using backslashes.  But you pass the old part of the prepared
string through it each time, which leads to doubling backslashes.

Below is a program I wrote to process the data in your message.  It prints
out
Memory 2 Summary=0, Speed=PC3200U-30330, Type=DDR SDRAM, Size=512, 
Slot=DIMM2/J13, ConfigurationType=2
Memory 3 Summary=0, Speed=PC3200U-30330, Type=DDR SDRAM, Size=512, 
Slot=DIMM3/J14, ConfigurationType=2
Memory 0 Summary=0, Speed=PC3200U-30330, Type=DDR SDRAM, Size=512, 
Slot=DIMM0/J11, ConfigurationType=2
Memory 1 Summary=0, Speed=PC3200U-30330, Type=DDR SDRAM, Size=512, 
Slot=DIMM1/J12, ConfigurationType=2
the result is out of order because the result of calling .items() on a
dict is in an arbitrary order.

Jeff

s = [['Memory', '0', 'Summary', '0'], ['Memory', '0', 'Speed',
 'PC3200U-30330'], ['Memory', '0', 'Type', 'DDR SDRAM'], ['Memory', '0',
 'Size', '512'], ['Memory', '0', 'Slot', 'DIMM0/J11'], ['Memory', '0',
 'ConfigurationType', '2'], ['Memory', '1', 'Summary', '0'], ['Memory',
 '1', 'Speed', 'PC3200U-30330'], ['Memory', '1', 'Type', 'DDR SDRAM'],
 ['Memory', '1', 'Size', '512'], ['Memory', '1', 'Slot', 'DIMM1/J12'],
 ['Memory', '1', 'ConfigurationType', '2'], ['Memory', '2', 'Summary',
 '0'], ['Memory', '2', 'Speed', 'PC3200U-30330'], ['Memory', '2',
 'Type', 'DDR SDRAM'], ['Memory', '2', 'Size', '512'], ['Memory', '2',
 'Slot', 'DIMM2/J13'], ['Memory', '2', 'ConfigurationType', '2'],
 ['Memory', '3', 'Summary', '0'], ['Memory', '3', 'Speed',
 'PC3200U-30330'], ['Memory', '3', 'Type', 'DDR SDRAM'], ['Memory', '3',
 'Size', '512'], ['Memory', '3', 'Slot', 'DIMM3/J14'], ['Memory', '3',
 'ConfigurationType', '2']]

query = {}

for a, b, c, d in s:
if not query.has_key((a,b)): query[(a,b)] = []
query[(a,b)].append("%s=%s" % (c, d))

for (a,b), v in query.items():
print a, b, ", ".join(v)


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

Re: Browser plug-in for Python?

2005-07-12 Thread Jeff Epler
Back in the day there was 'grail', which was a browser in its own right.
There may also have been a plug-in for other browsers, but I don't know
any real details about them.

Python itself has deprecated the 'restricted execution' environment it
had in previous versions, because ways to break out of the jail existed
or were thought to exist, and nobody stepped forward and offered to
spend the requisite time to create and validate (even in a hand-wavy
kind of way) a new security model.

If you want to write programs in Python and run them in today's
browsers, the shortest path from here to there is jython.  Several
applet demos are available at
http://www.jython.org/applets/index.html

I have used Jython a little bit, but never seriously and not in the past
few years.  Jython implements an older version of the Python
language, corresponding to cPython 2.1 if I remember correctly.

Jeff


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

Re: ssh popen stalling on password redirect output?

2005-07-15 Thread Jeff Epler
In your ssh configuration, specify something like
PreferredAuthentication "hostbased,publickey"
this will skip trying to use the methods called keyboard-interactive and
password.

You can give this flag on the ssh commandline, too.  read the ssh(1) and
ssh_config(5) manpages for more information.

Jeff


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

Re: stdin/stdout fileno() always returning -1 from windows service

2005-07-18 Thread Jeff Epler
On Sun, Jul 17, 2005 at 06:43:00PM -0700, chuck wrote:
> I have found that sys.stdin.fileno() and sys.stdout.fileno() always
> return -1 when executed from within a win32 service written using the
> win32 extensions for Python.
> 
> Anyone have experience with this or know why?

because there *is* no standard I/O for a windows service.

You should be able to execute code like
import sys
sys.stdout = sys.stderr = open("some.log", "w")
if you have code that expects the standard output files to be available.
You could also open sys.stdin in a similar way.

Jeff


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

Re: stdin/stdout fileno() always returning -1 from windows service

2005-07-18 Thread Jeff Epler
It seems to simply be common wisdom.  e.g.,
http://mail.python.org/pipermail/python-win32/2004-September/002332.html
http://mail.mems-exchange.org/pipermail/quixote-users/2004-March/002743.html
http://twistedmatrix.com/pipermail/twisted-python/2001-December/000644.html
etc

If you can find chapter and verse on MSDN, more power to you.  This page
implies that the "standard handles" are only available when there is a
console for the application.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getstdhandle.asp

Jeff


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

Re: Windows command line problem

2005-07-18 Thread Jeff Epler
I don't exactly know what is going on, but '\x96' is the encoding for
u'\N{en dash}' (a character that looks like the ASCII dash,
u'\N{hyphen-minus}', u'\x45') in the following windows code pages:
cp1250 cp1251 cp1252 cp1253 cp1254
cp1255 cp1256 cp1257 cp1258 cp874

Windows is clearly doing something clever.

Jeff


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

Re: Opinions on KYLIX 3 (Delphi 4 Linux)

2005-07-18 Thread Jeff Epler
I honestly don't know why anyone would spend money for a development
environment, no matter how fancy.  I don't know why anyone would develop
software in a language that doesn't have at least one open
implementation.

It's a great way to get screwed when Borland goes under or decides
they only want to sell a new, incompatible product.  What do you do with
your existing product when that happens?  Re-train on a new platform,
and re-write from scratch?

Just say no to proprietary software.

Jeff


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

Re: Image orientation and color information with PIL?

2005-07-18 Thread Jeff Epler
>>> i = Image.open("blue.jpg")
>>> i.size
(3008, 2000)
>>> i.mode
'RGB'

'RGB' is the value for color jpeg images.  I believe that for black&white
images, i.mode is 'L' (luminosity).

If you want to determine whether an existing image is landscape or portrait,
then just compare i.size[0] (width) and i.size[1] (height).

If by "determine if an image is horizontal/vertical", you want to find
the orientation data recorded by some digital cameras, you can do that
with PIL 1.1.4.  According to the release notes for 1.1.4,
+ Added experimental EXIF support for JPEG files.  To extract EXIF
  information from a JPEG file, open the file as usual, and call the
  "_getexif" method.  If successful, this method returns a dictionary
  mapping EXIF TIFF tags to values.  If the file does not contain EXIF
  data, the "_getexif" method returns None.

  The "ExifTags" module contains a dictionary mapping tags to tag
  names.

  This interface will most likely change in future versions.

The exif tag 274 is Orientation.  The values you'll see most often are 1
(Normal), 6 and 8 (90 and 270 degree rotations).  Orientation can also encode
180 degree rotation, as well as any of the four rotations combined with a
mirror operation.

>>> [k for (k,v) in ExifTags.TAGS.items() if v == 'Orientation']
[274]
>>> e = i._getexif()
>>> if e: print e[274]
1

I have written a standalone Python module that reads and changes the EXIF 
orientation data.  You can view it here:

http://unpy.net/cgi-bin/viewcvs.cgi/aethertool/disorient.py?rev=1.2&content-type=text/vnd.viewcvs-markup
It is available under the terms of the GNU GPL.

Here's another page about EXIF orientation data:
http://sylvana.net/jpegcrop/exif_orientation.html

Jeff


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

Re: Image orientation and color information with PIL?

2005-07-18 Thread Jeff Epler
On Mon, Jul 18, 2005 at 10:55:42AM -0600, Ivan Van Laningham wrote:
> How are you going to determine the orientation of an image without
> sophisticated image analysis? There is research on automatic image
> orientation detection.
[...]
> If you write it I'll use it;-)

There's research going on in this area.  Here are a few papers:
http://www.dcs.shef.ac.uk/teaching/eproj/msc2004/abs/m3zs2.htm
http://research.microsoft.com/research/pubs/view.aspx?pubid=918
there are probably many others.

I considered implementing one of these algorithms back in 2003 or so,
but instead I bought a digital camera with an orientation sensor.

Jeff


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

Re: Filling up commands.getstatusoutput's buffer

2005-07-21 Thread Jeff Epler
On Wed, Jul 20, 2005 at 03:10:49PM -0700, [EMAIL PROTECTED] wrote:
> Hey,
> 
> Has anyone ever had commands.getstatusoutput's buffer fill up when
> executing a verbose command? [...]

How much output are you talking about?  I tried outputs as large as
about 260 megabytes without any problem. (RedHat 9, Python 2.2)

>>> len(commands.getoutput("dd if=/dev/zero bs=512 count=512000 2>/dev/null"))
262144000
>>> 512 * 512000
262144000

Jeff


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

Re: time.time() under load between two machines

2005-07-22 Thread Jeff Epler
What makes you believe that the two machines' clocks are perfectly
synchronized?  If they're not, it easily explains the result.

I wrote a simple client/server program similar to what you described.
Running on two RedHat 9 machines on a local network, I generally
observed a time delta of 2ms (compared to typical 0.17ms latency
reported by "ping"), never in a negative direction.  These machines
times are synchronized by ntpd from the package ntp-4.1.2-0.rc1.2.

My program can be run like this:
rsh otherhost python timely.py -s | python timely.py -r
the values printed are the difference between the remote time before the
message is sent and the local time after the message is received.

You mention using Windows.  I don't know whether Windows machines by
default use anything as sophisticated as ntpd to keep their clocks
accurate.

Whatever is going on in your case, I suspect it is the operating system,
not Python.

Jeff

import os, sys, time

def serve():
while 1:
data = struct.pack("!d", time.time())
os.write(1, data)
time.sleep(1)

def recv(fileno):
while 1:
data = struct.unpack("!d", os.read(fileno, 8))[0]
now = time.time()
print now - data

if sys.argv[1] == "-s":
serve()
else:
recv(0)


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

Re: Mapping a drive to a network path

2005-07-22 Thread Jeff Epler
import os
os.system(r"net use z: \\computer\folder")

Something in the win32net module of win32all may be relevant if you
don't want to do it through os.system:

http://aspn.activestate.com/ASPN/docs/ActivePython/2.4/pywin32/win32net__NetUseAdd_meth.html

Jeff


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

Re: Mapping a drive to a network path

2005-07-22 Thread Jeff Epler
in fact, see this thread, it may have something useful for you:
http://mail.python.org/pipermail/python-win32/2003-April/000959.html

Jeff


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

Re: Getting TypeError in Changing file permissions

2005-07-22 Thread Jeff Epler
If you are using Unix, and all you have is the file object, you can use
os.fchmod(outfile.fileno(), 0700)

Jeff


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

Re: Question about namespaces and import. How to avoid calling os.system

2005-07-22 Thread Jeff Epler
In main.py, execfile("gen.py")

or

In gen.py, have something like
from __main__ import env_params

or

In main.py, have something like
import __builtins__; __builtins__.env_params = env_params

or

call a function in the gen.py with env_params as a parameter
import gen
gen.do(env_params)

Jeff


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

Re: How to realize ssh & scp in Python

2005-07-24 Thread Jeff Epler
Rather than doing anything with passwords, you should instead use public
key authentication.  This involves creating a keypair with ssh_keygen,
putting the private key on the machine opening the ssh connection
(~/.ssh/id_rsa), then listing the public key in the remote system's
~/.ssh/authorized_keys.

If you don't want to use this approach, `pexpect'[1] is supposed to be able
to perform this sort of tasks, and one of its examples is called
`sshls.py'.  I just downloaded it, and after changing the ssh
commandline to include
-o 'PreferredAuthentications password'
it worked for me.  In another recent thread, a different poster claimed
it didn't work, so your results may vary.

Jeff
[1] http://pexpect.sourceforge.net/


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

Re: Tkinter - Resizing a canvas with a window

2005-07-26 Thread Jeff Epler
You should just use 'pack' properly.  Namely, the fill= and expand=
parameters.  In this case, you want to pack(fill=BOTH, expand=YES).
For the button, you may want to use pack(anchor=E) or anchor=W to make
it stick to one side of the window.

The additional parameters for the button (both creation and packing)
give a geometry that is closer to the standard buttons in Windows 95
/ Windows 2000.  Use those or not, as you see fit.

Here's the new program:

from Tkinter import *

class testApp2:
 def __init__( self, master ):
 self.ma = master
 self.f = Frame( self.ma )
 self.f.pack(fill=BOTH, expand=YES)
 self.cv = Canvas(self.f, width=25, height=25, bg='red')
 self.cv.pack(fill=BOTH, expand=YES)
 self.b1 = Button( self.f, text='Hello', height=1, width=10,
padx=0, pady=1)
 self.b1.pack(side=BOTTOM, anchor=E, padx=4, pady=4)



root = Tk()
app = testApp2(root)
root.mainloop()

Jeff
PS thanks for including a full, runnable program in your post!


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

Re: Stripping C-style comments using a Python regexp

2005-07-27 Thread Jeff Epler
#
import re, sys

def q(c):
"""Returns a regular expression that matches a region delimited by c,
inside which c may be escaped with a backslash"""

return r"%s(\\.|[^%s])*%s" % (c, c, c)

single_quoted_string = q('"')
double_quoted_string = q("'")
c_comment = r"/\*.*?\*/"
cxx_comment = r"//[^\n]*[\n]"

rx = re.compile("|".join([single_quoted_string, double_quoted_string,
c_comment, cxx_comment]), re.DOTALL)

def replace(x):
x = x.group(0)
if x.startswith("/"): return ' '
return x

result = rx.sub(replace, sys.stdin.read())
sys.stdout.write(result)
#

The regular expression matches ""-strings, ''-character-constants,
c-comments, and c++-comments.  The replace function returns ' ' (space)
when the matched thing was a comment, or the original thing otherwise.
Depending on your use for this code, replace() should return as many
'\n's as are in the matched thing, or ' ' otherwise, so that line
numbers remain unchanged.

Basically, the regular expression is a tokenizer, and replace() chooses
what to do with each recognized token.  Things not recognized as tokens
by the regular expression are left unchanged.

Jeff
PS this is the test file I used:
/* ... */ xyzzy;
456 // 123
const char *mystr =  "This is /*trouble*/";
/* * */
/* /* */
// /* /* */
/* // /* */
/*
 * */


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

Re: codecs.getencoder encodes entire string ?

2005-07-28 Thread Jeff Epler

On Thu, Jul 28, 2005 at 08:42:57AM -0700, nicolas_riesch wrote:
> And a last question: can I call this "enc" function from multiple
> threads ?

Yes.

Jeff


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

Re: poplib.POP3.list() returns extra value?

2005-07-28 Thread Jeff Epler
With a judicious bit of UTSL, that count seems to be the total number of
octets in the reply.  This information comes from any user of
_getlongresp(), which actually returns a tuple (resp, list, octets).
These methods would be:
list
retr
top
uidl

I'd consider it a doc bug too.  If you feel comfortable doing it, dive
in and improve the documentation of poplib.  Submitting a patch to the
patch tracker on sf.net/projects/python is probably the best way to do
this, if you have the necessary knowledge of cvs to produce a patch.

Jeff


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

Re: A replacement for lambda

2005-07-30 Thread Jeff Epler
On Fri, Jul 29, 2005 at 10:14:12PM -0700, Tim Roberts wrote:
> C++ solves this exact problem quite reasonably by having a greedy
> tokenizer.  Thus, that would always be a left shift operator.  To make it
> less than and a function, insert a space:
> < 

Incidentally, I read in an article by Bjarne Stroustrup[1] that "C++0x" will 
parse
vector> v;
just like today's compilers parse
vector > v;

Another of the changes he discusses, letting 'auto i = ...' create i
with the type of the expression '...', will certainly be an improvement.
Even better if the keyword 'auto' could be made optional!  (Of course,
this is another break with C, where the declaration 
auto i;
makes 'i' an int)

And what's this got to do with Python?  I dunno.  Sorry.

Jeff
[1] 
http://www.informit.com/content/images/art_stroustrup_2005/elementLinks/rules.pdf


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

Re: Newb: Telnet 'cooked data','EOF' queries.

2005-07-31 Thread Jeff Epler
On Sun, Jul 31, 2005 at 01:30:43PM +0100, glen wrote:
> Could someone explain what "cooked data" is.

The telnet protocol contains special sequences which are interpreted by
the telnet client or server program.  These are discussed in the telnet
RFC, which is RFC854 according to the telnetlib docstring.

"Cooked" data is data after these special sequences are removed.

> Also when trying read_all() the program seems to lock up, which I assume 
> is because it is waiting for an EOF, but 'when' is an EOF received.

As far as I know, the only EOF in telnet is when the other side closes
the socket.

Jeff


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

Re: Getting not derived members of a class

2005-08-01 Thread Jeff Epler
On 'y', Python has no way of recording where '_a' and '_b' were set, so
you can't tell whether it comes from class 'a' or 'b'.

You can find the attributes that are defined on 'b' only, though, by
using 'b.__dict__.keys()', or 'y.__class__.__dict__.__keys__()'.  This
gives
['__module__', 'who1', '__init__', '__doc__']

If you want to limit yourself to current versions of cpython (because the
bytecode used in cpython is only an implementation detail) and define a 'member
of class a' as one where a.__init__ has a statement like 'self.z = ...', you
can peer into the bytecodes.  Something like this:
from dis import HAVE_ARGUMENT, opname
LOAD_FAST = chr(opname.index('LOAD_FAST'))
STORE_ATTR = chr(opname.index('STORE_ATTR'))
HAVE_ARGUMENT = chr(HAVE_ARGUMENT)

def find(cls):
ns = cls.__dict__
result = ns.keys()
init = ns.get('__init__', None)
if not init: return ns
f = ns['__init__'].func_code.co_code
n = ns['__init__'].func_code.co_names
i = 0
while i < len(f) - 6:
if (f[i] == LOAD_FAST and f[i+1] == f[i+2] == '\0'
and f[i+3] == STORE_ATTR):
j = ord(f[i+4]) + 256 * ord(f[i+5])
result.append(n[j])
i += 6
elif f[i] > HAVE_ARGUMENT:
i += 3
else:
i += 1
return result

>>> import franz
>>> franz.find(y.__class__)
['__module__', 'who1', '__init__', '__doc__', '_b']

Jeff


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

Re: [noob] Questions about mathematical signs...

2005-02-06 Thread Jeff Epler
On Sun, Feb 06, 2005 at 12:26:30PM -0800, administrata wrote:
> Hi! I'm programming maths programs.
> And I got some questions about mathematical signs.
> 
> 1. Inputing suqare like a * a, It's too long when I do time-consuming
>things. Can it be simplified?

You can write powers with the "**" operator.  In this case,
a ** 2

> 2. Inputing fractions like (a / b) + (c / d), It's tiring work too.
>Can it be simplified?

Because of the rules of operator precedence,
a / b + c / d
has the same meaning as the expression you gave.

> 3. How can i input root?

Assuming that you've already executed
import math
Here are some ways to find the square root of a number:
math.sqrt(4)
4 ** .5
math.pow(4, .5)

Jeff


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

Re: Is Python as capable as Perl for sysadmin work?

2005-02-08 Thread Jeff Epler
No.

Unlike Perl, Python implements only a *finite turning machine* model of
computation.  An easy way to see this limitation is in the following
code:
>>> 1.0 / 10.0
0.10001
In an infinite Turning machine, there would be an unbounded number of
zeros before the second 1, giving the exact result, not a numeric
approximation.

There's another little-known fact about Python:  No string is permitted
to end with a backslash!  You might think that variations like
r'\'
or
""\""
would allow you to create this elusive value, but you'd mistaken!
Now, this may not bother Unix sysadmins, but the honest truth is that
you'll be administrating Windows systems, too, anywhere you work!

Finally, Python just doesn't respond to threats as well as Perl does.
I have run into many Perl programs that just didn't quite work right
until I wrote '... or die "$!"' in the right places.  Well, if you ever
have to threaten Python, just keep in mind that '... or die' just plain
won't work.  You have to suggest that it 'try ... except', which is
really offensive.  If I want to beg my computer to run programs, I know
where to find Intercal with its "PLEASE" and "DO PLEASE" constructions.

Jeff


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

Re: Tkinter.Canvas saved as JPEG?

2005-02-10 Thread Jeff Epler
The Tkinter Canvas directly supports saving to postscript format, but
not any standard bitmap format (or even modern vector formats like pdf
or svg).

Jeff


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

Re: exec function

2005-02-12 Thread Jeff Epler
In this case, you can use getattr() instead of the exec statement:
getattr(self.frame, t).SetTable(DataTable(d, r[0]), True)

Jeff


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

Re: Why Tk treat F10, F11, F12 diferently from F1...F9?

2005-02-12 Thread Jeff Epler
The reason that F10 does nothing is because there is already a binding
on all for .  In Motif apps, F10 moves focus to the menu bar, like
pressing and releasing Alt does on Windows.  When there is a binding for
a key, the handling of the event "event add" never takes place.

If you want to get rid of this binding, you would do it with something
like
w.bind_all("", "")
for some widget w.

I don't know why you see the "L1" and "L2" keysyms.  I don't on my
system (Fedora Core 2)

Jeff


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

Re: SCons build tool speed

2005-02-13 Thread Jeff Epler
On Sun, Feb 13, 2005 at 08:39:10PM -0500, Peter Hansen wrote:
> That answer, combined with Mike's response pointing out
> that tools more sophisticated than basic "make" actually
> can delve into the source and identify the dependencies,

Argh argh argh.

Of course you can write a makefile to "delve into the source and
identify the dependencies".  And, by the way, there's no need for make
to be recursive when a project spans multiple directories. (the other
popular misconception about make)

Below is a GNU makefile that I use to build a small program.  I
re-organized the file a bit before posting to clearly separate the two
sections of the makefile, and it's possible I broke something in the
process.

Since my system has subsecond file timestamps and I do not build with source
or object files on network filesystems, the "timestamp comparison"
method works just fine to decide whether a target must be rebuilt or
not.

What programs like SCons *do* is include nice built-in definitions of
stuff like the 'obj/%.d: %.cc' rule (often with a built-in parser which
you can bet will not be 100% compatible with your compiler!), built-in
inclusion of dependency lists with no extra programmer action, and the
features I don't need like the use of cryptographic hashes to compare
files.

A larger system I work on uses GNU make to build around 6000 object
files into a single executable.  A "do-nothing" make takes around 8
seconds on a 2.8GHz machine.  This involves parsing about 6000 lines of
Makefiles and Submakefiles (per-directory lists of source files), and
about 650klines of dependency information.

I don't know how this measures up to "modern" tools like SCons.  I'd be
curious to know.

But it's my opinion that a well-designed "make library" and/or "make
preprocessor" to go with GNU Make would be right up there with SCons in
its "power", while avoiding the need for a tool that most people have
never heard of and don't already have installed on their system (yes, a
pretty Linux-centric view).

One criticism of Make that I'd take seriously is that it's a
domain-specific language and usually I say those are terrible ideas.  I
still have to refer to the manual to remember which of $*, $@, $^ or $<
I need, sometimes, even though I've been a GNU Make power user for two
years now.

Jeff


#
# This section of the makefile names the source files (one in this case)
# and other complier options.  It also has a rule for linking the target
# program
default: stippler
LFLAGS := -L/usr/X11R6/lib 
LIBS := -lGL -lGLU -lglut -lXi -lXmu -lX11 -lm -lnetpbm
SRCS := main.cc
CFLAGS := -mcpu=pentium4 -O0 -g -DOUTPUT_POSTSCRIPT

stippler: $(OBJS)
$(CCLD) $(LFLAGS) -o $@ $^ $(LIBS)

#
# This section, which is essentially boilerplate, says how to generate
# the list of dependencies or the object file from a given source file.  
# It also transforms the list of sources into a list of objects, and
# includes all the necessary dependency lists ("dot d files")
#
# The definition of OBJS would be more complex if source files of
# different types exist (.c and .cc files, for example)
OBJS := $(patsubst %.cc, obj/%.o, $(SRCS))
CCLD := $(CC)
include $(patsubst %.o,%.d,$(OBJS))

obj/%.d: %.cc
mkdir -p $(dir $@)
$(CC) -MM -MG -MT "$@ $(patsubst obj/%.d,obj/%.o,$@)" \
$(CFLAGS) $< -o [EMAIL PROTECTED] && mv -f [EMAIL PROTECTED] $@

obj/%.o: %.cc
$(CC) $(CFLAGS) -c $< -o [EMAIL PROTECTED] && mv -f [EMAIL PROTECTED] $@
#


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

Re: 64 bit Python

2005-02-14 Thread Jeff Epler
There's not enough information to guess the "real problem", but it could
be this:

"variable size" objects (declared with PyObject_VAR_HEAD) are limited to
INT_MAX items since the ob_size field is declared as 'int'.

This means that a Python string, tuple, or list (among other types) may
be limited to about 2 billion items on ILP32 and LP64 architectures.

Dicts and probably sets are also limited to INT_MAX elements, because
the "ma_fill" and "ma_used" fields are ints.

If you don't mind recompiling all your extensions, you could change the
type of ob_size to long in the "#define PyObject_VAR_HEAD".  I don't
know what breaks when you do this, but maybe a few google or google
groups searches could help you find others who have tried this.

Jeff
PS the limit of 500MB of "real data" in the 32-bit system may be because
a realloc may temporarily require (old size + new size) storage when it
does the equivalent of
new_ptr = malloc(new_size)
memcpy(new_ptr, old_ptr, old_size)
free(old_size)
which will temporarily use >900MB of data when resizing a ~450MB object.
Python frequently uses realloc() to grow structures like lists.  If you
are working with strings, then
s = s + t
doesn't ever use realloc (in 2.3.x anyway) but always allocates and
fills the result 's+t', only freeing the old value of s later when it is
no longer reacahable (which could be as soon as the assignment statement
completes)


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

Re: [Errno 18] Invalid cross-device link using os.rename

2005-02-14 Thread Jeff Epler
mv is a surprisingly complex program, while os.rename is a wrapper
around rename(2) which is probably documented on your system to return
EXDEV under these circumstanes.

os.xxx is generally a fairly thin wrapper around what your OS provides,
and inherits all the "gotchas".  For some activities, os.shutil provides
something that is between os.xxx and os.system("xxx") in complexity and
capability.

Jeff


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

Re: How can I tell if my host supports Python?

2005-02-14 Thread Jeff Epler
If you have a shell, it's as simple as typing "python" and seeing if the
interactive interpreter appears:
$ python
Python 2.3.3 (#1, May  7 2004, 10:31:40) 
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
If you've done this, you can find out the path to this Python
interpreter:
>>> import sys; print sys.executable
/usr/bin/python

Otherwise, the documentation for the 'cgi' module has a buried
suggestion with the simplest Python CGI program.
http://docs.python.org/lib/node462.html
that program would look something like
#!/usr/bin/python
import cgi; cgi.test()
though you must know the correct path to the Python interpreter for this
to work.

Your best bet may be to ask your webhost for help---only they know how
they've (mis)configured your system.

Jeff


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

Re: super not working in __del__ ?

2005-02-15 Thread Jeff Epler
When a Python program exits, various steps of cleanup occur.  One of
them is that each entry in the __dict__ of each module is set to 'None'.
Imagine that your __del__ runs after this step of the cleanup.  The reference
to the module-level variable that names your class is no longer available

I looked around at the documentation and didn't immediately find where
this is documented.  The closest I saw was in the C API reference:
void Py_Finalize(   )
Undo all initializations made by Py_Initialize() and subsequent use of
Python/C API functions, and destroy all sub-interpreters (see
Py_NewInterpreter() below) that were created and not yet destroyed
since the last call to Py_Initialize(). Ideally, this frees all memory
allocated by the Python interpreter. This is a no-op when called for a
second time (without calling Py_Initialize() again first). There is no
return value; errors during finalization are ignored.

This function is provided for a number of reasons. An embedding
application might want to restart Python without having to restart the
application itself. An application that has loaded the Python
interpreter from a dynamically loadable library (or DLL) might want to
free all memory allocated by Python before unloading the DLL. During a
hunt for memory leaks in an application a developer might want to free
all memory allocated by Python before exiting from the application.

*   Bugs and caveats: The destruction of modules and objects in modules is
*   done in random order; this may cause destructors (__del__() methods) to
*   fail when they depend on other objects (even functions) or modules.
Dynamically loaded extension modules loaded by Python are not unloaded.
Small amounts of memory allocated by the Python interpreter may not be
freed (if you find a leak, please report it). Memory tied up in
circular references between objects is not freed. Some memory allocated
by extension modules may not be freed. Some extensions may not work
properly if their initialization routine is called more than once; this
can happen if an application calls Py_Initialize() and Py_Finalize()
more than once. 
but this doesn't go into detail about exactly how modules are cleaned up.

If anybody knows if this is better-documented somewhere, please speak
up!

Jeff


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

Re: Attaching to a Python Interpreter a la Tcl

2005-02-24 Thread Jeff Epler
Cameron Laird mentioned Tk's send working with Python; if you are writing your
app with Tkinter, here is some code to let you use tcl commands like
send  python 
for remote control.  You could build a more sophisticated front-end for this,
and you'll probably also want to add stuff like sending the text of an
exception as the result of the 'send' command.

Jeff

#
import Tkinter

__all__ = 'python', 'setup_send'
def makecommand(master, name, func, subst=None, needcleanup=0):
f = Tkinter.CallWrapper(func, subst, master).__call__
master.tk.createcommand(name, f)
if needcleanup:
if master._tclCommands is None:
master._tclCommands = []
master._tclCommands.append(name)
return name


def setup_send(app, ns, name="python"):
def python(*args):
s = " ".join(args)
print args
try:
code = compile(s, '', 'eval')
return eval(code, ns)
except SyntaxError:
code = compile(s, '', 'exec')
exec code in ns
makecommand(app, name, python)

if __name__ == '__main__':
app = Tkinter.Tk()
setup_send(app, {})
app.mainloop()
#


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

Re: Running Python Scripts With 'sudo'

2005-03-02 Thread Jeff Epler
Does "sudo" sanitize the environment?  Imagine that the user can set
PYTHONPATH, PYTHONINSPECT, etc.

Beyond that, you have the same problems as with any code that runs with
"extra privileges".  Can the user supply any code that is fed to
patently unsafe primitives (like the unpickler, eval() or the exec
statement)?  If your program opens files with user-controlled names, did
you make all the right checks?

Jeff


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

Re: tkinter absorb chars

2005-03-03 Thread Jeff Epler
On Wed, Mar 02, 2005 at 04:58:03PM -0600, phil wrote:
> Sorry for the repost, but moderator
> jeld the last one,

We saw both posts.

> In a Tkinter entry field (or Pmw entry)
> how could I eat charactres?

create a binding on the widget for the particular character you want to
treat specially.  If you want to suppress that character, return the
string "break" from the binding.  Example:

from Tkinter import *
t = Tkinter.Tk()
e = Tkinter.Entry(t); e.pack()
e.bind("x", lambda event: "break")

Jeff


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

Re: Tough Spawn Problem

2005-03-06 Thread Jeff Epler
By using os.spawn* and the os.P_NOWAIT, the spawn function will return
immediately, with the return value being the PID of the new process.
Later, you can use os.kill() to force the program to terminate,
os.waitpid() to retrieve the exit status if it has terminated, or you
could use the signal module to wait for SIGCHLD to be delivered at the
time the child terminates.

With os.spawn*, the child's open files (including stdin and stdout) are
the same as the parent's; using the popen2 module, you can send the
program input and capture its output too.

Here's the program I ran on Linux (Fedora Core 2 / Python 2.3) to show
that os.P_NOWAIT works fine:
import os
pid = os.spawnv(os.P_NOWAIT, "/bin/sh",
["sh", "-c", "sleep 1; echo spawned program"])
print "child is pid", pid
print "waitpid result is", os.waitpid(pid, 0)
and the output is
$ python /tmp/googlemike.py 
child is pid 13874
spawned program
waitpid result is (13874, 0)
the fact that "spawned program" is printed after "child is pid N"
shows that the python program continues executing while the child is
working (or, in this case, sleeping).

Jeff


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

Re: reversed heapification?

2005-03-07 Thread Jeff Epler
Can you use something like (untested)
class ComparisonReverser:
def __init__(self, s): self.s = s
def __cmp__(self, o): return cmp(o, self.s)
def __lt__...  # or whichever operation hashes use
then use (ComparisonReverser(f(x)), i, x) as the decorated item
instead of (f(x), i, x)

Jeff


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

Re: pyconfig.h

2005-03-07 Thread Jeff Epler
The pyconfig.h file (/usr/include/python2.3/pyconfig.h) should begin
something like this
/* pyconfig.h.  Generated by configure.  */
/* pyconfig.h.in.  Generated from configure.in by autoheader.  */
and shouldn't cause problems.

If it starts in a wildly different way than that, then it's probably due
to something unique SUSE did in packaging Python.  If you're having
problems with SUSE's own modifications, then the place to go is probably
their bug database or mailing lists.

Jeff


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

Re: Unicode BOM marks

2005-03-07 Thread Jeff Epler
On Mon, Mar 07, 2005 at 11:56:57PM +0100, Francis Girard wrote:
> BTW, the python "unicode" built-in function documentation says it returns a 
> "unicode" string which scarcely means something. What is the python 
> "internal" unicode encoding ?

The language reference says farily little about unicode objects.  Here's
what it does say: [http://docs.python.org/ref/types.html#l2h-48]
Unicode
The items of a Unicode object are Unicode code units. A Unicode
code unit is represented by a Unicode object of one item and can
hold either a 16-bit or 32-bit value representing a Unicode
ordinal (the maximum value for the ordinal is given in
sys.maxunicode, and depends on how Python is configured at
compile time). Surrogate pairs may be present in the Unicode
object, and will be reported as two separate items. The built-in
functions unichr() and ord() convert between code units and
nonnegative integers representing the Unicode ordinals as
defined in the Unicode Standard 3.0. Conversion from and to
other encodings are possible through the Unicode method encode
and the built-in function unicode().

In terms of the CPython implementation, the PyUnicodeObject is laid out
as follows:
typedef struct {
PyObject_HEAD
int length; /* Length of raw Unicode data in buffer */
Py_UNICODE *str;/* Raw Unicode buffer */
long hash;  /* Hash value; -1 if not set */
PyObject *defenc;   /* (Default) Encoded version as Python
   string, or NULL; this is used for
   implementing the buffer protocol */
} PyUnicodeObject;
Py_UNICODE is some "C" integral type that can hold values up to
sys.maxunicode (probably one of unsigned short, unsigned int, unsigned
long, wchar_t).

Jeff


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

Re: pymem.h In function '__declspec'

2005-03-07 Thread Jeff Epler
What does this command print?
gcc -c -I/usr/Python-2.3.3/Include -x c -o /dev/null \
/usr/Python-2.3.3/Include/pymem.h
If it prints an error like the one you included in this message, then
the set of header files in /usr/Python-2.3.3/Include is damaged,
incomplete, or wrong for your compiler environment.  If the files in
/usr/Python-2.3.3 are from SUSE, then contact your vendor.

If it doesn't give an error, but the build of your "application RPM"
does, then investigate the compiler defines that are present in the
application that aren't on the above commandline.  For instance, I was
able to get errors like yours by using inappropriate compiler
options:
$ gcc -DPy_ENABLE_SHARED -DHAVE_DECLSPEC_DLL -c -x c -o /dev/null \
/usr/include/python2.3/Python.h
In file included from /usr/include/python2.3/Python.h:67:
/usr/include/python2.3/pymem.h: In function `__declspec':
/usr/include/python2.3/pymem.h:51: error: syntax error before "__declspec"


You're right that __declspec has something to do with Windows DLLs.
Normally, Python uses the macro 'PyAPI_FUNC' to declare all functions in
its header files.  For certain compilers (msvc, cygwin, mingw) it's
appropriate to use the __declspec extension to make the Python shared
library work properly.  For other compilers, PyAPI_FUNC doesn't do
anything special.  It's through the presence of absence of defines like
Py_ENABLE_SHARED and HAVE_DECLSPEC_DLL that Python decides how to define
PyAPI_FUNC, and this little detail is usually below the radar.

Jeff


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

Re: problem with "time"

2005-03-08 Thread Jeff Epler
Without your code, it's hard to tell.

Here's a small program I wrote:
import time

t = time.time()
print time.localtime(t - 86400)
print time.localtime(t)
on both lines, the tm_isdst flag is the same.

If I choose two times that are on either side of the DST change in
my timezone, I get different answers:
import time
 
t = time.time()
print time.localtime(t)
print time.localtime(t+86400 * 90)
Here, I get different values for tm_isdst because DST is in effect for
one of the times but not the other.

> I have _no_ idea on how localtime() could reasonably synthesize
> different values for that flag for _any_ time value converted in about
> the same millisecond. What gives?

The tm_isdst field is not about the DSTness at the time of
conversion---it's about the DSTness of the converted time.

Jeff


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

Re: Is there a short-circuiting dictionary "get" method?

2005-03-09 Thread Jeff Epler
untested

def my_getter(m, i, f):
try:
return m[i]
except (KeyError, IndexError):
return f()

my_getter(d, 'x', bigscaryfunction)
my_getter(d, 'y', lambda: scaryinlineexpresion)


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

Re: Apparently, I don't understand threading

2005-03-14 Thread Jeff Epler
Ah -- I'm sorry I was off-target, and I'm glad someone else had what may
be better advice for you.

Jeff


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

Re: Accessing the contents of a 'cell' object from Python

2005-03-15 Thread Jeff Epler
Here's an old thread I contributed to which had a similar function
(called 'cell_get' in this case)

http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/baba3b943524a92c/71b57a32b311ffc8?q=func_closure#71b57a32b311ffc8
http://groups-beta.google.com/group/comp.lang.python/msg/71b57a32b311ffc8?dmode=source

Jeff


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

Re: Pre-PEP: Dictionary accumulator methods

2005-03-18 Thread Jeff Epler
Maybe something for sets like 'appendlist' ('unionset'?)

Jeff


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

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Jeff Epler
> [Jeff Epler]
> > Maybe something for sets like 'appendlist' ('unionset'?)
> 
On Sat, Mar 19, 2005 at 04:18:43AM +, Raymond Hettinger wrote:
> I do not follow.  Can you provide a pure python equivalent?

Here's what I had in mind:

$ python /tmp/unionset.py
Set(['set', 'self', 'since', 's', 'sys', 'source', 'S', 'Set', 'sets', 
'starting'])

#
try:
set
except:
from sets import Set as set

def unionset(self, key, *values):
try:
self[key].update(values)
except KeyError:
self[key] = set(values)

if __name__ == '__main__':
import sys, re
index = {}

# We need a source of words.  This file will do.
corpus = open(sys.argv[0]).read()
words = re.findall('\w+', corpus)

# Create an index of the words according to the first letter.
# repeated words are listed once since the values are sets
for word in words:
unionset(index, word[0].lower(), word)

# Display the words starting with 'S'
print index['s']
#

Jeff


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

Re: subprocess 'wait' method causes .py program to hang.

2005-03-19 Thread Jeff Epler
You can use PROC.poll() to find out whether the process has exited yet
or not (for instance, in a 'while' loop along with a delay).  I don't know
what facilities exist to forcibly terminate programs on Windows, though.
On Unix, os.kill() can be used to kill a process given its pid.  Perhaps
some of the functions in the 'win32process' module of win32all can help.

Here's an example for Windows written in C:
http://win32.mvps.org/processes/fkill.html

Jeff


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

Re: List limits

2004-12-20 Thread Jeff Epler
I'm referring to Python 2.2's C headers as I answer this question.  I
believe some of may have changed by 2.4.

The number of elements in a "variable-sized object" (those with
Py_VAR_HEAD; I believe this includes lists, tuples, and strings) is
stored in a platform 'int'.

On most (desktop) systems, this means the limit of any sized object is
no more than 2**31-1, or about 2 billion.

On those same systems, a list object of length 128 million, give or
take, approximately 512 megabytes of memory will be allocated for the
list object itself (4 bytes for each pointer-to-element).  If each
element of the list is distinct, those objects will each require
additional memory---The smallest useful Python object takes around 16
bytes, IIRC, which would bring the total memory required to around 2560
megabytes if I didn't screw up my arithmetic.  This is close to (or
over, depending on the system) the maximum amount of physical RAM these
machines can accomodate, and the maximum amount of address space
available to a single program.

While performing list-resizing operations, there may be a temporary need
for two copies of the list object itself, bumping the memory used up to 3
gigs.

Finally, the speed of some operations (l.index(item), l.pop(0),
l.insert(0, item)) are related linearly to the size of the list, so your
program may slow down as the lists it manipulates grow.  Others, such as
l[i], l.pop(), and l.append(item), are constant-time or amortized-constant-
time.

Jeff


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

Re: Processes and their childs

2004-12-21 Thread Jeff Epler
"flush" your files before forking.

For me, this program gives the correct output 'hello\n' when correct=1.  
When correct=0, I get either 'hello\nhello' or 'hellohello\n' as output.


correct = 0
import sys, os
sys.stdout.write('hello')
if correct: sys.stdout.flush()
if os.fork() == 0: sys.stdout.write('\n')


Jeff


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

Re: character set gobbledy-gook ascii translation ...

2004-12-24 Thread Jeff Epler
>>> email.Header.decode_header("=?us-ascii?Q?Re=3A=20=5Bosg=2Duser=5D=20Culling=20problem?=")
[('Re: [osg-user] Culling problem', 'us-ascii')]
>>> email.Header.decode_header("=?gb2312?B?cXVlc3Rpb24gYWJvdXQgbG9hZGluZyBmbHQgbGFyZ2UgdGVycmFpbiA=?=")
[('question about loading flt large terrain ', 'gb2312')]
>>> help(email.Header.decode_header)
Help on function decode_header:

decode_header(header)
Decode a message header value without converting charset.

Returns a list of (decoded_string, charset) pairs containing each of
the decoded parts of the header.  Charset is None for non-encoded
parts of the header, otherwise a lower-case string containing the
name of the character set specified in the encoded string.

An email.Errors.HeaderParseError may be raised when certain decoding
error occurs (e.g. a base64 decoding exception).

Jeff


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

Re: Clearing the screen

2004-12-24 Thread Jeff Epler
I don't know about idle, but the "real" python supports the
PYTHONSTARTUP environment variable.
   PYTHONSTARTUP
  If  this is the name of a readable file, the Python commands in
  that file are executed before the first prompt is displayed  in
  interactive  mode.  The file is executed in the same name space
  where interactive commands are executed so that objects defined or
  imported  in  it  can be used without qualification in the
  interactive session.  You can also change the  prompts sys.ps1 and
  sys.ps2 in this file.

Jeff


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

Re: Execute code after death of all child processes - (corrected posting)

2004-12-25 Thread Jeff Epler
First, you'll want to exit from each forked copy, or else it will reach
the code-after-the-for-loop:
import sys, os, time
texts = ['this is text1', 'this is text 2']
for current_text in texts[0:]:
pid = os.fork()
if pid == 0:
time.sleep(2)
print current_text
raise SystemExit

Next, you'll want to wait for each process you started:
for current_text in texts:
os.waitpid(-1, 0)
print 'this is the end'
 

$ python /tmp/franz.py 
this is text1
this is text 2
this is the end

Jeff


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

Re: copying classes?

2004-12-29 Thread Jeff Epler
You copied an instance, not a class.

Here's an example of attempting to deepcopy a class:
>>> class X: pass
... 
>>> import copy
>>> X is copy.deepcopy(X)
Traceback (most recent call last):
  File "", line 1, in ?
  File "/usr/lib/python2.2/copy.py", line 179, in deepcopy
raise error, \
copy.Error: un-deep-copyable object of type 

In theory, one could provide a metaclass that allows copying of
instances of that metaclass.  I'll leave this as an exercise to the
reader.

Jeff


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

Re: how can I put a 1Gb file in a zipfile??

2005-03-20 Thread Jeff Epler
The limits of ZIP files according to the folks who make info-zip:
http://www.info-zip.org/pub/infozip/FAQ.html#limits

 statistic limit
number of files65,536
uncompressed size of a single file   4 GB
compressed size of a single file 4 GB
total size of archive  256 TB
maximum path/filename length64 KB 

I had no trouble creating a zip file from a 4GB file filled with '\0'
bytes:
$ python bennie.py
$ ls -ls test.zip big
  12 -rw-rw-r--  1 jepler jepler 4294967296 Mar 20 14:11 big
4084 -rw-rw-r--  1 jepler jepler4174545 Mar 20 14:14 test.zip

I'm using Python 2.3.3 on Fedora Core 2.
#
# bennie.py
def make_4gb_file(f):
f = open(f, "w")
f.seek ( 4 * 1024 * 1024 * 1024 - 1)
f.write("\0")
f.close()

import zipfile
z = zipfile.ZipFile("/tmp/test.zip", "w", zipfile.ZIP_DEFLATED)
make_4gb_file("/tmp/big")
z.write("/tmp/big")
z.close()
#


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

Re: Shell re-direction

2005-03-20 Thread Jeff Epler
buffering.

In the first case, there is either no buffering, or line buffering on
sys.stdout, so you see the lines in order.

In the second case, there is a buffer of a few hundred or thousand bytes
for stdout in the python process, and you see the two lines of python
output together (in this case, when the python process exits and flushes
all its buffers).

You can use the "flush" method on file objects to "clear out" these
buffers.

Jeff


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

Re: spaces in re.compile()

2005-03-21 Thread Jeff Epler
Maybe you want r'\b'.  From 'pydoc sre':
\b   Matches the empty string, but only at the start or end of a word.


import re
r = re.compile( r'\btest\b' )
print r.findall("testy")
print r.findall(" testy ")
print r.findall(" test ")
print r.findall("test")


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

Re: possible bug?

2005-03-22 Thread Jeff Epler
On Tue, Mar 22, 2005 at 07:16:11AM -0700, Earl Eiland wrote:
> I've been having trouble with a program hanging when using the
> subprocess modules "wait()" method.  Replacing it with with a loop that
> used "poll()" solved the problem.

Please include an example, and more information about what platforn you're using
This simple program worked fine for me on Linux with Python 2.4:
#
import subprocess, time

s = subprocess.Popen(['sleep', '2'])
while 1:
time.sleep(.1)
if s.poll() is not None: break
print "polling wait done", s.returncode

s = subprocess.Popen(['sleep', '2'])
s.wait()
print "blocking wait done", s.returncode
#

Jeff


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

Re: possible bug?

2005-03-22 Thread Jeff Epler
I wrote a program to use subprocess.Popen 1 times, and never had
.wait() hang.  If this is a bug, it may be Windows specific.

Here's the program I ran:
#-
import subprocess, signal
def timeout(*args):
print "Timed out waiting on", i
raise SystemExit, 1

signal.signal(signal.SIGALRM, timeout)

for i in xrange(1):
signal.alarm(5)
subprocess.Popen(['/bin/true']).wait()
if i % 100 == 0: print "done with", i
print "done!"
#-

If the wait method ever hangs, the signal handler shuld be invoked.  On
Unix, "/bin/true" is a very simple program that does nothing, so it's
virtually guaranteed to run in less than 5 seconds.  On Windows, maybe
you want something like subprocess.popen('cmd.exe /c rem') as a command
that will do nothing and terminate quickly.  What happens if you run my
program with that change to the Popen line?

Jeff


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

Re: possible bug?

2005-03-22 Thread Jeff Epler
hm, I guess SIGALRM doesn't exist on Windows.  You can run the program
without the 'signal.signal' line or the 'signal.alarm' line, and you'll
be stuck with a hung Python if subprocess.Popen exhibits the bug.

Jeff


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

Re: possible bug?

2005-03-22 Thread Jeff Epler
On Tue, Mar 22, 2005 at 02:19:52PM -0700, Earl Eiland wrote:
> Well, your program ran successfully.  Perhaps WinRK is not well
> behaved.  How can a process terminate in such a way that poll() can read
> it, but wait() won't?

I don't have any idea.  Both are implemented in terms of
win32event.WaitForSingleObject (which is itself a thin wrapper over the
win32 API of the same name[1]), one with a zero timeout and one with an
INFINITE timeout.  

This caveat is in the msdn page on WFSO:
Use caution when calling the wait functions and code that directly
or indirectly creates windows. If a thread creates any windows, it
must process messages. Message broadcasts are sent to all windows in
the system. A thread that uses a wait function with no time-out
interval may cause the system to become deadlocked. Two examples of
code that indirectly creates windows are DDE and the CoInitialize
function. Therefore, if you have a thread that creates windows, use
MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather
than WaitForSingleObject.
... which is pretty much greek to this unix geek.  If your Python
program has a GUI, can you strip out the gui-related parts to make it a
pure commandline program, and see if you still have the problem?

You could try something like (untested, obviously)
import subprocess
if subprocess.mswindows:
from win32event import WaitForSingleObject
from win32process import GetExitCodeProcess
def wait(self):
"""Wait for child process to terminate.  Returns returncode
attribute."""
while self.returncode is None:
obj = WaitForSingleObject(self._handle, 1000)
self.returncode = GetExitCodeProcess(self._handle)
_active.remove(self)
return self.returncode
subprocess.Popen.wait = wait
to make the .wait() method call WFSO with a timeout, and then maybe you
can simply forget about the weird behavior you ran into.

Jeff
[1] http://msdn.microsoft.com/library/en-us/dllproc/base/waitforsingleobject.asp


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

Re: Python RegExp

2005-03-22 Thread Jeff Epler
On my machine the program finishes in 30 seconds. (it's a 1.5GHz machine)
If the 'parm' group is removed, or if the buffer is shortened, the time
is reduced considerably.

There are "pathological cases" for regular expressions which can take
quite a long time.  In the case of your expression, it's happening for
the group 'parm'.  I think, but don't know, that each time a candidate
for 'parm' is found, the following '#' (or maybe the second '<'?) is not
found, and it backtracks to try to match 'parm' in a different way,
which involves considering many different combinations (basically, each
'name=' could be the start of a new instance of the first parenthsized
subgroup of , or it could be part of the character class that
includes [a-zA-Z=])

You may wish to consider using other approaches for parsing this text
than regular expressions.

Jeff


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

Re: How to get TabError?

2005-03-27 Thread Jeff Epler
When running with "-tt", you can get this error.

[EMAIL PROTECTED] src]$ python -tt
Python 2.3.3 (#1, May  7 2004, 10:31:40) 
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exec "def f():\n\ta\nb"
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 3
b
^
TabError: inconsistent use of tabs and spaces in indentation



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

Re: [Tkinter] LONG POST ALERT: Setting application icon on Linux

2005-03-27 Thread Jeff Epler
Here is a short program that sets Tk's window icon on Linux.  My window
manager is icewm, and it uses a scaled version of the "flagup" image
both at the upper-left corner of the window and on the task bar entry
for the window.

import Tkinter
app = Tkinter.Tk()
app.iconbitmap("@/usr/X11R6/include/X11/bitmaps/flagup")
app.mainloop()

As often happens, the Tkinter documentation doesn't tell the whole
story---you have to dig into the Tk documentation.  I started with "man
n wm", and read the following:
If  bitmap is specified, then it names a bitmap in the standard
forms accepted by Tk (see the  Tk_GetBitmap  manual  entry for
details).
OK, on to Tk_GetBitmap... 
@fileName FileName  must  be the name of a file containing a
  bitmap description in the standard X11 or X10 format.
and I happened to know that some bitmaps in this format exist in the
directory I mentioned above.  Note that the "standard X11 format" is
monochrome, so you will not be able to use color images with
"iconbitmap" on Linux.  Tk doesn't support _NET_WM_ICON for setting
full-color icons.

Jeff


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

Re: [Tkinter] LONG POST ALERT: Setting application icon on Linux

2005-03-30 Thread Jeff Epler
I have written a rather hackish extension to use NET_WM_ICON to set
full-color icons in Tkinter apps.  You can read about it here:
http://craie.unpy.net/aether/index.cgi/software/01112237744
you'll probably need to take a look at the EWMH spec, too.  If KDE
supports NET_WM_ICON, this may work for you (but you'll have to convert
your image manually to the format required for NET_WM_ICON)

Best of luck!  Unfortunately, the code is not supported.

Jeff


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

Re: Our Luxurious, Rubinesque, Python 2.4

2005-03-31 Thread Jeff Epler
In my experience, when built with the same compiler (gcc 3.3.3) the size
of the python library file (libpython2.x.a on unix machines) hasn't
changed much between 2.3, 2.4, and current CVS:
-rw-r--r--  1 jepler jepler  950426 Mar 31 21:37 libpython2.3.a
-rw-rw-r--  1 jepler jepler 1002158 Mar 31 21:36 libpython2.4.a
-rw-rw-r--  1 jepler jepler 1001982 Mar 31 21:36 libpython2.5.a

Between Python 2.3 and 2.4, the python.org people switched to a
different version of the Microsoft C compiler.  Perhaps this is (part
of) the explanation.

Jeff


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

Re: Spider - path conflict [../test.htm,www.nic.nl/index.html]

2005-04-01 Thread Jeff Epler
I think you want urllib.basejoin().

>>> urllib.basejoin("http://www.example.com/test/page.html";, "otherpage.html")
'http://www.example.com/test/otherpage.html'


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

Re: Corectly convert from %PATH%=c:\\X; "c:\\a; b" TO ['c:\\X', 'c:\\a; b']

2005-04-03 Thread Jeff Epler
if your goal is to search for files on a windows-style path environment
variable, maybe you don't want to take this approach, but instead wrap
and use the _wsearchenv or _searchenv C library functions

http://msdn.microsoft.com/library/en-us/vclib/html/_crt__searchenv.2c_._wsearchenv.asp

Incidentally, I peeked at the implementation of _searchenv in wine (an
implementation of the win32 API for Unix), and it doesn't do the
quote-processing that you say Windows does.  The msdn page doesn't give
the syntax for the variable either, which is pretty typical.  Do you
have an "official" page that discusses the syntax?

Jeff


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

Re: "specialdict" module

2005-04-03 Thread Jeff Epler
The software you used to post this message wrapped some of the lines of
code.  For example:
> def __delitem__(self, key):
> super(keytransformdict, self).__delitem__(self,
> self._transformer(key))

In defaultdict, I wonder whether everything should be viewed as a
factory:
def setdefaultvalue(self, value):
def factory(): return value
self.setdefaultfactory(factory)

and the "no-default" mode would either cease to exist, or 
def cleardefault(self):
def factory(): 
raise KeyError, "key does not exist and no default defined"
self.setdefaultfactory(factory)
(too bad that the key isn't available in the factory, this degrades the
quality of the error messge)

if so, __getitem__ becomes simpler:
__slots__ = ['_default']
def __getitem__(self, key):
  try:
  return super(defaultdict, self).__getitem__(key)
  except KeyError:
  return self.setdefault(key, apply(*self._default))

I don't ever have an itch for sorted dictionaries, as far as I can
remember, and I don't immediately understand the use of
keytransformdict.  Can you give an example of it?

Jeff


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

Re: Corectly convert from %PATH%=c:\\X; "c:\\a; b" TO ['c:\\X', 'c:\\a; b']

2005-04-03 Thread Jeff Epler
The C code that Python uses to find the initial value of sys.path based
on PYTHONPATH seems to be simple splitting on the equivalent of
os.pathsep.  See the source file Python/sysmodule.c, function
makepathobject().
for (i = 0; ; i++) {
p = strchr(path, delim); // ";" on windows, ":" on unix
if (p == NULL) ...
w = PyString_FromStringAndSize(path, (int) (p - path));
if (w == NULL) ...
PyList_SetItem(v, i, w);
if (*p == '\0')
break;
path = p+1;
}
No special handling of quote characters happens here.

> I think I will stick to a simple splitting at the ;. Luckily all the
> directories I am dealing with have nice names.

If you do this, you'll match the behavior of python itself, and you'll match
the behavior of wine.

> I have not even tried to see what quirks there exist with unix.

None.  There's no way to quote anything in paths, so while you can't place a
directory with a colon in its name on your path, nobody loses any sleep over
it either.  Here's what the Open Group has to say about PATH:
PATH
This variable shall represent the sequence of path prefixes that
certain functions and utilities apply in searching for an executable
file known only by a filename. The prefixes shall be separated by a
colon ( ':' ). When a non-zero-length prefix is applied to this
filename, a slash shall be inserted between the prefix and the
filename. A zero-length prefix is a legacy feature that indicates the
current working directory. It appears as two adjacent colons ( "::" ),
as an initial colon preceding the rest of the list, or as a trailing
colon following the rest of the list. A strictly conforming application
shall use an actual pathname (such as .) to represent the current
working directory in PATH . The list shall be searched from beginning
to end, applying the filename to each prefix, until an executable file
with the specified name and appropriate execution permissions is found.
If the pathname being sought contains a slash, the search through the
path prefixes shall not be performed. If the pathname begins with a
slash, the specified path is resolved (see Pathname Resolution). If
PATH is unset or is set to null, the path search is
implementation-defined.
ah, if only windows was so well-defined!

Jeff


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

Re: Silly question re: 'for i in sys.stdin'?

2005-04-03 Thread Jeff Epler
The iterator for files is a little bit like this generator function:
def lines(f):
while 1:
chunk = f.readlines(sizehint)
for line in chunk: yield line
Inside file.readlines, the read from the tty will block until sizehint
bytes have been read or EOF is seen.

If you want this kind of line-at-a-time functionality, then you could
use the iter(callable, sentinel) form, and switch between it and the
readlines method based on a commandline flag or whether the file
satisfies 'os.isatty()':
def lines(f):  # untested
"""lines(f)
If f is a terminal, then return an iterator that gives a value after
each line is entered.  Otherwise, return the efficient iterator for
files."""
if hasattr(f, "fileno") and isatty(f.fileno()):
return iter(f.readline, '')
return iter(f)

for line in lines(sys.stdin):
doSomethingWith(line)

Jeff


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

Re: Silly question re: 'for i in sys.stdin'?

2005-04-04 Thread Jeff Epler
On Sun, Apr 03, 2005 at 09:49:42PM -0600, Steven Bethard wrote:
> Slick.  Thanks!

does isatty() actually work on windows?  I'm a tiny bit surprised!

Jeff


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

Re: Tkinter - pixel or widget color

2005-04-04 Thread Jeff Epler
On Mon, Apr 04, 2005 at 10:43:11AM +0200, pavel.kosina wrote:
> I would need to get at canvas pixel color under certain moving widget or 
> better (= faster?) colors/"types" of underlying static widgets that are 
> of polygon shape (not rectangle).

I don't believe this information is available anywhere, unfortunately.

Jeff


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

  1   2   >