Skip to main content

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 custom_handler__name_of_this_func_doesnt_matter(a_list): # the siphon redirects args 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 arg1: isinstance(arg1, type(None)) ), is_true_for=to_torch_tensor)
def name_of_this_func_doesnt_matter(arg1):
    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


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.1.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

slick_siphon-0.1.1-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file slick_siphon-0.1.1.tar.gz.

File metadata

  • Download URL: slick_siphon-0.1.1.tar.gz
  • Upload date:
  • Size: 2.7 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

Hashes for slick_siphon-0.1.1.tar.gz
Algorithm Hash digest
SHA256 264f70842965f8539a62261e6b7bbedc03e842081ec9fe574c7d6d6c86b71e97
MD5 2f82d0b03f5600c63bd106a710ba0b61
BLAKE2b-256 8bf4c266f5e9d01cd8b17f5aefc8a3a96baefcd9006208a3881b9b60ec5981f6

See more details on using hashes here.

File details

Details for the file slick_siphon-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: slick_siphon-0.1.1-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

Hashes for slick_siphon-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ea537c9c01ae5ef89bd74a22aefd064bc4f703074f104f5d83ff067ccc5943d
MD5 629900ba4b84a6daaabdfad3cfe6ee6d
BLAKE2b-256 5bb977b04256f5abcf0c2948e913fae5aec5d1e3d103748e8da446b374d9d3f9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page