Skip to main content

Imperium is a python package that allows you to easily evaluate python expressions

Project description

Imperium

Imperium is a python package that allows you to easily evaluate python expressions

Installation

pip install imperium

Usage

from imperium import Expression

obj = {
    'name': 'iPhone',
    'model': '11 Pro',
    'price': 1299.90,
    'state': 'new'
}

expr = Expression()
if expr.evaluate('$subject.state == "new"', obj): # "$subject" is a reserved key
    # Your logic

To access the data in the given subject, use the "$subject" key as shown above.

Check if the subject has an attribute

from imperium import Expression

obj = {
    'name': 'iPhone',
    'model': '11 Pro',
    'price': 1299.90,
    'state': 'new'
}

someother_obj = {
    'name': 'John Doe'
}

expr = Expression()
res = expr.evaluate('exists("$subject.price", $subject)', subject=obj, source=someother_obj) # REMINDER: "$subject" key let's you access the object that you passed to the evaluate method (obj in this case)
print(res) # Output: True

Testing regular expressions

from imperium.evaluator import Expression

obj = {
    'name': 'iPhone',
    'model': '11 Pro',
    'price': 1299.90,
    'state': 'new'
}

buyer = {
    'fullname': 'John Doe'
}

# Passing the name attribute of the subject
# REMINDER: "$subject" let's you access the object/subject passed to the evaluate method (obj in this case)
# and "$source" let's you access the optional source object passed to the evaluate method 

# INFO: "matches" function let's you evaluate a regular expression against a value, note that the supported flag are "i" and "m"
# if you use something else than these two flags, the evaluation will procede by ignoring the flag since it didn't recognize it.

expression = "exists('$subject.name', $subject) && matches('IPHONE', $subject['name'], 'i')"

expr = Expression()
res = expr.evaluate(expression, subject=obj, source=buyer)
print(res) # Output: True

Imperium has built-in functions to simplify certain actions/verifications.

Function Argument(s) Description
exists() key (Ex: $subject.price), $subject Checks if the given attribute/key exists in the given subject.
matches() regex, value, flag (i or m) Tests a regular expression
date() datestring, format datestring and format are both optional, provide them to the function if you want to convert a date string to date object.
If these two arguments are left emtpy, the function returns date object with current date and time.
date_modify() date, operation (+ | -), valuetype (days | months | years), value: int This function can add and subtract days, months and years from a date object.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

imperium_legacy-0.0.4-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file imperium_legacy-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: imperium_legacy-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.9

File hashes

Hashes for imperium_legacy-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d6d38500571a7f33fd3a49ea0f72240a468527cceb63fbc476125dbb10060c99
MD5 783bf518e1e30951bee1c187b1e8ec71
BLAKE2b-256 47c9cef556c6abbbab6026cdc68d51e5b99a868f92757d8b087e530316ea1f2d

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