Skip to content

Commit

Permalink
Updates pockets.inspect.import_star() so that it returns a map of att…
Browse files Browse the repository at this point in the history
…rs instead of setting them on the module
  • Loading branch information
RobRuana committed Sep 23, 2018
1 parent f3b8323 commit 09b3a92
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
27 changes: 12 additions & 15 deletions pockets/inspect.py
Expand Up @@ -139,7 +139,9 @@ def hoist_submodules(package, extend_all=True):
module = resolve(package)
hoisted_attrs = []
for submodule in import_submodules(module):
hoisted_attrs.extend(import_star(module, submodule))
for attr_name, attr in import_star(submodule).items():
hoisted_attrs.append(attr_name)
setattr(module, attr_name, attr)

if extend_all:
if getattr(module, '__all__', None) is None:
Expand All @@ -150,34 +152,29 @@ def hoist_submodules(package, extend_all=True):
return hoisted_attrs


def import_star(to_module, from_module):
def import_star(module):
"""
Imports all exported attributes of `from_module` into `to_module`.
Imports all exported attributes of `module` and returns them in a `dict`.
Note:
This only considers attributes exported by `__all__`. If `from_module`
This only considers attributes exported by `__all__`. If `module`
does not define `__all__`, then nothing is imported.
Effectively does::
from from_module import *
from module import *
Args:
to_module (str or module): The module into which the attributes
are imported.
from_module (str or module): The module from which a wildcard import
module (str or module): The module from which a wildcard import
should be done.
Returns:
list: List of all imported attribute names.
dict: Map of all imported attributes.
"""
to_module = resolve(to_module)
from_module = resolve(from_module)
attrs = getattr(from_module, '__all__', [])
for attr in attrs:
setattr(to_module, attr, getattr(from_module, attr))
return attrs
module = resolve(module)
attrs = getattr(module, '__all__', [])
return dict([(attr, getattr(module, attr)) for attr in attrs])


def import_submodules(package):
Expand Down
2 changes: 1 addition & 1 deletion pockets/iterators.py
Expand Up @@ -42,7 +42,7 @@ class iterpeek(object):
See Also:
`iterpeek` can operate as a drop in replacement for the built-in
`iter <http://docs.python.org/2/library/functions.html#iter>`_
`iter <https://docs.python.org/3/library/functions.html#iter>`_
function.
Attributes:
Expand Down

0 comments on commit 09b3a92

Please sign in to comment.