473,803 Members | 2,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need an alternative to getattr()

Hi,

AIM: I have a config file that contains configuration under headings
like this:

heading1:
<configuratio n 1>
<configuratio n 2>
heading2:
<configuratio n 1>
<configuratio n 2>
....
....

i parse this file to get heading1, heading2, etc and then i want to
call heading1.proces s(), heading2.proces s(), etc.
What i am trying to do is: (this is not the exact code, it just
represents what i am trying to do)

import heading1
import heading2
While True:
heading = get_next_headin g(file_ptr) # This func will return
"heading1", then "heading2"( on next call)
if heading = "":
break
getattr(heading , "process")(file _ptr) # The problem, as you would
have noticed, is that the first
# argument to getattr is a string!!

Is there an alternatice to getattr() that will solve my problem, or is
there another way to do it.

-pranav

Aug 7 '06 #1
3 3097
I think you would benefit from looking at the ConfigParser module.
I haven't tried it yet, but it looks like a nice interface for writing
and reading configuration files.

-Uffe.

On 7 Aug 2006 07:30:41 -0700, pr************* *@gmail.com
<pr************ **@gmail.comwro te:
Hi,

AIM: I have a config file that contains configuration under headings
like this:

heading1:
<configuratio n 1>
<configuratio n 2>
heading2:
<configuratio n 1>
<configuratio n 2>
...
...

i parse this file to get heading1, heading2, etc and then i want to
call heading1.proces s(), heading2.proces s(), etc.
What i am trying to do is: (this is not the exact code, it just
represents what i am trying to do)

import heading1
import heading2
While True:
heading = get_next_headin g(file_ptr) # This func will return
"heading1", then "heading2"( on next call)
if heading = "":
break
getattr(heading , "process")(file _ptr) # The problem, as you would
have noticed, is that the first
# argument to getattr is a string!!

Is there an alternatice to getattr() that will solve my problem, or is
there another way to do it.

-pranav

--
http://mail.python.org/mailman/listinfo/python-list
Aug 7 '06 #2
Ant
getattr(heading , "process")(file _ptr)
....
Is there an alternatice to getattr() that will solve my problem, or is
there another way to do it.
How about:

eval("%s.proces s(%s)" % (heading, file_ptr))

Aug 7 '06 #3
In article <11************ **********@i42g 2000cwa.googleg roups.com>, pr************* *@gmail.com wrote:
import heading1
import heading2
While True:
heading = get_next_headin g(file_ptr) # This func will return
"heading1", then "heading2"( on next call)
if heading = "":
break
getattr(heading , "process")(file _ptr) # The problem, as you would
have noticed, is that the first
# argument to getattr is a string!!

Is there an alternatice to getattr() that will solve my problem, or is
there another way to do it.
globals()[heading].process(file_p tr)

or

sys.modules[heading].process(file_p tr)

but note that, unless you are checking 'heading' against a 'known
good configuration keywords' list beforehand, you are trusting the
author of the configuration file.
Aug 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1905
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 >>> getattr(Parrot, "__dict__") is Parrot.__dict__ False
6
1747
by: Pablo | last post by:
Hello all, sorry if this is a faq... Problem: The intended effect is to override the method 'getattr' in a way that i dont need to override the property explicitly too. class Base(object): def __init__(self, attr): self._attr = attr def getattr(self): return self._attr
4
2428
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 string
3
3080
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 Chapter 4 and reading about "Getting Object References with getattr". First, it say in there that, "you can get a reference to a function without knowing its name until run−time, by using the getattr function". That is the first thing that is a...
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7600
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.