Re: __import__ with dict values
I don't get *why* someone would like to write that. Does it look cool? Is it some kind of Look, ma, I know those hidden names! syndrome? Is it contagious? I think thats what it is. It does look cool, thats for telling me about the fact that prevents the interpreter from doing many optimizations... that makes sense to me, where would I read more about python optimizations? I want to see code more, I like code, comments are good too :) I have this piece of code I would like to turn into a generator, can some one please help me do it? I want to make it very optimized. The data comes from a options file that is parsed and used for this header/field function mapping function . I need to generate this, unless you advise against it then what are my options? How to wrap my head around this? def loop_lines(self): self.soc.me_him(['ENTER:',__name__],self.func_me_color) print self.lines for k in range(len(self.lines)): #for every line in csv file self.line=self.lines[k] for optv in self.order: for optvv in self._optv[optv][headers]: if self._optv[optv][headers][optvv]!=: _optvk=string.split(self._optv[optv][headers][optvv],,) for optvk in _optvk: for optvvv in self._optv[optv][mappings]: if optvk == optvvv: if self._optv[optv][mappings][optvvv].find(self.fun.func) = 0: self.field=optvvv self.value=self.parsed[k][optvv].replace('', '\\').replace(', \\') try: exec (self._optv[optv][mappings][optvvv]) #execute custom function self.last_value=self.value except NameError, e: self.soc.w([\n\nERROR: %s \n\nFUNCTION or VARIABLE IS DEFINED IN JAR_CONTROLLER % e],'white_on_red') sys.exit(1) #print self.opt['data'] #self.opt['data'][self.field]=self.fun.data[self.field] #print self.opt['data'] else: #self.soc.write([FUNC NOT FOUND:,pfhpv[pfhpvi],self._pfmp[pfhpv[pfhpvi]]],'red') #if self._pfmp[pfhpv[pfhpvi]]== pfhp: self.opt['data']['defaults'][optv][optvvv]=self.value self.last_item=self.value for ff in self.final_funcs.keys(): if self.final_funcs[ff]: exec ( ff ) self.soc.me_him(['EXIT:',__name__],self.func_me_color) Thank you, -Alex Goretoy http://www.goretoy.com On Fri, Mar 13, 2009 at 11:56 PM, Gabriel Genellina gagsl-...@yahoo.com.arwrote: prevents the interpreter from doing many optimizations... -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
I'm actually still getting to this section of my code, I've been working a logging,stdout colors lately, Although I am going to need __import__ in several places in this program -Alex Goretoy http://www.goretoy.com On Sat, Mar 14, 2009 at 3:05 AM, alex goretoy aleksandr.gore...@gmail.comwrote: I don't get *why* someone would like to write that. Does it look cool? Is it some kind of Look, ma, I know those hidden names! syndrome? Is it contagious? I think thats what it is. It does look cool, thats for telling me about the fact that prevents the interpreter from doing many optimizations... that makes sense to me, where would I read more about python optimizations? I want to see code more, I like code, comments are good too :) I have this piece of code I would like to turn into a generator, can some one please help me do it? I want to make it very optimized. The data comes from a options file that is parsed and used for this header/field function mapping function . I need to generate this, unless you advise against it then what are my options? How to wrap my head around this? def loop_lines(self): self.soc.me_him(['ENTER:',__name__],self.func_me_color) print self.lines for k in range(len(self.lines)): #for every line in csv file self.line=self.lines[k] for optv in self.order: for optvv in self._optv[optv][headers]: if self._optv[optv][headers][optvv]!=: _optvk=string.split(self._optv[optv][headers][optvv],,) for optvk in _optvk: for optvvv in self._optv[optv][mappings]: if optvk == optvvv: if self._optv[optv][mappings][optvvv].find(self.fun.func) = 0: self.field=optvvv self.value=self.parsed[k][optvv].replace('', '\\').replace(', \\') try: exec (self._optv[optv][mappings][optvvv]) #execute custom function self.last_value=self.value except NameError, e: self.soc.w([\n\nERROR: %s \n\nFUNCTION or VARIABLE IS DEFINED IN JAR_CONTROLLER % e],'white_on_red') sys.exit(1) #print self.opt['data'] #self.opt['data'][self.field]=self.fun.data[self.field] #print self.opt['data'] else: #self.soc.write([FUNC NOT FOUND:,pfhpv[pfhpvi],self._pfmp[pfhpv[pfhpvi]]],'red') #if self._pfmp[pfhpv[pfhpvi]]== pfhp: self.opt['data']['defaults'][optv][optvvv]=self.value self.last_item=self.value for ff in self.final_funcs.keys(): if self.final_funcs[ff]: exec ( ff ) self.soc.me_him(['EXIT:',__name__],self.func_me_color) Thank you, -Alex Goretoy http://www.goretoy.com On Fri, Mar 13, 2009 at 11:56 PM, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: prevents the interpreter from doing many optimizations... -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
I can show you people more code if you want :) I like to learn -Alex Goretoy http://www.goretoy.com On Sat, Mar 14, 2009 at 3:09 AM, alex goretoy aleksandr.gore...@gmail.comwrote: I'm actually still getting to this section of my code, I've been working a logging,stdout colors lately, Although I am going to need __import__ in several places in this program -Alex Goretoy http://www.goretoy.com On Sat, Mar 14, 2009 at 3:05 AM, alex goretoy aleksandr.gore...@gmail.com wrote: I don't get *why* someone would like to write that. Does it look cool? Is it some kind of Look, ma, I know those hidden names! syndrome? Is it contagious? I think thats what it is. It does look cool, thats for telling me about the fact that prevents the interpreter from doing many optimizations... that makes sense to me, where would I read more about python optimizations? I want to see code more, I like code, comments are good too :) I have this piece of code I would like to turn into a generator, can some one please help me do it? I want to make it very optimized. The data comes from a options file that is parsed and used for this header/field function mapping function . I need to generate this, unless you advise against it then what are my options? How to wrap my head around this? def loop_lines(self): self.soc.me_him(['ENTER:',__name__],self.func_me_color) print self.lines for k in range(len(self.lines)): #for every line in csv file self.line=self.lines[k] for optv in self.order: for optvv in self._optv[optv][headers]: if self._optv[optv][headers][optvv]!=: _optvk=string.split(self._optv[optv][headers][optvv],,) for optvk in _optvk: for optvvv in self._optv[optv][mappings]: if optvk == optvvv: if self._optv[optv][mappings][optvvv].find(self.fun.func) = 0: self.field=optvvv self.value=self.parsed[k][optvv].replace('', '\\').replace(', \\') try: exec (self._optv[optv][mappings][optvvv]) #execute custom function self.last_value=self.value except NameError, e: self.soc.w([\n\nERROR: %s \n\nFUNCTION or VARIABLE IS DEFINED IN JAR_CONTROLLER % e],'white_on_red') sys.exit(1) #print self.opt['data'] #self.opt['data'][self.field]=self.fun.data[self.field] #print self.opt['data'] else: #self.soc.write([FUNC NOT FOUND:,pfhpv[pfhpvi],self._pfmp[pfhpv[pfhpvi]]],'red') #if self._pfmp[pfhpv[pfhpvi]]== pfhp: self.opt['data']['defaults'][optv][optvvv]=self.value self.last_item=self.value for ff in self.final_funcs.keys(): if self.final_funcs[ff]: exec ( ff ) self.soc.me_him(['EXIT:',__name__],self.func_me_color) Thank you, -Alex Goretoy http://www.goretoy.com On Fri, Mar 13, 2009 at 11:56 PM, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: prevents the interpreter from doing many optimizations... -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
This is a chunk of what its starting to look like now, thanks from colors import _available_colors as _ck from has_colors import _has_colors as _ha from log import _brush as _er class stdout_colors(object): def __init__(self,colors_active=1,output_caller=1,caller_color=red,default=1, show_lineno_write=1,show_lineno_caller=1,break_all=1, logging=1,log_type=INFO,depth=10): self.caller_color = caller_color #stdout_colors function caller called output color self.output_caller = output_caller # output caller and called function self.colors_active = colors_active #active colors for output self.colors = _ck().colors crack=_er(colors_active,default,show_lineno_write,show_lineno_caller, break_all,logging,log_type,depth) #Thanks Gabriel Genellina, no crack was smoked during the making #list( ( self.__setattr__(x.replace(b_,),getattr(B,x)) for x in dir(B) if x.startswith(b_) ) ) for smoke in dir(crack): if smoke.startswith(b_): setattr(self, smoke[2:], getattr(crack, smoke)) -Alex Goretoy http://www.goretoy.com On Sat, Mar 14, 2009 at 3:13 AM, alex goretoy aleksandr.gore...@gmail.comwrote: I can show you people more code if you want :) I like to learn -Alex Goretoy http://www.goretoy.com On Sat, Mar 14, 2009 at 3:09 AM, alex goretoy aleksandr.gore...@gmail.com wrote: I'm actually still getting to this section of my code, I've been working a logging,stdout colors lately, Although I am going to need __import__ in several places in this program -Alex Goretoy http://www.goretoy.com On Sat, Mar 14, 2009 at 3:05 AM, alex goretoy aleksandr.gore...@gmail.com wrote: I don't get *why* someone would like to write that. Does it look cool? Is it some kind of Look, ma, I know those hidden names! syndrome? Is it contagious? I think thats what it is. It does look cool, thats for telling me about the fact that prevents the interpreter from doing many optimizations... that makes sense to me, where would I read more about python optimizations? I want to see code more, I like code, comments are good too :) I have this piece of code I would like to turn into a generator, can some one please help me do it? I want to make it very optimized. The data comes from a options file that is parsed and used for this header/field function mapping function . I need to generate this, unless you advise against it then what are my options? How to wrap my head around this? def loop_lines(self): self.soc.me_him(['ENTER:',__name__],self.func_me_color) print self.lines for k in range(len(self.lines)): #for every line in csv file self.line=self.lines[k] for optv in self.order: for optvv in self._optv[optv][headers]: if self._optv[optv][headers][optvv]!=: _optvk=string.split(self._optv[optv][headers][optvv],,) for optvk in _optvk: for optvvv in self._optv[optv][mappings]: if optvk == optvvv: if self._optv[optv][mappings][optvvv].find(self.fun.func) = 0: self.field=optvvv self.value=self.parsed[k][optvv].replace('', '\\').replace(', \\') try: exec (self._optv[optv][mappings][optvvv]) #execute custom function self.last_value=self.value except NameError, e: self.soc.w([\n\nERROR: %s \n\nFUNCTION or VARIABLE IS DEFINED IN JAR_CONTROLLER % e],'white_on_red') sys.exit(1) #print self.opt['data'] #self.opt['data'][self.field]=self.fun.data[self.field] #print self.opt['data'] else: #self.soc.write([FUNC NOT FOUND:,pfhpv[pfhpvi],self._pfmp[pfhpv[pfhpvi]]],'red') #if self._pfmp[pfhpv[pfhpvi]]== pfhp: self.opt['data']['defaults'][optv][optvvv]=self.value self.last_item=self.value for ff in self.final_funcs.keys(): if self.final_funcs[ff]: exec ( ff ) self.soc.me_him(['EXIT:',__name__],self.func_me_color) Thank you, -Alex Goretoy http://www.goretoy.com On Fri, Mar 13, 2009 at 11:56 PM, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: prevents the interpreter from doing many optimizations... -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
Gabriel Genellina wrote: En Thu, 12 Mar 2009 09:27:35 -0200, alex goretoy aleksandr.gore...@gmail.com escribió: note i would still like to be able to do __import__(sys).path p = __import__(sys).path That's a convoluted way of doing: import sys p = sys.path (except that the latter one inserts sys in the current namespace) That's a convoluted way of doing: from path import sys (except that the latter one doesn't insert sys in the current namespace) -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
wow, ok, thank you Gabriel, I wasn't aware of x,'y',z This is what I decided to go with for now in one of my classes, but another class will need a modified version of this, as mentioned x,'y',z B=_brush() list( ( self.__setattr__(x.replace(b_,),getattr(B,x)) for x in dir(B) if x.startswith(b_) ) ) -Alex Goretoy http://www.goretoy.com On Fri, Mar 13, 2009 at 1:46 AM, Lie Ryan lie.1...@gmail.com wrote: Gabriel Genellina wrote: En Thu, 12 Mar 2009 09:27:35 -0200, alex goretoy aleksandr.gore...@gmail.com escribió: note i would still like to be able to do __import__(sys).path p = __import__(sys).path That's a convoluted way of doing: import sys p = sys.path (except that the latter one inserts sys in the current namespace) That's a convoluted way of doing: from path import sys (except that the latter one doesn't insert sys in the current namespace) -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
En Fri, 13 Mar 2009 17:12:49 -0200, alex goretoy aleksandr.gore...@gmail.com escribió: wow, ok, thank you Gabriel, I wasn't aware of x,'y',z This is what I decided to go with for now in one of my classes, but another class will need a modified version of this, as mentioned x,'y',z B=_brush() list( ( self.__setattr__(x.replace(b_,),getattr(B,x)) for x in dir(B) if x.startswith(b_) ) ) __special__ methods are an implementation detail that you should not use explicitely; instead of obj.__setattr__(name, value) use setattr(obj, name, value). And building a list of None just to discard it isn't good style (that is: don't use a generator expression / list comprehension just by its side effect if you don't want the resulting list object) So I'd write the above as: for name in dir(B): if name.startswith(b_): setattr(self, name[2:], getattr(B, name)) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
Gabriel Genellina wrote: En Fri, 13 Mar 2009 17:12:49 -0200, alex goretoy aleksandr.gore...@gmail.com escribió: wow, ok, thank you Gabriel, I wasn't aware of x,'y',z This is what I decided to go with for now in one of my classes, but another class will need a modified version of this, as mentioned x,'y',z B=_brush() list( ( self.__setattr__(x.replace(b_,),getattr(B,x)) for x in dir(B) if x.startswith(b_) ) ) __special__ methods are an implementation detail that you should not use explicitely; instead of obj.__setattr__(name, value) use setattr(obj, name, value). Except if you want to override the special methods like in operator overloading. -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
En Sat, 14 Mar 2009 00:31:55 -0200, Lie Ryan lie.1...@gmail.com escribió: Gabriel Genellina wrote: En Fri, 13 Mar 2009 17:12:49 -0200, alex goretoy aleksandr.gore...@gmail.com escribió: list( ( self.__setattr__(x.replace(b_,),getattr(B,x)) for x in dir(B) if x.startswith(b_) ) ) __special__ methods are an implementation detail that you should not use explicitely; instead of obj.__setattr__(name, value) use setattr(obj, name, value). Except if you want to override the special methods like in operator overloading. Perhaps I should have written you should not *call* __special__ methods explicitely. Even if one overrides, e.g., __setitem__, the right way to use it (or call, or invoke, or operate) is still a[x] = y I've seen plenty of code spelling it a.__setitem__(x, y) instead. It is usually slower, exposes an implementation detail, is far less clear, it's not exactly the same thing, prevents the interpreter from doing many optimizations... I don't get *why* someone would like to write that. Does it look cool? Is it some kind of Look, ma, I know those hidden names! syndrome? Is it contagious? -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
I have resolved this problem in my code. It has something to do with your current working directory when you append cwd/jars to sys.path and try to import from interactive console -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 4:58 AM, alex goretoy aleksandr.gore...@gmail.comwrote: How would I import with __import__ from dict values? I want sys.path value inside d['syspath'], below code doesn't work for me d={} d['sys']='sys' d['path']='path' d['syspath']=__import__(d['sys'],fromlist=[d['path']]) and how come does above line doesn't give me diff value than below line? d['syspath']=__import__(d['sys']) Meaning, when I do this doesn't work. Which makes sense. d['syspath']() d['syspath'].d['path'] but this works both with fromlist and without. d['syspath'].path -Alex Goretoy http://www.goretoy.com -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
yay, no more exec (import + sys) in my code -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:42 AM, alex goretoy aleksandr.gore...@gmail.comwrote: I have resolved this problem in my code. It has something to do with your current working directory when you append cwd/jars to sys.path and try to import from interactive console -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 4:58 AM, alex goretoy aleksandr.gore...@gmail.com wrote: How would I import with __import__ from dict values? I want sys.path value inside d['syspath'], below code doesn't work for me d={} d['sys']='sys' d['path']='path' d['syspath']=__import__(d['sys'],fromlist=[d['path']]) and how come does above line doesn't give me diff value than below line? d['syspath']=__import__(d['sys']) Meaning, when I do this doesn't work. Which makes sense. d['syspath']() d['syspath'].d['path'] but this works both with fromlist and without. d['syspath'].path -Alex Goretoy http://www.goretoy.com -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
or eval for that matter -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:43 AM, alex goretoy aleksandr.gore...@gmail.comwrote: yay, no more exec (import + sys) in my code -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:42 AM, alex goretoy aleksandr.gore...@gmail.com wrote: I have resolved this problem in my code. It has something to do with your current working directory when you append cwd/jars to sys.path and try to import from interactive console -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 4:58 AM, alex goretoy aleksandr.gore...@gmail.com wrote: How would I import with __import__ from dict values? I want sys.path value inside d['syspath'], below code doesn't work for me d={} d['sys']='sys' d['path']='path' d['syspath']=__import__(d['sys'],fromlist=[d['path']]) and how come does above line doesn't give me diff value than below line? d['syspath']=__import__(d['sys']) Meaning, when I do this doesn't work. Which makes sense. d['syspath']() d['syspath'].d['path'] but this works both with fromlist and without. d['syspath'].path -Alex Goretoy http://www.goretoy.com -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
note i would still like to be able to do __import__(sys).path maybe if __import__ had __str__ defined, How is my thinking on this? and how would I achieve something like this? -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:44 AM, alex goretoy aleksandr.gore...@gmail.comwrote: or eval for that matter -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:43 AM, alex goretoy aleksandr.gore...@gmail.com wrote: yay, no more exec (import + sys) in my code -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:42 AM, alex goretoy aleksandr.gore...@gmail.com wrote: I have resolved this problem in my code. It has something to do with your current working directory when you append cwd/jars to sys.path and try to import from interactive console -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 4:58 AM, alex goretoy aleksandr.gore...@gmail.com wrote: How would I import with __import__ from dict values? I want sys.path value inside d['syspath'], below code doesn't work for me d={} d['sys']='sys' d['path']='path' d['syspath']=__import__(d['sys'],fromlist=[d['path']]) and how come does above line doesn't give me diff value than below line? d['syspath']=__import__(d['sys']) Meaning, when I do this doesn't work. Which makes sense. d['syspath']() d['syspath'].d['path'] but this works both with fromlist and without. d['syspath'].path -Alex Goretoy http://www.goretoy.com -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
__import__(opt['imp_mod']).options eval(opt['imp_mod']+.+opt['imp_opt']) how to make top work like bottom? -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:56 AM, alex goretoy aleksandr.gore...@gmail.comwrote: note i would still like to be able to do __import__(sys).path maybe if __import__ had __str__ defined, How is my thinking on this? and how would I achieve something like this? -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:44 AM, alex goretoy aleksandr.gore...@gmail.com wrote: or eval for that matter -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:43 AM, alex goretoy aleksandr.gore...@gmail.com wrote: yay, no more exec (import + sys) in my code -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 5:42 AM, alex goretoy aleksandr.gore...@gmail.com wrote: I have resolved this problem in my code. It has something to do with your current working directory when you append cwd/jars to sys.path and try to import from interactive console -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 4:58 AM, alex goretoy aleksandr.gore...@gmail.com wrote: How would I import with __import__ from dict values? I want sys.path value inside d['syspath'], below code doesn't work for me d={} d['sys']='sys' d['path']='path' d['syspath']=__import__(d['sys'],fromlist=[d['path']]) and how come does above line doesn't give me diff value than below line? d['syspath']=__import__(d['sys']) Meaning, when I do this doesn't work. Which makes sense. d['syspath']() d['syspath'].d['path'] but this works both with fromlist and without. d['syspath'].path -Alex Goretoy http://www.goretoy.com -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
En Thu, 12 Mar 2009 09:27:35 -0200, alex goretoy aleksandr.gore...@gmail.com escribió: note i would still like to be able to do __import__(sys).path p = __import__(sys).path That's a convoluted way of doing: import sys p = sys.path (except that the latter one inserts sys in the current namespace) maybe if __import__ had __str__ defined, How is my thinking on this? and how would I achieve something like this? __str__ has absolutely nothing to do. __import__(opt['imp_mod']).options eval(opt['imp_mod']+.+opt['imp_opt']) how to make top work like bottom? If you think you have to use eval: you don't. Never. module = __import__(opt['imp_mod']) module.options If the name options is not known until runtime, use getattr: getattr(module, name_of_attribute) The above assumes you want an attribute (like logging.ERROR). If you want a sub-module (a module inside a package) use __import__(dotted.name) and then retrieve the module by name from sys.modules; see http://docs.python.org/library/functions.html#__import__ -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
Gabriel, Thank you. This makes sense to me. I will go with sys.modules. Can you give me a good example how to do it getattr way? currently I am having this problem in my code. Kinda off subject, but not entirely. I set default variable in self.opt after that I import jar.properties into self.opt['properties']. Now my self.opt doesn't have the same defaults set. in other words we load our configuration properties. Then we over write any configuration properties with supplied sys.argv[1::] arguments I am passing my sys.argv to a class and inside the class I use getopt to get site_name and files_name, also many other variable that overwrite the configuration that is set from the jar.properties This was working when I was using exec and eval. I was not able to just use exec I had to use exec on import and eval on module.module it was wierd, can someone tell me why? -Alex Goretoy http://www.goretoy.com On Thu, Mar 12, 2009 at 1:00 PM, Gabriel Genellina gagsl-...@yahoo.com.arwrote: En Thu, 12 Mar 2009 09:27:35 -0200, alex goretoy aleksandr.gore...@gmail.com escribió: note i would still like to be able to do __import__(sys).path p = __import__(sys).path That's a convoluted way of doing: import sys p = sys.path (except that the latter one inserts sys in the current namespace) maybe if __import__ had __str__ defined, How is my thinking on this? and how would I achieve something like this? __str__ has absolutely nothing to do. __import__(opt['imp_mod']).options eval(opt['imp_mod']+.+opt['imp_opt']) how to make top work like bottom? If you think you have to use eval: you don't. Never. module = __import__(opt['imp_mod']) module.options If the name options is not known until runtime, use getattr: getattr(module, name_of_attribute) The above assumes you want an attribute (like logging.ERROR). If you want a sub-module (a module inside a package) use __import__(dotted.name) and then retrieve the module by name from sys.modules; see http://docs.python.org/library/functions.html#__import__ -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: __import__ with dict values
En Thu, 12 Mar 2009 23:00:30 -0200, alex goretoy aleksandr.gore...@gmail.com escribió: Thank you. This makes sense to me. I will go with sys.modules. Can you give me a good example how to do it getattr way? obj.attr is the same as getattr(obj, attr) (note the quotes). This makes more sense when you don't know the attribute name in advance: getattr(obj, some_variable_containing_the_attribute_name) If you find yourself using eval in this way: eval(leftpart.%s % rightpart) replace it with getattr(leftpart, rightpart). This statement: from logging import ERROR becomes: logging = __import__(logging) ERROR = getattr(logging, ERROR) (except that the intermediate logging variable doesn't exist in the first case). currently I am having this problem in my code. Kinda off subject, but not entirely. I set default variable in self.opt after that I import jar.properties into self.opt['properties']. Now my self.opt doesn't have the same defaults set. in other words we load our configuration properties. Then we over write any configuration properties with supplied sys.argv[1::] arguments I am passing my sys.argv to a class and inside the class I use getopt to get site_name and files_name, also many other variable that overwrite the configuration that is set from the jar.properties This was working when I was using exec and eval. I was not able to just use exec I had to use exec on import and eval on module.module it was wierd, can someone tell me why? Quoting myself from last post: If you think you have to use eval: you don't. Never. Same is valid for exec. There are very few cases when exec/eval is really the right thing to do. If you build an expression x.y just to eval() it, use getattr instead, as above. Same for exec´ing x.y = z - use setattr(x, y, z) instead. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list