-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from minrk/utils
add traitlets.utils.importstring
- Loading branch information
Showing
6 changed files
with
43 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .traitlets import * | ||
from .utils.importstring import import_item | ||
from ._version import version_info, __version__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# encoding: utf-8 | ||
""" | ||
A simple utility to import something by its string name. | ||
""" | ||
|
||
# Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
|
||
def import_item(name): | ||
"""Import and return ``bar`` given the string ``foo.bar``. | ||
Calling ``bar = import_item("foo.bar")`` is the functional equivalent of | ||
executing the code ``from foo import bar``. | ||
Parameters | ||
---------- | ||
name : string | ||
The fully qualified name of the module/package being imported. | ||
Returns | ||
------- | ||
mod : module object | ||
The module that was imported. | ||
""" | ||
|
||
parts = name.rsplit('.', 1) | ||
if len(parts) == 2: | ||
# called with 'foo.bar....' | ||
package, obj = parts | ||
module = __import__(package, fromlist=[obj]) | ||
try: | ||
pak = getattr(module, obj) | ||
except AttributeError: | ||
raise ImportError('No module named %s' % obj) | ||
return pak | ||
else: | ||
# called with un-dotted string | ||
return __import__(parts[0]) |
File renamed without changes.