Re: Proxying every function in a module



Josh West wrote:
First off, don't attempt to start a new thread by replying to a previous one. Many newsreaders will merge the two, confusing the hell out of everyone and generally not helping.

Ahh, yes. I see what you mean. Explains why it didn't appear the first time I posted (until later..).

Sorry for bother and thanks for the advice.
Second, what makes you think you need a module? I'd have thought an instance of some user-defined class would have been better, as that way you can redefine the __getattr__() method to return appropriate functions.
The functions are ported from a java class static methods. I was trying to be "pythonic" - since my 100 functions don't share state, I thought they should be packaged as a module rather than as a class of bunch of effectively static methods.
This seems to work, though I haven't tested it extensively (i.e. I have called one instance precisely once ;-)

>>> import re
>>> pat = re.compile("([a-z]+)(.+)")
>>> class myRewriter:
... def srt(self, s):
... m = pat.match(s)
... if not m: raise ValueError(s)
... return m.group(1), m.group(2).lower()
... def __getattr__(self, name):
... n1, n2 = name.split("_")
... def f(val):
... s1, s2 = self.srt(val)
... return "/%s/%s/?sort=%s_%s" % \
... (n1, n2, s1, s2)
... return f
...
>>> r = myRewriter()
>>> r.organisations_list('dateCreated')
'/organisations/list/?sort=date_created'
>>>

regards
Steve
Genius! Embarrassingly, I hadn't realised that __getattr__() is called when a method is invoked, thus making the method name (attribute name) so easily available as a string. I was therefore thinking in terms of gnarly introspection/reflection things. This is much better.

Thanks very much


A pleasure. Welcome to Python, where genius is available to us all.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

.



Relevant Pages

  • Another try at Pythons selfishness
    ... Python programmer who doesn't particularly like python's "self" ... def bar: ... used for static methods. ...
    (comp.lang.python)
  • Trying to come to grips with static methods
    ... I've been doing a lot of reading about static methods in Python, ... def foo: ... unbound method foo() must be called with D instance as first ...
    (comp.lang.python)
  • Re: String and StringBuilder sealed ... another WHY
    ... But the post was about the String and StringBuilder classes, ... > Sherif ElMetainy wrote: ... >> static methods that manipulates strings. ... >> Best regards, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Calling a static method by name
    ... want case sensitivity. ... Regards, ... > Hi Joe, ... > The class which contains the static methods is not intantiable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Static?
    ... I would probably create static methods. ... Regards, ... Jose Luis Manners, MCP ...
    (microsoft.public.dotnet.framework)