Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python extras #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ python:
- '2.7'
install:
- make compile

script:
- make test
- make publish

publish:
make publish

stages:
- compile
- test
- publish
if: branch = master
52 changes: 52 additions & 0 deletions extras/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
python package "extras" module

*Attempt* to import a library, without knowing if it's installed.
May optionally throw an error


"""
import inspect
import logging

logger = logging.getLogger(__name__)

MODULES={}


def __getitem__(module_name):
""" hash interface with extras, throw error if
module doesn't exist.
"""
module = MODULES.get(module_name)

if module:
return module

module = __import__(module_name)
MODULES[module_name] = module

return module


def get(module_name):
""" return package if package exists, None if fails.
should cache imported modules
"""

try:
return __getitem__(module_name)

except ImportError as err:
logger.info("Excluding `%s` because it's not installed."%module_name)


def not_important(module_name):
""" `import` directly to the class binding.
reference the class just like you used `import`.
`module_name is None` checks for existance.
"""
stack = inspect.stack()
klass = stack[1][0].f_locals[module_name] = get(module_name)

optional = not_important
23 changes: 23 additions & 0 deletions tests/unit/extras/getter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pytest import raises, mark
import extras

class TestExtrasGetters:
def test_get_module(self):
numpy = extras.get('numpy')
# assert type(numpy.array([1,2,3,4,5])) == numpy.array
arr = numpy.array([0,1,2,3,4,5])

assert arr[0] == 0


def test_get_non_existing_module(self):
nopy = extras.get('nopy')

assert nopy is None


@mark.skip("nice to have, jacking module")
def test_brackets(self):
numpy = extras['numpy']
assert isinstance(numpy.array([1,2,3,4,5]), numpy.array)

15 changes: 15 additions & 0 deletions tests/unit/extras/optional_import_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pytest import raises
from extras import not_important
not_important('numpy')
not_important('garbage')


class TestExtrasGetters:
def test_import_module(self):
arr = numpy.array([0,1,2,3,4,5])

assert arr[0] == 0


def test_non_existing_module(self):
assert garbage is None