virtualmod
Python package for creating and importing virtual modules.
Install
pip install virtualmod
Examples
Module object
Manually creating and registering a module with virtualmod.
import virtualmod
# Create a new empty virtual module
module = virtualmod.create_module('custom_module')
# Add attribute to module
module.key = 'value'
# Use decorator to add a function to the module
# NOTE: You can use `add_to_module(module, name='new_name')` to override the module attribute name
@virtualmod.add_to_module(module)
def module_function(name):
print('Hello', name)
# Use decorator to add a class to the module
@virtualmod.add_to_module(module)
class ModuleClass:
pass
# Import and use our virtual module
import custom_module
print('Key:', custom_module.key)
custom_module.module_function('virtualmod')
print(custom_module.ModuleClass())
Class definition
virtualmod also comes with the ability to use class definitions to define virtual modules.
import virtualmod
# Use class definition to define our virtual module "custom_module"
class CustomModule(virtualmod.VirtualModule):
# Define the module's name (would be "CustomModule" otherwise)
__module_name__ = 'custom_module'
# Add an attribute
key = 'value'
# Add a function
# NOTE: There is no `cls` or `self`
def module_function(name):
print('Hello', name)
# Add a class to the module
class ModuleClass:
pass
# Import and use our virtual module
import custom_module
print('Key:', custom_module.key)
custom_module.module_function('virtualmod')
print(custom_module.ModuleClass())
Override an existing module
virtualmod's module finder is registered before the standard builtin finders.
This means if you register a module under a name of an existing module yours would be found and loaded first
import virtualmod
# Create a virtual module under the name "socket"
my_socket = virtualmod.create_module('socket')
# Import socket module
import socket
# Test if the loaded socket module is the one we created
print(socket is my_socket)
Metadata
Release files for virtualmod 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| virtualmod-1.0.0.tar.gz | 3.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| virtualmod-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.8 kB
Release files / virtualmod-1.0.0.tar.gz
| Download URL | virtualmod-1.0.0.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6be0e925aff122f706d9df4028290be6e58ecaec4db574e34d372a20c3098d10
|
|
BLAKE2b-256 checksum How to use checksums |
9e2a45f49fed95987b19767845616ee5b3af58a90948807b41ef7f8cfc1ab416
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0
|
Release files / virtualmod-1.0.0-py3-none-any.whl
| Download URL | virtualmod-1.0.0-py3-none-any.whl |
|---|---|
| Size | 3.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ca0c277f78d461e6287aad5c1929c53437443b9c1a17f6d676d7058aca7fe2f5
|
|
BLAKE2b-256 checksum How to use checksums |
2334f46d684f990177477599e8926fed29dfab183761837abdfb1f350258c5ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0
|