Skip to content

Commit

Permalink
Merge pull request #7 from minrk/utils
Browse files Browse the repository at this point in the history
add traitlets.utils.importstring
  • Loading branch information
takluyver committed Apr 18, 2015
2 parents f85a7f3 + 701f2f2 commit 4b26dff
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions traitlets/__init__.py
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__
6 changes: 3 additions & 3 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
from warnings import warn

from ipython_genutils import py3compat
from .getargspec import getargspec
from ipython_genutils.importstring import import_item
from ipython_genutils.py3compat import iteritems, string_types

from .sentinel import Sentinel
from .utils.getargspec import getargspec
from .utils.importstring import import_item
from .utils.sentinel import Sentinel
SequenceTypes = (list, tuple, set, frozenset)

#-----------------------------------------------------------------------------
Expand Down
Empty file added traitlets/utils/__init__.py
Empty file.
File renamed without changes.
39 changes: 39 additions & 0 deletions traitlets/utils/importstring.py
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.

0 comments on commit 4b26dff

Please sign in to comment.