Re: [Tutor] Import module function using variable
> On Jun 13, 2018, at 3:37 AM, Peter Otten <__pete...@web.de> wrote: > > Slater, Joseph C. wrote: > >> Dear friends, >> >> I am trying to import a function in a module by variable name. The >> specific example is that my function allows the user to select which >> function my code will use (in this case, which optimizer in scipy). There >> is a default for a named variable. I have thing working in a quite unclean >> way- I import them all and then look them up in globals. I'm not trying to >> clean it up. >> >> Essentially I'd like to >> >> method = 'newton_krylov' >> >> chosen_optimize_func = (from scipy.optimize import method) >> >> Of course, even I know this shouldn't work. What I tried is >> >> solver_method = __import__('scipy.optimize.' + method, fromlist=['']) >> >> which yields: >> > --- >> ModuleNotFoundError >> Traceback (most recent call last) >> >> in () >> > 1 solver_method = __import__('scipy.optimize.' + method, >> fromlist=['']) >> >> >> >> ModuleNotFoundError >> : No module named 'scipy.optimize.newton_krylov' >> >> >> Well, no, it's not a module but a function. > > Indeed, the function is a name in a module, or an attribute of the module > object. You need two steps to access it. First import the module, then use > getattr() to extract the function, preferrably > import scipy.optimize method = 'newton_krylov' chosen_optimize_func = getattr(scipy.optimize, method) chosen_optimize_func > > > but if the module varies, too, use import_module(): > import importlib getattr(importlib.import_module("scipy.optimize"), "newton_krylov") > 4 years of Python with 4 packages on pypi and getattr was unknown to me. Thank you for your graciousness. This should have been quite trivial to obtain. FWIW: no prompting for this code. It wouldn't make sense. However, I do, at some point, need to include checking to ensure that users provide a response that is an available one. So far, this seems to be necessary as the default has been so consistently the best choice as to beg the question why I even offer another choice. Best Regards- Joe https://people.wright.edu/joseph.slater ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Import module function using variable
On 06/13/2018 08:35 AM, Mats Wichmann wrote: > On 06/13/2018 01:37 AM, Peter Otten wrote: >> Slater, Joseph C. wrote: >> >>> Dear friends, > > as others have said, I'll also say: your problem statement is fuzzy. Sorry folks, this was obviously in reply to a different thread, operator error! ([Tutor] tab separated file handling) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Import module function using variable
On 06/13/2018 01:37 AM, Peter Otten wrote: > Slater, Joseph C. wrote: > >> Dear friends, as others have said, I'll also say: your problem statement is fuzzy. if your data looks like this: > d23 87 9 NA 67 5 657 NA 76 8 87 78 90 800 > er 21 8 908 9008 9 7 5 46 3 5 757 7 5 is it meaningful that there are 14 items in a "row"? or is the row still valid if you remove some items from it? That is, does the position in the row matter? If it does, then you should replace NA by some value Python can recognize - for example None - instead of trying to delete the element. tab-separated is a pretty poor choice of data format if fields can be blank - you can't spot blanks easily, unless you're fortunate enough that each "column" can be displayed in less than a tab-width so it always lines up. that's a human problem, not a computer problem, but could impede your ability to visually inspect that things look sane. Using a non-whitespace separator character would be better. or... is this just a list of values and there is really no row concept? In that case, don't bring rows into it, and drop the invalid entries on reading. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Import module function using variable
Slater, Joseph C. wrote: > Dear friends, > > I am trying to import a function in a module by variable name. The > specific example is that my function allows the user to select which > function my code will use (in this case, which optimizer in scipy). There > is a default for a named variable. I have thing working in a quite unclean > way- I import them all and then look them up in globals. I'm not trying to > clean it up. > > Essentially I'd like to > > method = 'newton_krylov' > > chosen_optimize_func = (from scipy.optimize import method) > > Of course, even I know this shouldn't work. What I tried is > > solver_method = __import__('scipy.optimize.' + method, fromlist=['']) > > which yields: > --- > ModuleNotFoundError >Traceback (most recent call last) > > in () > > 1 solver_method = __import__('scipy.optimize.' + method, > fromlist=['']) > > > > ModuleNotFoundError > : No module named 'scipy.optimize.newton_krylov' > > > Well, no, it's not a module but a function. Indeed, the function is a name in a module, or an attribute of the module object. You need two steps to access it. First import the module, then use getattr() to extract the function, preferrably >>> import scipy.optimize >>> method = 'newton_krylov' >>> chosen_optimize_func = getattr(scipy.optimize, method) >>> chosen_optimize_func but if the module varies, too, use import_module(): >>> import importlib >>> getattr(importlib.import_module("scipy.optimize"), "newton_krylov") ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Import module function using variable
On 13/06/18 02:52, Slater, Joseph C. wrote: > I am trying to import a function in a module by variable name. I'd suggest getattr(): >>> import sys >>> xt = getattr(sys,exit) >>> xt() Alternatively, if its a fixed set of options (and it sound as if it is) set up a dictionary: funcs = {'exit': sys.exit, size: sys.getsizeof) xt = funcs('exit') xt() Which has the advantage of providing a menu of friendly function names to the user: for func in funcs: print(func) fn = input('choose a function from the list above') And allows you to validate the selection if fn in funcs.keys(): f = funcs[fn] else: print(...) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor