Skip to main content

Safer way to sympify unsanitized input

Project description

Safer Sympify

Convert unsanitized user input into SymPy expression.

This does not prevent all possible attacks. However, this is much safer than using eval(), which SymPy still does.

See SymPy PR12524 for more details: https://github.com/sympy/sympy/pull/12524

Examples

    Create a SymPy expression from user input. 
    This uses pure Python syntax. 
    Whitelisted operators and functions only are allowed.
    >>> expr = SaferSympify().str2sympy('-sqrt(1 + a**b*b)/((a**b)*b+1)')
    >>> expr
    -1/sqrt(a**b*b + 1)

    Get free symbols:
    >>> sorted(expr.free_symbols, key=lambda x: str(x))
    [a, b]

    Evaluate expression:
    >>> expr.evalf(subs={'a': 1, 'b': 3, 'c': 5})  # Note extra values can be passed too
    -0.500000000000000

    Simplify expression:
    >>> expr.simplify()
    -1/sqrt(a**b*b + 1)

    Pretty-print expression as Latex (could be displayed in browser with MathJax)
    >>> sympy.latex(expr)
    '- \\frac{1}{\\sqrt{a^{b} b + 1}}'

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

safersympify-0.1.18.tar.gz (2.6 kB view details)

Uploaded Source

File details

Details for the file safersympify-0.1.18.tar.gz.

File metadata

  • Download URL: safersympify-0.1.18.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for safersympify-0.1.18.tar.gz
Algorithm Hash digest
SHA256 f22d5b2dae200a28e29917da71cf5851d6bc03cc1c3c322c3326c8c19a546b5b
MD5 990f1b6ee1e9e7b26de274b2343ae856
BLAKE2b-256 6f76de0769208375bc242c72c60b4f1b5601a83a853c2627a19498c6cbdcd9e0

See more details on using hashes here.

Supported by

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