Alternatives api is just a syntax sugar for selecting alternative variants from some set of values
Project description
***********************
Alternatives api
***********************
Alternatives basics
---------------------
Alternatives api is just a syntax sugar for selecting alternative variants from some set of values.
The main thing is alternative class that implements late call on callbacks.
This allow to apply boolean logic on callbacks without being executed::
a1 = Alternative(lambda: a)
b1 = Alternative(lambda: b)
a_b = a1 | b1
assert isinstance(a_b, Alternative)
assert bool(a_b) is (a or b)
Also you can note, in the example above, that alternative can provide python truth when it's needed by
calling callbacks and evaluating boolean expressions.
Alternatives usage
---------------------
How it may be used::
package(one_of({
os_centos() or os_windows(): 'php',
os_ubuntu(): 'php5'
}))
Here one of values will be selected: php or php5.
Alternative is object, that's why we can use it as dictionary key.
In the example above you can see one_of() method:
.. automodule:: pywizard.api
:members: one_of
:imported-members:
Another style of evaluating alternatives is all_of:
.. automodule:: pywizard.api
:members: all_of
:imported-members:
And nobody restrict you from creating your own alternatives selection style.
Use source of one_of, all_of as reference.
Checks declaration
-----------------------------
*Check* is just a python function that return boolen with added annotation on it::
@alternative
def os_linux():
"""
os_linux()
Checks if it is linux-like os.
"""
info = os_info()
return 'linux' in info['platform']
.. note::
As function is annotated there is some extra work needed to provide correct documentation for this function.
@alternative handles copying __doc__ transparently for your function. But, you should take care
of specifying correct method signature in first line of docstring.
Alternatives api
***********************
Alternatives basics
---------------------
Alternatives api is just a syntax sugar for selecting alternative variants from some set of values.
The main thing is alternative class that implements late call on callbacks.
This allow to apply boolean logic on callbacks without being executed::
a1 = Alternative(lambda: a)
b1 = Alternative(lambda: b)
a_b = a1 | b1
assert isinstance(a_b, Alternative)
assert bool(a_b) is (a or b)
Also you can note, in the example above, that alternative can provide python truth when it's needed by
calling callbacks and evaluating boolean expressions.
Alternatives usage
---------------------
How it may be used::
package(one_of({
os_centos() or os_windows(): 'php',
os_ubuntu(): 'php5'
}))
Here one of values will be selected: php or php5.
Alternative is object, that's why we can use it as dictionary key.
In the example above you can see one_of() method:
.. automodule:: pywizard.api
:members: one_of
:imported-members:
Another style of evaluating alternatives is all_of:
.. automodule:: pywizard.api
:members: all_of
:imported-members:
And nobody restrict you from creating your own alternatives selection style.
Use source of one_of, all_of as reference.
Checks declaration
-----------------------------
*Check* is just a python function that return boolen with added annotation on it::
@alternative
def os_linux():
"""
os_linux()
Checks if it is linux-like os.
"""
info = os_info()
return 'linux' in info['platform']
.. note::
As function is annotated there is some extra work needed to provide correct documentation for this function.
@alternative handles copying __doc__ transparently for your function. But, you should take care
of specifying correct method signature in first line of docstring.
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
alternatives-0.1.0.zip
(5.0 kB
view details)
File details
Details for the file alternatives-0.1.0.zip
.
File metadata
- Download URL: alternatives-0.1.0.zip
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a27fbe2487fcf16feb4f54d4a94bfaf656d8ec2eef48f0d87608fe8b5aa490e8 |
|
MD5 | ce8d73e29db2549ae45d662c1fe3f913 |
|
BLAKE2b-256 | 1bb3a2c5af3fb67d6f4222aa568575fef345d991f6b4e0d04ce89f29d3001e62 |