Re: SPE IDE for Python

2005-11-08 Thread Chris Spencer
py wrote:
> Anyone here use SPE (http://www.stani.be/python/spe/blog/). ...the IDE?
> 
> Also, anyone know if it supports CVS or has a plugin for CVS?  If not,
> what do you use to get your code into CVS (via an IDE preferably)?

I used to use SPE quite frequently, until it went nearly unmaintained 
for most of late last year. I switched to the PyDev plugin for Eclipse, 
which comes with standard support for CVS (there's also an SVN plugin). 
SPE development looks to be back in full swing, which in good, since 
it's one of the best pure Python IDEs around.

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


Re: Recompile AST?

2005-11-10 Thread Chris Spencer
Leif K-Brooks wrote:
> [EMAIL PROTECTED] wrote:
> 
>>Is it possible to recompile the AST generated by compiler.parse, back
>>into code or an executable code object?
> 
> 
> Into a bytecode object:
> 
>  >>> from compiler.pycodegen import ModuleCodeGenerator
>  >>> from compiler.misc import set_filename
>  >>> from compiler import parse
>  >>>
>  >>> tree = parse('foo = 42')
>  >>> set_filename('', tree)
>  >>> code = ModuleCodeGenerator(tree).getCode()
>  >>> exec code
>  >>> foo
>  42
> 
> Into Python source code: .

Thanks, that's exactly what I was looking for. I had almost figured that 
out, but I had overlooked the need for set_filename.

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


Re: Dynamically Update Class Definitions?

2005-11-11 Thread Chris Spencer
Alex Martelli wrote:
> <[EMAIL PROTECTED]> wrote:
> 
> 
>>Is there a way to loop through all instantiated objects and update
>>their classes when a source file changes? I know about Michael Hudson's
>>method
>>(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164), but
>>you have to modify all your classes to subclass AutoReloader. Is there
>>something less intrusive (visitor pattern?) that you can use like
>>update(old_class, new_class) to automagically do the work?
> 
> 
> If you're in no hurry, you COULD loop over all of gc.get_objects(),
> identify all those which are instances of old_class and "somehow" change
> their classes to new_class -- of course, x.__class__ = new_class may
> well not be sufficient, in which case you'll have to pass to update a
> callable to do the instance-per-instance job.

Couldn't I just loop over gc.get_referrers(cls), checking for instances 
of the class object? Since class instances refer to their class, the gc 
seems to be doing the exact same thing as Hudson's fancy metaclass. Or 
am I missing something?

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


Re: Dynamically Update Class Definitions?

2005-11-11 Thread Chris Spencer
Chris Spencer wrote:
> Alex Martelli wrote:

>> If you're in no hurry, you COULD loop over all of gc.get_objects(),
>> identify all those which are instances of old_class and "somehow" change
>> their classes to new_class -- of course, x.__class__ = new_class may
>> well not be sufficient, in which case you'll have to pass to update a
>> callable to do the instance-per-instance job.
> 
> 
> Couldn't I just loop over gc.get_referrers(cls), checking for instances 
> of the class object? Since class instances refer to their class, the gc 
> seems to be doing the exact same thing as Hudson's fancy metaclass. Or 
> am I missing something?
> 
> Chris

In fact, the following code seems to work, and doesn't require any 
modification to new-style class based code:

import os
import time
import threading
import inspect
import gc
import copy

class ModuleUpdater(object):
 '''
 This will constantly check a module's source file for updates, reload
 if any are detected, and update all class instances.
 Only works for new-style classes.

 Use like:
 checker = ModuleUpdater(module=mymod)
 checker.start()
 '''
 def __init__(self, module):
 self.module = module
 self.lastloaded = time.time()
 self.running = 0
 self.t = None
 def __call__(self):
 self.running = 1
 while self.running:
 self.check()
 time.sleep(1)
 def check(self):
 lastmodified = os.stat(inspect.getsourcefile(self.module))[8]
 if lastmodified > self.lastloaded:
 print 'update detected for',self.module.__name__
 oldmod = copy.copy(self.module.__dict__)
 newmod = reload(self.module)
 try:
 for name,obj in oldmod.items():
 if isinstance(obj,type) and name in newmod.__dict__:
 newobj = newmod.__dict__[name]
 referrers = gc.get_referrers(obj)
 for referrer in referrers:
 if isinstance(referrer,obj):
 # update old class instances to use new 
class
 referrer.__class__ = newobj
 print 'update loaded for',self.module.__name__
 except Exception, e:
 print 'unable to load update for %s: %s' % 
(self.module.__name__, str(e))
 self.lastloaded = lastmodified
 return 1
 return 0
 def start(self):
 t = threading.Thread(target=self)
 t.setDaemon(1)
 t.start()
 self.t = t
 return t
 def stop(self, blocking=0):
 self.running = 0
 if blocking:
 self.t.join()

if __name__ == '__main__':

 import testmod # any module containing class Bar with method meth
 uc = ModuleUpdater(testmod)

 uc.start()

 b=testmod.Bar(1)
 while 1: # meanwhile, modify the source to testmod.py
 time.sleep(1)
 print b.meth()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically Update Class Definitions?

2005-11-12 Thread Chris Spencer
Jean-Paul Calderone wrote:

> There are lots of cases where you cannot rebind the __class__ 
> attribute.  For a comprehensive treatment of this idea (but still not a 
> completely functionality implementation), take a look at 
> .
>   
> On another note, the usage of threads in this code is totally insane and 
> unsafe.  Even for strictly development purposes, I would expect it to 
> introduce so many non-deterministic and undebuggable failures as to make 
> it cost more time than it saves.  You really want to stop the rest of 
> the program, then update things, then let everything get going again.

I used a thread to quickly detect changes in the source code, but you're 
absolutely right. In any non-toy application you'll definitely need 
control over when the upgrade process occurs. Thanks for the help.

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


Rich Graphics?

2005-07-27 Thread Chris Spencer
I'm trying to write a Gui in Python for manipulating rich graphical 
representations, similar to something like Inkscape. I've tried tkinter, 
wxPython, pyGtk, and while they all do traditional widgets well enough, 
none of them really handle anti-aliased, transparent, transformed shapes 
typical of vector based displays. I've noticed tkZinc, which seems to 
better handle vector graphics through the use of openGL, but it's 
traditional widget set is still limited and based on tkinter. Ideally, 
what I'm looking for is something like wxWidgets which can display SVG 
along side standard widgets and allow there manipulation through Python. 
I was thinking of a web-based route, by accessing the SVG capabilities 
in Mozilla's Deer Park browser through Twisted+Livepage, but this has 
been extremely complicated and limiting. Are there any other options I 
haven't considered?

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


Persistent XMLRPC Connection

2005-08-14 Thread Chris Spencer
I noticed the SimpleXMLRPCServer/ServerProxy creates a new socket for 
remote procedure call. I've written a simple IP based authentication 
scheme for the Server, and I'd like to include the port in the 
authentication, which is currently not possible since the port keeps 
changing.

I've looked at the code in SimpleXMLRPCServer.py, xmlrpclib.py, 
SocketServer.py, and BaseHTTPServer.py, but it's a little overwhelming. 
How difficult would it be to reuse connections so that only one port per 
persistent client connection is used?

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


Socket Troubles

2005-08-28 Thread Chris Spencer
I've written a simple class to manage P2P socket connections. However, 
whenever I try to receive data, the socket raises an exception with the 
error message  (11, 'Resource temporarily unavailable').

My code's fairly straight-forward, with much of it right out of the 
Python docs, so I'm not sure what I'm doing wrong. You can see it all at 
http://deadbeefbabe.org/paste/1525/0

Any help is immensely appreciated.

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


Re: Socket Troubles

2005-08-28 Thread Chris Spencer
Chris Spencer wrote:
> I've written a simple class to manage P2P socket connections. However, 
> whenever I try to receive data, the socket raises an exception with the 
> error message  (11, 'Resource temporarily unavailable').
> 
> My code's fairly straight-forward, with much of it right out of the 
> Python docs, so I'm not sure what I'm doing wrong. You can see it all at 
> http://deadbeefbabe.org/paste/1525/0
> 
> Any help is immensely appreciated.
> 
> Thanks,
> Chris

One more thing. The code I posted is also a complete demo of my problem. 
Run the script in two different terminals simultaneously, like
script.py 8000 8001
and
script.py 8001 8000

They should then try talking to each other, reproducing the problem.

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


Re: Socket Troubles

2005-08-28 Thread Chris Spencer
Michael Sparks wrote:
> Chris Spencer wrote:
> 
> At one point in your code you do this: 
> self._socket.setblocking(0)
> 
> This says "if we can't recieve or send data without blocking, fail rather 
> than succeed". One of the failure modes is to return error 11. This is 
> infact normally defined as:
> #define EAGAIN  11  /* Try again */
> 
> What this means is "sorry, I couldn't do this without blocking right now, 
> try again very shortly!".
> 
> Looking at your code it continually loops (in BaseConnectionHandler_recv) 
> receiving data - whether or not there's any data to receive:
> def _recv(self):
> while self.running:
> try:
> data = self._socket.recv(4096)
> if not len(data):
> time.sleep(0.1)
> continue
> except Exception, e:
> log('Recieve failed for handler',self.address,'because of',e)
> 
> break
> 
> Since you never actually check to see if the socket is ready to give you
> data, and you've set it non-blocking, seeing lots of EAGAIN errors is
> pretty much what you'd expect to see. (Simply sleeping is not sufficient!)
> 
> I suppose the short answer though really is this: you set the socket
> non-blocking, you should therefore expect to see failures telling you
> to try again, and follow their advice! :)

You're quite right. I fixed this by using select(). However, I was still 
having problems with open() blocking the main thread. Then I realized a 
slight problem:
 t = threading.Thread(target=self._connection_handler(h))

I changed this to:
 t = threading.Thread(target=self._connection_handler, args=(h,))
and now it appears to be working correctly.

Thanks for your help! I truly appreciate it.

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


Lossless Number Conversion

2005-08-28 Thread Chris Spencer
Is there any library for Python that implements a kind of universal 
number object. Something that, if you divide two integers, generates a 
ratio instead of a float, or if you take the square root of a negative, 
generates a complex number instead of raising an exception? Lisp has 
something like this, and it makes number crunching much more convenient.

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


Object Persistence Using a File System

2006-07-11 Thread Chris Spencer
Before I get too carried away with something that's probably 
unnecessary, please allow me to throw around some ideas. I've been 
looking for a method of transparent, scalable, and human-readable object 
persistence, and I've tried the standard lib's Shelve, Zope's ZODB, 
Divmod's Axiom, and others. However, while they're all useful, none 
satisfies all my criteria. So I started writing some toy code of my own: 
http://paste.plone.org/5227

All my code currently does is transparently keep track of object changes 
without requiring any special coding on part of the user, and a function 
to convert an object to a file system hierarchy of folders and files. 
Please, let me know what you think.

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


InteractiveConsole History on Linux

2006-07-14 Thread Chris Spencer
Why does code.InteractiveConsole support command history on Windows, but 
not in a Gnome terminal (all I get is ^[[A^[[B)? Or does it not support 
history at all, and the Windows console is implementing it's own? Is 
there any way to get command history working with InteractiveConsole on 
Linux?

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


Re: InteractiveConsole History on Linux

2006-07-15 Thread Chris Spencer
vbgunz wrote:
> vbgunz wrote:
>>> Why does code.InteractiveConsole support command history on Windows, but
>>> not in a Gnome terminal (all I get is ^[[A^[[B)? Or does it not support
>>> history at all, and the Windows console is implementing it's own? Is
>>> there any way to get command history working with InteractiveConsole on
>>> Linux?
>> The only time I see [A[B on Linux in a console is when I am not logged
>> in. Check to see if you're logged in and then try again. AFAIK, Linux
>> does support console history. I hope this helps :)
> 
> Sorry, I missed "code.InteractiveConsole" *but* maybe being logged in
> has something to do with it?

Heh, how could I run Python if I weren't logged in :P
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: InteractiveConsole History on Linux

2006-07-15 Thread Chris Spencer
Robert Kern wrote:
> Chris Spencer wrote:
>> Why does code.InteractiveConsole support command history on Windows, 
>> but not in a Gnome terminal (all I get is ^[[A^[[B)? Or does it not 
>> support history at all, and the Windows console is implementing it's 
>> own? Is there any way to get command history working with 
>> InteractiveConsole on Linux?
> 
> Be sure that the readline module is installed.

Yeah, "import readline" works just fine. My problem isn't hard to 
replicate. Can anyone else on Linux get command history to work with the 
following code? Note, it should be saved and run from a file.

from code import InteractiveConsole
i = InteractiveConsole(globals())
i.interact()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: InteractiveConsole History on Linux

2006-07-15 Thread Chris Spencer
Robert Kern wrote:
> Chris Spencer wrote:
>> Robert Kern wrote:
>>> Chris Spencer wrote:
>>>> Why does code.InteractiveConsole support command history on Windows, 
>>>> but not in a Gnome terminal (all I get is ^[[A^[[B)? Or does it not 
>>>> support history at all, and the Windows console is implementing it's 
>>>> own? Is there any way to get command history working with 
>>>> InteractiveConsole on Linux?
>>> Be sure that the readline module is installed.
>>
>> Yeah, "import readline" works just fine. My problem isn't hard to 
>> replicate. Can anyone else on Linux get command history to work with 
>> the following code? Note, it should be saved and run from a file.
>>
>> from code import InteractiveConsole
>> i = InteractiveConsole(globals())
>> i.interact()
> 
> You will also have to make sure that you import readline, too. This 
> works for me:
> 
> import readline
> from code import InteractiveConsole
> i = InteractiveConsole(globals())
> i.interact()

Actually, it seems redirecting sys.stdout for my logger was the issue. 
Thanks for your help though.

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


Re: InteractiveConsole History on Linux

2006-07-16 Thread Chris Spencer
[EMAIL PROTECTED] wrote:
> Chris> Yeah, "import readline" works just fine. My problem isn't hard to
> Chris> replicate. Can anyone else on Linux get command history to work
> Chris> with the following code? Note, it should be saved and run from a
> Chris> file.
> 
> Command history across sessions or just command recall from the current
> session?  On my Mac your script works just fine for me (no import readline
> as Robert Kern indicated in his reply) to do command recall from the the
> current session, but more is needed if you expect to get command history
> across sessions.

I was just looking for recall in the current session, although the 
stdlib docs have an example of recall across sessions ( 
http://docs.python.org/lib/readline-example.html ).

On Linux, it seems you have to explicitly import readline in order to 
get command history, although my application was also redirecting 
sys.stdout, which was also causing problems.
-- 
http://mail.python.org/mailman/listinfo/python-list


Remembering History in code.InteractiveConsole

2006-07-09 Thread Chris Spencer
I'd like to make code.InteractiveConsole function just like the normal 
Python console. However, when I try to use the arrow keys to recall 
command history, all I get is ^[[A^[[B. I've seen the example at 
http://docs.python.org/lib/readline-example.html
but this doesn't seem to work at all, although I don't really want to 
persist command history across sessions anyways. Is it possible to 
remember command history with IC? I'm on Linux. Any help is appreciated.

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


ElementTree Namespace Prefixes

2005-06-12 Thread Chris Spencer
Does anyone know how to make ElementTree preserve namespace prefixes in 
parsed xml files? The default behavior is to strip a document of all 
prefixes and then replace them autogenerated prefixes like ns0, ns1, 
etc. The correct behavior should be to write the file in the form that 
it was read, which it seems to do correctly for everything except 
namespace prefixes. The docs mention "proper" output can be achieved by 
using the Qname object, but they don't go into any detail. Any help is 
appreciated.

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


Re: How to get/set class attributes in Python

2005-06-12 Thread Chris Spencer
Kalle Anke wrote:
> On Sun, 12 Jun 2005 13:59:27 +0200, deelan wrote
> (in article <[EMAIL PROTECTED]>):
> 
> void doSomething( data : SomeClass ){ ... }
> 
> and I would be sure at compile time that I would only get SomeClass objects 
> as parameters into the method.

Being an untyped language, Python does not require you to enforce types. 
However, for those that require such functionality, you can get away 
with using the "assert" statement. For example, if I wanted to make sure 
my function foo was only given instances of class Bar, I'd write 
something like:

 >>> class Bar: pass
 >>> def foo(bar):
... assert isinstance(bar, Bar), "argument is not of type Bar"
... print "argument must be of type Bar"
...
 >>> bar = Bar()
 >>> foo(bar)
argument must be of type Bar
 >>> foo(123)
Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 2, in foo
AssertionError: argument is not of type Bar
 >>>

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


Re: ElementTree Namespace Prefixes

2005-06-12 Thread Chris Spencer
Andrew Dalke wrote:
> On Sun, 12 Jun 2005 15:06:18 +0000, Chris Spencer wrote:
> 
> 
>>Does anyone know how to make ElementTree preserve namespace prefixes in 
>>parsed xml files?
> 
> 
> See the recent c.l.python thread titled "ElemenTree and namespaces"
> and started "May 16 2:03pm".  One archive is at
> 
> http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/31b2e9f4a8f7338c/363f46513fb8de04?&rnum=3&hl=en

Thanks, although that thread didn't seem to resolve the issue. All the 
first few links talk about is how to hack your own parser to make sense 
of the Clark notation.

The problem at hand is with how Elementtree outputs namespaces and 
represents the tag name in memory.

Given xml with no namespaces, Elementtree works perfectly. However, if 
you give the root tag an xmlns attribute, Elementtree relabels all child 
nodes with it's own prefix, completely defeating the purpose of the 
default namespace. In my opinion, this is unacceptable behavior.

If an XML parser reads in and then writes out a document without having 
altered it, then the new document should be the same as the original. 
With Elementtree this isn't so. Lundh apparently believes he knows 
better than you and I on how our namespaces should be represented.

It's a shame the default ns behavior in Elementtree is in such a poort 
staten. I'm surprised no one's forked Elementtree solely to fix this issue.

Anyways, Python's native minidom works as expected, so I'll probably use 
that instead, even if the api is slightly less intuitive.

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


Re: How to get/set class attributes in Python

2005-06-12 Thread Chris Spencer
Bruno Desthuilliers wrote:

> And *this* is highly unpythonic. And un-OO too, since it makes foo() 
> dependant on *class* Bar, when it should most probably be enough that it 
> only depends on (probably part of) the *interface* of class Bar.

I was providing the original poster with a simple way to ensure 
appropriate type. You can assert whatever aspect you wish. If you want 
to ensure compatibility with some other property simply use "assert 
blah" subsituting blah for whatever test you wish to perform (e.g. 
assert MyInterface.implementedBy(bar) to test for interface implementation).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get/set class attributes in Python

2005-06-12 Thread Chris Spencer
Bruno Desthuilliers wrote:
> Chris Spencer a écrit :
> 
>> I was providing the original poster with a simple way to ensure 
>> appropriate type.
> 
> 
> s/appropriate type/specific implementation/
> 
> Hint : the appropriate type for print >> XXX is "whatever has a 
> 'write(anything_that_can_be_coerced_to_a_string)' method".

Certainly, although now I think you're splitting hairs over my 
terminology. Let's synchronize our wording with a point made in the 
Python docs. Section 2.1 of the Library Reference recommends that the 
builtin function isinstance() be used for testing the object type. Does 
this then mean that "type" refers to the class of an object? In which 
case, wouldn't "interface" refer to a collection of methods, that when 
present in a class create an implementation?

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


Re: How to get/set class attributes in Python

2005-06-12 Thread Chris Spencer
Peter Dembinski wrote:
> Bruno Desthuilliers <[EMAIL PROTECTED]> writes:
> 
>>Nope. Python *is* typed. But it doesnt confuse implementation 
>>with semantic.
> 
> 
> Python is typed.  And its type system may look strange for anyone who
> did only Java or C++ programming before :>

Of course, in that Python is dynamically typed as opposed to the static 
typing of Java or C++. Please excuse my previous mis-wording :)

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


Re: How to get/set class attributes in Python

2005-06-12 Thread Chris Spencer
Peter Dembinski wrote:
> Chris Spencer <[EMAIL PROTECTED]> writes:
> 
> 
>>Peter Dembinski wrote:
>>
>>Of course, in that Python is dynamically typed as opposed to the
>>static typing of Java or C++. Please excuse my previous mis-wording :)
> 
> 
> Mis-wording?

I previously said Python was "untyped", when it's more accurate to say 
"dynamically typed".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: searching for IDE

2005-06-12 Thread Chris Spencer
jean-marc wrote:
> if you are familiar with eclipse, you could use the PyDev python
> plugin.
> 
> jm

Thanks for that reference. I had been using SPE for my Python editing, 
but it's gone unmaintained as of late, so I've been looking for an 
alternative. Aside from an autocomplete bug (which I've reported) 
Pydev's been working great for me.

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


Re: [Py2exe-users] py2exe 0.6.9 released

2008-11-16 Thread Chris Spencer
After I "compile" my program with py2exe 0.6.9 with Python 2.6, I'm
still getting the "Application Did Not Initialize Properly" error
dialog whenever I run my code.  What am I doing wrong?

Note that py2exe 0.6.9 with Python 2.5 works just fine.

Help!

Chris.


On Sat, 15 Nov 2008 23:21:15 -0800, Jimmy Retzlaff
<[EMAIL PROTECTED]> wrote:

>py2exe 0.6.9 released
>=
--
http://mail.python.org/mailman/listinfo/python-list