Veils
veils is a python implementation of a ruby veils package.
Long story short, it provides convenient object decorators for data memoization.
Installation
pip install veils
Usage
from veils import veil
obj = veil(
obj,
methods={"__str__": "hello, world!", "foo": "42"}
)
str(obj) # returns "hello, world!"
obj.foo() # returns "42"
The methods __str__ and foo will return "Hello, world!" and "42" respectively
until some other method is called and the veil is "pierced".
You can also use unpiercable decorator, which will never be pierced: a very good instrument for data memoization.
And it works the same way for asynchronous methods too
obj = veil(
obj,
async_methods={"foo": "42"}
)
await obj.foo() # returns "42"
And also for properties
obj = veil(
obj,
props={"bar": "42"}
)
obj.bar # equals "42"
This library also extends the original one with a caching decorator memo. Use it like this:
from veils import memo
obj = memo(
obj,
cacheable={"foo", "bar", "baz", "__str__"}
# 'cacheable' is a collection of methods (both regular and asynchronous)
# and properties to be cached
)
Advanced usage
The python implementations of veil is somewhat tricky due to the magic methods which
are being accessed bypassing the __getattribute__ method.
Therefore, this implementation, in this particular case, relies on metaclasses in order to define magic methods on the fly in the veil object so that they correspond to those defined in the object being wrapped.
veil and unpiercable are just shortcuts to VeilFactory(Veil).veil_of and VeilFactory(Unpiercable).veil_of.
In some advanced cases you may want a different list of magic methods to be transparent or proxied by a veil object. In oder to obtain such behavior
you may create a custom veil factory like so: VeilFactory(Veil, proxied_dunders, naked_dunders).
naked_dunders is a list of methods bypassing the veil.
proxied_dunders is a list of methods to be veiled.
Release files for veils 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| veils-0.2.1.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| veils-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.4 kB
Release files / veils-0.2.1.tar.gz
| Download URL | veils-0.2.1.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8498d2cb625407663cc17354d238993526910943096a51d8fd9558d824d70f94
|
|
BLAKE2b-256 checksum How to use checksums |
2b11951bee214ae5b5780b75047f5d827782ca3de44dc334c3beb043a2242dc4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.7.1 Linux/4.15.0-1077-gcp
|
Release files / veils-0.2.1-py3-none-any.whl
| Download URL | veils-0.2.1-py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd6f8c3f0b43c528a8132eb3571786644a9ab39baed7c4572e1292e33033b090
|
|
BLAKE2b-256 checksum How to use checksums |
a9e78b381fd228d68bd03f8f6a99308047675f46754179181c4183fa388018c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.4 CPython/3.7.1 Linux/4.15.0-1077-gcp
|