469,337 Members | 7,865 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,337 developers. It's quick & easy.

Something like the getattr() trick.

I'm working with the following class heirarchy (I've snipped out the code
from the classes):

class Vuln:
def __init__(self, url):
pass

def _parse(self):
pass

def get_link(self):
pass

class VulnInfo(Vuln):
pass

class VulnDiscuss(Vuln):
pass

def main(url):
vuln_class = ['Info', 'Discuss']
vuln = Vuln(url)
vuln._parse()
for link in vuln.get_link():
i = VulnInfo(link)
i._parse()
d = VulnDiscuss(link)
d._parse()
Is there a way to get references to VulnInfo and VulnDiscuss objects using
something like the getattr trick? For example, something like:

for _class in vuln_class:
class_obj = getattr('Vuln%s' % (_class,) ..)
a = class_obj(link)
a._parse()

getattr() takes an object as its first argument. I can't seem to figure
out how to make it work here.

--
Ayaz Ahmed Khan

A witty saying proves nothing, but saying something pointless gets
people's attention.
Feb 10 '07 #1
3 1256
On Feb 10, 3:34 pm, Ayaz Ahmed Khan <a...@dev.slash.nullwrote:
I'm working with the following class heirarchy (I've snipped out the code
from the classes):

class Vuln:
def __init__(self, url):
pass

def _parse(self):
pass

def get_link(self):
pass

class VulnInfo(Vuln):
pass

class VulnDiscuss(Vuln):
pass

def main(url):
vuln_class = ['Info', 'Discuss']
vuln = Vuln(url)
vuln._parse()
for link in vuln.get_link():
i = VulnInfo(link)
i._parse()
d = VulnDiscuss(link)
d._parse()

Is there a way to get references to VulnInfo and VulnDiscuss objects using
something like the getattr trick? For example, something like:

for _class in vuln_class:
class_obj = getattr('Vuln%s' % (_class,) ..)
a = class_obj(link)
a._parse()

getattr() takes an object as its first argument. I can't seem to figure
out how to make it work here.

--
Ayaz Ahmed Khan

A witty saying proves nothing, but saying something pointless gets
people's attention.
eval('Vuln' + _class)
or,
Vuln.Discuss = VulnDiscuss
getattr(Vuln, _class)

Feb 10 '07 #2
This is a really common question. What you really need here is to
lookup some value (one of the two classes) by a key (the names of the
classes). Does that sound like something familiar? You seem to need a
dictionary, where you think you want lookup some global objects by
name.

Alternatively, if you use new-style classes (by`inheriting the object
class in your base class), you could perhaps add a method such as
getSubClass() like:

class Vuln(object):
...
@classmethod
def getSubClass(cls, name):
for c in cls.__subclasses__():
if c.__name__ == name:
return c
raise ValueError("No subclass named '%s' found." % name)

Of course, this only makes sense if you needs dont extend outside the
pattern of looking up subclasses by name. It has the advantage that
you can also put the subclasses in other modules and still look them
up from one place.

On 2/10/07, Ayaz Ahmed Khan <ay**@dev.slash.nullwrote:
I'm working with the following class heirarchy (I've snipped out the code
from the classes):

class Vuln:
def __init__(self, url):
pass

def _parse(self):
pass

def get_link(self):
pass

class VulnInfo(Vuln):
pass

class VulnDiscuss(Vuln):
pass

def main(url):
vuln_class = ['Info', 'Discuss']
vuln = Vuln(url)
vuln._parse()
for link in vuln.get_link():
i = VulnInfo(link)
i._parse()
d = VulnDiscuss(link)
d._parse()
Is there a way to get references to VulnInfo and VulnDiscuss objects using
something like the getattr trick? For example, something like:

for _class in vuln_class:
class_obj = getattr('Vuln%s' % (_class,) ..)
a = class_obj(link)
a._parse()

getattr() takes an object as its first argument. I can't seem to figure
out how to make it work here.

--
Ayaz Ahmed Khan

A witty saying proves nothing, but saying something pointless gets
people's attention.
--
http://mail.python.org/mailman/listinfo/python-list

--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/
Feb 10 '07 #3
On 2/10/07, Ayaz Ahmed Khan <ay**@dev.slash.nullwrote:
>class Vuln:

class VulnInfo(Vuln):

class VulnDiscuss(Vuln):

def main(url):
vuln_class = ['Info', 'Discuss']
vuln = Vuln(url)
vuln._parse()
for link in vuln.get_link():
i = VulnInfo(link)
i._parse()
d = VulnDiscuss(link)
d._parse()
Is there a way to get references to VulnInfo and VulnDiscuss objects
In addition to what other people has suggested:

- Use some kind of registry:
register("Vuln", Vuln, VulnInfo, VulnDiscuss)
(a dictionary would do) and then look up by name

- If all three classes are contained inside the same module, look up them
by name into globals: InfoClass = globals()["%sInfo" %
self.__class__.__name__]
(assuming self is an instance of Vuln, this would give you the VulnInfo
class)

- Replace your line: vuln_class = ['Info', 'Discuss']
with vuln_class = [VulnInfo, VulnDiscuss]

As you see, there are many ways to do that - choose what better fits your
needs.

--
Gabriel Genellina

Feb 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by daishi | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
4 posts views Thread by Hole | last post: by
3 posts views Thread by Jm lists | last post: by
2 posts views Thread by Sledge | last post: by
8 posts views Thread by Gregor Horvath | last post: by
9 posts views Thread by Gabriel Rossetti | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.