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
from torch import tensor

# a new data type that torch.tensor() doesn't know how to handle
class MyCustomContainerStackQueWhatever:
    def __init__(self, list_items):
        self.list_items = list_items
        self.other_data = "blah blah blah"

# wrap torch.tensor with a siphon!
# -> when the lambda returns true
# -> the function below is run INSTEAD of the original torch.tensor()
#    EVEN IN OTHER MODULES
@siphon(
    when=lambda a_list: isinstance(a_list, MyCustomContainerStackQueWhatever),
    is_true_for=tensor,
)
def tensor(a_list): # the siphon redirects args to <- this custom handler
    actually_a_custom_container = a_list
    print("this is the siphoned case!")
    return tensor(actually_a_custom_container.list_items)
    


# 
# usage!
# 
import torch
torch.tensor(MyCustomContainerStackQueWhatever([1,2,3]))
# >>> "this is the siphoned case!"
# >>> torch.tensor([1,2,3])
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=torch.tensor)
def torch.tensor(arg1):
    return torch.tensor([])


# 
# usage!
# 
torch.tensor(None)
# >>> torch.tensor([])
torch.tensor(MyCustomContainerStackQueWhatever([1,2,3]))
# >>> "this is the siphoned case!"
# >>> torch.tensor([1,2,3])
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.2.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

slick_siphon-0.1.2-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: slick_siphon-0.1.2.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/6.6.0 pkginfo/1.9.6 requests/2.30.0 requests-toolbelt/1.0.0 tqdm/4.65.0 CPython/3.8.13

File hashes

Hashes for slick_siphon-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a55bdfdfb0c8cb4ba4d3a209b7123d47228627eed35e925efef684559c1e54e3
MD5 100b252599c00dc647c3968ea80c4eaa
BLAKE2b-256 9080c584226aac83e3590a382b6c19ee31bc2ad819301fa163e3fe3d195275e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: slick_siphon-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 2.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/6.6.0 pkginfo/1.9.6 requests/2.30.0 requests-toolbelt/1.0.0 tqdm/4.65.0 CPython/3.8.13

File hashes

Hashes for slick_siphon-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 70f6743bcb208e5fb3379a674c5c2ccd7ec082f21ee0746159e11e0551e78eb3
MD5 7636075f29accefaf91e1b448020c565
BLAKE2b-256 915ad7fe156aa6e69b687a0855a81296aef10f18f977acf3bf32c8ab7bc1f21f

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