Skip to main content

Callable subclass of the tuple type for representing logical operators/connectives based on their truth tables.

PyPI version and link. Read the Docs documentation status. GitHub Actions status. Coveralls test coverage summary.

Installation and Usage

This library is available as a package on PyPI:

python -m pip install logical

The library can be imported in the usual ways:

import logical
from logical import *

Examples

Each instance of the logical class (derived from the built-in tuple class) represents a boolean function that accepts n inputs by specifying its output values across all possible inputs. In other words, an instance represents the output column of a truth table for 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 accepts n inputs must have length 2**n.

For example, consider the truth table below for a boolean function f that accepts three inputs:

x

y

z

f (x, y, z)

0

0

0

1

0

0

1

0

0

1

0

1

0

1

1

0

1

0

0

0

1

0

1

1

1

1

0

1

1

1

1

0

Notice 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 function f can be represented using the right-most column. For the example function f defined 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 to apply the instance f defined above to any three-component input vector:

>>> f(0, 1, 1)
0
>>> f(1, 1, 0)
1

It is also possible to create a new logical instance that has a function attribute corresponding to a compiled Python function that 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)
0

Pre-defined instances are provided for all nullary, unary, and binary boolean functions. These are available both as constants and as attributes of the logical class:

>>> logical.xor_(1, 0)
1
>>> and_(1, 0)
0

The constants nullary, unary, and binary are also defined. Each is a set containing exactly those instances of logical that represent functions having that arity:

>>> unary
{(0, 0), (1, 0), (1, 1), (0, 1)}
>>> len(binary)
16

For convenience, the constant every is defined as the union of nullary, unary, and binary.

Development

All installation and development dependencies are fully specified in pyproject.toml. The project.optional-dependencies object is used to specify optional requirements for various development tasks. This makes it possible to specify additional options (such as docs, lint, and so on) when performing installation using pip:

python -m pip install .[docs,lint]

Documentation

The documentation can be generated automatically from the source files using Sphinx:

python -m pip install .[docs]
cd docs
sphinx-apidoc -f -E --templatedir=_templates -o _source .. && make html

Testing and Conventions

All unit tests are executed and their coverage is measured when using pytest (see the pyproject.toml file for configuration details):

python -m pip install .[test]
python -m pytest

Alternatively, all unit tests are included in the module itself and can be executed using doctest:

python src/logical/logical.py -v

Style conventions are enforced using Pylint:

python -m pip install .[lint]
python -m pylint src/logical

Contributions

In order to contribute to the source code, open an issue or submit a pull request on the GitHub page for this library.

Versioning

The version number format for this library and the changes to the library associated with version number increments conform with Semantic Versioning 2.0.0.

Publishing

This library can be published as a package on PyPI by a package maintainer. First, install the dependencies required for packaging and publishing:

python -m pip install .[publish]

Ensure that the correct version number appears in pyproject.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 an automation rule that 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 to PyPI:

python -m twine upload dist/*

Metadata

Release files for logical 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logical 2.0.0
File Size Uploaded
logical-2.0.0.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logical 2.0.0
File Interpreter ABI Platform
logical-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.5 kB

Release files / logical-2.0.0.tar.gz

Download URL logical-2.0.0.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5f719dfca48bcec8a728db3f6c838f4063dc18e51709c547da5631c10a2d0f14
BLAKE2b-256 checksum
How to use checksums
d170b2a1a0ebb918450975bacd6d4e9abc9a84fa658e7a8229fd90025b15d637
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.2

Release files / logical-2.0.0-py3-none-any.whl

Download URL logical-2.0.0-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
46d3000ca55a804564d1a772b29efefa4858a5deab90f3c560bd2c9d69378944
BLAKE2b-256 checksum
How to use checksums
43a05630a6261a10ef040fb85ca2cec4337b743af6351f7242158ed18697eb13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.2

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.0

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page