Skip to main content

A library to load modules/classes based on their dotted string path.

Project description

Readme

This library is the same as the Django utils module loader, but without needing Django as dependency. It is used to load modules/classes based on their dotted string path.

Install

pip install inuits-module-loader

Usage

from inuits_module_loader import import_string
import_string("dotted.path.to.your.module.or.class")

Example

This example will dynamically load classes in the extensions/resources folder that extend the default classes in the application. Default class: resources.your_file_name.YourClassName

Class in extensions folder: extensions.resources.your_file_name.YourClassName

It is recommended to name the extension file and class the same as the default class, and import the default class with an alias in your extension class.

Extended class

from resources.your_file_name.YourClassName import YourClassName as DefaultYourClassName

class YourClassName(DefaultYourClassName):

    def do_something(self):
        return True

App

from inuits_module_loader import import_string
import logging

logger = logging.getLogger(__name__)

# This function will try to load your class from the extension folder and fall back to the default class if there is no extension available for that class.
def load_resource(resource: str):
    try:
        logger.info(f"Loading {resource} extension.")
        return import_string(f"extensions.resources.{resource}")
    except ModuleNotFoundError:
        logger.info(f"No {resource} extension found, loading core {resource}")
        return import_string(f"resources.{resource}")
    except ImportError as error:
        logger.info(f"{error}, loading core {resource}")
        return import_string(f"resources.{resource}")

# If you name your extended file and class the same as the default file and class you can easily add or remove classes from the extensions folder without adding additional code in the app.
your_class = load_resource("your_file_name.YourClassName")()

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

inuits_module_loader-1.0.3.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

inuits_module_loader-1.0.3-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file inuits_module_loader-1.0.3.tar.gz.

File metadata

  • Download URL: inuits_module_loader-1.0.3.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.8.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for inuits_module_loader-1.0.3.tar.gz
Algorithm Hash digest
SHA256 16a6e024be44aefe19963d27192907065f457ffd27d4f56b35f2b748cda59afc
MD5 2cc157c8c937945883fe2f4e0e0c063a
BLAKE2b-256 8faab2c476ccefdaa742154dd055da61ee38f6fe7e09accfe1124af090cea823

See more details on using hashes here.

File details

Details for the file inuits_module_loader-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: inuits_module_loader-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.8.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for inuits_module_loader-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7ec06db50c91eaf43a0c830b8c2894ab96ef70c1abc60737968ae6f086980389
MD5 44b82401651430b81be03494744ec8c3
BLAKE2b-256 b967d8933d254823dfaa0365b51a3e7544dced8a512365403aafd9bc8651244c

See more details on using hashes here.

Supported by

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