The clean way to extend existing functions
Project description
What is this?
Make 3rd-party functions capable of hanlding new inputs
Examples
pip install slick-siphon
from slick_siphon import siphon
import torch
# a function you might want to extend
def to_torch_tensor(a_list):
print("this is the non-siphoned part")
return torch.tensor(a_list)
# an new data type which that^ function should handle
class MyCustomContainerStackQueWhatever:
def __init__(self, list_items):
self.list_items = list_items
self.other_data = "blah blah blah"
# wrap to_torch_tensor with a siphon!
# -> when the lambda returns true
# -> the function below is run INSTEAD of the original to_torch_tensor()
@siphon(when=( lambda a_list: isinstance(a_list, MyCustomContainerStackQueWhatever) ), is_true_for=to_torch_tensor)
def name_of_this_func_doesnt_matter(a_list): # then siphon redirects it to this custom handler
actually_a_custom_container = a_list
print("this is the siphoned case!")
return torch.tensor(actually_a_custom_container.list_items)
#
# usage!
#
to_torch_tensor(MyCustomContainerStackQueWhatever([1,2,3]))
# >>> "this is the siphoned case!"
# >>> torch.tensor([1,2,3])
to_torch_tensor([1,2,3])
# >>> "this is the non-siphoned part"
# >>> torch.tensor([1,2,3])
# extend it again, so it'll accept None as an input (not recommended but its an example)
@siphon(when=( lambda a_list: isinstance(a_list, type(None)) ), is_true_for=to_torch_tensor)
def name_of_this_func_doesnt_matter(a_list):
return torch.tensor([])
#
# usage!
#
to_torch_tensor(None)
# >>> torch.tensor([])
to_torch_tensor(MyCustomContainerStackQueWhatever([1,2,3]))
# >>> "this is the siphoned case!"
# >>> torch.tensor([1,2,3])
to_torch_tensor([1,2,3])
# >>> "this is the non-siphoned part"
# >>> torch.tensor([1,2,3])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
slick_siphon-0.1.0.tar.gz
(2.6 kB
view details)
Built Distribution
File details
Details for the file slick_siphon-0.1.0.tar.gz
.
File metadata
- Download URL: slick_siphon-0.1.0.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b54fbf5fb90d03f69ff571f4774481bb1f473fe9f266ae605ea3987bd358eb0 |
|
MD5 | acc4d8e8461e380ed07ac62d06dc9792 |
|
BLAKE2b-256 | 75a6e63d5a0965a76e6702dc340d2de53a096a5278d4bbcd5dad1d5b51314b78 |
File details
Details for the file slick_siphon-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: slick_siphon-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07d2cb145283d06aef15c0a5866ef911d43a87809e2e87f1dc8ef763917368b4 |
|
MD5 | c9c49fa4d9983a342a29b73a67fa8b84 |
|
BLAKE2b-256 | 40802f1f288b968c506eb987a279ac234d27474f0bfb0523579c0a282804a66a |