Nop, doet het niet.  Maar toch dank voor je suggestie.

 

Frans

 

Van: python-nl-bounces+fs=de-bleek.demon...@python.org 
[mailto:python-nl-bounces+fs=de-bleek.demon...@python.org] 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 <f.schnei...@de-bleek.demon.nl>

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
Python-nl@python.org
http://mail.python.org/mailman/listinfo/python-nl

Antwoord per e-mail aan