how to use new AST goodies

2005-10-24 Thread Simon Burton
Hello,

i've just found out that the ast-branch has been merged into the main
python cvs.
** Woohoo ! **
I'd like to experiment with this, does anyone know where to start ?
It seems that the parser module still produces the same junk as before.
So where do we find these nice high level AST objects ?

thanks,

Simon.

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


Re: namespace dictionaries ok?

2005-10-24 Thread Simon Burton

Yes!

I do this a lot when i have deeply nested function calls
a->b->c->d->e
and need to pass args  to the deep function without changing the
middle functions.

In this situation I think i would prefer this variation:

class Context(dict):
  def __init__(self,**kwds):
dict.__init__(self,kwds)
  def __getattr__(self, name):
return self.__getitem__(name)
  def __setattr__(self, name, value):
self.__setitem__(name, value)
  def __delattr__(self, name):
self.__delitem__(name)

def foo(ctx):
   print ctx.color, ctx.size, ctx.shape

foo( Context(color='red', size='large', shape='ball') )


This is looking like foo should be a method of Context now,
but in my situation foo is already a method of another class.

Simon.

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


Re: namespace dictionaries ok?

2005-10-25 Thread Simon Burton
In my case the deeply nested function calls are recursive calls for a
tree traversal.
This is similar to the visitor design pattern, where the Context class
above is the Visitor. The difference is that the Context instance does
not "visit" or "act" upon the nodes, but just stores state/context
information about the current traversal.

Simon.

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


linking one extension module to another (Mac OSX)

2005-11-21 Thread Simon Burton

Hi,

I'm having some trouble linking one extension module to another because
the linker expects a "lib" prefix and my python modules cannot have
this prefix.

I found two ways of doing it on a linux box (either symlink or create a
dummy .so that links to extension module) but I can get neither of them
work on OSX (let alone windows).

Simon.

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


if not DEBUG: log = null_log

2005-07-12 Thread Simon Burton

Hi,

I'm after a no-op command, so that i can redirect
logging commands in performance critical code.

Something like this:

def log(*args): print args
def null_log(*args): pass
if not DEBUG: log = null_log

is unacceptable because of the overhead of calling
functions in python.

log ("about to slip into python feature request mode.")

Maybe this is what the PEP 336 guy was thinking of (Make None Callable).
Obviously we don't want None to be callable, but
what about a "Null" [1] that's callable, with any args ?

But I guess what I am really asking for is something on the bytecode
level that tells the VM to "do nothing". 

Here's an idea: make "pass" into an expression (a value) that is callable, 
with any args, and returns None.

log ("finished with python feature request mode.")

I recently discovered "pyc" [2], but i don't quite see how i can
use it while maintaining python source compatability.

bye!

Simon.

[1]: http://occs.cs.oberlin.edu/~jwalker/nullObjPattern/
[2]: http://students.ceid.upatras.gr/~sxanth/pyc/

-- 
Simon Burton, B.Sc.
Licensed PO Box 8066
ANU Canberra 2601
Australia
Ph. 61 02 6249 6940
http://arrowtheory.com 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: namespaces

2005-08-09 Thread Simon Burton
I've been needing a module level __getattr__ for some c library
wrapping. This has solved the problem:

# mymod.py:
import sys
from new import module
class ModuleProxy(module):
  def __init__( self, name, master ):
module.__init__( self, name )
self._master = master
self.__dict__["__all__"] = dir(master)
  def __getattr__(self, name):
attr = getattr( self._master, name )
return attr

# ... end of file:
sys.modules["mymod"] = ModuleProxy("mymod",sys.modules["mymod"])


--Simon Burton

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


PyCon Vs. Europython

2006-01-18 Thread Simon Burton
My employer has given me the choice to go to either Pycon or Europython
this year,
and I need some help deciding which would be more useful (for me, my
company, and python itself).

I am mainly interested in scientific/engineering applications of
python; and am
keen to work on/learn about some of the more esoteric python projects,
such as the AST integration,
and PyPy. Participating in a sprint would be brilliant.

The line-up of talks at this year's PyCon looks more interesting to me,
compared to Europython 2005.

thanks,

Simon Burton.

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


find class where method was defined ?

2006-09-19 Thread Simon Burton

>>>
>>> class A(object):
...   def foo(self): pass
...
>>>
>>> class B(A):
...   pass
...
>>>
>>> b=B()
>>> b.foo
>

How can I work out what class b.foo was defined in ?


Simon.

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


Re: find class where method was defined ?

2006-09-19 Thread Simon Burton

def methclass(meth):
cls = meth.im_class
name = meth.im_func.__name__
meth = getattr(cls,name,None)
for cls in cls.mro():
_meth = getattr(cls,name,None)
if _meth is not None and _meth==meth:
result = cls
return result


>>> methclass(b.foo)



(Turns out equality bypasses the methodwraper trickery.)

Simon.

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


compile your python programs with rpython

2007-01-15 Thread Simon Burton
The pypy'ers have written a brief description on
using rpython to create standalone executables:

http://codespeak.net/pypy/dist/pypy/doc/standalone-howto.html

This is definately worth playing around with, it's very nice writing
(r)python code that gets executed as if it were c code.

Simon.

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


realization: no assignments inside expressions

2006-05-18 Thread Simon Burton

I've been doing a little c programming again (ouch!) and it's just hit
me
why python does not allow assignment inside expressions (as in c):
because it is absolutely essential that all assignments are as visible
as possible. In python the assignment is the declaration; when
declarations
are as cheap as this it's hard not to fall to the temptation to re-use
a
variable unknowingly. I am constantly scanning the line of assignments
to see what variables are already live, and I just realised how easy it
is
to do that. Up to now I guess I thought it was just a restriction to
stop new
users of python from writing too obfuscated code.

wow.

Simon.

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