Proper way of handling "plug-in" methods

2007-01-08 Thread Franck PEREZ
All,

My application deals with strings formatting. I have built-in methods
but I also expect the user to add its methods in its own .py files
(some sort of plugin methods, all user methods should be exposed in my
application).

Here is the structure I have thought of :

formatting.py
_formattingDict = {} #a dict composed of all available methods, both
builtin and user
def expose(...) : #adds a method to the dict

builtinformatting.py
import myapp.formatting.expose
@expose
def builtinMethod(inputStr) : return someOutput

/home/user/.myapp/customformatting.py
import myapp.formatting.expose
@expose
def customMethod(inputStr) : return someOutput

model.py
#References all the methods, both builtin and custom
execfile("builtinformatting.py")
execfile("/home/user/.myapp/customformatting.py")

Expected result after the execfile : formatting._formattingDict
contains the 2 methods builtinMethod and customMethod

Is this a proper way of structuring my application ? Do you see
anything better ?

Moreover, I dislike execfile("builtinformatting.py") since classic
import would do the job. However I first wanted a united method for
both builtin and custom scripts since it is the same logic for me.

Thanks very much in advance for your advice.
Franck
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Automatic class attribute

2006-02-03 Thread Franck PEREZ
On 2/3/06, Kirk McDonald <[EMAIL PROTECTED]> wrote:
> Franck PEREZ wrote:
> > Hello all,
> >
> > Considering the following code :
> >
> > class C(object):
> >...: observers = []
> >...:
> >...: @classmethod
> >...: def showObservers(cls):
> >...: print cls.observers
> >
> > class D1(C):
> >...: observers = [] #could it be moved in C ?
> >
> > class D2(C):
> >...: observers = [] #could it be moved in C ?
> >
> > I want each child class of C to have it's own "observers" class attribute.
> >
> > The code I provided works... but I'd like to avoid typing "observers =
> > []" in each child class.
> >
> > Is it possible to define something in C which would basically mean :
> > "for each child class, automatically bind a new list attribute called
> > observers" ?
> >
> > Are metaclasses a way ? Is it possible to avoid them ?
> > Thanks a lot,
> > Franck
>
> By an astounding coincidence, I was just working on a similar problem.
> Metaclasses can do this, no problem:
>
> class M(type):
>  def __init__(cls, name, bases, dict):
>  cls.observers = []
>
>  def showObservers(cls):
>  print cls.observers
>
> class C(object):
>  __metaclass__ = M
>
> class D1(C): pass
> class D2(C): pass
>
> -Kirk McDonald
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Works great. Thanks a lot.
-- 
http://mail.python.org/mailman/listinfo/python-list


Automatic class attribute

2006-02-03 Thread Franck PEREZ
Hello all,

Considering the following code :

class C(object):
   ...: observers = []
   ...:
   ...: @classmethod
   ...: def showObservers(cls):
   ...: print cls.observers

class D1(C):
   ...: observers = [] #could it be moved in C ?

class D2(C):
   ...: observers = [] #could it be moved in C ?

I want each child class of C to have it's own "observers" class attribute.

The code I provided works... but I'd like to avoid typing "observers =
[]" in each child class.

Is it possible to define something in C which would basically mean :
"for each child class, automatically bind a new list attribute called
observers" ?

Are metaclasses a way ? Is it possible to avoid them ?
Thanks a lot,
Franck
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] XML Serializer module

2006-01-12 Thread Franck PEREZ
Dear all,

I wished to announce here my first Python module : XML Serializer. It
recursively serializes and deserializes Python objects and their
children. Children may be an instance's attributes, or list/dict/tuple
elements.

When serializing an instance, you may customize the attributes which
are dumped, and the constructor values. In simple cases, the module
finds out the constructor attributes by itself.

You may download the module here, and see documentation and examples :
http://eleves.ec-lille.fr/~perezf/python/

Any comment welcomed !
Franck
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: books: Dive into Python vs Beginning Python

2005-11-25 Thread Franck PEREZ
On 11/25/05, Sebastien Douche <[EMAIL PROTECTED]> wrote:
> On 11/25/05, Franz Mueller <[EMAIL PROTECTED]> wrote:
> > Hi,
>
> Hi Franz! :)
>
> > which of the following books would you recommend:
> > "Dive into Python" or "Beginning Python: From Novice to Professional"?
>
> Both are very good books but I suggest the latter because more recent.
> Beginning Python talk python 2.3 and 2.4 : set data structure,
> generator, iterator...
>

"Dive into Python" actually mentions generators :
http://diveintopython.org/dynamic_functions/stage6.html

And it's a very well written book btw.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Importing a class without knowing the module

2005-11-18 Thread Franck PEREZ
On 11/18/05, Mike Meyer <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] (Alex Martelli) writes:
> > Mike Meyer <[EMAIL PROTECTED]> wrote:
> >...
> >> >> >> How about adding Foo.__file__ to the serialized data?
> >...
> >> >> depends on somewhere on it. You can use the module name if you have it
> >> >> available. If not, deriving the module name from the file name is
> >> >> about the best you can do.
> >> > I disagree with the last sentence.  From a filepath of
> >...
> >> You should read the next-to-last sentence, which says to use the
> >> module name if you have it. The last sentence starts "If not" -
> >> meaning you don't have the module name. *That's* the case for which
> >> the file name is about the best you can do.
> > I see!  Thanks for clarifying.  Could you please give me an example of a
> > Foo class which has a Foo.__file__ attribute but not a Foo.__module__
> > attribute?  Sorry, must be some moment of weakness on my mind's part
> > (quite possible, since I am recovering from recent surgery), but I
> > cannot think of a situation where that would be the case (while classes
> > with __module__ and w/o __file__ are the normal situation).  Were there
> > no case in which, given a class, you can learn about its file (by a
> > __file__ attribute) but not about its module (by a __module__
> > attribute), I would of course hope that my inability to parse that
> > sentence of yours, which would under such hypothetical circumstaces be
> > an absurd hypothesis, might be more forgivable.
>
> A classes __module__ attribute doesn't always tell you the name of the
> module - or at least, not a name that would be usefull for the the OPs
> use case. That's the case where you don't have the module name.  The
> reference to a classes __file__ attribute was meant to be to the
> modules __file__ attribute - I'm surprised that no one picked up on
> that. Again, assuming that the module has an __file__ attribute at
> all. Getting the __file__ attribute to a module you don't know the
> name of is a bit tricky, but possible.
>
>  --
> Mike Meyer <[EMAIL PROTECTED]>  
> http://www.mired.org/home/mwm/
> Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Thanks for your answers. And btw, sorry for top-posting, I'll be more
careful next time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Importing a class without knowing the module

2005-11-17 Thread Franck PEREZ
I thought about it, but it would make the XML file depend on the
machine... no more portability...

On 11/18/05, Mike Meyer <[EMAIL PROTECTED]> wrote:
> Franck PEREZ <[EMAIL PROTECTED]> writes:
> > ### My test application 
> > class Foo(object):
> >   #The class I'd like to serialize
> >   pass
> >
> > import myMarshaller
> > foo = Foo()
> > s = myMarshaller.dumps(foo) #works fine, spits something like  > class = "Foo"...>
> > another_foo = loads(s) #fails, see below
> >
> > ### My marshaller (in its own module) 
> > def loads(s):
> >   #First, get class name (here "Foo")
> >   klass = eval(className) #fails because "Foo" is not in the
> > marshaller's namespace !
> >
> > How could I tell the marshaller to locate the Foo class and any other
> > class I try to deserialize ?
>
> How about adding Foo.__file__ to the serialized data?
>
>  --
> Mike Meyer <[EMAIL PROTECTED]>  
> http://www.mired.org/home/mwm/
> Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Importing a class without knowing the module

2005-11-17 Thread Franck PEREZ
Hello,

I'm developing a small XML marshaller and I'm facing an annoying
issue. Here's some sample code:

### My test application 
class Foo(object):
  #The class I'd like to serialize
  pass

import myMarshaller
foo = Foo()
s = myMarshaller.dumps(foo) #works fine, spits something like 
another_foo = loads(s) #fails, see below

### My marshaller (in its own module) 
def loads(s):
  #First, get class name (here "Foo")
  klass = eval(className) #fails because "Foo" is not in the
marshaller's namespace !

How could I tell the marshaller to locate the Foo class and any other
class I try to deserialize ? I've tried to pass my test application's
globals() to the marshaller, it works but it's dirty IMHO... I've
tried also to locate the class (here "Foo") somewhere in sys.modules
in the "loads" method, but it was heavy and unsuccessful.

Thanks a lot for your help !
Franck
-- 
http://mail.python.org/mailman/listinfo/python-list