Skip to main content

Handle all your optional dependencies with a single call!

Project description

generalimport

Handle all your optional dependencies with a single call!

Contents

generalimport
├─ Contents
├─ Examples
│  ├─ Minimal Example
│  ├─ Tests Showcase
│  ├─ Recommended Installation
│  └─ How It Works
├─ Installation
├─ Information
├─ Attributes
├─ Contributions
└─ Todo

Examples

Minimal Example

Call GeneralImporter before importing any optional dependencies.

Here is a simple minimal example:

from generalimport import GeneralImporter
GeneralImporter("notinstalled")

import notinstalled  # No error

def func():
    notinstalled.missing_func()  # Error occurs here

func()
...MissingOptionalDependency: Optional dependency 'notinstalled' was used but it isn't installed.

Imports fail when they are used, not imported.

This means you don't need to keep checking if the package is installed before importing it. Simply import your optional package and use it like you would any package and let it fail wherever it fails, with a nice error message.

Tests Showcase

The beauty of this package is that the error raised isn't just any exception. It has two bases classes: unittest.case.SkipTest and _pytest.outcomes.Skipped (If available).

This means that if a test method uses an uninstalled optional package then that test is automatically skipped. This means no more manual skip decorators for optional dependencies!

from generalimport import GeneralImporter
GeneralImporter("optional_uninstalled_package")

from optional_uninstalled_package import missing_func

from unittest import TestCase

class MyTest(TestCase):
    def test_missing_func(self):
        self.assertEqual(3, missing_func(1, 2))
Ran 1 test in 0.002s

OK (skipped=1)

Skipped: Optional dependency 'optional_uninstalled_package' was used but it isn't installed.

Recommended Installation

I recommend to put this at the top of your main __init__.py file.

from generalimport import GeneralImporter
GeneralImporter("your", "optional", "dependencies")

This is all you need to write to use this package.

You can also write GeneralImporter("*") to make any package importable.

How It Works

  • When GeneralImporter is instantiated it creates a new importer for sys.meta_path.
  • This importer will return 'fake' modules for specified names.
  • The fake module will recursively return itself when asked for an attribute.
  • When used in any way (__call__, __add__, __str__ etc) it raises generalimport.MissingOptionalDependency.
  • This exception has the 'skip-exceptions' from unittest and pytest as bases, which means that tests will automatically be skipped.

Installation

Command setuptools
pip install generalimport Yes

Information

Package Ver Latest Release Python Platform Lvl Todo Cover
generalimport 0.1.1 2022-09-08 19:53 CEST 3.8, 3.9 Windows, Ubuntu 0 0 97.3 %

Attributes

Module: generalimport
├─ Class: FakeModule
│  └─ Method: error_func
├─ Class: GeneralImporter
│  ├─ Method: disable
│  ├─ Method: disable_all
│  ├─ Method: enable
│  ├─ Method: find_module
│  ├─ Method: get_enabled
│  ├─ Method: is_enabled
│  └─ Method: load_module
├─ Class: MissingOptionalDependency
├─ Function: get_installed_packages
├─ Function: import_module
└─ Function: package_is_installed

Contributions

Issue-creation and discussion is most welcome!

Pull requests are not wanted, please discuss with me before investing any time.

Generated 2022-09-08 19:53 CEST for commit d672a82.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

generalimport-0.1.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

generalimport-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file generalimport-0.1.1.tar.gz.

File metadata

  • Download URL: generalimport-0.1.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for generalimport-0.1.1.tar.gz
Algorithm Hash digest
SHA256 33e681bdc017ab07ca4b5038e2ad0f65fa51903bf12f07925f1f6dc60fe3eb69
MD5 1757a904085359f17dd9a97e36df79de
BLAKE2b-256 2dbe71312bb0702eef0969812de9cc64beba132fce09d001ae6c31a4bd062fb0

See more details on using hashes here.

File details

Details for the file generalimport-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: generalimport-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for generalimport-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ea569e4d02da149478505aeac9159634bda4c0c4e3239237c5a07dd7ae6bac79
MD5 14abf3324ef461f8ad35c4ee18970aa9
BLAKE2b-256 1496d81c347d05fab50aa0bc806281bf56947e9825aac371584dcd5f31f13d1c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page