Package: logical
Callable subclass of the tuple type for representing logical operators/connectives based on their truth tables.Installation and UsageThis library is available as apackage on PyPI:python -m pip install logicalThe library can be imported in the usual ways:import logical
from logical import *ExamplesEach instance of thelogicalclass (derived from the built-intupleclass) represents a boolean function that acceptsninputs by specifying its output values across all possible inputs. In other words, an instance represents theoutput columnof atruth tablefor a function (under the assumption that the input vectors to which each output value corresponds are sorted in ascending order). Thus, each instance representing a function that acceptsninputs must have length2**n.For example, consider the truth table below for a boolean functionfthat accepts three inputs:xyzf(x,y,z)00010010010101101000101111011110Notice that the input vectors (i.e., the left-most three column values in each row) are sorted in ascending order from top to bottom. If we always assume this order for input vectors, the entire functionfcan be represented using the right-most column. For the example functionfdefined by the table above, this can be done in the manner illustrated below:>>> from logical import *
>>> f = logical((1, 0, 1, 0, 0, 1, 1, 0))It is then possible toapplythe instancefdefined above to any three-component input vector:>>> f(0, 1, 1)
0
>>> f(1, 1, 0)
1It is also possible to create a newlogicalinstance that has afunctionattribute corresponding to acompiled Python functionthat has the same behavior as the__call__method (at least, on valid inputs). This Python function does not check that inputs are of the correct type and format, but has an execution time that is usually at most half of the execution time of the__call__method:>>> f = logical((1, 0, 0, 1, 0, 1, 0, 1))
>>> g = f.compiled()
>>> g.function(0, 0, 0)
1
>>> g.function(1, 1, 0)
0Pre-defined instances are provided for all nullary, unary, and binary boolean functions. These are available both as constants and as attributes of thelogicalclass:>>> logical.xor_(1, 0)
1
>>> and_(1, 0)
0The constantsnullary,unary, andbinaryare also defined. Each is a set containing exactly those instances oflogicalthat represent functions having that arity:>>> unary
{(0, 0), (1, 0), (1, 1), (0, 1)}
>>> len(binary)
16For convenience, the constanteveryis defined as the union ofnullary,unary, andbinary.DevelopmentAll installation and development dependencies are fully specified inpyproject.toml. Theproject.optional-dependenciesobject is used tospecify optional requirementsfor various development tasks. This makes it possible to specify additional options (such asdocs,lint, and so on) when performing installation usingpip:python -m pip install .[docs,lint]DocumentationThe documentation can be generated automatically from the source files usingSphinx:python -m pip install .[docs]
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make htmlTesting and ConventionsAll unit tests are executed and their coverage is measured when usingpytest(see thepyproject.tomlfile for configuration details):python -m pip install .[test]
python -m pytestAlternatively, all unit tests are included in the module itself and can be executed usingdoctest:python src/logical/logical.py -vStyle conventions are enforced usingPylint:python -m pip install .[lint]
python -m pylint src/logicalContributionsIn order to contribute to the source code, open an issue or submit a pull request on theGitHub pagefor this library.VersioningThe version number format for this library and the changes to the library associated with version number increments conform withSemantic Versioning 2.0.0.PublishingThis library can be published as apackage on PyPIby a package maintainer. First, install the dependencies required for packaging and publishing:python -m pip install .[publish]Ensure that the correct version number appears inpyproject.toml, and that any links in this README document to the Read the Docs documentation of this package (or its dependencies) have appropriate version numbers. Also ensure that the Read the Docs project for this library has anautomation rulethat activates and sets as the default all tagged versions. Create and push a tag for this version (replacing?.?.?with the version number):git tag ?.?.?
git push origin ?.?.?Remove any old build/distribution files. Then, package the source into a distribution archive:rm -rf build dist src/*.egg-info
python -m build --sdist --wheel .Finally, upload the package distribution archive toPyPI:python -m twine upload dist/*