Package: generalimport
generalimportHandle all your optional dependencies with a single call!Table of Contentsgeneralimport├─Dependency Diagram for ManderaGeneral├─Installation showing dependencies├─Information├─Examples│  ├─Minimal Example│  ├─Tests Showcase│  ├─Recommended Setup│  └─How It Works├─Attributes└─ContributionsDependency Diagram for ManderaGeneralflowchart LR
0([import]) --> 3([file])
0([import]) --> 2([library])
1([tool]) --> 2([library])
3([file]) --> 4([packager])
2([library]) --> 4([packager])
2([library]) --> 3([file])
click 0 "https://github.com/ManderaGeneral/generalimport"
click 1 "https://github.com/ManderaGeneral/generaltool"
click 2 "https://github.com/ManderaGeneral/generallibrary"
click 3 "https://github.com/ManderaGeneral/generalfile"
click 4 "https://github.com/ManderaGeneral/generalpackager"
style 0 stroke:#482Installation showing dependenciespip installgeneralimportNo dependencies✔️InformationPackageVerLatest ReleasePythonPlatformCovergeneralimport0.5.22024-01-24 07:20 CET3.8,3.9,3.10,3.11,3.12Windows, Ubuntu97.2 %ExamplesMinimal ExampleCallgeneralimportbefore importing any optional dependencies.fromgeneralimportimportgeneralimportgeneralimport("notinstalled")fromnotinstalledimportmissing_func# No errormissing_func()# Error occurs hereMissingDependencyException: Optional dependency 'notinstalled' was used but it isn't installed.Imports fail when they areused,notimported.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 ShowcaseThe beauty of this package is that the error raised isn't just any exception.
It has two base classes:unittest.case.SkipTestand_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!fromgeneralimportimportgeneralimportgeneralimport("optional_uninstalled_package")fromoptional_uninstalled_packageimportmissing_funcfromunittestimportTestCaseclassMyTest(TestCase):deftest_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 SetupPut this in your__init__.pyfile to affectallimports inside the folder__init__.pyresides in.fromgeneralimportimportgeneralimportgeneralimport("your","optional","dependencies")generalimport("*")makes it handleallnames (If missing of course):warning:generalimport("*")._scope = Nonedisables the scopeMakes it handle missing imports anywhereFor example it will overridepandasinternal custom optional dependency handlingHow It WorksWhengeneralimportis instantiated it creates a new importer forsys.meta_path.This importer will return 'fake' modules for matching names and scope.The scope ensures only your own imports are faked.The fake module will recursively return a FakeModule instance when asked for an attribute.When used in any way (__call__, __add__, __str__ etc) it raisesgeneralimport.MissingDependencyException.This exception has the 'skip-exceptions' fromunittestandpytestas bases, which means that tests will automatically be skipped.AttributesModule: generalimport├─Class: DynamicDunder│  ├─Method: result│  └─Method: trigger├─Class: FakeModule│  ├─Method: error_func│  └─Method: error_func_class├─Class: GeneralImporter│  ├─Method: catch│  ├─Method: create_module│  ├─Method: exec_module│  └─Method: find_spec├─Class: ImportCatcher│  └─Method: handle├─Class: MissingDependencyException├─Class: MissingOptionalDependency├─Function: fake_module_check├─Function: generalimport├─Function: get_importer├─Function: get_installed_modules_names├─Function: get_spec├─Function: import_module├─Function: is_imported├─Function: module_is_installed├─Function: module_is_namespace├─Function: module_name_is_namespace├─Function: reset_generalimport└─Function: spec_is_namespaceContributionsIssue-creation, discussions and pull requests are most welcome!Generated 2024-01-24 07:20 CET for commitmaster.