How should I refer to any arbitrary class attributes starting with a fixed string and differing by their ending numbers?

StackOverflow https://stackoverflow.com/questions/22343540

Question

I am writing a Python program which uses ConfigParser to read a configuration file intended to control various aspects of the program's configuration, execution and orientation to its environment and landscape. I am using Python 2.6.6 on RHEL 6.4.

One aspect of its configuration is how many rsyslog daemons it needs to interact with, and details about each of those instances. I have chosen the format instance<#>_ to enable the user to specify any arbitrary number of instances with a consistent set of attributes to configure. An excerpt from the config file appears here:

[rsyslog]
rules_dir:                      /etc/rsyslog.d

instance1_enable:               no
instance1_name:                 rsyslog-Group01
instance1_startupscript:        /etc/init.d/%(instance1_name)s
instance1_conf:                 /etc/%(instance1_name)s
instance1_rules:                %(rules_dir)s/rules-%(instance1_name)s
instance1_restart:              no

instance2_enable:               no
instance2_name:                 rsyslog-Group02
instance2_startupscript:        /etc/init.d/%(instance2_name)s
instance2_conf:                 /etc/%(instance2_name)s
instance2_rules:                %(rules_dir)s/rules-%(instance2_name)s
instance2_restart:              no

I build an object called 'rsyslog' such that its attributes look like this:

rsyslog.instance2_enable="no"
rsyslog.instance2_name="rsyslog-Group02"
...etc

My problem comes when I pass the rsyslog object and an instance number to a function to have that function do operations on the instance#_rules. I call the function, for example, in this way:

list_of_rules=read_files(rsyslog,2)

The function should return a list of rules which it parses out of the rules file for instance2

/etc/rsyslog.d/rules-rsyslog-Group02

Parsing the rules is no problem when I hard code the instance:

for line in fileinput.input(rsyslog_object.instance7_rules, mode='r'):
    ruleline=re_ruleline.search(line)

But how do I allow for something like the following where '' represents the instance number I passed to the function:

for line in fileinput.input(rsyslog_object.instance<instancenumber>_rules, mode='r'):

I have used the locals() and globals() functions to do variable indirection in other contexts but I am not sure how I would apply them here.

Alternately, if you can see a better, more elegant or Pythonic way to solve the problem of allowing for any arbitrary number of consecutively numbered instances which can be referred to by number in iterations easily and more Pythonicly; please explain how and also why it is a better or more Pythonic way of accomplishing the task.

I am not married to using instance if there is another way to do it. ConfigParser did not allow '.' in the configuration

Was it helpful?

Solution

I'm not sure that I completely understand the question, but isn't getattr what you want:

for line in fileinput.input(
   getattr(rsyslog_object, 'instance%d_rules' % instancenumber),
   mode='r'):
    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top