Skip to main content

Safe execute expr code.

Project description

📦 perfect-eval

Safe eval module

Installation

pip install -i https://mirrors.aliyun.com/pypi/simple/ --extra-index-url https://pypi.org/simple/ perfect-eval

Example

from perfect_eval import safe_eval

safe_eval("print('11111111')", {"print": print})
# output: 11111111
print(safe_eval("now.strftime('%m-%d')", {"now": datetime.now()}))
# output: 10-28
print(safe_eval("a + b", {"a": 123, "b": 456}))
# output: 579
from perfect_eval import safe_eval, Tmpl

template_str = Tmpl("My name is [name] and my age is [age]")
print(safe_eval(template_str, {"name": "Xiaoming", "age": "17"}))
# output: My name is Xiaoming and my age is 17
template_str = Tmpl("[age] is Xiaoming's age")
print(safe_eval(template_str, {"name": "Xiaoming", "age": "17"}))
# output: 17 is Xiaoming's age
template_str = Tmpl("Xiao Ming's age is [age]")
print(safe_eval(template_str, {"name": "Xiaoming", "age": "17"}))
# output: Xiao Ming's age is 17
template_str = Tmpl("[str(a)] + [str(b)] = [str(a + b)]")
# output: 12 + 13 = 25
template_str = Tmpl("['Have a fever' if temp > 37.5 else 'be free']")
print(safe_eval(template_str, {"temp": 36.6}))
print(safe_eval(template_str, {"temp": 38}))
# output:be free\nHave a fever 

To Do

  • Be the best version of you.

More Resources

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

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

perfect-eval-0.0.3.tar.gz (9.9 kB view hashes)

Uploaded Source

Built Distribution

perfect_eval-0.0.3-py3-none-any.whl (9.5 kB view hashes)

Uploaded Python 3

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