Please reconsider. The only thing worse than import is magic import.
Please reconsider. The only thing worse than import * is magic import *. If you really want to: m = __import__ (S) try: attrlist = m.
__all__ except AttributeError: attrlist = dir (m) for attr in attrlist: globals()attr = getattr (m, attr).
Maybe better: for attr in m. __all__? – Sergei Stolyarov Sep 29 '08 at 4:47 @Sergei: there's no guarantee that modules will define all – John Millikin Sep 29 '08 at 4:55 1 @John Millikin: But if a module defines all you should observe it – Florian Bösch Sep 29 '08 at 6:56 1 @BMH: Have you considered a settings aggregator like the one used in Django?
Define an object that stores a list of modules to search, and then override getattr to perform a search of them using getattr(). – John Millikin Sep 29 '08 at 18:31 1 Yes, there's absolutely no reason why each setting should live in a global namespace, that is options. Some_option is obviously better than some_option.
You can also use dict.update() on module's distionaries in your specific case. – ilya n. Sep 29 '087 at 9:27.
Here's my solution for dynamic naming of local settings files for Django. Note the addition below of a check to not include attributes containing '__' from the imported file. The __name__ global was being overwritten with the module name of the local settings file, which caused setup_environ(), used in manage.Py, to have problems.
Try: import socket HOSTNAME = socket.gethostname(). Replace('.','_') # See docs.python.org/library/functions.html#_... m = __import__(name="settings_%s" % HOSTNAME, globals=globals(), locals=locals(), fromlist="*") try: attrlist = m. __all__ except AttributeError: attrlist = dir(m) for attr in a for a in attrlist if '__' not in a: globals()attr = getattr(m, attr) except ImportError, e: sys.stderr.
Write('Unable to read settings_%s.Py\n' % HOSTNAME) sys. Exit(1).
It appears that you can also use dict.update() on module's dictionaries in your case: config = __import__(name) for name in names_list options = {} for conf in config: options. Update(conf. __dict__) Update: I think there's a short "functional" version of it: options = reduce(dict.
Update, map(__import__, names_list)).
The underlying problem is that I am developing some Django, but on more than one host (with colleagues), all with different settings. I was hoping to do something like this in the project/settings. Py file: from platform import node settings_files = { 'BMH.
Lan': 'settings_bmh. Py", ... } __import__( settings_files node() ) It seemed a simple solution (thus elegant), but I would agree that it has a smell to it and the simplicity goes out the loop when you have to use logic like what John Millikin posted (thanks). Here's essentially the solution I went with: from platform import node from settings_global import * n = node() if n == 'BMH.
Lan': from settings_bmh import * # add your own, here... else: raise Exception("No host settings for '%s'. See settings.py. " % node()) Which works fine for our purposes.
I didn't find a good way to do it so I took a simpler but ugly way from djangosnippets.org/snippets/600/ try: import socket hostname = socket.gethostname(). Replace('. ','_') exec "from host_settings.
%s import *" % hostname except ImportError, e: raise e.
1 I can see where this code is coming from, but that call to exec sends shivers down my back. I don't really know if it's a big deal in this case, but I've learned to trust my instincts about things like this. – Jason Baker Nov 15 '08 at 23:10 This is illegal inside the function, and looks terrible indeed.
– ilya n. Aug 29 '09 at 9:31.
You don't, that's a very silly idea and entirely unpythonic.
1 You don't answer like this, it's a very silly answer and entirely unhelpful. – Jürgen A. Erhard Mar 24 '10 at 13:00 4 Of course I do.
I try to use StackOverflow like I use other avenues of programmer education, to educate programmers. Not to facilitate them writing poor programs. That would be evil.
– Jerub Mar 29 '10 at 2:32 1 Sorry dude, but this is a silly and unhelpful answer. You're making the bold assertion that import * is something that is never useful… Which is demonstrably not true: grep -R 'import \*' Python-trunk/Lib/. – David Wolever Sep 26 '10 at 20:28 Ignore test_*.
Py files, Tkinter, Carbon, mac, sunos and distutils you're left with 70-odd actual imports. Most of those are doing import * from an accompanying c module that they wrap. – Jerub Sep 27 '10 at 0:09 2 So are you implying that tests, Tkinter, Carbon, mac, sunos, disutils and c modules are "entirely unpythonic"?
Or are you saying "those are special cases — not something average programmers like Brian need to deal with"? – David Wolever Sep 30 '10 at 17:15.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.