Skip to main content

Tool for parsing SQL like where expressions and evaluating against live data

Project description

WherEval

Tool for parsing SQL like where expressions and evaluating against live data

Installation

python3 -m pip install whereval

Inspiration

This tool will scratch an itch I've had for some cli tools where I want to pass complex filter expressions to control output / processing. For instance with my histstat fork, I would like to have better filtering of networking information to be output to sqlite. See: https://github.com/JavaScriptDude/histstat

Another usecase for this is a tool I've wanted to write where I can write a tail -f wrapper in python where I can define a filter in cli parameters to limit output to the console. Have not written this yet but its on my todo now that I've got this API.

API Example

(Ex1) Basic Concept:

from datetime import date
from whereval import Where, util as wutil

print("\n(Ex1) Basic Idea")

sw = wutil.StopWatch()

# Where query (terse example)
qry = "(f0>=2+f1=1+f2='s')|(f3~'foo%')"

# Field and type spec
spec = {'f0': int,'f1': bool,'f2': str,'f3': str, 'f4': dt_date}

# Instantiate Where
#  - Parses query and uses data to form rules for data types and fields
wher = Where(query=qry, spec=spec)


print(f"Query:\n . raw:\t{qry}\n . compiled: {wher}\n\nTests:")

def _print(wher, data, tup): 
	(ok, result, issues) = tup
	if not ok:
		print(f"Eval failed for {wher}. Issues: {issues}")
	else:
		print(f"\t{result}\tw/ data: {data}")

# Evaluate expression against real data
data = {'f0': 2, 'f1': True ,'f2': 's', 'f3': 'foobar'}
_print(wher, data, wher.evaluate(data))

# For different data
data['f3'] = 'bazbar'
_print(wher, data, wher.evaluate(data))

# For different data
data['f0'] = 1
_print(wher, data, wher.evaluate(data))

print(f"Completed. Elapsed: {sw.elapsed(3)}s")

Output of print:

(Ex1) Basic Idea
Query:
 . raw:	(f0>=2+f1=1+f2='s')|(f3~'foo%')
 . compiled: ( ( f0 >= 2 AND f1 = 1 AND f2 = 's' ) OR ( f3 like 'foo%' ) )

Tests:
	True	w/ data: {'f0': 2, 'f1': True, 'f2': 's', 'f3': 'foobar'}
	True	w/ data: {'f0': 2, 'f1': True, 'f2': 's', 'f3': 'bazbar'}
	False	w/ data: {'f0': 1, 'f1': True, 'f2': 's', 'f3': 'bazbar'}
Completed. Elapsed: 0.003s

Query Syntax:

# General:
#  Query must begin with '(' and end with ')'
#  NOT, IN, BETWEEN must be followed by parenthesis '('
# Boolean Operators:
#   AND, OR, NOT
# Equality Operators:
#  =, !=, <, <=, >, >=, like
# Special:
#   !   -->  NOT
#   +   -->  AND
#   |   -->  OR
#   <>  -->  !=
#   ~   -->  like

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

whereval-0.1.8.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

whereval-0.1.8-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file whereval-0.1.8.tar.gz.

File metadata

  • Download URL: whereval-0.1.8.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.7.9 Linux/5.15.0-1034-oracle

File hashes

Hashes for whereval-0.1.8.tar.gz
Algorithm Hash digest
SHA256 9702d0ae68f416fc8ef26ef945cd936e3a7211f893e8dcf973827e1234c7c6e4
MD5 701e87f3f5d5a8486b8a88431827b39d
BLAKE2b-256 5f0270a79e7d6d917b99f93ad2ead4e123c5e8ee8170b5dd3255f60dc3d0df22

See more details on using hashes here.

File details

Details for the file whereval-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: whereval-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.7.9 Linux/5.15.0-1034-oracle

File hashes

Hashes for whereval-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 d4a376e9ba184ad06a5566440b855c784198ec898e588df62fea778c6ff55e1b
MD5 d379818c52384f9338a1c9e8190b3423
BLAKE2b-256 04fe9255f2493daf110384dfcc50c7b495449e0aa3938bb9e8f5d774003dccf4

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