Skip to main content

No project description provided

Project description

Introduction

pydictsql is a library which allows you to filter records using SQL. It is aimed at filtering JSON records, or records returned by reading a CSV file with a DictReader, but of course may have many other uses.

Example

Say we have the following data:

SALES_TEAM = [
	{'name': 'Adam', 'city': 'London', 'sales': 100},
	{'name': 'Bob', 'city': 'London', 'sales': 400},
	{'name': 'Charles', 'city': 'Birmingham', 'sales': 350},
	{'name': 'David', 'city': 'London', 'sales': 290},
	{'name': 'Edward', 'city': 'Cardiff', 'sales': 180},
	{'name': 'Frank', 'city': 'Glasgow', 'sales': 320},
	{'name': 'Geoff', 'city': 'Cardiff', 'sales': 500},
	{'name': 'Hugh', 'city': 'London', 'sales': 380},
	{'name': 'Ian', 'city': 'Birmingham', 'sales': 350},
	{'name': 'John', 'city': 'Birmingham', 'sales': 460}
]

If we want to find the names of all sales people with a sales figure of over 400, we can then use the library to filter that list as follows:

import pydictsql

filter = pydictsql.DictFilter("SELECT {name} FROM {sales_team} WHERE {sales} > 400")
results = filter.filter(sales_team = SALES_TEAM)
for record in results:
	print(record["name"])

SQL Syntax

The syntax is a subset of the SQL syntax that you will be familiar with when working with databases.

Because field names in json and CSV files may not adhere to the same standards as those in databases, all field names are enclosed in curly brackets.

pydictsql supports the following logical operators:

  • AND
  • OR
  • NOT

and the following comparison operators:

  • less than <
  • greater than >
  • less than or equal to <=
  • greater than or equal to >=
  • equal to =
  • not equal to <>

Note that the reference in the FROM clause of the SQL must match the named parameter passed to the filter / filtergen methods.

Class Reference

pydictsql.DictFilter()

Details

Constructs a DictFilter object, taking the SQL which will be applied to filter data.

Parameters
  • sql SQL Select statement which is used to filter data
Raises
  • InvalidTokenError: Raised when tokenising and an invalid value is read
  • UnexpectedTokenError: Raised when an unexpected token is encountered parsing the SQL

pydictsql.DictFilter.filter()

Details

Applies the SQL to the given data, return the records which satisfy the given SQL.

Parameters
  • <collection> Data to be filtered. Note that the kwarg name <collection> must match that in the FROM reference in the SQL
Returns
  • Tuple (If passed a tuple) or list of records satisfying the given SQL, with each records having the selected fields as defined in the SQL.
Raises
  • ValueError: Raised when an invalid parameter is passed to the method, for example a parameter which is not a list, tuple or generator, or the kwarg name not matching the FROM clause in the SQL.
  • UnexpectedReferenceError: Raised when a field reference in the SQL is not found in the passed data.

pydictsql.DictFilter.filtergen()

Details

Applies the SQL to the given data, yielding each matching record as the given data is iterated over. This may be preferred when processing larger data sets.

Parameters
  • <collection> Data to be filtered. Note that the kwarg name <collection> must match that in the FROM reference in the SQL
Returns
  • Tuple (If passed a tuple) or list of records satisfying the given SQL, with each records having the selected fields as defined in the SQL.
Raises
  • ValueError: Raised when an invalid parameter is passed to the method, for example a parameter which is not a list, tuple or generator, or the kwarg name not matching the FROM clause in the SQL.
  • UnexpectedReferenceError: Raised when a field reference in the SQL is not found in the passed data.

General Points

filter() vs filtergen()

Filter will return a collection containing each of the records which meet the SQL criteria, meaning that they will be loaded into memory. If you are processing a large amount of data and do not wish to store all matching records at the same time, then filtergen should be used.

Why the named parameter?

I'm future proofing here, leaving the door open to easily adding multiple data sources with joins or unions.

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

pydictsql-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

pydictsql-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file pydictsql-0.1.0.tar.gz.

File metadata

  • Download URL: pydictsql-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.4 Windows/10

File hashes

Hashes for pydictsql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 710ddbf07ed36510f7bb630660406a9c096bc93e1d2adec8e9f9bc27986e9e45
MD5 5802a29172f7d0e6d642535183da7c0e
BLAKE2b-256 38957ed90c8f9b6784b62552779efaa348d8aed05bfe436c02aff6c0626df24d

See more details on using hashes here.

File details

Details for the file pydictsql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydictsql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.4 Windows/10

File hashes

Hashes for pydictsql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 639854b74664f9e7fdaa29a65f50459544a650ca83634555fe79790b80573e9b
MD5 06348efd7d87240a15e9ec81df0aa1e0
BLAKE2b-256 5c9bc5f6ddd6394c58d639f122ae19a49168ea90ccbbd5e40f77e23953ddd720

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