Browse Source

initial commit

master
Trey Morris 4 years ago
parent
commit
1c9036a2ef
5 changed files with 206 additions and 2 deletions
  1. +3
    -0
      MANIFEST.in
  2. +86
    -2
      README.md
  3. +32
    -0
      setup.py
  4. +18
    -0
      truths/__init__.py
  5. +67
    -0
      truths/truths.py

+ 3
- 0
MANIFEST.in View File

@@ -0,0 +1,3 @@
include *.rst
include *.md
include LICENSE

+ 86
- 2
README.md View File

@@ -1,2 +1,86 @@
# truths
auto generate truth tables
# truths - auto generate truth tables
truths is a simple tool that allows you to quickly generate a truth table from python variable names and phrases


## install
`pip install truths` or `git clone` and `pip install -e` to play with the code


### use is simple:
start by creating some base variables

```python
print Truths(['a', 'b', 'x'])
+---+---+---+
| a | b | x |
+---+---+---+
| 0 | 0 | 0 |
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 0 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
| 1 | 1 | 1 |
+---+---+---+
```


### add some phrases
now let's use those base variables and pass in some phrases!

```python
print Truths(['a', 'b', 'x', 'd'], ['(a and b)', 'a and b or x', 'a and (b or x) or d'])
+---+---+---+---+-----------+--------------+---------------------+
| a | b | x | d | (a and b) | a and b or x | a and (b or x) or d |
+---+---+---+---+-----------+--------------+---------------------+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | 0 | 0 | 1 |
| 0 | 0 | 1 | 0 | 0 | 1 | 0 |
| 0 | 0 | 1 | 1 | 0 | 1 | 1 |
| 0 | 1 | 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 1 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 | 0 | 1 | 1 |
| 1 | 0 | 1 | 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 | 1 | 1 | 1 |
| 1 | 1 | 0 | 1 | 1 | 1 | 1 |
| 1 | 1 | 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+-----------+--------------+---------------------+
```


### prefer boolean words?
neat eh? if you prefer True/False over the numbers pass `ints=False`:

```python
print Truths(['a', 'b', 'x', 'd'], ['(a and b)', 'a and b or x', 'a and (b or x) or d'], ints=False)
+-------+-------+-------+-------+-----------+--------------+---------------------+
| a | b | x | d | (a and b) | a and b or x | a and (b or x) or d |
+-------+-------+-------+-------+-----------+--------------+---------------------+
| False | False | False | False | False | False | False |
| False | False | False | True | False | False | True |
| False | False | True | False | False | True | False |
| False | False | True | True | False | True | True |
| False | True | False | False | False | False | False |
| False | True | False | True | False | False | True |
| False | True | True | False | False | True | False |
| False | True | True | True | False | True | True |
| True | False | False | False | False | False | False |
| True | False | False | True | False | False | True |
| True | False | True | False | False | True | True |
| True | False | True | True | False | True | True |
| True | True | False | False | True | True | True |
| True | True | False | True | True | True | True |
| True | True | True | False | True | True | True |
| True | True | True | True | True | True | True |
+-------+-------+-------+-------+-----------+--------------+---------------------+
```


### how it works
check out the code! behind the scenes it's putting the bases in an object context and generating a grid of values for them. then, the phrases are `eval`uated in the object's context against each row in that grid of values

+ 32
- 0
setup.py View File

@@ -0,0 +1,32 @@
#!/usr/bin/env python
#
# Copyright 2013 Trey Morris
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from setuptools import setup, find_packages


setup(
name='truths',
version='1.0',
author='Trey Morris',
author_email='trey@treymorris.com',
description='auth generate truth tables',
long_description=open('README.rst').read(),
classifiers=['Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: Apache Software License'],
keywords=['truth', 'table', 'truth table', 'truthtable', 'logic'],
packages=find_packages(),
license='Apache Software License',
url='https://github.com/tr3buchet/truths')

+ 18
- 0
truths/__init__.py View File

@@ -0,0 +1,18 @@
#!/usr/bin/env python
#
# Copyright 2016 Trey Morris
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from truths import Truths

+ 67
- 0
truths/truths.py View File

@@ -0,0 +1,67 @@
#!/usr/bin/env python
#
# Copyright 2016 Trey Morris
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import itertools
from prettytable import PrettyTable
import re


class Gob(object):
pass


class Truths(object):
def __init__(self, base=None, phrases=None, ints=True):
if not base:
raise Exception('Base items are required')
self.base = base
self.phrases = phrases or []
self.ints = ints

# generate the sets of booleans for the bases
self.base_conditions = list(itertools.product([False, True],
repeat=len(base)))

# regex to match whole words defined in self.bases
# used to add object context to variables in self.phrases
self.p = re.compile(r'(?<!\w)(' + '|'.join(self.base) + ')(?!\w)')

def calculate(self, *args):
# store bases in an object context
g = Gob()
for a, b in zip(self.base, args):
setattr(g, a, b)

# add object context to any base variables in self.phrases
# then evaluate each
eval_phrases = []
for item in self.phrases:
item = self.p.sub(r'g.\1', item)
eval_phrases.append(eval(item))

# add the bases and evaluated phrases to create a single row
row = [getattr(g, b) for b in self.base] + eval_phrases
if self.ints:
return [int(item) for item in row]
else:
return row

def __str__(self):
t = PrettyTable(self.base + self.phrases)
for conditions_set in self.base_conditions:
t.add_row(self.calculate(*conditions_set))
return str(t)

Loading…
Cancel
Save