SRL Archive - Development

Alt. (auto generated?) libmml wrappers for python?


Wizzup?

2011-01-24 15:49:42

Was making something similar for a different project and then suddenly realized we could use this with pymml. What it basically does is add a set of functions to an existing class, dynamically. Then that class would call the CDLL/WinDLL and all the actual function. I could probably add type / variable checking to this as well…

Not sure if it is better than the current system; just throwing it out here.

import types

_methods = { 'find_color' : ('find_color', """Finds color""") }

class B(object):
def __getattr__(self, name):
print 'Calling...'
# Call CTypes CDLL/WinDLL here

if name in ('find_color',):
return lambda *x: 42
else:
raise AttributeError, name

class A(object):
def __init__(self, b):
self.b = b

for x, y in _methods.iteritems():
caller = (lambda name: lambda self, *args: getattr(self.b,name)(*args))(y[0])
caller.__doc__ = y[1]
setattr(A, x, types.MethodType(caller, None, A))

a = A(B())
a.find_color(x, y)

Nava2

2011-01-24 18:06:27

A bit more explanation would be nice.

moparisthebest

2011-01-25 17:04:18

Excellent, this way python development won't constantly lag behind the rest of the development, and the python interpreter can be embedded in Simba ASAP, right? :D

Wizzup?

2011-01-26 17:16:13

From: Nava2
A bit more explanation would be nice.


Basically, you can add methods to a class on the fly; runtime. In this case a list / dict of functions would suffice to quickly generation functions. CDLL also supports this, because you just call the function by the name. EG: mydll.function_name(). So we wouldn't have to write all the methods ourself. Although most (TPA ones, etc) will probably require quite some wrapper, so I'm unsure if this will actually work properly.

I'm not really sure if this will benefit libmml though… May become too complex. Really, libmml lags behind because I don't have a lot of time to work on it. It's really not that hard to extend it.

Nava2

2011-01-27 15:38:21

From: Wizzup?
Basically, you can add methods to a class on the fly; runtime. In this case a list / dict of functions would suffice to quickly generation functions. CDLL also supports this, because you just call the function by the name. EG: mydll.function_name(). So we wouldn't have to write all the methods ourself. Although most (TPA ones, etc) will probably require quite some wrapper, so I'm unsure if this will actually work properly.


Well, what we *could* do is use this to print out all the necessary functions and save some effort.

But the big issue I see is documentation lacking here.

Wizzup?

2011-01-29 05:36:26

From: Nava2
Well, what we *could* do is use this to print out all the necessary functions and save some effort.

But the big issue I see is documentation lacking here.


__doc__ = y[1] ?

Sphinx uses __doc__ for autogeneration, so it works. ;)

Example: http://docs.wizzup.org/pyroTorrent/