473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1365
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: daishi | last post by:
Hi, The following code appears to be doing what I'd expect, but I'm wondering if someone could confirm that there aren't any "gotchas" hidden in using methods accessed in this way. In...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
4
by: Hole | last post by:
Hi There! I'm trying to use Zope and the product OpenFlow. I got the following error while I was using the built-in function getattr() to retrieve an OpenFlow object: attribute name must be...
3
by: Jm lists | last post by:
Hello, Since I can write the statement like: Test whether a path is a directory Why do I still need the getattr() func as below? Test whether a path is a directory
1
by: Sergio Correia | last post by:
This works: # Module spam.py import eggs print getattr(eggs, 'omelet')(100) That is, I just call the function omelet inside the module eggs and evaulate it with the argument 100.
2
by: Sledge | last post by:
Hi. I am trying to dynamically load a class and attributes at run time. I do not know what classes will be referenced until run time. I have it loading the module correctly, but when I use...
3
numberwhun
by: numberwhun | last post by:
Hello everyone! I am presently going through the "Dive Into Python" tutorial which I obtained from their website. No, this is not for any class, I am self-learning the Python language. I am in...
8
by: Gregor Horvath | last post by:
Hi, class A(object): test = "test" class B(object): a = A() In : B.a.test
9
by: Gabriel Rossetti | last post by:
Hello, I can't get getattr() to return nested functions, I tried this : .... def titi(): .... pass .... f = getattr(toto, "titi") .... print str(f) .... Traceback...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.