[python-nl] Decorator

Schneider f.schneider at de-bleek.demon.nl
Mon May 14 18:49:33 CEST 2012


Werkt jammer genoeg ook niet. Er wordt wel wat geregistreerd in CLIPS, maar
niet de functie waarom het gaat. Ik kan niet goed nagaan wat er in CLIPS
gebeurd omdat zich dit diep ergens in de interface afspeelt.  De call crasht
in ieder geval als deze gemaakt wordt vanuit CLIPS.

 

Frans

 

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

Dat zou best wel eens kunnen. Dat zou betekenen dat iets met @.... helemaal
niet gaat?

 

Frans

 

Van: python-nl-bounces+fs=de-bleek.demon.nl at python.org
[mailto:python-nl-bounces+fs <mailto:python-nl-bounces%2Bfs>
=de-bleek.demon.nl at python.org] 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 <f.schneider at de-bleek.demon.nl>

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

 

Frans

 

Van: python-nl-bounces+fs=de-bleek.demon.nl at python.org
[mailto:python-nl-bounces+fs <mailto:python-nl-bounces%2Bfs>
=de-bleek.demon.nl at 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.schneider at 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 at python.org
http://mail.python.org/mailman/listinfo/python-nl

 


_______________________________________________
Python-nl mailing list
Python-nl at python.org
http://mail.python.org/mailman/listinfo/python-nl

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-nl/attachments/20120514/903bab50/attachment-0001.html>


More information about the Python-nl mailing list