Skip to main content

convert a logical expression written in `Python` syntax, into a `Sympy` expression

Project description

logic_python2sympy

PyPI: https://pypi.org/project/logic-python2sympy/
Source: https://github.com/duntytelessia/logic_python2sympy/
License: MIT

Sympy has the advantage of using Python syntax in its own expressions, but with some exceptions : logical expressions in Sympy differ a bit from their Python counterparts (a and b is written a & b in Sympy). Thus, this module provides functions that can convert a logical expression written in Python syntax, into a Sympy expression.

Usage

>>> import logic_python2sympy as p2s
>>> code = "((0 < x <= 10) or not y"
>>> print(p2s.convert(code))
Or(And(0 < x, x <= 10), Not(y))
>> print(p2s.get_expression(code))
~y | ((0 < x) & (x <= 10))

convert

convert(expr: str) -> str

Transform a Python logical expression into Sympy syntax.
expr: valid logical expression, i.e. expr must be valid Python syntax, and must be a combintion of the following ast nodes: BoolOp, BinOp, UnaryOp, Compare, Call, Constant, Name
raises: NotImplentedError, when the expression contains operators that are not supported in Sympy

get_expression

get_expression(expr: str) -> Any

Transform a Python logical expression into a Sympy expression.
expr: valid logical expression, i.e. expr must be valid Python syntax, and must be a combintion of the following ast nodes: BoolOp, BinOp, UnaryOp, Compare, Call, Constant, Name raises: NotImplentedError, when the expression contains operators that are not supported in Sympy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logic_python2sympy-0.1.3.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

logic_python2sympy-0.1.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file logic_python2sympy-0.1.3.tar.gz.

File metadata

  • Download URL: logic_python2sympy-0.1.3.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for logic_python2sympy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7c4386f0ff62e2b44f67c3d33b8df7c8797a56e03775a6daef7cd02e669868f5
MD5 cd6a89335dce79ba3b153f9e72677e00
BLAKE2b-256 b7afa68e30ac33cd25493016479980e0d0a60431c164e9be1e2a001074806114

See more details on using hashes here.

File details

Details for the file logic_python2sympy-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for logic_python2sympy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c720903bb2f5fbfd6712cc218c650caa61476e119864419c4b1cc3f8cb605bf0
MD5 5cbad75aa8c2273042b82cfec244af05
BLAKE2b-256 bcfcb31b49035d063a688dc26d98a77fc7579e76d4924d90ddb7ea5edd8e6376

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page