GithubHelp home page GithubHelp logo

boolean-py-export-example's Introduction

This is a very small example of using the boolean.py library to simplify boolean expressions.

I provide some context in this YouTube video.

Their library allows me to construct a boolean expression like so:

(~w) & (z | (x & y & (~z)) | (~y | z))

And then the library simplifies it as this (super cool!):

~w&(x|~y|z)

You can details on how to do this yourself in their excellent documentation.

You can also see me doing it example.py.

Exporting expressions

For my use case I want to let boolean.py do the heavy lifting of simplifying a boolean expression. But then once that happens, I want to export the expression from boolean.py into my own system of objects.

Here is the code that I wrote to do that, which you will find in boolean_export.py:

def export(expr, *, f_and, f_or, f_not, f_symbol):
    def f(expr):
        if type(expr) == boolean.boolean.OR:
            return f_or([f(arg) for arg in expr.args])
        elif type(expr) == boolean.boolean.AND:
            return f_and([f(arg) for arg in expr.args])
        elif type(expr) == boolean.boolean.NOT:
            return f_not([f(arg) for arg in expr.args])
        elif type(expr) == boolean.boolean.Symbol:
            return f_symbol(expr.obj)
        else:
            assert False
    return f(expr)

The code works fine for me, but it is obviously relying on some implementation details of their library.

Here is what I do with export inside example.py:

def paren(s):
    return f"({s})"

def OUT_AND(args):
    return paren(" AND ".join(str(arg) for arg in args))

def OUT_OR(args):
    return paren(" OR ".join(str(arg) for arg in args))

def OUT_NOT(args):
    assert len(args) == 1
    return paren(f"NOT {args[0]}")

def OUT_SYMBOL(arg):
    return arg.capitalize()

my_expr = boolean_export.export(expr, f_and=OUT_AND, f_or=OUT_OR, f_not=OUT_NOT, f_symbol=OUT_SYMBOL)
assert my_expr == "((NOT W) AND (X OR (NOT Y) OR Z))"

In my actual use case I want to do something a bit more sophisticated than just rendering the expression in ALL-CAPS, but I believe that my export function will handle a lot of typical use cases.

Feature request

I am hoping that the folks who maintain boolean.py will consider adding my export function into the library and supporting it. For now I filed bastikr/boolean.py#116

boolean-py-export-example's People

Contributors

showell avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.