Skip to main content

Python package for creating and importing virtual modules.

Project description

virtualmod

PyPI version

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)

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

virtualmod-1.0.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

virtualmod-1.0.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file virtualmod-1.0.0.tar.gz.

File metadata

  • Download URL: virtualmod-1.0.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for virtualmod-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6be0e925aff122f706d9df4028290be6e58ecaec4db574e34d372a20c3098d10
MD5 4492a6a1cc12ecd99fbcfbf114b626e6
BLAKE2b-256 9e2a45f49fed95987b19767845616ee5b3af58a90948807b41ef7f8cfc1ab416

See more details on using hashes here.

File details

Details for the file virtualmod-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: virtualmod-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for virtualmod-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca0c277f78d461e6287aad5c1929c53437443b9c1a17f6d676d7058aca7fe2f5
MD5 82f226729e79c4ab6127d72556ea2f09
BLAKE2b-256 2334f46d684f990177477599e8926fed29dfab183761837abdfb1f350258c5ab

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