Skip to main content

Tweak your modules the way you want

Project description


Pypi Github PythonVers

Tweak your modules the way you want


pip install -U modkit


import modkit

a = 1
d = {}

def __getattr__(name):
    # we have this supported with python3.7+
    # but with modkit, you can do it with python3.6+
    return name.upper()

def __getitem__(name):
    return name.lower()

def __call__(name):
    return name * 2

import module

module.a # 1
module.b # B
module.d # {}
module['c'] # C
module('e') # ee

Baking a module

You can make a copy of your module with modkit easily.

from modkit import install, bake

d = {}

def __call__():
    module2 = bake()
    return module2
    # or return bake('module2')

import module

m2 = module()
# <module 'module2' from './' (wrapped by modkit)>
m2.__name__ # 'module2
m2.d['a'] = 1

module.d == {}

Note that baking re-execute the original module and generate a new module.


Say we have following structure:

|- module

If __getattr__ is defined in, when we do:

from module import sub
# or
# import module
# module.sub

__getattr__ will first handle this, meaning the sub module will not be imported as expected. You have to do it inside __getattr__:

modkit has a helper function submodule, which tries to import the submodule under current one.

from modkit import install, submodule
def __getattr__(name):
    submod = submodule(name)
    if submod:
        # submodule imported
        return submod
    # other stuff you want to do with name
    # or raise error


Then from module import sub or module.sub will work as expected.

Note that submodule will not raise ImportError. If import fails, it will return None.

Note that we can also import submodules from a baked module, just like we do for the original module.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for modkit, version 0.3.1
Filename, size File type Python version Upload date Hashes
Filename, size modkit-0.3.1.tar.gz (4.2 kB) File type Source Python version None Upload date Hashes View
Filename, size modkit-0.3.1-py3-none-any.whl (4.0 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page