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 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
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))
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
>>>...
|
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):...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |