diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-11-13 13:19:36 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-11-13 13:19:36 -0500 |
commit | af4285e6adf2a052ce985f9d3d97cc89778fca96 (patch) | |
tree | 56364c556828d5c7e103e57a82ef82e41d603fb9 /lib/sqlalchemy/util.py | |
parent | 5400ea2c1e2c605843255f5eb15b5ce1d8fba025 (diff) | |
download | sqlalchemy-af4285e6adf2a052ce985f9d3d97cc89778fca96.tar.gz |
- move inline "import" statements to use new "util.importlater()" construct. cuts
down on clutter, timeit says there's a teeny performance gain, at least where
the access is compared against attr.subattr. these aren't super-critical
calls anyway
- slight inlining in _class_to_mapper
Diffstat (limited to 'lib/sqlalchemy/util.py')
-rw-r--r-- | lib/sqlalchemy/util.py | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index 922d5bee5..8665cd0d4 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -1558,7 +1558,51 @@ class group_expirable_memoized_property(object): self.attributes.append(fn.__name__) return memoized_property(fn) - +class importlater(object): + """Deferred import object. + + e.g.:: + + somesubmod = importlater("mypackage.somemodule", "somesubmod") + + is equivalent to:: + + from mypackage.somemodule import somesubmod + + except evaluted upon attribute access to "somesubmod". + + """ + def __init__(self, path, addtl=None): + self._il_path = path + self._il_addtl = addtl + + @memoized_property + def _il_module(self): + m = __import__(self._il_path) + for token in self._il_path.split(".")[1:]: + m = getattr(m, token) + if self._il_addtl: + try: + return getattr(m, self._il_addtl) + except AttributeError: + raise AttributeError( + "Module %s has no attribute '%s'" % + (self._il_path, self._il_addtl) + ) + else: + return m + + def __getattr__(self, key): + try: + attr = getattr(self._il_module, key) + except AttributeError: + raise AttributeError( + "Module %s has no attribute '%s'" % + (self._il_path, key) + ) + self.__dict__[key] = attr + return attr + class WeakIdentityMapping(weakref.WeakKeyDictionary): """A WeakKeyDictionary with an object identity index. |