473,396 Members | 2,020 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.

need an alternative to getattr()

Hi,

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

heading1:
<configuration 1>
<configuration 2>
heading2:
<configuration 1>
<configuration 2>
....
....

i parse this file to get heading1, heading2, etc and then i want to
call heading1.process(), heading2.process(), 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_heading(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 3073
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.comwrote:
Hi,

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

heading1:
<configuration 1>
<configuration 2>
heading2:
<configuration 1>
<configuration 2>
...
...

i parse this file to get heading1, heading2, etc and then i want to
call heading1.process(), heading2.process(), 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_heading(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.process(%s)" % (heading, file_ptr))

Aug 7 '06 #3
In article <11**********************@i42g2000cwa.googlegroups .com>, pr**************@gmail.com wrote:
import heading1
import heading2
While True:
heading = get_next_heading(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_ptr)

or

sys.modules[heading].process(file_ptr)

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
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 >>>...
6
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):...
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
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.