Skip to main content

Library that enables the creation of alternate constructor-likevariant forms for arbitrary functions.

Project description

variants

https://img.shields.io/pypi/v/variants.svg https://img.shields.io/travis/python-variants/variants.svg Documentation Status Updates

variants is a library that provides syntactic sugar for creating alternate forms of functions and other callables, in the same way that alternate constructors are class methods that provide alternate forms of the constructor function.

To create a function with variants, simply decorate the primary form with @variants.primary, which then adds the .variant decorator to the original function, which can be used to register new variants. Here is a simple example of a function that prints text, with variants that specify the source of the text to print:

import variants

@variants.primary
def print_text(txt):
    print(txt)

@print_text.variant('from_file')
def print_text(fobj):
    print_text(fobj.read())

@print_text.variant('from_filepath')
def print_text(fpath):
    with open(fpath, 'r') as f:
        print_text.from_file(f)

@print_text.variant('from_url')
def print_text(url):
    import requests
    r = requests.get(url)
    print_text(r.text)

print_text and its variants can be used as such:

print_text('Hello, world!')                 # Hello, world!

# Create a text file
with open('hello_world.txt', 'w') as f:
    f.write('Hello, world (from file)')

# Print from an open file object
with open('hello_world.txt', 'r') as f:
    print_text.from_file(f)                 # Hello, world (from file)

# Print from the path to a file object
print_text.from_filepath('hello_world.txt') # Hello, world (from file)

# Print from a URL
hw_url = 'https://ganssle.io/files/hello_world.txt'
print_text.from_url(hw_url)                 # Hello, world! (from url)

Requirements

This is a library for Python, with support for versions 2.7 and 3.4+.

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

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

variants-0.0.0a0.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

variants-0.0.0a0-py2.py3-none-any.whl (5.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file variants-0.0.0a0.tar.gz.

File metadata

  • Download URL: variants-0.0.0a0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for variants-0.0.0a0.tar.gz
Algorithm Hash digest
SHA256 5cb867f488c23451ef8e9bd909ba54c144b5e5550a98b44cd7cd6fe1826d4bce
MD5 300b0e6b066367acd7b9e5bd58959fe4
BLAKE2b-256 c2ec3a0a98e48150b61062567876dc872e3b978567d78e60f8f3fa8616117374

See more details on using hashes here.

File details

Details for the file variants-0.0.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for variants-0.0.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fbab53ed5d790577c4f0639c20bfe2faed898d061b54dc933b10ba248876718a
MD5 89a163be17c2886a43ae9f1717780337
BLAKE2b-256 01a2afef26291fbac2b5710b371157c5bcc9c982f593de76d70948ebe6e8e7f2

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