Skip to main content

Serialize and deserialize nested compound expression strings such as (a = 1 or (b = 2 and c = 3)) into parsable expression graphs such as [ key:a operator:= value:1, conjunction:or, [ key:b operator:= value:2, conjunction:and, key:c operator:= value:3 ] ].

Project description

Expression

About

Serialize and deserialize nested compound expression strings such as (a = 1 or (b = 2 and c = 3)) into parsable expression trees such as [key:a operator:= value:1, conjunction:or, [key:b operator:= value:2, conjunction:and, key:c operator:= value:3]].

You may want to:-

  • provide an easy to configure filter on an endpoint such as GET /services?filter=(price lt 200 and duration is 2hrs).
  • build an ORM filter based on the expression.
  • simply evaluate that an expression is True or False.

These sets of classes make few assumptions as to how conditions will be evaluated. It lets the author configure handlers that can be used to resolve conditions.

Install

  • Todo

Configure

Use

Deserialize a string into a tree of conditions.

expression = Expression('(a = 1 and b = 2)')
conditions = expression.to_conditions()

print(conditions.conditions[0].key))        # a
print(conditions.conditions[0].operator))   # =
print(conditions.conditions[0].value))      # 1

print(conditions.conditions[1].value))      # and

print(conditions.conditions[2].key)))       # b
print(conditions.conditions[2].operator))   # =
print(conditions.conditions[2].value))      # 2

Evaluate an expression such date date_between 2020-09-26,2020-09-28 as a boolean.

Instantiate the Evaluate object, passing through resolved expression arguments. In this case the date key will be resolved to a date object of 2020-09-27.

evaluate = Evaluate({
    "date": datetime.strptime("2020-09-27", '%Y-%M-%d')
})

Before evaluating ensure that the date_between expression can be handled by a designated Handler such as DateBetweenHandler.

evaluate.add_condition_handlers({
    'date_between': DateBetweenHandler
})

Now evaluate the expression from string.

result = evaluate.from_expression('date date_between 2020-09-26,2020-09-28')
self.assertTrue(result)

You can plug in any custom handler to suit the usecase.

class DateBetweenHandler(Handler):

    def handle(self, condition):
        key = self._data.get(condition.key)

        values = condition.value.split(',')
        date1 = datetime.strptime(values[0], '%Y-%M-%d')
        date2 = datetime.strptime(values[1], '%Y-%M-%d')

        return date1 <= key <= date2

Contribute

Running tests

coverage run -m unittest2 discover -p="*Test.py"
coverage html

or use

bash bin/run-tests
bash bin/run-tests *Test.py

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

python-expression-0.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_expression-0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file python-expression-0.1.tar.gz.

File metadata

  • Download URL: python-expression-0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.5

File hashes

Hashes for python-expression-0.1.tar.gz
Algorithm Hash digest
SHA256 910b568bd348c98b7b696936f40b1129799feed97e095e1c33951a2b4b945d19
MD5 86dd57ca0f7b33e33dfcc4143443fe71
BLAKE2b-256 eece716605b1d10fa9ce0c60414059dc81fc09f10842eead95152a66bf2d4821

See more details on using hashes here.

File details

Details for the file python_expression-0.1-py3-none-any.whl.

File metadata

  • Download URL: python_expression-0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.5

File hashes

Hashes for python_expression-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da19d22493d7d66b538eaaa591a39dd0923733a3447f65c7a1bf744adbb2ac3f
MD5 df08e54474f3ef283e005f489eae02e6
BLAKE2b-256 039c0af33e4f1a70a1394bad4bd7734d734b306cedde9f2eb8003189c4b5a4c1

See more details on using hashes here.

Supported by

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