Re: generating method names 'dynamically'

2006-01-29 Thread Fredrik Lundh
"Unknown" wrote: > >> Well, I would normally do what you suggest, using parameters, but in > >> the example at hand I have to have the method names as variables and > >> the reason is that the whole thing will be run by apache using > >> mod_python and the publisher handler. There a URL > >> http:

Re: generating method names 'dynamically'

2006-01-29 Thread Max
Magnus Lycka wrote: > Daniel Nogradi wrote: > >> Well, I would normally do what you suggest, using parameters, but in >> the example at hand I have to have the method names as variables and >> the reason is that the whole thing will be run by apache using >> mod_python and the publisher handler. T

Re: generating method names 'dynamically'

2006-01-27 Thread Daniel Nogradi
> *>* I don't get it, why is it more safe to accept GET variables than > *>* method names? Concretely, why is the URL > *>* http://something.com/script?q=parameter safer than > *>* http://something.com/script/parameter if in both cases exactly the > *>* same things are happening with 'parameter'? >

Re: generating method names 'dynamically'

2006-01-27 Thread Peter Hansen
Daniel Nogradi wrote: >>Ouch! This certainly seems like a possible security hole! >> >>As someone else said, use rewrite rules to get this passed >>in as a parameter. > > I don't get it, why is it more safe to accept GET variables than > method names? Concretely, why is the URL > http://something.

Re: generating method names 'dynamically'

2006-01-27 Thread Daniel Nogradi
> Ouch! This certainly seems like a possible security hole! > > As someone else said, use rewrite rules to get this passed > in as a parameter. I don't get it, why is it more safe to accept GET variables than method names? Concretely, why is the URL http://something.com/script?q=parameter safer th

Re: generating method names 'dynamically'

2006-01-27 Thread Magnus Lycka
Daniel Nogradi wrote: > Well, I would normally do what you suggest, using parameters, but in > the example at hand I have to have the method names as variables and > the reason is that the whole thing will be run by apache using > mod_python and the publisher handler. There a URL > http://something

Re: generating method names 'dynamically'

2006-01-27 Thread Steve Holden
Daniel Nogradi wrote: >>Here you go: >> >> >>> database = { >> ... "Alice": 24, >> ... "Bob":25} >> ... >> >>> class Lookup(object): >> ... def __catcher(self, name): >> ... try: >> ... print "Hello my name is %s and I'm %s" % (name, >>database[name]) >> ...

Re: generating method names 'dynamically'

2006-01-27 Thread Bengt Richter
On Fri, 27 Jan 2006 01:20:52 +0100, Daniel Nogradi <[EMAIL PROTECTED]> wrote: >> > Is it possible to have method names of a class generated somehow >> dynamically? >> > >> > More precisely, at the time of writing a program that contains a class >> > definition I don't know what the names of its ca

Re: generating method names 'dynamically'

2006-01-27 Thread bruno at modulix
Daniel Nogradi wrote: > Is it possible to have method names of a class generated somehow dynamically? >>> class Dummy(object): ... pass ... >>> def mymethod(self, *args, **kw): ... pass ... >>> setattr(Dummy, 'a_dynamically_generated_method_name', mymethod) >>> >>> Dummy.a_dynamically_generate

Re: generating method names 'dynamically'

2006-01-26 Thread Peter Hansen
Daniel Nogradi wrote: > Well, I would normally do what you suggest, using parameters, but in > the example at hand I have to have the method names as variables and > the reason is that the whole thing will be run by apache using > mod_python and the publisher handler. There a URL > http://something

Re: generating method names 'dynamically'

2006-01-26 Thread Daniel Nogradi
> Here you go: > > >>> database = { > ... "Alice": 24, > ... "Bob":25} > ... > >>> class Lookup(object): > ... def __catcher(self, name): > ... try: > ... print "Hello my name is %s and I'm %s" % (name, > database[name]) > ... except KeyErro

Re: generating method names 'dynamically'

2006-01-26 Thread Michael Spencer
Daniel Nogradi wrote: ... > - database content --- > > Alice 25 > Bob 24 > > - program1.py - > > class klass: ... > > inst = klass() > > - program2.py --- > > import program1 > > # The code in klass above should be such that the following

Re: generating method names 'dynamically'

2006-01-26 Thread Daniel Nogradi
> > My database has 1 table with 2 fields, one called 'name' and the other > > one called 'age', let's suppose it has the following content, but this > > content keeps changing: > > > > Alice 25 > > Bob 24 > > > > --- program1.py > > > > class klass: > > > > # do the

Re: generating method names 'dynamically'

2006-01-26 Thread Peter Hansen
Daniel Nogradi wrote: > Thanks for all the replies, it became clear that I need to look into > getattr, __getattr__ and __call__. I'll do that, but since you asked, > here is the thing I would like to do in a little more detail: > > My database has 1 table with 2 fields, one called 'name' and the

Re: generating method names 'dynamically'

2006-01-26 Thread Daniel Nogradi
> > Is it possible to have method names of a class generated somehow > dynamically? > > > > More precisely, at the time of writing a program that contains a class > > definition I don't know what the names of its callable methods should > > be. I have entries stored in a database that are changing

Re: generating method names 'dynamically'

2006-01-26 Thread Murali
x.__class__.__dict__[mname](x,*args,**kwargs) here x is an instance of a class FOO FOO has a method "bar" (if the value of mname is "bar") args is a tuple whose length is the number of positional arguments accepted by bar kwargs is a dictionary corresponding to the keyword arguments accepted by ba

Re: generating method names 'dynamically'

2006-01-26 Thread Peter Hansen
Daniel Nogradi wrote: > Is it possible to have method names of a class generated somehow dynamically? > > More precisely, at the time of writing a program that contains a class > definition I don't know what the names of its callable methods should > be. I have entries stored in a database that ar

Re: generating method names 'dynamically'

2006-01-26 Thread Murali
import inspect x = ABC() # create an instance of class ABC print inspect.getmembers(x,inspect.ismethod) Most of any introspection stuff can be done using the module "inspect". -- http://mail.python.org/mailman/listinfo/python-list

Re: generating method names 'dynamically'

2006-01-26 Thread Farshid Lashkari
> Is it possible to have method names of a class generated somehow dynamically? If you don't know the name of the method ahead of time, how do you write the code for it? Do you use some dummy name? If so, once you have the name determined then you could use setattr to set the method name. Here'