Re: need an alternative to getattr()
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: need an alternative to getattr()
> 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)) -- http://mail.python.org/mailman/listinfo/python-list
Re: need an alternative to getattr()
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, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > > AIM: I have a config file that contains configuration under headings > like this: > > heading1: > > > heading2: > > > ... > ... > > 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 > -- http://mail.python.org/mailman/listinfo/python-list
need an alternative to getattr()
Hi, AIM: I have a config file that contains configuration under headings like this: heading1: heading2: ... ... 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