Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

A package to manage your python modules.

Project description

modkit

Pypi Github PythonVers Travis building

A package to manage your python modules

Install

# released version
pip install modkit
# latest version
pip intall git+https://github.com/pwwanbg/modkit

Usage

Allowing specific names to be imported, even with from ... import *

mymodule.py:

from modkit import Modkit
modkit = Modkit()
modkit.exports('a', 'b')
# Now you can specify glob patterns for exports via 0.1.0
# modkit.exports('p*') # works with dynamic exports
a = 1
b = 2
c = 3

Try to import this module:

import mymodule as mm
mm.a # 1
mm.b # 2
mm.c # NameNotImportable
from mymodule import *
a # 1
b # 2
c # NameError

# NameNotImportable
from mymodule import c

Banning certain names

mymodule.py:

from modkit import Modkit
modkit = Modkit()
modkit.ban('a')
a = 1
import mymodule as mm
mm.a # NameBannedFromImport

# NameBannedFromImport
from mymodule import a

Aliasing names

mymodule.py:

from modkit import Modkit
modkit = Modkit()
modkit.alias('some_internal_wired_name', 'a')
some_internal_wired_name = 1
from mymodule import a
a # 1

Importing names dynamically

mymodule.py:

from modkit import Modkit
modkit = Modkit()

def delegate(name):
	if name == 'a':
		return 1
	if name == 'b':
		return 2
	if name == 'c':
		return lambda: 3

modkit.delegate(delegate)
from mymodule import a, b, c
a # 1
b # 2
c() # 3

# NameBannedFromImport
from mymodule import delegate

# if you want to reuse it
# mymodule.modkit.unban('delegate')

modkit has reserved delegate function with name _modkit_delegate. With this function defined in your module, you won't have to call modkit.delegate

mymodule.py

import modkit
modkit.Modkit()

def _modkit_delegate(name):
	if name == 'a':
		return 1
	if name == 'b':
		return 2
	if name == 'c':
		return lambda: 3

# nothing needs to do

Then you are able to import a, b and c from mymodule:

from mymodule import a, b, c

Generating a new module based on current one

mymodule.py

import modkit
modkit.Modkit()

A = 1
def _modkit_call(module, a):
	setattr(module, 'A', 2)
	return module
import mymodule
A # 1
mymodule2 = mymodule(2)
mymodule2.A # 2
from mymodule2 import A
A # 2
from mymodule import A
A # 1

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.1.0
Filename, size File type Python version Upload date Hashes
Filename, size modkit-0.1.0-py3-none-any.whl (3.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size modkit-0.1.0.tar.gz (3.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page