Skip to content

Commit 50cad84

Browse files
committed
Add basic example to README.rst
1 parent 038123c commit 50cad84

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

README.rst

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,72 @@ variants
66
.. image:: https://img.shields.io/pypi/v/variants.svg
77
:target: https://pypi.python.org/pypi/variants
88

9-
.. image:: https://img.shields.io/travis/pganssle/variants.svg
10-
:target: https://travis-ci.org/pganssle/variants
9+
.. image:: https://img.shields.io/travis/python-variants/variants.svg
10+
:target: https://travis-ci.org/python-variants/variants
1111

1212
.. image:: https://readthedocs.org/projects/variants/badge/?version=latest
1313
:target: https://variants.readthedocs.io/en/latest/?badge=latest
1414
:alt: Documentation Status
1515

16-
.. image:: https://pyup.io/repos/github/pganssle/variants/shield.svg
17-
:target: https://pyup.io/repos/github/pganssle/variants/
16+
.. image:: https://pyup.io/repos/github/python-variants/variants/shield.svg
17+
:target: https://pyup.io/repos/github/python-variants/variants/
1818
:alt: Updates
1919

20+
``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.
2021

21-
Library that enables the creation of altern constructor-like variant forms for arbitrary functions.
22+
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:
2223

24+
.. code-block:: python
2325
24-
* Free software: Apache Software License 2.0
25-
* Documentation: https://variants.readthedocs.io.
26+
import variants
2627
28+
@variants.primary
29+
def print_text(txt):
30+
print(txt)
2731
28-
Features
29-
--------
32+
@print_text.variant('from_file')
33+
def print_text(fobj):
34+
print_text(fobj.read())
35+
36+
@print_text.variant('from_filepath')
37+
def print_text(fpath):
38+
with open(fpath, 'r') as f:
39+
print_text.from_file(f)
40+
41+
@print_text.variant('from_url')
42+
def print_text(url):
43+
import requests
44+
r = requests.get(url)
45+
print_text(r.text)
46+
47+
48+
``print_text`` and its variants can be used as such:
49+
50+
.. code-block:: python
51+
52+
print_text('Hello, world!') # Hello, world!
53+
54+
# Create a text file
55+
with open('hello_world.txt', 'w') as f:
56+
f.write('Hello, world (from file)')
57+
58+
# Print from an open file object
59+
with open('hello_world.txt', 'r') as f:
60+
print_text.from_file(f) # Hello, world (from file)
61+
62+
# Print from the path to a file object
63+
print_text.from_filepath('hello_world.txt') # Hello, world (from file)
64+
65+
# Print from a URL
66+
hw_url = 'https://ganssle.io/files/hello_world.txt'
67+
print_text.from_url(hw_url) # Hello, world! (from url)
68+
69+
70+
Requirements
71+
------------
72+
73+
This is a library for Python, with support for versions 2.7 and 3.4+.
3074

31-
* TODO
3275

3376
Credits
3477
---------

0 commit comments

Comments
 (0)