Re: Time Complexity of String Operations

2008-07-21 Thread David Wahler
On Mon, Jul 21, 2008 at 10:31 PM, youtoo <[EMAIL PROTECTED]> wrote:
> It has been extensively discussed the time complexity (quadratic) of
> string concatenation (due to string's immutability).

Actually, it is roughly linear, at least for reasonable string lengths:

$ python -V
Python 2.5.2
$ python -mtimeit -s "n=1000; a='#'*n" "a+a"
100 loops, best of 3: 1 usec per loop
$ python -mtimeit -s "n=1; a='#'*n" "a+a"
10 loops, best of 3: 5.88 usec per loop
$ python -mtimeit -s "n=10; a='#'*n" "a+a"
1 loops, best of 3: 59.8 usec per loop

Repeatedly constructing a string by appending a constant number of
characters at a time, however, is quadratic in the final string length
(although VM optimizations may affect this).

> But what is:
>
> == the time complexity of string indexing? Is it constant?

Yes.

> == the time complexity of string slicing? Is it O(K) with K the
> slice's length?

I suspect so, since the time is dominated by the time taken to copy
the data into a new string object.

> How are strings stored in Python? As arrays? As linked lists?

Arrays; see Include/stringobject.h in the Python source distribution.

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


Re: tail-rec decorator, well still blows the stack...

2008-07-21 Thread David Wahler
On Mon, Jul 21, 2008 at 10:01 PM, ssecorp <[EMAIL PROTECTED]> wrote:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
>
> so I try it and when I run:
> @Decorators.tail_recursion
> def fibtr(n):
>def fibt(a, b, n):
>if n <= 1:
>return b
>else:
>return fibt(b, a + b, n - 1)
>if n == 0:
>return 0
>else:
>return fibt(0, 1, n);
>
> it still blows the stack. so what is the point? is it impossible to
> get "real" tail-recursion in Python?

Python does not perform tail-call elimination, and there are currently
no plans to make it do so. See
http://mail.python.org/pipermail/python-dev/2004-July/046171.html and
the ensuing discussion for an explanation.
--
http://mail.python.org/mailman/listinfo/python-list


Re: sort(cmp=func)

2008-07-09 Thread David Wahler
On Wed, Jul 9, 2008 at 10:58 PM, Daniel Fetchinson
<[EMAIL PROTECTED]> wrote:
>
> > I have a list of objects that generate code.  Some
> > of them depend on others being listed first, to
> > satisfy dependencies of others.
> >
> > I wrote a cmp function something like this:
> >
> > def dep_cmp(ob1, ob2):
> >
> >   if ob1.name in ob2.deps:
> >   return -1
> >   else:
> >   return 1
> >
> > I also tried returning 0 when there were no dependency
> > relationships between the objects.
> >
> > This failed, because every object was not compared with
> > ever other.  I imagine that this is because sort assumes
> > that if a > b and b > c, then a > c.  In my case, this
> > isn't really true.  If a is not dependent on b, and
> > b is not dependent on c, that doesn't mean that a is not
> > dependent on c.
> >
> > Is there a better way?
>
> It's only meaningful to talk about sorting if your particular
> definition of "<" is transitive. I.e. a < b and b < c should imply a <
> c. If this is not satisfied, it doesn't make sense to sort according
> to your "<" definition. It's just not a well-defined operation and no
> trick will make it work.
>

Presumably what the OP really wants is to sort using the transitive
closure of dep_cmp, which is a topological sort.

http://en.wikipedia.org/wiki/Topological_sorting has details and a
linear-time algorithm.

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


Re: ctypes, GetWindowLongPtr

2008-01-11 Thread David Wahler
On Jan 11, 2008 9:14 PM, Henry Baxter <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I have been happily using ctypes for a while to do win32 programming. I use 
> the Microsoft documentation to understand the function, then call it with the 
> help of ctypes.
>
> The problem is that the docs says user32.dll has GetWindowLongPtr, but ctypes 
> can't find it using windll.user32.GetWindowLongPtrA or 
> windll.user32.GetWindowLongPtrW or windll.user32.GetWindowLongPtr. Errors 
> look like this:
>
> Traceback (most recent call last):
>   File "Z:\experiments\windowsapp3.py", line 106, in 
> GetWindowLongPtr = windll.user32.GetWindowLongPtrA
>   File "C:\Python25\lib\ctypes\__init__.py", line 353, in __getattr__
> func = self.__getitem__(name)
>   File "C:\Python25\lib\ctypes\__init__.py", line 358, in __getitem__
> func = self._FuncPtr((name_or_ordinal, self))
> AttributeError: function 'GetWindowLongPtrA' not found
>
> I have the same problem with the SetWindowLongPtr function.
>
> I can use plenty of other functions (GetParent, CreateWindowExA, 
> DefWindowProcA, and etc) but not these ones.
>
>
> I don't understand what goes on with ctypes under the hood really, so my 
> troubleshooting abilities at this point are quite lacking! I would appreciate 
> any help you could offer.
>
>
> Thanks!

I don't have a copy of the official Win32 headers handy, but the MinGW
version of winuser.h contains this section of code:

WINUSERAPI LONG WINAPI GetWindowLongA(HWND,int);
WINUSERAPI LONG WINAPI GetWindowLongW(HWND,int);
#ifdef _WIN64
WINUSERAPI LONG_PTR WINAPI GetWindowLongPtrA(HWND,int);
WINUSERAPI LONG_PTR WINAPI GetWindowLongPtrW(HWND,int);
#else
#define GetWindowLongPtrA GetWindowLongA
#define GetWindowLongPtrW GetWindowLongW
#endif

which I would take to mean you need to use GetWindowLong on 32-bit
windows, and GetWindowLongPtr on 64-bit windows.

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


Re: encrypting files + filestreams?

2007-08-15 Thread David Wahler
On 8/15/07, per9000 <[EMAIL PROTECTED]> wrote:
> Hi python people,
>
> I am trying to figure out the best way to encrypt files in python.
>
> I've build a small script (see below) that encrypts the ubuntu 7.04
> iso file in 2 minutes (I like python :) ).
>
> But I have some thoughts about it. By pure luck (?) this file happened
> to be N*512 bytes long so I do not have to add crap at the end - but
> on files of the size N*512 + M (M != 521) I will add some crap to make
> it fit in the algorithm. When I later decrypt I will have the stuff I
> do not want. How do people solve this? (By writing the number of
> relevant bytes in readable text in the beginning of the file?)

The term you're looking for is "padding". See
http://en.wikipedia.org/wiki/Padding_%28cryptography%29 for a brief
introduction, and especially the two RFCs mentioned about halfway
down.

> Also I wonder if this can be solved with filestreams (Are there
> streams in python? The only python file streams I found in the evil
> search engine was stuff in other forums.)

I don't know how to answer this, because it's not clear what you mean
by "file streams". Python's file objects act similarly to things
called streams in other languages, such as Java's InputStreams and
Readers, if that's what you're asking.

> Other comments are of course also welcome,
> Per
>
>
> # crypto_hardcoded.py starts here
[snip]

I notice there's some repeated code in your main loop. This generally
means there's room for improvement in your program flow. Here's one
possible way you could structure it: separate out the file reading and
padding logic into a generator function that takes a filename or file
object, and yields blocks one at a time, padded to the correct block
size. Then your main loop can be simplified to something like this:

for plaintext_block in read_as_blocks(in_file, block_size):
ciphertext_block = cryptor.encrypt(plaintext_block)
out_file.write(ciphertext_block)

Techniques like these can make it easier to follow what's going on,
especially as your programs get more complicated. Don't be afraid to
experiment!

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


Re: Something in the function tutorial confused me.

2007-08-11 Thread David Wahler
On 8/11/07, Gregory D. Weber <[EMAIL PROTECTED]> wrote:
> I too thought += was an assignment.  And it bit me very painfully a few weeks 
> ago.
>
> If it's an assignment, then wouldn't "x += y" mean the same thing as "x = x + 
> y"?
>
> If so, why does an assignment to variable a, below, have the *side effect* of 
> changing variable b ... ?
>
> >>> a = [1, 2, 3]
> >>> b = a
> >>> b
> [1, 2, 3]
> >>> a += [4]
> >>> a
> [1, 2, 3, 4]
> >>> b
> [1, 2, 3, 4]
>
> ... but using the "x = x + y" style, the assignment to variable c, below, 
> does *not* have a side effect on variable d (as indeed it should not!)?
>
> >>> c = [1, 2, 3]
> >>> d = c
> >>> d
> [1, 2, 3]
> >>> c = c + [4]
> >>> c
> [1, 2, 3, 4]
> >>> d
> [1, 2, 3]

>>> help(list.__iadd__)
Help on wrapper_descriptor:

__iadd__(...)
x.__iadd__(y) <==> x+=y

For mutable (built-in) objects such as lists, __iadd__ modifies the
list and then returns the list object; for immutable objects, __iadd__
is equivalent to __add__ and just returns the new value. However, in
both cases, the result is then rebound to x. This can lead to some
behaviors you might not normally expect:

>>> t = ([],)
>>> t[0] += [1]
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment
>>> t
([1],)

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


Re: Something in the function tutorial confused me.

2007-08-07 Thread David Wahler
On 8/7/07, Ricardo Aráoz <[EMAIL PROTECTED]> wrote:
> Lee Fleming wrote:
> > Thanks for all the help, everyone. I guess I was confused with default
> > arguments that were mutable and immutable. I will continue to look
> > over these posts until I understand what is happening.
> >
> > I cannot believe the number of helpful responses I got!
> >
>
> Apparently he didn't understand.
> Neither did I.
>
> Either (i)y's initial value (None or []) is saved somewhere to be
> retrieved every time the function is called without 2nd argument, or
> (ii) y keeps the value it has when last exiting the function (if there
> is a third option, please mention it).
>

It's option (i).

> (i) (a) whenever the function is called without 2nd argument the value
> None is retrieved and assigned to y, thus causing [] to be assigned to y
> by the 'if' statement.

Yep.

> (i) (b) But then if it is "def f(x, y = [])" the list [] should be ALSO
> saved somewhere and when the function is called without 2nd argument it
> should be retrieved and assigned to y, thus y would always be [] when
> you enter the function without 2nd arg.

No, when the def statement is executed, the expression [] is
evaluated, and the result of this evaluation is an empty list. It's a
reference to this list that is saved, not the expression itself. All
invocations of the function that use the default argument refer to the
same list, so if it gets modified the modifications persist.

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

Re: Can I change one line in a file without rewriting the whole thing?

2007-07-13 Thread David Wahler
On 7/13/07, J. J. Ramsey <[EMAIL PROTECTED]> wrote:
> In Perl, there is a module called "Tie::File". What it does is tie a
> list to each line of a file. Change the list, and the file is
> automatically changed, and on top of this, only the bits of the file
> that need to be changed are written to disk. At least, that's the
> general idea.
>
> I was wondering if something roughly similar could be done in Python,
> or at the very least, if I can avoid doing what amounts to reading the
> whole file into memory, changing the copy in memory, and writing it
> all out again.

The mechanism behind Perl's ties -- an array that, when read from or
written to, passes control to a user function -- is easy to implement
in Python. See the documentation of the mapping protocol
:

>>> class SpecialList(object):
... def __getitem__(self, index):
...return "Line %d" % index
>>> foo = SpecialList()
>>> foo[42]
'Line 42'

>From the documentation for Tie::File, it doesn't look like a trivial
piece of code; for example, it has to maintain a table in memory
containing the offset of each newline character it's seen for fast
seeking, and it has to handle moving large chunks of the file if the
length of a line changes. All this could be implemented in Python, but
I don't know of a ready-made version off the top of my head.

If all you want is to read the file line-by-line without having the
whole thing in memory at once, you can do "
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with using ConfigParser.py

2007-06-21 Thread David Wahler
On 6/22/07, shridhar kurhade <[EMAIL PROTECTED]> wrote:
> Hi,
> Thanks for your reply. I tried changing the ownership and it looks as below:
> #  ls -l /home/ast/ast-linux.conf
> -rw-r--r-- 1 ast ast 7936 Jun 21 11:11 /home/ast/ast-linux.conf
>
> But when I try to read through browser, it gives permission denied error:
>
> [Errno 13] Permission denied: '/home/ast/ast-linux.conf' targetsandbox
>
> Also other files in the /home/ast/ have ast:ast as ownership.
> Can you please tell me if there any other problem with this?

That looks OK, but as I said before, you need to check the permissions
on the /home/ast directory. What's the output of "ls -ld /home/ast"?
It needs to grant execute permissions to the user or group that the
script is running as.

If that looks OK, you might want to try calling os.getuid() and
os.getgid() from your script, to make sure it's running under the same
UID/GID you think it is.

-- David

P.S. When using Gmail, it's better to use "Reply to All" instead of
"Reply"; that way your messages continue to go to the mailing list,
and other people can help you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with using ConfigParser.py

2007-06-21 Thread David Wahler
On 6/21/07, Anthony Raj <[EMAIL PROTECTED]> wrote:
> Hi Folks,
>
> Having a very strange problem in using python with apache/mod_python.
>
> [conf.py attached]
>
> >>> from ast.conf import conf
>  >>> c = conf()
>  >>> a = c.readPath('sandbox')
>  >>>  len (a)
>
> >>>  30
>
> The same code when run thru the a web script gives following error -
>
> [Errno 13] Permission denied: '/home/ast/ast-linux.conf'
[snip]
> The permission for the ast-linux.conf file is the same as the user/group
> defined in httpd.conf.
>
> [EMAIL PROTECTED] ast-sandbox]# ls -l /home/ast/ast-linux.conf
> -rw-rw-rw- 1 777 ast 7936 Jun 21 11:11 /home/ast/ast-linux.conf

What are the permissions on /home/ast?

Also, you seem to have done "chown 777 ast-linux.conf" at some point,
which sets the owning UID of the file to 777, which doesn't exist as a
user on your system. Because the file permissions are read/write for
everybody it shouldn't matter, but it probably wasn't what you
intended.

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


Re: need help with re module

2007-06-20 Thread David Wahler
On 6/20/07, Gabriel Genellina <[EMAIL PROTECTED]> wrote:
> En Wed, 20 Jun 2007 13:58:34 -0300, linuxprog <[EMAIL PROTECTED]>
> escribió:
>
> > i have that string "helloworldok" and i want to
> > extract all the text , without html tags , the result should be some
> > thing like that : helloworldok
> >
> > i have tried that :
> >
> > from re import findall
> >
> > chaine = """helloworldok"""
> >
> > print findall('[a-zA-z][^(<.*>)].+?[a-zA-Z]',chaine)
> >   >>> ['html', 'hell', 'worl', 'anyt', 'ag>o']
> >
> > the result is not correct ! what would be the correct regex to use ?
>
> You can't use a regular expression for this task (no matter how
> complicated you write it).
[snip]

I agree that BeautifulSoup is probably the best tool for the job, but
this doesn't sound right to me. Since the OP doesn't care about tags
being properly nested, I don't see why a regex (albeit a tricky one)
wouldn't work. For example:

regex = re.compile(r'''
<[^!] # beginning of normal tag
([^'">]*# unquoted text...
|'[^']*'# or single-quoted text...
|"[^"]*")*  # or double-quoted text
> # end of tag
   |

Re: How to hide a program?

2007-06-20 Thread David Wahler
On 6/20/07, jvdb <[EMAIL PROTECTED]> wrote:
> The thing is, i don't want to see anything of my program, just the
> launched program.
> I already have the program working. But when i create an executable of
> it with py2exe and start it, i don't want to see that it is running,
> perhaps just in the systemtray. That is my problem.

Try renaming your main script to have a .pyw extension as Diez said,
or use the --windows option when running py2exe.

http://mail.python.org/pipermail/python-list/2003-December/241319.html

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


Re: avoid script running twice

2007-06-18 Thread David Wahler
On 6/18/07, Robin Becker <[EMAIL PROTECTED]> wrote:
> Evan Klitzke wrote:
> > Another method that you can use is to open up a socket on some
> > predetermined port (presumably above 1024), and then have your program
> > try to connect to that port and "talk" to the other program to
> > determine whether or not to run (or whether to do some of the
> > remaining work, etc.).
> >
>
> that might work, but this runs on  someone's java solaris box with possible 
> many
> connections going on. I doubt I'd be able to guarantee a particular port.

On platforms where it's supported -- I assume this includes Solaris --
you can use a UNIX-domain socket to avoid the risk of clashing with
network connections. It would be relatively straightforward to fall
back to a normal IP socket on Windows; just test for the existence of
socket.AF_UNIX.

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


Re: sqlite3 bug??

2007-06-17 Thread David Wahler
On 6/17/07, mark carter <[EMAIL PROTECTED]> wrote:
> I hesitate to ask, but ...
>
> I'm using Ubuntu Feisty:
> * Python 2.5.1 (r251:54863, May  2 2007, 16:56:35)
> [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
> * SQLite version 3.3.13
>
> Suppose I run the following program:
> import sqlite3
>
> conn = sqlite3.connect('example')
>
>
> c = conn.cursor()
>
> # Create table
> c.execute('''create table stocks
> (date text, trans text, symbol text,
>   qty real, price real)''')
>
> # Insert a row of data
> c.execute("""insert into stocks
>values ('2006-01-05','BUY','RHAT',100,35.14)""")
>
> and then I go into sqlite:
> % sqlite3 example
> sqlite3> select * from stocks ;
>
> It returns 0 rows. I'm in the right directory. I have experienced this
> problem with some other sqlite3 database work I have done with python,
> so I'm figuring there is something fishy going on. I've tried doing
> similar exercises with Ruby, and they have worked OK.
>
> Anyone else getting these problems?

See http://www.python.org/dev/peps/pep-0249/ (emphasis mine):

.commit()

Commit any pending transaction to the database. *Note that
if the database supports an auto-commit feature, this must
be initially off.* An interface method may be provided to
turn it back on.

(This really should be a FAQ...)

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


Re: Function that returns a tuple

2007-06-17 Thread David Wahler
On 6/17/07, Marcpp <[EMAIL PROTECTED]> wrote:
> On 17 jun, 03:53, Dan Hipschman <[EMAIL PROTECTED]> wrote:
> Hi, I need to return a tupla like this function:
>
> def BDllids(a):
> a = ()
> conn = sqlite.connect('tasques.db')
> cursor =  conn.cursor()
> cursor.execute('SELECT * FROM tasques')
> for row in cursor:
> a.append (row[0])
> return a()
>
> I'm doing the correct, method?

Tuples are immutable (can't be modified once created). Try this:

def BDllids():
conn = sqlite.connect('tasques.db')
cursor =  conn.cursor()
cursor.execute('SELECT * FROM tasques')
a = [row[0] for row in cursor]
return tuple(a)

Is there some particular reason you need to return a tuple as opposed to a list?

P.S. Accessing fields of a SELECT * by numeric index is prone to
breakage if the order of your fields changes. You can make your code
more robust by either specifying the column name explicitly in the
SELECT statement. Alternatively, you may be interested in the
sqlite.Row object (see the documentation for details).

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


Re:

2007-06-15 Thread David Wahler
On 6/15/07, Wiley Thomas <[EMAIL PROTECTED]> wrote:
> I'm trying to write a script to open a file on our (windows) network.  The
> file is located on a machine that is not part of the domain and requires a
> separate user id and password to access.  I tried using urllib2 and the
> password_manager to authenticate but as some point urllib2 sees I'm trying
> to access a local file and passes it off to os.  The error message I get is:
>
> "WindowsError: [Error 1326] Logon failure: unknown user name or bad
> password: 
> Does anyone know of a better approach.

Urllib2 is used for making HTTP/FTP requests. When you open a file on
a network share, the networking is handled by Windows; as far as
Python is concerned, it's treated the same as a local file.

You could try something like this (untested):

os.system(r"NET USE \\computer\share password /USER:username")
# do something with file
os.system(r"NET USE \\computer\share /DELETE")

That should authenticate and de-authenticate you properly, but it
seems like a fragile way of doing it -- for one thing, I think it
would grant access to any other program running under your login
session. There may be a better way of doing this using the Windows
API. Alternatively, depending on your application, you might want to
consider using a simple web or FTP server.

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


Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread David Wahler
On Jun 2, 3:00 pm, Peter Otten <[EMAIL PROTECTED]> wrote:

> Then you have modified the code posted by Steven Bethard.
>
> > I don't see how your behaviour should come about ... a new observer-list
> > is created for every decorated method, so there is no problem.
>
> Yes, but that list is shared across instances of SomeActor.
>
> > Now I'm confused ?-|
>
> You start with one Observable per method:
>
> >>> SomeActor.meth
>
> >>> SomeActor.meth is SomeActor.meth
>
> True
>
> This Observable knows nothing about a SomeActor instance (because there is
> none).
>
> >>> SomeActor.meth.instance is None
>
> True
>
> Everytime you access meth from a SomeActor instance the __get__() method is
> invoked and a new Observable is created, this time with an instance:
>
> >>> SomeActor().meth is SomeActor.meth
> False
> >>> SomeActor().meth.instance
>
> 
>
> But all of these Observables share the same observables list
>
> >>> SomeActor().meth.observers is SomeActor.meth.observers
> True
> >>> SomeActor().meth.observers is SomeActor().meth.observers
>
> True
>
> If you want per-instance callbacks you have to store the observers list (or
> the bound method) in the instance:
>
> >>> class SomeActor(object):
>
> ... def __init__(self):
> ... self.meth = Observable(self.meth, self)
> ... def meth(self, foo): print foo
> ...>>> a = SomeActor()
> >>> b = SomeActor()
> >>> def make_callback(s):
>
> ... def f(instance):
> ... print s, instance
> ... return f
> ...>>> a.meth.add_callback(make_callback("alpha"))
> >>> b.meth.add_callback(make_callback("beta"))
> >>> a.meth(1)
>
> 1
> alpha <__main__.SomeActor object at 0x401d5c6c b.meth(2)
>
> 2
> beta <__main__.SomeActor object at 0x401d5ccc>
>
> Note that with this approach Observable need not be a descriptor; I was just
> too lazy to rewrite it.

Here's my attempt at an implementation that works as a decorator. It
stores the observers in a dictionary attribute of the instance, using
the method name as a key. For example:

>>> a = SomeActor()
>>> a.meth.add_callback(callback)
>>> a._observers
{'meth': []}
>>> a.meth.observers is a._observers['meth']
True

##

class Observable(object):
def __init__(self, func, instance=None):
self.func = func
self.instance = instance
if instance is not None and not hasattr(self.instance,
'_observers'):
self.instance._observers = {}

def __get__(self, obj, cls=None):
if obj is None:
return self
else:
func = self.func.__get__(obj, cls)
return Observable(func, obj)

def __call__(self, *args, **kwargs):
result = self.func(*args, **kwargs)
for observer in self.observers:
observer(self.instance)
return result

@property
def observers(self):
func_name = self.func.im_func.func_name
return self.instance._observers.setdefault(func_name,[])

def add_callback(self, callback):
self.observers.append(callback)

##

This implementation has a few drawbacks that I can see:

  * It doesn't allow for the syntax SomeActor.meth(instance, *args).
I haven't spent the time to make it work on unbound as well as
bound methods.

  * It may not play well with inheritance. If the subclass overrides
an Observable method of the superclass, and declares its override
Observable as well, the callback will be called twice. I'm not
sure exactly how to handle this.

  * Perhaps it would be better to use the function object itself as
the dictionary key, rather than its name?

Anyway, these are just my initial thoughts -- I don't have the time to
really think this through thoroughly.

-- David

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


Re: Observer-Pattern by (simple) decorator

2007-06-02 Thread David Wahler
On Jun 2, 12:27 am, Steven Bethard <[EMAIL PROTECTED]> wrote:
> I think you want to define __get__ on your Observable class so that it
> can do the right thing when the method is bound to the instance:
>
>  >>> class Observable(object):
> ... def __init__(self, func, instance=None, observers=None):
> ... if observers is None:
> ... observers = []
> ... self.func = func
> ... self.instance = instance
> ... self.observers = observers
> ... def __get__(self, obj, cls=None):
> ... if obj is None:
> ... return self
> ... else:
> ... func = self.func.__get__(obj, cls)
> ... return Observable(func, obj, self.observers)
> ... def __call__(self, *args, **kwargs):
> ... result = self.func(*args, **kwargs)
> ... for observer in self.observers:
> ... observer(self.instance)
> ... return result
> ... def add_callback(self, callback):
> ... self.observers.append(callback)
> ...
>  >>> class SomeActor(object):
> ... @Observable
> ... def meth(self, foo):
> ... print foo
> ...
>  >>> def callback(instance):
> ... print "Yippie, I've been called on", instance
> ... instance.bar = True
> ...

Is this desired behavior?

>>> a = SomeActor()
>>> b = SomeActor()
>>> a.meth.observers is b.meth.observers
True
>>> a.meth.add_callback(callback)
>>> b.meth(42)
42
Yippie, I've been called on <__main__.SomeActor object at 0x00C23550>

-- David

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


Re: Cookie: Not understanding again

2007-06-02 Thread David Wahler
On Jun 2, 9:32 am, mosscliffe <[EMAIL PROTECTED]> wrote:
> David,
>
> Thanks for your help.
>
> I spent all last night trying to get it to work, but I can not work
> out how to force my cookie into the response header.  The most
> annoying part is on one occasion it did and the cookie sat there until
> I killed the session.  By the time I discovered 'pageinfo' add-in for
> firefox, the cookie had gone.

Your CGI script needs to produce output of the form:

Header-1: ...
Header-2: ...

response data goes here...

Each line before the blank line is an HTTP header. To send cookies to
the browser, you need to generate a header of the form:

Set-Cookie: name=value

Printing the SimpleCookie object does this for you.

> I have read a lot about cookies, but the grey matter is very slow at
> absorbing nowadays.  I was wondering if I should add the 'path=/' to
> my cookie, as I am using a host that has numbers for the IP address
> and my domain name when used, is via a frame.  Although the python bit
> to display the contents (when it did work), did not show any value for
> 'path'.

If you specify a path attribute, then the browser will only send the
cookie on requests that begin with that path. If you don't, it
defaults to the URL of the page that generates the cookie. For a
single test script like this, there's no need to bother with it, but
you can set it like this:

>>> cookie["someCookieName"]["path"] = "/path/to/whatever"

> Could I create the response header in python and get it executed as
> part of my form submission ?
>
> Obviously, still struggling.

Here's a simple, complete example to get you started, based loosely on
your original code:

##

import Cookie, os
import cgitb; cgitb.enable()

def getCookie():
c = Cookie.SimpleCookie()
if 'HTTP_COOKIE' in os.environ:
c.load(os.environ['HTTP_COOKIE'])

if os.environ['QUERY_STRING'] == 'reset' or 'mysession' not in c:
c['mysession'] = 0
else:
c['mysession'] = int(c['mysession'].value) + 1

return c

if __name__ == '__main__':

print "Content-type: text/html"
myCookie = getCookie()
print myCookie

if os.environ['QUERY_STRING'] == 'reset':
print "Status: 302 Moved"
print "Location:", os.environ['SCRIPT_NAME']
print
else:
print
print 'Current Value: ', myCookie['mysession'].value
print 'Reset Counter'

##

Hope this helps!

-- David

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


Re: Cookie: Not understanding again

2007-06-01 Thread David Wahler
On Jun 1, 3:49 pm, mosscliffe <[EMAIL PROTECTED]> wrote:
> I have the following code, which I thought would create a cookie, if
> one did not exist and on the html form being sent to the server, it
> would be availabe for interrogation, when the script is run a second
> time.
>
> It seems to me there is something basic missing in that I should need
> to tell the server I am sending a cookie, but all the docs I have read
> imply it is done automatically, if one creates a 'Cookie.SimpleCookie'
>
> I know my understanding is poor of web server logic, but if anyone can
> help, I will be most grateful.
>
> This may be more to do with Web Server behaviour than python
> programming, but surely there is a way of doing this in python.
>


The cookie values have to be sent to the browser as part of the
response headers. (Modifying the Cookie object doesn't do this for you
-- the Cookie instance just acts as a container.) In other words,
something like this:

c = Cookie.SimpleCookie()
c.load(os.environ['HTTP_COOKIE'])
# ...modify cookie object...
print "Content-Type: text/html"
print c
print
# send the HTML text

-- David


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


Re: Need an identity operator because lambda is too slow

2007-02-18 Thread David Wahler
On Feb 18, 5:59 am, "Deron Meranda" <[EMAIL PROTECTED]> wrote:
> Consider a much-simplified example of such an iteration where
> sometimes you need to transform an item by some function, but most of
> the time you don't:
>
> if some_rare_condition:
> func = some_transform_function
> else:
> func = lambda x: x
> for item in some_sequence:
> item2 = func(item)
> . # more stuff
>
> Now the "lambda x:x" acts suitably like an identity operator.  But it
> is very slow, when compared to using more complex-looking code:
>
> do_transform = some_rare_condition
> for item in some_sequence:
> if do_transform:
> item2 = transform_function(item)
> else:
> item2 = item
> . # more stuff
>
> What python needs is something like a built-in "operator.identity"
> function, which acts like "lambda x:x", but which the byte compiler
> could recognize and completely optimize away so there is no function
> call overhead.

Unless I'm misunderstanding you, you seem to be proposing that the
compiler should avoid generating the call to func2() if it has a
certain value. How could that information possibly be available at
compile time?

-- David

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


Re: Default/editable string to raw_input

2006-03-22 Thread David Wahler

Paraic Gallagher wrote:
> Hi,
>
> This is my first post to the list, I hope somebody can help me with this
> problem. Apologies if it has been posted before but I have been internet
> searching to no avail.
>
> What I am trying to do is provide a simple method for a user to change a
> config file, for a test suite.
> The config file consists of a number of keys, eg. build number, target
> device, etc.
>
> What I would like to do is give the user a key, and the previous value
> for that key.
> The user can then accept the previous value or edit it on the line.
>
> I have a method where the previous value of a key is presented to the
> user and can be accepted if the user hits return. But as some of the
> values are somewhat long, and the user may only need to change 1 or 2
> characters, it would be a nice feature to offer line editing.
>
> I can get the readline.set_pre_input_hook() function to send text after
> a raw_input prompt, but this text cannot be edited.

With the disclaimer that, as others have said, this may not be the best
user-interface choice:

  import readline
  readline.set_startup_hook(lambda: readline.insert_text(old_value))
  try:
new_value = raw_input()
  finally:
readline.set_startup_hook(None)

Note that, among other issues, this isn't threadsafe (but then, you
really shouldn't be doing console I/O from multiple threads anyway).
Hope this helps.

-- David

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


Re: My Generator Paradox!

2006-03-16 Thread David Wahler
vbgunz wrote:
> I am afraid that this is the first time in which I would probably need
> something explained to me as if I were a little child. I am having a
> hard time getting this through my thick skull. What in the world is
> wrong with this!?
>
> ''' ### '''
>
> def generatorFunction(sequence=['item1', 'item2', 'item3']):
> for item in sequence:
> yield item
>
> yieldedValue = generatorFunction()
>
> '''this seems to work perfectly.'''
> print '-' * 32
> print yieldedValue  # 
> print yieldedValue.next()   # item1
> print yieldedValue.next()   # item2
> print yieldedValue.next()   # item3
>
> '''this is where things don't make any sense!'''
> print '-' * 32
> print generatorFunction()   # 
> print generatorFunction().next()# item1
> print generatorFunction().next()# item1
> print generatorFunction().next()# item1
>
> ''' ### '''
>
> the first set of calls assigned to yieldedValue work but the second set
> without assignment don't.  I asked for help on this at #python (I love
> those people in there!) and was told the following...
> generatorFunction() is a call (obvious) when calling the second set, I
> am resetting the iteration and this explains why I only and always get
> item1.
>
> ok. *but* why in the world does the first set of calls work?
> technically, isn't yieldedValue == generatorFunction() on a name basis?
> I mean isn't the following technically the same?
>
> generatorFunction()
> yieldedValue = generatorFunction()
>
> aren't they both the same?
[snip]

In that short example, they happen to be different, but equivalent,
objects, but that will not always be the case. Consider this:

>>> a = generatorFunction()
>>> b = generatorFunction()
>>> print a.next()
item1

Now a and b are both generators for generatorFunction, but calling
a.next() again will return 'item2', while calling b.next() will return
'item1'. The value returned by generatorFunction is an object, which
has an internal state that makes it distinct from other objects of the
same type. So once your yieldedValue has been altered by calling the
next() method, it is no longer equivalent to a fresh instance of the
generator.

-- David

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


Re: question about sys.exc_type and sys.exc_value

2006-03-04 Thread David Wahler
John Salerno wrote:
> Here is an exercise from Learning Python that I wrote myself:
>
> import sys
> import traceback
>
> class MyError: pass
>
> def oops():
>  raise MyError()
>
> def safe(func, *args):
>  try:
>  apply(func, args)
>  except:
>  traceback.print_exc()
>  print 'Got', sys.exc_type, sys.exc_value
>
> safe(oops)
>
> And here is the output:
>
> Traceback (most recent call last):
>File "C:/Python24/oops.py", line 11, in safe
>  apply(func, args)
>File "C:/Python24/oops.py", line 7, in oops
>  raise MyError()
> MyError: <__main__.MyError instance at 0x00B475A8>
> Got Queue.Empty
>
> Why does it show Queue.Empty as the values for sys.exc_type and
> sys.exc_value? I guess I can somewhat understand Empty, because the
> instance is basically nothing. But why does it say Queue instead of
> MyError? The sample in the book shows "Got hello world", because hello
> is the error name and world is the extra data. But mine doesn't seem to
> work that way. (They are also using a string exception in the book,
> instead of a class.)
>
> Thanks.

Are you using the first edition of "Learning Python"? According to
 that was published in 1999,
which puts it right around the end of Python 1.5. I'd strongly suggest
you find a much more recent tutorial, as a lot of python has been
improved and altered since then (sometimes in incompatible ways).

If you look at the sys module's documentation at
, you'll notice that
sys.exc_{type,value,traceback} have been deprecated since version 1.5.
That's because there's really no way to use them safely. The reason is
that as global variables, they hold information on the most recent
exception handled _anywhere_ in the program, possibly in another
thread. You should use sys.exc_info() instead, which only returns the
most recent one in the current thread.

If you do a little experimentation, you should notice that your
anomalous result only occurs when you run the code through IDLE -- it
works fine at the command prompt. Also, if you print exc_type and
exc_value first, at the beginning of the except block, it works fine.
The reason for this behavior is that IDLE runs your code in a separate
thread of the same interpreter instance used to run IDLE itself.
Presumably, some code in IDLE's user interface uses a queue to manage
the display, and that results in a Queue.Empty exception being thrown
and caught internally in another thread whenever a line of output is
printed.

The point is that sys.exc_type, sys.exc_value and sys.exc_traceback are
fundamentally unsafe, which is why they've been deprecated for the last
7 years. Incidentally, string exceptions are also deprecated IIRC, so
it's probably best to steer clear of them as well.

Hope this helps,
-- David

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


Re: Cross compile generation of .pyc from .py files...

2006-03-01 Thread David Wahler
[EMAIL PROTECTED] wrote:
> I was under the impression, that the .pyc files will be used (if found)
> by python to speed up execution of scripts... and so we packaged,
> deployed and installed the .py/.pyc files on to the ppc-target system.
> That package includes, site.py(c), types.py(c) etc., among others.
>
> Though I see these errors when I invokde 'python -v', I'm able to
> execute the .py files... but was hoping to use the .pyc files to
> benefit from the enhanced speed of execution.
[snip...]

.pyc files do not increase the speed of execution -- only the speed of
importing modules. Even then, the benefit is only seen the first time
the modules are loaded, as the .pyc files will be generated
automatically. I'd say the tiny, one-time speedup isn't worth it; and
if you really want the modules to be precompiled, it's better to do it
at installation time using distutils.

-- David

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


Re: urllib2 - My POST Request just isn't working right

2006-02-01 Thread David Wahler
Gregory Piñero wrote:
> Ok, I tried the changes you guys suggested but same error still:
> 1. Corrected spelling of referrer.
> 2. Don't specify host.
>
> Here is what they say my request should look like:
> -
> POST /GatewayDC HTTP/1.0
> Referer: YourCompanyNameGoesHere
> Host: SSLserver.fedex.com
> Accept: image/gif, image/jpeg, image/pjpeg, text/plain, text/html, */*
> Content-Type: image/gif Content-length: %d
>
> Your FedEx Transaction
> -
> Note that Host here is ficticious.  My host should be correct.  And
> they have referrer mispelled here so who knows which to use!
>

"Referer" is the correct header name. The HTTP specification spells it
incorrectly, so "Referrer" is the proper English spelling but invalid
in the context of HTTP. This is for historical reasons, and is not
likely to change.

> Here are some suspicions I have:
>
> 1 . One thing that is weird is that the data part just says "Your
> FedEx Transaction".  I know I put the XML here, but I also notice that
> urllib2 is putting this as:
>
> request=
> Can I have my xml placed here without that something= part?  That
> don't specify what parameter name I should use.

Are you sure it's supposed to be URL-encoded? Try just sending the XML
data directly as part of the request.

-- David

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


Re: How to generate graphics dynamically on the web using Python CGI script?

2006-01-20 Thread David Wahler
Xavier Morel wrote:
> Steve Holden wrote:
> > Luiz Geron wrote:
> >> I don't have experience on this, but I think that you can make the
> >> script return the image "contents" directly to the img tag, without
> >> passing it to a img file, so you can use something like this:
> >>
> >> 
> >>
> >> wich saves some processing and I/O.
> >>
> > No it doesn't, because the script that generates the graphic is then a
> > different script from the one that generates the referring HTML. I agree
> > that scripted generation of the graphical content is a viable option
> > that I overlooked, though it seems from the OP's inquiry that he already
> > uses CGI to generate the HTML.
> >
> > regards
> >   Steve
>
> Generate inline base64 encoded images in your HTML page and you're done.
> (yes, this is ugly, but it generates both HTML and graphics in the same
> script)

I believe that won't work with Internet Explorer (although it's been a
while since I checked).

-- David

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


Re: Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread David Wahler
Russell Warren wrote:
> Not true as above.  The longhand for 'self.I += 1' is 'self.I = self.I
> + 1', which normally needs self.I to exist due to the RHS of this.

Try this:

>>> class foo(object):
...   I = 1
...   def __init__(self):
... print self.__dict__
... self.I = self.I + 1
... print self.__dict__
...
>>> a=foo()
{}
{'I': 2}


Notice that the result is the same! The catch is that the two
occurrences of "self.I" occur in different contexts -- on the left-hand
side of an assignment, and in an expression -- and are therefore
subject to different lookup rules. Specifically, evaluation of "self.I"
is delegated from instances to their classes and superclasses, while
assignment is not.

As an expression, "self.I" first tries and fails to look up
self.__dict__['I']; then, finding foo.__dict__['I'] to be present, it
returns that (1) instead. When the result of the expression is then
assigned to self.I, no delegation takes place and the value 2 is stored
in self.__dict__['I'].

A note of caution: you might be tempted to think that with objects such
as lists, which implement the __iadd__ method, no assignment would take
place. This is actually not the case -- it works exactly the same way!
To use another variation of your example:


>>> class foo(object):
...   lst = ['a','b','c']
...   def __init__(self):
... print self.__dict__
... self.lst += [1,2,3]
... print self.__dict__
...
>>> a=foo()
{}
{'lst': ['a','b','c',1,2,3]}
>>> foo.l
['a','b','c',1,2,3]
>>> id(foo.lst) == id(a.lst)
True

The list is mutated in-place, but there is _also_ an implicit
assignment to self.lst. In other words, it expands to:

self.lst = self.lst.__iadd__([1,2,3])

Normally this implicit assignment doesn't matter, since the context the
variable is stored into is the same one it was retrieved from. It's
only in situations like yours where it becomes important.

-- David

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


Re: CGI errror in (only in) IIS 6.0 server 2003

2006-01-09 Thread David Wahler
[EMAIL PROTECTED] wrote:
> Hi, I've been using ActivePython 2.3 (cgi) on IIS on win xp, 2000 and
> they work fine.
> Until I upgrade the system the server 2003. The ASP script engine is ok
> but not the CGIs.
>
> I got these errors:
>
> HTTP Error 403.1 - Forbidden: Execute access is denied.
>
> I already have execute access on the directory.
>
> Can anyone help me?

This is an IIS configuration issue, and has nothing to do with Python.
You will probably get much better responses if you post it in a more
appropriate group -- a quick search of Google Groups turns up
microsoft.public.inetserver.iis and
comp.os.ms-windows.nt.admin.networking, to get you started.

Hope this helps,

-- David

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


Re: Microsoft IronPython?

2006-01-03 Thread David Wahler
Nainto wrote:
> I came across this link today. http://tinyurl.com/9c7ta It seems
> Microsoft is getting involved with Python. What do you think of it? Is
> it any good? Anything to worry about?
> --
> Zach

See http://www.ironpython.com/ -- apparently this happened over a year
ago.

-- David

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


Re: IRC sockets and queries

2005-12-30 Thread David Wahler
Jay wrote:
> ok have this program and i set a socket 2  connect to the server...so i
> can chat on the channels and stuff. my question is how would i go about
> set a socket directly 2 an individual query?

The last part of this paragraph is completely incomprehensible.

> my program is right here...
> http://h1.ripway.com/jay001/CopyofPyIRCnMo.txt

Ah, thank you. Now I can see what you're trying to accomplish:

>if sys.platform == 'linux':
>print "Cant run on a gay linux box!"
>print "Get windows! XP or new... Vista!"
>sys.exit

Not only is this obnoxious, it doesn't even work.

>def dccpopuper(self):
>"""Kill everyone with 30 pop ups requesting a dcc chat"""
>try:
>for number in range(31):
>self.irc.send("DCC CHAT" + self.CHANNEL+"\r\n")
>irc.generateNICK()
>time.sleep(5)
>print number*10
>except:
>print "I know u want 2 kill them but connect first"

Not only is this _extremely_ obnoxious, but it doesn't even work. Don't
expect any help from me.

-- David

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


Re: Passwords in cron scripts

2005-12-24 Thread David Wahler
Mark Carter wrote:
> I have some python scripts that run as cron jobs. They connect to
> external resources (like a newsserver) - for which passwords are
> required. I currently have them stored in the scripts themselves (ouch!)
> - and was wondering if there was a more secure solution.

Any form of authentication requires some kind of secret data that
uniquely identifies the party in question -- in this case, the password
identifies your script. To run on behalf of you, the Python interpreter
must be able to have that password. Short of a full-blown Trusted
Computing system, it's impossible for that data to be stored in a form
which Python can use but nobody else can. I'm afraid you just have to
rely on the security of your operating system.

-- David

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


Re: Timing out arbitrary functions

2005-12-24 Thread David Wahler
Steven D'Aprano wrote:
> I have a problem and I don't know where to start looking for a solution.
>
> I have a class that needs to call an arbitrary function and wait for a
> result. The function, being completely arbitrary and not under my control,
> may be very time consuming and possibly may not even halt. My class needs
> to be able to give up waiting for a result after a specified amount of
> time.
>
> I'm thinking something conceptually like this:
>
> # pseudo-code:
> set time out to 30 seconds
> try:
> result = somefunction()
> except TimeOut:
> # 30 second time out happened
> print "somefunction() timed out without returning"
> else:
> print "somefunction() returned %s" % result
>
>
> The easy (for some definition of easy) solution would be to code
> somefunction() so that it raised an exception if it hadn't returned a
> result within a certain time. Unfortunately, I can't do rely on that -- I
> only have control over the calling code, not the called somefunction(),
> which may be any arbitrary function.
>
> How do others handle something like this? What should I be looking for?
> I'm after a lightweight solution, if any such thing exists.

For simple cases, I would use signal.alarm() with a SIGALARM handler
that raises a TimeOut exception. However, this is by no means
foolproof; you have to rely on the called function not to mess with
your signal handler. Plus, if your alarm occurs within a try-except
block that catches the TimeOut, it'll still be dropped. And to the best
of my knowledge, you can't otherwise forcibly terminate the execution
of a Python thread or block of code.

If you're going to be running untrusted code, I would use the
subprocess module to invoke a separate Python instance which takes the
code to be executed on stdin, and returns a pickled copy of the return
value on stdout. Then you can start it running, wait 30 seconds, and
then kill it if it hasn't already returned.

-- David

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


Re: Linux > python > file-I/O ?

2005-12-24 Thread David Wahler
[EMAIL PROTECTED] wrote:
> I've just started to test/learn python.
> I've got Linux > mandrake9 > python  & documentation.
> What I'll initially want to be doing needs file I/O, so I
> wanted to confirm file I/O early in my tests.
>
> Following the examples :
> >>> f=open('/tmp/workfile', 'w')
> >>> print f
><-- OK
>
> But:
> >>> f.read(size)
> Traceback (most recent call last):
>   File "", line 1, in ?
> NameError: name 'size' is not defined  <-- ?? Obj-method unknown ??

The expression "f.read(size)" means: take the object referred to by "f"
(a variable), and call the method named "read" with a single parameter,
determined by the expression "size". Since you haven't declared a
variable called size, Python has no idea what you're talking about. You
have to either assign a value to size, or pass in a number directly, as
in "f.read(1000)". You can also call the read method with no parameters
to read the entire contents of the file.

Even if you do this, you'll still have problems because passing the
parameter "w" to the open function means that you're opening the file
in write-only mode. If you want to read from it, you'll need to use
"r", "r+" or "w+" instead.

Hope this helps.

-- David

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


Re: Some errors when running code in diveintopython: (

2005-12-23 Thread David Wahler
Istvan Albert wrote:
> I remeber once I had truly puzzling problem that manifested itself the
> same way ... Firefox and cygwin python would work fine but the windows
> python would raise errors when trying to connect via http ...
>
> ... finally I realized that the day before IE was set to use a proxy
> server ( to capture traffic for testing but the proxy was not on that
> moment) ... I know little on how Windows works but it was quite a
> surprise that setting IE to work some way had some unwanted
> reprecussions somewhere else ...

See the documentation for urllib.urlopen:

"""
In a Windows environment, if no proxy environment variables are set,
proxy settings are obtained from the registry's Internet Settings
section.
"""

-- David

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


Re: ANNOUNCE; Try python beta

2005-12-19 Thread David Wahler
Mike Meyer wrote:
> The previous version was in a jail, which is why I didn't want it
> generally announced. The logs made amusing reading. I like Gerhard's
> idea of removing __import__, and have done that.

Oh, you have, eh? Are you absolutely sure? Try running "grep 'all your
base' log".

-- David

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


Re: writing IM bots

2005-12-13 Thread David Wahler
Amit Khemka wrote:
> Hello,
> I am trying to write an IM Bot, which automatically replies to a
> message, in Python.
> I was wondering If there are python modules for connecting to Yahoo!,
> msn networks ...
> ideally I would like to have a multithreaded module.

I have found that the best solution is to use the Jabber protocol via
xmpppy (http://xmpppy.sourceforge.net/). It's very easy to use; once
you have it working with Jabber, you can use a Jabber to Yahoo/MSN/AIM
gateway to access all the different networks without writing code for
each one.

-- David

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


Re: "0 in [True,False]" returns True

2005-12-12 Thread David Wahler
Pierre Quentel wrote:
> Hi all,
>
> In some program I was testing if a variable was a boolean, with this
> test : if v in [True,False]
>
> My script didn't work in some cases and I eventually found that for v =
> 0 the test returned True
>
> So I changed my test for the obvious "if type(v) is bool", but I still
> find it confusing that "0 in [True,False]" returns True

>From the docs: Python Library Reference, section 2.3.10.9:
"Boolean values are the two constant objects False and True. They are
used to represent truth values (although other values can also be
considered false or true). In numeric contexts (for example when used
as the argument to an arithmetic operator), they behave like the
integers 0 and 1, respectively."

I don't blame you for not knowing about this; it is rather unintuitive.

-- David

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


Re: debug 'continue' does not appear to work right

2005-12-12 Thread David Wahler
[EMAIL PROTECTED] wrote:
> When I enter 'c' at the (Pdb) prompt it just goes to the next line, and
> doesn't "continue" as it should.
[...]
>
> Here's the sample output:
>
> S:\tmp>python epdb1.py
> --Return--
> > c:\python21\lib\pdb.py(895)set_trace()->None
> -> Pdb().set_trace()
[...]

Works for me with Python 2.4.2. Since you seem to be using Python 2.1,
which was released about 4 years ago, it's likely just an old,
long-since-fixed bug. Is there some reason you can't upgrade?

-- David

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


Re: spawnle & umask

2005-12-08 Thread David Wahler
Yves Glodt wrote:
> It does, I did like this:
>
> os.umask(0113)
> newpid =
> os.spawnl(os.P_NOWAIT,'/usr/local/bin/wine','/usr/local/bin/wine',executable)
>
> But I wanted to use spawnle and it's env argument, to avoid setting
> umask manually...

The umask is not part of the environment, so there's no way to set it
directly through spawnle. Why don't you want to use os.umask?

-- David

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


Re: Is there an equivalent to Java Webstart in Python?

2005-12-05 Thread David Wahler
Nic Bar wrote:
> The problem with Jython is that I can only live inside the aplet
> virtual machine,
> I need a full features application with access to the local computer
> resources.
> Regarding IronPyhton, there is no released version yet.
> I am looking for something that can be used now and plataform
> independent (assuming the correct version of python is already
> installed on the local computer)

All JWS does is download a few jar files and run them locally. I would
think it would be pretty easy to write a simple script that would take
an application from a zip file, expand it to a temporary directory and
run it with Python. Then all you have to do is give the zip files a
custom extension (maybe .par, for Python archive?) and associate that
extension with your script on the users' machines. But if you're asking
if something like this is included with Python out of the box, then the
answer is not as far as I know, sorry.

-- David

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


Re: Persist a class (not an instance)

2005-11-25 Thread David Wahler
Kent Johnson wrote:
> Is there a way to persist a class definition (not a class instance,
> the actual class) so it can be restored later? A naive approach
> using pickle doesn't work:
[snip]
> The idea is to persist classes that are created and modified at runtime.

I couldn't resist the challenge, so I decided to take a crack at it. My
code is below. (I hope it's OK to post it even though it's a bit on the
long side.) So far, it seems to work OK; the biggest caveat to be aware
of is that functions' global context is not preserved.

My approach was to use pickle's __reduce__ protocol to store functions
and classes. Of course, you can't modify the built-in function and
classobj types, so I subclassed Pickler to override them. The advantage
of this is that you don't need an extension to the pickling data
format, and you can use the standard unpickler. (The custom module
still needs to have been imported, as it adds the classobj type to
__builtins__.)

Unfortunately, I'm not sure how to go about making it work for
new-style classes. It would seem to involve messing with dictproxy and
descriptor objects, and that's getting me into more unfamiliar
territory.

I'm sure there's a better way to do this; this seemed like "the
simplest thing that could possibly work".

-- David

#
# code_pickle.py

import sys, copy_reg, pickle, new, marshal, types, StringIO

# Needed to unserialize old-style classes
sys.modules['__builtin__'].classobj = new.classobj

# from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439096
def get_cell_value(cell):
return type(lambda: 0)(
(lambda x: lambda: x)(0).func_code, {}, None, None, (cell,)
)()

def func_constructor(name, code, defaults, closure):
return new.function(marshal.loads(code), globals(), name,
defaults, closure)

class CodePickler(pickle.Pickler):
def __init__(self, *args, **kwargs):
pickle.Pickler.__init__(self, *args, **kwargs)
self.dispatch = self.dispatch.copy()
self.dispatch[types.ClassType] = CodePickler.do_class
self.dispatch[types.FunctionType] = CodePickler.do_function

def save(self, ob, *args, **kwargs):
print ob
pickle.Pickler.save(self, ob, *args, **kwargs)

def do_class(self, ob):
if ob in (types.__dict__.values()):
self.save_global(ob)
else:
args = (ob.__name__, ob.__bases__, ob.__dict__)
self.save_reduce(type(ob), args)

def do_function(self, ob):
if ob == func_constructor:
self.save_global(ob)
else:
if ob.func_closure:
closure = tuple(map(get_cell_value, ob.func_closure))
else:
closure = None
args = (ob.func_name, marshal.dumps(ob.func_code),
ob.func_defaults, closure)
self.save_reduce(func_constructor, args)

def dumps(ob):
s = StringIO.StringIO()
CodePickler(s).dump(ob)
return s.getvalue()

# Example:
#
# import code_pickle
# class Foo:
# def show(self):
# print "Foo!"
#
# s = code_pickle.dumps(Foo)
# --
# import code_pickle, pickle
# Foo = pickle.loads(s)
# Foo().show
#

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


Re: syntax errors while building pypgsql

2005-11-23 Thread David Wahler

Tin Gherdanarra wrote:
> typedef struct {
>  PyObject_HEAD /* Here is the syntax error, and rightly so */
[snip]
> } PgConnection;
>
>
> I don't know what PyObject_HEAD or PGconn is,
> but if they are types, a syntax error is justified here:
>
>  PyObject_HEAD /* Here is the syntax error */
>  PGconn *conn;

PyObject_HEAD is a macro defined in the Python headers. It provides the
internal fields common to all Python objects to provide garbage
collection and other services. If that's throwing errors, then your
compiler isn't finding the headers. Check to see if you have the file
/usr/include/python*/Python.h on your system.

-- David

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


Re: Web-based client code execution

2005-11-19 Thread David Wahler
Steve wrote:
> AJAX works because browsers can execute javascript.  I don't know of a
> browser that can execute python.  Basically your stuck with java or
> javascript because everything else really isn't cross platform

Don't jump to conclusions...
http://dwahler.ky/python/

If you really, really want Python in a browser, it's certainly
possible. :)

-- David

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


Re: the PHP ternary operator equivalent on Python

2005-11-18 Thread David Wahler
Daniel Crespo wrote:
> I would like to know how can I do the PHP ternary operator/statement
> (... ? ... : ...) in Python...
>
> I want to something like:
>
> a = {'Huge': (quantity>90) ? True : False}

Well, in your example the '>' operator already returns a boolean value
so you can just use it directly. Hoewver, I agree that there are
situations in which a ternary operator would be nice. Unfortunately,
Python doesn't support this directly; the closest approximation I've
found is:

>>> (value_if_false, value_if_true)[boolean_value]

This exploits the fact that False and True are converted to integers as
zero and one, respectively. The downside is that it's really ugly.
Also, it doesn't use minimal evaluation; in other words, if you try an
expression like:

>>> (None, func())[callable(func)]

You might think this would return the value of func() if it's callable,
and None otherwise. Unfortunately, func() is evaluated no matter what,
even if the condition is false.

Of course, you can always get around this by doing really cryptic stuff
with lambdas:

>>> (lambda: None, lambda: func())[callable(func)]()

... but by that point, you're better off just using an if/then/else.

-- David

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


Re: different binding behavior

2005-11-10 Thread David Wahler
Gabriel Zachmann wrote:
> It seems to me that the following behavior of python (2.4.1) is inconsistent:
[snip]
> Why was it implemented like this??

Lists are mutable objects; integers are not. For a list, a += b is
equivalent to a.__iadd__(b), which is an in-place modification.

For an integer, no __iadd__ method is provided, so a += b is equivalent
to a = a.__add__(b), which is a rebinding operation rather than a
modification.

This question (or variants thereof) is asked on an almost daily basis;
please search before posting.

-- David

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


Re: Goto XY

2005-11-08 Thread David Wahler
[EMAIL PROTECTED] wrote:
> I assume I have to use a header somewhere (import WConio ?).

If you had tried it, you would have discovered that "import WConio" is
exactly what you need. Don't be afraid to experiment!

-- David

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


Re: Goto XY

2005-11-08 Thread David Wahler

[EMAIL PROTECTED] wrote:
> Is there some command in python so that I can read a key's input and
> then use a gotoxy() function to move the cursor on screen?  e.g.:
> (psuedo-code)
>
> When the right arrow is pushed, cursor gotoxy(x+1,y)
>
> Thanks.

On Unix-like platforms, this functionality is provided by the standard
curses module. A similar module for Windows appears to be available at
http://www.effbot.org/zone/console-index.htm but I haven't tested it
out.

-- David

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


Re: How can I do this in Python?

2005-11-04 Thread David Wahler
Lad wrote:
> Hi,
> I have a web program and a user can have access to a page only after he
> logged in.
> So, the program comes with  a Login form and the user logins.But I do
> not know how to return the user back to where he came from, after a
> successful login.
>
> Something like this:
>
> PageWhereUserMustBeLogin >UserSigned->-
>
> ^--<---<|
>
>
> Thank you for help
>
> L.

You'll need to either use a hidden form field or check the HTTP
"Referer" header to determine the page the user was on. Then, just use
an HTTP redirect to send them back to that page.

Are you using a particular web application framework, or separate CGI
scripts?

-- David

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


Re: Threading-> Stopping

2005-11-04 Thread David Wahler
Tuvas wrote:
> Is there a way to stop a thread with some command like t.stop()? Or any
> other neat way to get around it? Thanks!

Sadly, no. While Java and many other programming languages have an
interrupt() primitive, Python does not. You can approximate this by
using a global variable to tell the thread when to stop, for example:

shutdown = False

class MyThread(Thread):
def run(self):
while not shutdown:
# do whatever

def kill_thread():
shutdown = True

There's no general way to wake up a thread that's blocked--you have to
satisfy the condition that's causing it to block. If it's waiting for
input from a Queue, you have to push a dummy value down it to wake up
the thread and give it a chance to check the shutdown flag. If it's
blocking to do I/O, you'll have to use select() and provide a timeout
value to check the flag periodically.

-- David

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


Re: Shareware in Python

2005-11-04 Thread David Wahler

Ivan Sas wrote:
> I want to create some shareware program in Python. Can I distribute
> this program with
> python24.dll file from Python 2.4.2?  I'm not sure if Python GPL
> compatible license allowing for doing it.
> Thanks,
> Ivan Sas

Python is distributed under its own license, not the GPL: see
http://www.python.org/2.4.2/license.html for details. I've just skimmed
it, and it looks like you're fine as long as you include the Python
license agreement and copyright notice.

-- David

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


Re: Can Anyone Help me on this

2005-11-03 Thread David Wahler
[EMAIL PROTECTED] wrote:
> If what you want is a reversed copy, you could just append list1
> elements to list2, and use the reverse function such as
> >>> ...
> >>> for i in list1:
> ...   list2.append(i)
> ...
> >>> list2.reverse()
> >>> list1
> ['1', '2', '3', '4', '5', '6', '7', '8', '9']
> >>> list2
> ['9', '8', '7', '6', '5', '4', '3', '2', '1']

It's much clearer and simpler to just do:
>>> list2 = list1[:]
>>> list2.reverse()
This saves all the messing around with append, and has the advantage
that it runs faster (by a factor of 30, on my computer).

-- David

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


Re: declaring Hex in python

2005-11-03 Thread David Wahler
[EMAIL PROTECTED] wrote:
> I want to use some old C code such as
> #define GEN_STREAMTYPE_NULL 0x51
> I need to send this to a server, but it must be declared as a unsigned
> four byte constant.  I can not just send a string.  I love python, but
> am looking for information where I can declare this and then send it
> through a socket.
> Also are there any classes or methods which can manipulate what is
> returned, such as shifting bits returned.
> Thank-you

See the "struct" module (http://docs.python.org/lib/module-struct.html)
for a couple of really useful functions. For example, you could use:

>>> GEN_STREAMTYPE_NULL = 0x51
>>> def send4bytes(num):
... mysocket.send(struct.pack("L", num))
...
>>> send4bytes(GEN_STREAMTYPE_NULL)

As for bit manipulation--it's built in, just like in C.

>>> hex(0x1234 & 0xFF)
'0x34'
>>> hex(0x34 << 8)
'0x3400'

Hope this helps.

-- David

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


Re: question about urllib and parsing a page

2005-11-02 Thread David Wahler
[EMAIL PROTECTED] wrote:
> hey there,
> i am using beautiful soup to parse a few pages (screen scraping)
> easy stuff.
> the issue i am having is with one particular web page that uses a
> javascript to display some numbers in tables.
>
> now if i open the file in mozilla and "save as" i get the numbers in
> the source. cool. but i click on the "view source" or download the url
> with urlretrieve, i get the source, but not the numbers.
>
> is there a way around this ?
>
> thanks

If the Javascript is automatically generated by the server with the
numbers in a known location, you can use a regular expression to
extract them. For example, if there's something in the code like:

var numbersToDisplay = [123,456,789];

Then you could use: (warning, this is not fully tested):

import re
js_source = "... the source inside the 

Re: callback for ctypes

2005-11-01 Thread David Wahler

James Hu wrote:
> Hi, gurus,
>
> I would like to use ctypes to implement callback function for QImage
> Camera to capture image asynchronously, and I have the c++ code of
> callback, but I am totally in the dark, the ctypes tutorial is not good
> enough for me to do that, does someone know where to dig more info for
> ctypes callback implementation?
>
> Thanks,
>
> James

Ctypes is designed to interface with C code, not C++. C++ compilers
perform name mangling to allow function overloading and virtual
functions. To use ctypes, you'll need to know the exact details of how
your compiler translates C++ code--which is certainly a non-trivial
task.

Also, I can't find any references for this "QImage Camera". Could you
post some additional information?

-- David

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


Re: Reuse base-class implementation of classmethod?

2005-10-31 Thread David Wahler

Giovanni Bajo wrote:
> Hello,
>
> what's the magic needed to reuse the base-class implementation of a
> classmethod?
>
> class A(object):
>@classmethod
>def foo(cls, a,b):
># do something
>pass
>
> class B(A):
> @classmethod
> def foo(cls, a, b):
>  A.foo(cls, a, b)   # WRONG!
>
> I need to call the base-class classmethod to reuse its implementation, but I'd
> like to pass the derived class as first argument.
> --
> Giovanni Bajo

See the super object. In your case, it can be used like this:

class B(A):
@classmethod
def foo(cls, a, b):
super(B, cls).foo(a, b)

This all assumes you want to modify the behavior of foo in a subclass.
If not, of course, you don't need to override it at all.

-- David

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


Re: Where to save classes? How to access classes?

2005-10-31 Thread David Wahler

David Mitchell wrote:
> Hi,
>
> I'm trying to get into the object oriented aspect of Python. If I create
> a custom class (in it's own file), how do I access that class in a
> function in a different file? In Java there's the notion of a CLASSPATH,
> where you can tell the compiler to look for classes. Is there something
> similar to this in Python?
>
> Thanks,
>
> Dave

Python is a little different than Java in this respect. Whereas Java
loads code one public class at a time, Python loads it in modules,
where a module is (generally speaking) one source file.

Say you have a file, foo.py:
###
class Xyz:
def do_stuff(self):
pass

class Abc:
def do_more_stuff(self):
pass
###

Then in another file, inserting the statement "import foo" will execute
foo.py and make its global variables available as part of an object
called "foo". There is no automatic loading by class and package name
like Java does it. Once you've done this, you can access the members
using expressions such as "foo.Xyz" e.g. "foo.Xyz().do_stuff()".

As for the search path: sys.path (the path attribute of the sys module)
is a list of directories which are searched, in order, for modules
specified by "import". By default this is the current directory
followed by a few standard Python library directories; most of the time
you shouldn't need to change it, at least for simple projects.

Hope this helps,

-- David

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


Re: popen2

2005-10-28 Thread David Wahler
g.franzkowiak wrote:
> I start a process in my application with popen2.popen3('MyOtherProcess').
> That's ok, but what can I do if the other process is running ?
> Can I fetch some information and start with that ?
>
> gerd

It's not clear what you're asking for. Could you please clarify?

-- David

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


Re: How to processing multi redirect?

2005-10-26 Thread David Wahler
Gonnasi wrote:
> I want fetching some articles from nytimes.com for my Palm, and I want
> a clear, simple article too, my Palm has only 8M RAM.
>
> With the WGET, I can fetching the page like:
> "http://www.nytimes.com/2005/10/26/business/26fed.html?pagewanted=print";,
> and when WGET works, I can see the URL have been redirect many times.
>
> When I run the below code with Python:
> >>> thing = urllib2.HTTPRedirectHandler()
> >>> opener = urllib2.build_opener(thing)
> >>> url = 
> >>> http://www.nytimes.com/2005/10/26/business/26fed.html?pagewanted=print'
> >>> page = opener.open(url)
>
> I just get a error message: "HTTP Error 302: The HTTP server returned a
> redirect error that would lead to an infinite loop. The last 30x error
> message was: Moved Temporarily"
>
> Why I can't fetching the page with python, but WGET can do it?
>
> Thanks for your help in advance!
>
> --
> Gonnasi

Hi,

Your problem is that you're not preserving cookies from one request to
the next. nytimes.com redirects you to an automatic login page which
sets a cookie; this cookie is required to view the original page, or
else it'll get stuck in a loop. This fixes the problem:

>>> thing = urllib2.HTTPRedirectHandler()
>>> thing2 = urllib2.HTTPCookieProcessor()
>>> opener = urllib2.build_opener(thing, thing2)
>>> url = 
>>> 'http://www.nytimes.com/2005/10/26/business/26fed.html?pagewanted=print'
>>> page = opener.open(url)

Hope this helps, 

-- David

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


Re: Binding a variable?

2005-10-21 Thread David Wahler
Paul Dale wrote:
> Hi everyone,
>
> Is it possible to bind a list member or variable to a variable such that
>
> temp = 5
>
> list = [ temp ]
>
> temp == 6
>
> list
>
> would show
>
> list = [ 6 ]
>
> Thanks in advance?
>
> Paul

Python doesn't have "variables" -- a statement like "temp = 5" just
binds the name "temp" to an int object with value 5. When you say "list
= [temp]", that's putting the int object into the list; the name "temp"
is just a label for it, not a variable or container of any kind. Since
int objects are immutable, you can only change the value by assigning a
new object in its place.

That said, you can accomplish something like this by creating a new
class:

---
class MyInt:
pass

>>> temp = MyInt()
>>> temp.i = 5
>>> list = [temp]
>>> temp.i = 6
>>> print list[0].i
6
---

Once your object is mutable, any changes to it will be reflected
anywhere there is a reference to it.

-- David

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


Re: threading/forking and IPC

2005-10-14 Thread David Wahler

Sophia Cao wrote:
> Hello,
>
> I am seeking a python solution for my project.  I am trying to
> implement an architecture where there is a server who receives incoming
> messages from several clients, then those messages need to be written
> to a MySQL database.  To avoid too many connections to the database, I
> plan to save the incoming messages into a queue, while use a seperate
> process/thread to constantly monitoring the queue and storing the
> messages to the database.  I am not sure wheather I should use
> threading or forking and how to implement the sharing of this message
> queue.
>
> Thanks a lot for any advice.

If you want to have a shared queue, it's much easier and simpler to use
threading, as it allows you to share data structures between threads.
Python already has a multithreaded queue class -- see
http://docs.python.org/lib/module-Queue.html. Just create a global
Queue object or pass each thread a reference to it, and they can all
share it.

-- David

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


Re: KeyboardInterrupt being lost?

2005-10-14 Thread David Wahler
Operation Latte Thunder wrote:
> I have a simple test proggie that isn't behaving like I expect ( found
> below ).  The script will infinitely run ( as expected ), but seems to
> completely ignore control-C's.  Shouldn't the interpreter pass along
> KeyboardInterrupts and break out of the while loop, or am I missing
> something?
>
> Using python 2.4.2 on linux ( if it matters )
>
> -- Script Below --
>
> import threading, traceback, time
>
> class TestThread ( threading.Thread ):
> def __init__ ( self ):
> threading.Thread.__init__ ( self )
> def run ( self ):
> print "Starting..."
> while True:
> time.sleep ( 1 )
> return
>
> if __name__ == '__main__':
> test = TestThread ( )
> test.start()
> print "Started..."
> test.join()
>
>
> --
> chris

Chris,

Thread.join() is implemented using a lock, and the acquisition of a
lock is uninterruptible. (See
http://docs.python.org/lib/module-thread.html) Therefore, your main
thread will block until the other thread terminates or the process is
forcibly killed. Even if it could be interrupted, I don't think there's
any way to raise that exception in the other thread. (Python's
threading support leaves something to be desired when compared to, say,
Java.)

-- David

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