Re: Extend Python

2005-09-01 Thread Filip Dreger
> My Question:
> Swig offers some great features but is to basic for us. Is there
> another program that creates more readble code that can be easily
> edited? How much work is it to write our own wrappers?

Not too much, and practicaly not at all if you want to wrap procedures 
(not objects or types):
 - marshaslling an argument list from Python to C and from C to Python 
takes exactly 1 line of code,
 - handling errors takes 2 lines of code (unless you want to do 
something fancy with them; I mean just catching an exception and 
getting the error message).
 - entry in the PyMethodDef is one simple line
Bottomline: you need 5 additional lines of C code per procedure to 
make it usable from Python.
Unless you have hundreds of procedures, there is no point in using 
special tools to do that. Especially if you need full control over the 
results.

regards,
Filip Dreger
-- 
Akademia Linuksa - http://www.akademia.linuksa.pl 


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


func_code vs. string problem

2005-04-23 Thread Filip Dreger
Each function has a func_code property that is suposed to contain the 
pure bytecode of the function. All the context (including reference to 
relevant namespaces) is stored in different fields of the function 
object. Since 'exec' is able to execute any string or bytecode in the 
current scope, it would seem possible to execute code of any function 
in any namespace. But no matter how I tried, I could not do it. There 
must be something I am missing.
Here's what I do:(if anyone wants to help, I placed the source 
under http://www.bajobongo.net/foo.py - tested on Python 2.4.1)

1. I declare a function. In the next steps I will try to run its code 
from inside a class:

def myfunction():
   print abc
   self.test()

2. I declare a class foo, with two methods. The first one tries to 
reach some local variables from a string passed to exec. The other one 
tries to do the same from inside a bytecode (from myfunction). IMHE 
this should make no difference to 'exec' - [spoiler: it does].

class foo:
   def test(self):
  print "ABC"
   def checkfunction(self):
  abc=10
  exec myfunction.func_code
   def checkstring(self):
  abc=10
  exec "print abc;self.test()"

3. I test the both methods. Sadly, the 'checkfunction' fails to find 
the correct namespace (id does not see 'abc' nor 'self'). Adding 
things like:
"exec myfunction.func_code in globals(),locals()" does not help.

i=foo()
i.checkstring()
i.checkfunction()  # this throws exception; why???

4. I try to find some help here, and hope to also gain better 
undesrtanding of how Python works :-)

Thanks for any suggestions,
regards,
Filip Dreger 


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


Re: func_code vs. string problem

2005-04-23 Thread Filip Dreger
I came up with a simpler description of the problem.
It's all in the simple source:

# we define 'print b' in three different ways: as a string,
# a bytecode and a function
string="print b"
code=compile(string,'string','exec')
def function():
print b

# now we make functions that test if it is possible to execute 'print 
b'
# in some local scope

def execstring():
b=5
exec string

def execfunction():
b=5
exec function.func_code

def execcode():
b=5
exec code

execstring()   # works
execcode() # works
execfunction() # throws name error exception...

My problem is that both 'string' and 'code' are references to code 
objects, so they _should_ behave in the same way... I am close to 
giving up...
I am trying to find a way of executing functions without creating a 
nested scope, so they can share local and global namespace (even if 
they are declared in some other module). I _could_ turn them into 
strings and pass around as compiled objects, butthis would be very 
ugly. I am sure Python has some better, cleaner way to do this.

regards,
Filip Dreger 


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


Re: func_code vs. string problem

2005-04-23 Thread Filip Dreger

Uzytkownik "Steven Bethard" <[EMAIL PROTECTED]> napisal w 
wiadomosci news:[EMAIL PROTECTED]
> Filip Dreger wrote:
>> I am trying to find a way of executing functions without creating a 
>> nested scope, so they can share local and global namespace (even if 
>> they are declared in some other module).
>
> Why?  Can you explain what the use case is?

I have a large set of objects of a single class - 'actor'. They are 
created all at once, and they all live in a sort of container written 
in C. The methods of the functions are few and general. One of them is 
simply 'act()', that is called by the C program every X seconds (tics, 
whatever).
Each object can play 0..N roles, and the roles played by an object 
often change during its lifetime. The roles should be played in the 
act() method. Also, the roles should be defined in some simple way, 
and in an external python file (so adding/removing roles does not 
require recompiling of the parts embedded in C).

If I had a magic function 'exec in current scope' I would implement it 
like this:

class actor:
  def __init__():
self.roles=[]
  def act():
for i in self.roles:
  exec i in current scope

then the roles would simply be functions defined in any importable 
file. For example creating an actor that logs each time it is called 
would be as simple as:

import actor

def log():
  self.counter+=1
  print "called %d times"%self.counter

a=actor.actor()
a.counter=0
a.roles.append(log)

Let me recapitulate (is that the right English word?):
1. I need to keep the definitions of roles in a separate file (they 
can not simply be additional methods)
2. I need the roles to have full access to global and local namespace 
of the actor object (sometimes they have to change it, and sometimes 
they have to use some external modules) - the roles should work like 
plugins.
3. I guess I also need a fresh look at the problem. Maybe I am simply 
going the wrong way? I am more than willing to change my design, if 
someone shows me the light :-)

regards,
Filip Dreger 


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


Re: func_code vs. string problem

2005-04-23 Thread Filip Dreger
Uzytkownik "Steven Bethard" <[EMAIL PROTECTED]> napisal w 
wiadomosci news:[EMAIL PROTECTED]

> See the documentation:
>
> http://docs.python.org/ref/dynamic-features.html
>
> """The eval(), execfile(), and input() functions and the exec 
> statement do not have access to the full environment for resolving 
> names. Names may be resolved in the local and global namespaces of 
> the caller. Free variables are not resolved in the nearest enclosing 
> namespace, but in the global namespace."""

Thank you! I feel silly I have not found the piece of instruction 
myself. And even worse, as I still do not understand: if exec() 
resolves free bariable names in the global namespace, how come it 
works well with code in a string? Or with a code in a compiled string?

> Note the last sentence, which tells you that your free variable, 
> 'abc', will be resolved in the *global* namespace.  In your 
> particular problem, you can solve this by substituting your local 
> namespace for the global namespace.

This particular problem - sadly - is a simplified version of the real 
one. I need access to both: global and local namespaces, so this 
solution is not for me.
The manual says I can pass two dictionaries to exec, one for global 
namespace and one for local. But, strange as it seems, I could not get 
it to work. In the example I gave, changing exec f.func_code to exec 
f.func_code in globals(),locals() does not help a bit.

> One possible workaround might be:
>
> py> class foo:
> ... def test(self):
> ... print "ABC"
> ... def checkfunction(self):
> ... abc=10
> ... l = locals()
> ... l.update(globals())
> ... exec myfunction.func_code in l
> ... def checkstring(self):
> ... abc=10
> ... exec "print abc;self.test()"
> ...
> py> foo().checkfunction()
> __main__.foo
> 10
> ABC

I thought about that... I don't know why, but it seems wrong. Maybe 
the updating dictionaries is not very expensive, but there must be 
some better way. This code has a 'workaround - fixme' written all over 
it.
Up to now, each time I encountered such a workaroundish solution, it 
turned out Python has a cleaner way to do it. I sure hope this is also 
the case here :-).

> But I'd have to know what your real use case is to tell you whether 
> or not this is worth the trouble.  Why do you want to exec the 
> func_code anyway?  Why can't you just call the function?

I put the description in the other post. Perhaps it's jkust my design 
that's broken.
Thanks again.

regards,
Filip Dreger


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


Re: func_code vs. string problem

2005-04-24 Thread Filip Dreger

Uzytkownik "Steven Bethard" <[EMAIL PROTECTED]> napisal w 
wiadomosci news:[EMAIL PROTECTED]
> Any reason you can't define it like:
>
> class actor(object):
> def __init__(self):
> self.roles = []
> def act(self):
> for role_func in self.roles:
> role_func(self)
[snip]
> By importing actor, they have full access to the global namespace of 
> the actor.actor object, by simply accessing the actor module 
> attributes.
>
> So the issue here is really about full *local* namespace access.  Do 
> you really need *full* local namespace access?  Why isn't access to 
> the actor.actor instance sufficient?

!!! Yep, of course it is sufficient. Abondoning the obvious 
role_func() must have had some good reasons some time ago, but now I 
can not even remember them, probably they were not so important :-)
Thanks a million,
Filip 


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


Re: Can .py be complied?

2005-04-27 Thread Filip Dreger

U¿ytkownik "monkey" <[EMAIL PROTECTED]> napisa³ w wiadomo¶ci 
news:[EMAIL PROTECTED]

> If using Jython to complie, is the end-user need JRE instead of 
> Python
> installed, or need both of them?

Only JRE. Just like Java.

>> I don't know the exact details, but try using the compiled Python
>> scripts (bytecode). I believe they are semi-optimized and platform
>> independent. They are the .pyc and .pyo files generated when the 
>> script
>> is run.
> Is that means a .py convert to .pyc or .pyo, without the need of 
> "make file"
> as using py2exe?

.pyc files are generated every time a module (any .py file can be a 
module) is imported. So if you have a program, say, example.py, you 
just start the python interpreter and write:
>>> import example
And then example.pyc will appear beside example.py. This new file does 
not require example.py (you can even delete it), and works on any 
computer with Python installed (on Windows you can just double-click 
it)
If you start the Python interpreter using:
python -OO (if you are using Windows, you shoud start the interpreter 
from the command line, probably something like:
c:
cd \
python24\python -OO)
and then import your example.py, you will get a file example.pyo, 
which is also stripped of any documentation strings (a bit harder to 
decode).

regards,
Filip Dreger 


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


Re: why "import wx" doesn't work?

2005-04-28 Thread Filip Dreger

U¿ytkownik "monkey" <[EMAIL PROTECTED]> napisa³ w wiadomo¶ci 
news:[EMAIL PROTECTED]
>> Which version of wxPython are you running? What do you mean by 
>> "does not
>> work"...does the import fail or is your code giving errors?
>
> It is the current new version 2.6. The error message said that the 
> class
> wxApp is not defined...

This is very good! wxApp is never defined if you use "import wx". You 
must use "wx.wxApp" instead.

If you import a module using "import anything", then all the names 
imported from the module must begin with "anything.". If you import wx 
using "import wx", then ALL the wx commands, classes and variables 
(all the names) MUST begin with 'wx.". Change them, and your program 
will work.

"from wx import *" is a special shortcut, allowing you to use all the 
names without "wx.". If you change "from something import *" to 
"import something", your code will always break, this is normal.

regards,
Filip Dreger 


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