🌱 Turn any object into a module 🌱
Ever wanted to call a module directly, or index it? Or just sick of seeing
from foo import foo
in your examples?
Give your module the awesome power of an object, or maybe just save a
little typing, with xmod
.
xmod
is a tiny library that lets a module to do things that normally
only a class could do - handy for modules that "just do one thing".
Example: Make a module callable like a function!
# In your_module.py
import xmod
@xmod
def a_function():
return 'HERE!!'
# Test at the command line
>>> import your_module
>>> your_module()
HERE!!
Example: Make a module look like a list!?!
# In your_module.py
import xmod
xmod(list(), __name__)
# Test at the command line
>>> import your_module
>>> assert your_module == []
>>> your_module.extend(range(3))
>>> print(your_module)
[0, 1, 2]
API Documentation
xmod(extension=None, name=None, full=None, omit=None, mutable=False)
Extend the system module at name
with any Python object.
The original module is replaced in sys.modules
by a proxy class
which delegates attributes to the original module, and then adds
attributes from the extension.
In the most common use case, the extension is a callable and only the
__call__
method is delegated, so xmod
can also be used as a
decorator, both with and without parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
extension |
Optional
|
The object whose methods and properties extend the namespace. This includes magic methods like call and getitem. |
None
|
name |
Optional[str]
|
The name of this symbol in This only needs to be be set if If the |
None
|
full |
Optional[bool]
|
If If If |
None
|
mutable |
bool
|
If |
False
|
omit |
Optional[Sequence[str]]
|
A list of methods not to delegate from the proxy to the extension If |
None
|
Returns:
Type | Description |
---|---|
Any
|
|
Source code in xmod/xmod.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|