Quantcast

Decorator

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Decorator

Schneider-11

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Ronald Oussoren

On 14 May, 2012, at 11:25, Schneider wrote:

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?

De decorator wordt aangeroepen bij het uitvoeren van de class definitie, dus voordat CLIPS instantie gemaakt wordt.

Om met de decorator hetzelfde gedrag te krijgen als zonder decorator moet je de de decorator in twee-en splitsen: de decorator zelf markeert de functies als clips_callable (door ze in een lijstje te plaatsen, of een functie attribuut toe te voegen), en in __init__ kan je daarna RegisterPythonFunction aanroepen voor alle functies die je eerder gemarkeerd hebt.  

Als alle "pyprint" functies methoden van de CLIPS klasse zijn zou je ook het patroon kunnen gebruiken dat in cmd.Cmd in de stdlib gebruikt wordt: geeft alle CLIPS functies een naam met een specifieke prefix (bijvoorbeeld "def clips_pyprint(self, value): ...") en maak in __init__ een loop over alle methoden van de klasse en registreer de methoden waarvan de naam met deze prefix begint.

Ronald


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl

smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Tikitu de Jager -- Buzzcapture
In reply to this post by Schneider-11
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 <[hidden email]>

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Schneider-11

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:python-nl-bounces+fs=[hidden email]] 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 <[hidden email]>

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Dexter-24
In reply to this post by Tikitu de Jager -- Buzzcapture
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 <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl



_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Schneider-11

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:python-nl-bounces+fs=[hidden email]] 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 <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl

 


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Schneider-11
In reply to this post by Ronald Oussoren

Ok. Dat klinkt logisch. Kijk ik naar.

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:python-nl-bounces+fs=[hidden email]] Namens Ronald Oussoren
Verzonden: maandag 14 mei 2012 11:44
Aan: Dutch Python developers and users
Onderwerp: Re: [python-nl] Decorator

 

 

On 14 May, 2012, at 11:25, Schneider wrote:



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?

 

De decorator wordt aangeroepen bij het uitvoeren van de class definitie, dus voordat CLIPS instantie gemaakt wordt.

 

Om met de decorator hetzelfde gedrag te krijgen als zonder decorator moet je de de decorator in twee-en splitsen: de decorator zelf markeert de functies als clips_callable (door ze in een lijstje te plaatsen, of een functie attribuut toe te voegen), en in __init__ kan je daarna RegisterPythonFunction aanroepen voor alle functies die je eerder gemarkeerd hebt.  

 

Als alle "pyprint" functies methoden van de CLIPS klasse zijn zou je ook het patroon kunnen gebruiken dat in cmd.Cmd in de stdlib gebruikt wordt: geeft alle CLIPS functies een naam met een specifieke prefix (bijvoorbeeld "def clips_pyprint(self, value): ...") en maak in __init__ een loop over alle methoden van de klasse en registreer de methoden waarvan de naam met deze prefix begint.

 

Ronald

 


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Dexter-24
In reply to this post by Dexter-24
Jawel

class CLIPSClass(object):
    callables = []
    
    @classmethod


class CLIPS(CLIPSClass):
    @clipsCallable
    def pyprint(self, value):
        self.data, "".join(map(str, value))

2012/5/14 Schneider <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl

 


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl



_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Dexter-24
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 <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl

 


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl




_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Schneider-11

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=[hidden email] [mailto:python-nl-bounces+fs=[hidden email]] 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 <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl

 


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl

 

 


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Decorator

Schneider-11
In reply to this post by Dexter-24

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: python-nl-bounces+fs=[hidden email] [mailto:python-nl-bounces+fs=[hidden email]] 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 <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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

 

Frans

 

Van: python-nl-bounces+fs=[hidden email] [mailto:[hidden email]=[hidden email]] 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 <[hidden email]>

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl

 


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl

 

 


_______________________________________________
Python-nl mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-nl
Loading...