lb
utils for ipython importing
To install: pip install lb
Overview
The lb package provides utility functions primarily aimed at simplifying the import process in Python, particularly useful in interactive environments like IPython. It includes functionality to handle environment variables, dynamically import modules from strings, and retrieve specific objects from modules using dot notation paths.
Features
Environment Variable Handling
get_environment_variable(var, ignore=True): Fetches the value of an environment variable. If the variable is not set andignoreisTrue, it issues a warning and returns a placeholder string. IfignoreisFalse, it raises aRuntimeError.
Dynamic Module Import
module_import_from_string(import_path_string, params_file, verbose=False): Imports a module based on a path to a Python file. This function is particularly useful when the module to be imported is not in the standard Python path. It allows for specifying the module dynamically by its path.
Object Retrieval from Dot String
import_from_dot_string(dot_string): Allows importing a specific object from a module using a dot notation string. This is useful for accessing deeply nested objects without importing the entire module hierarchy.
Usage Examples
Getting Environment Variables
from lb import get_environment_variable
# Attempt to get an environment variable that might not be set
db_host = get_environment_variable('DB_HOST')
print(db_host) # Output depends on whether 'DB_HOST' is set or not
Importing Modules Dynamically
from lb import module_import_from_string
# Dynamically import a module from a specific file
module_path = '/path/to/your/module.py'
imported_module = module_import_from_string('your_package', module_path)
print(imported_module) # The imported module object
Importing Objects Using Dot Strings
from lb import import_from_dot_string
# Import a specific object from a module
path_join = import_from_dot_string('os.path.join')
print(path_join) # <function join at ...>
# You can also import submodules
path_module = import_from_dot_string('os.path')
print(path_module) # <module 'posixpath' from ...>
Function Documentation
get_environment_variable(var, ignore=True)
Fetches and returns the value of the specified environment variable. If the variable is not set and ignore is True, it issues a warning and returns a message indicating the missing variable. If ignore is False, it raises a RuntimeError to signal the error more forcefully.
module_import_from_string(import_path_string, params_file, verbose=False)
Imports a module given the path to its file. If the file ends with .py, it is treated as a Python script; otherwise, it assumes the file name without an extension and appends .py to it. If verbose is True, prints the paths being used for the import. This function handles import errors by adding the directory to sys.path and retrying the import.
import_from_dot_string(dot_string)
Imports and returns an object specified by a dot notation string. This can be a module, a function, a class, or any other object within a module. The function splits the dot string, imports the relevant module, and then retrieves the specified object from that module. If the dot string refers to a module directly, it imports and returns the module.
Metadata
Release files for lb 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lb-0.0.5.tar.gz | 8.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lb-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.1 kB
Release files / lb-0.0.5.tar.gz
| Download URL | lb-0.0.5.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7b391233a3cbf70df79ea37e22597c653f916c4b878543517f61aba5b779d16
|
|
BLAKE2b-256 checksum How to use checksums |
002da0a998d2616474adc8c691e38086ba6bda37e4a3580308e9d23d619c07c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / lb-0.0.5-py3-none-any.whl
| Download URL | lb-0.0.5-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a8f8a810501a346645f06d0d16d340c632fe036bb07d50ed4000c72abfd2cdfa
|
|
BLAKE2b-256 checksum How to use checksums |
50a211623045884f80c355085ad75392c8ef225de1a8c4c8494409c47e41a7ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|