Geprobeerd, maar werkt ook niet. Verwijzen de callables nu ook niet naar
unbound methods?
Ik zag iets met een constructie als deze:
class clips_callable(object):
def __init__(self, func):
self.func = func
def __get__(self, obj, type=None):
f = self.__class__(self.func.__get__(obj, type))
print "__get__", f, self.func.__name__
clips.RegisterPythonFunction(f, self.func.__name__)
return self.__class__(self.func.__get__(obj, type))
def __call__(self, *args, **kw):
return self.func(*args, **kw)
class CLIPS(object):
.
@clips_callable
def pyprint(self, value):
print self.data, "".join(map(str, value))de functie pyprint een keer
benaderd worden:
Om dit te laten werken moet een instance gemaakt worden van CLIPS en de
functie pyprint 1 maal benaderd worden.
Dit werkt wel:
c = CLIPS("%s %s" % (p.name, p.pid))
print c.pyprint
Maar dit wekt niet
c = CLIPS("%s %s" % (p.name, p.pid))
#print c.pyprint
De hele decorator definitie gaat mij boven mijn pet en misschien dat iemand
deze kan uitleggen?
Frans
Van: [email protected]
[mailto:[email protected]] Namens Dexter
Verzonden: maandag 14 mei 2012 14:21
Aan: Dutch Python developers and users
Onderwerp: Re: [python-nl] Decorator
ow, was nog niet klaar
Jawel
class CLIPSClass(object):
callables = []
def __init__(self):
for c in self.callables:
clips.RegisterPythonFunction(c, c.__name__)
@classmethod
def clipsCallable(cls, f):
cls.callables.append(f)
class CLIPS(CLIPSClass):
@CLIPSClass.clipsCallable
def pyprint(self, value):
self.data, "".join(map(str, value))
zoiets zou moeten werken
2012/5/14 Schneider <[email protected]>
Dat zou best wel eens kunnen. Dat zou betekenen dat iets met @.... helemaal
niet gaat?
Frans
Van: [email protected]
[mailto:python-nl-bounces+fs <mailto:python-nl-bounces%2Bfs>
[email protected]] Namens Dexter
Verzonden: maandag 14 mei 2012 12:59
Aan: Dutch Python developers and users
Onderwerp: Re: [python-nl] Decorator
Ik denk dat dat komt omdat er nog geen instantie van de class CLIPS is op
het moment dat de functie meegegeven wordt aan de decorator.
2012/5/14 Schneider <[email protected]>
Nop, doet het niet. Maar toch dank voor je suggestie.
Frans
Van: [email protected]
[mailto:python-nl-bounces+fs <mailto:python-nl-bounces%2Bfs>
[email protected]] Namens Tikitu de Jager
Verzonden: maandag 14 mei 2012 11:49
Aan: Dutch Python developers and users
Onderwerp: Re: [python-nl] Decorator
Met CLIPS heb ik geen ervaring, maar je decorator doet volgens mij meer dan
echt nodig is. Ik zou zeggen:
def clips_callable(f):
clips.RegisterPythonFunction(f, f.__name__)
return f
Of dat je probleem oplost is een andere vraag...
gr,
Tikitu
2012/5/14 Schneider <[email protected]>
Dames, heren,
Omdat ik weinig ervaring met decorators en multiprocessing heb, ben ik
opzoek naar een beetje hulp.
Ik maak gebruik van CLIPS via PyClips (http://pyclips.sourceforge.net/)
waarbij CLIPS in een apart proces gestart wordt i.v.m. performance e.d. Om
Python aan te kunnen roepen vanuit CLIPS, moeten de Python functies in CLIPS
worden geregistreerd.
Meest basale vorm zonder decorators.
import clips
import multiprocessing
class CLIPS(object):
def __init__(self, data):
self.environment = clips.Environment()
self.data = data
clips.RegisterPythonFunction(self.pyprint, "pyprint")
self.environment.Load("test.clp")
self.environment.Reset()
self.environment.Run()
def pyprint(self, value):
print self.data, "".join(map(str, value))
class CLIPSProcess(multiprocessing.Process):
def run(self):
p = multiprocessing.current_process()
self.c = CLIPS("%s %s" % (p.name, p.pid))
pass
if __name__ == "__main__":
cp = CLIPSProcess()
cp.start()
Inhoud van test.clp is:
(defrule MAIN::start-me-up
=>
(python-call pyprint "Hello world")
)
Output is CLIPSProcess-1 2456 Hello world
Werkt goed. Nu wil ik heel wat "pyprint" achtige functies kunnen registreren
via iets als:
@clips_callable
def pyprint(self, value):
.
zonder dat ik steeds clips.RegisterPythonFunction hoef aan te roepen. Een
simpele decorator zoals hieronder werkt niet:
import clips
import multiprocessing
def clips_callable(f):
from functools import wraps
@wraps(f)
def wf(*args, **kwargs):
print 'calling {}'.format(f.__name__)
return f(*args, **kwargs)
clips.RegisterPythonFunction(wf, f.__name__)
return wf
class CLIPS(object):
def __init__(self, data):
self.environment = clips.Environment()
self.data = data
#clips.RegisterPythonFunction(self.pyprint, "pyprint")
self.environment.Load("test.clp")
self.environment.Reset()
self.environment.Run()
@clips_callable
def pyprint(self, value):
print self.data, "".join(map(str, value))
class CLIPSProcess(multiprocessing.Process):
def run(self):
p = multiprocessing.current_process()
self.c = CLIPS("%s %s" % (p.name, p.pid))
pass
if __name__ == "__main__":
cp = CLIPSProcess()
cp.start()
Met als output
calling pyprint
De decorator doet duidelijk niet wat ik wil. Heeft iemand misschien een
oplossing?
Met vriendelijke groet,
Frans
_______________________________________________
Python-nl mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-nl
_______________________________________________
Python-nl mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-nl
_______________________________________________
Python-nl mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-nl