Skip to content

Fix eval of constants #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tomas789
Copy link
Contributor

@tomas789 tomas789 commented Dec 5, 2022

This fixes two problems.

First is that the classes _TRUE and _FALSE don't accept keyword arguments for the __call__ function. This means that when evaluating model with constants it raises a similar exception to this:

Traceback (most recent call last):
  File "/Users/tomaskrejci/Developer/quine-mccluskey/test.py", line 10, in <module>
    print(formula(**model))
          ^^^^^^^^^^^^^^^^
  File "/Users/tomaskrejci/Developer/quine-mccluskey/boolean.py/boolean/boolean.py", line 1596, in __call__
    return reduce(self._pyoperator, (a(**kwargs) for a in self.args))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomaskrejci/Developer/quine-mccluskey/boolean.py/boolean/boolean.py", line 1596, in <genexpr>
    return reduce(self._pyoperator, (a(**kwargs) for a in self.args))
                                     ^^^^^^^^^^^
TypeError: _FALSE.__call__() got an unexpected keyword argument 'x0'

After adding the keyword arguments the second problem is that the __call__ magic function returns self instead of the boolean value. This leads to exception similar to this:

Traceback (most recent call last):
  File "/Users/tomaskrejci/Developer/quine-mccluskey/fuzzer.py", line 58, in <module>
    print(k, model, ones_formula(**model))
                    ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomaskrejci/Developer/quine-mccluskey/boolean.py/boolean/boolean.py", line 1213, in __call__
    return not self.args[0](**kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomaskrejci/Developer/quine-mccluskey/boolean.py/boolean/boolean.py", line 1596, in __call__
    return reduce(self._pyoperator, (a(**kwargs) for a in self.args))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomaskrejci/Developer/quine-mccluskey/boolean.py/boolean/boolean.py", line 881, in __or__
    return self.OR(self, other)
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/tomaskrejci/Developer/quine-mccluskey/boolean.py/boolean/boolean.py", line 1647, in __init__
    super(OR, self).__init__(arg1, arg2, *args)
  File "/Users/tomaskrejci/Developer/quine-mccluskey/boolean.py/boolean/boolean.py", line 1245, in __init__
    super(DualBase, self).__init__(arg1, arg2, *args)
  File "/Users/tomaskrejci/Developer/quine-mccluskey/boolean.py/boolean/boolean.py", line 1051, in __init__
    assert all(
AssertionError: Bad arguments: all arguments must be an Expression: (TRUE, False)

Code used to reproduce both bugs:

import boolean

algebra = boolean.BooleanAlgebra()
TRUE, FALSE, NOT, AND, OR, symbol = algebra.definition()

formula = FALSE
model = {"x0": False}

# This works
print(formula())

# This does not
print(formula(**model))

Copy link
Collaborator

@pombredanne pombredanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! can you add tests too?
The fact that we eval FALSE and TRUE to themselves may be a design decision, so getting tests is important!

@pombredanne
Copy link
Collaborator

gentle ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants