Skip to main content

Condition

PyPI version PyPI Docs pipeline status coverage report

Project Description

This project provides a user friendly way to construct a condition object. The condition object can be passed around and later used in various contexts, such as querying pandas Dataframe, filtering pyarrow partitions or generating where clauses in SQL. It takes care of formating and syntax for you. It also supports extensibility to new usage contexts.

A Quick Example

>>> from condition import *
>>> import pandas as pd
>>> fl = FieldList('A B C date value'.split())
>>> cond = ((fl.date >= pd.to_datetime('20000101')) 
...             & (fl.date <= pd.to_datetime('20010131'))
...             & (fl.A == 'a1 a3'.split()) \
...             & (fl.B == 'b1')) \
...             | (fl.C != 'c3 c5'.split())
>>> print(cond) # show as sql where condition

        (
                (date >= '2000-01-01 00:00:00'
                and date <= '2001-01-31 00:00:00'
                and A in ('a1','a3')
                and B = 'b1')
        or C not in ('c3','c5'))

>>> cond.to_df_query()
"(((date >= '2000-01-01 00:00:00')&(date <= '2001-01-31 00:00:00')&(A in ('a1','a3'))&(B == 'b1'))|(C not in ('c3','c5')))"
>>> cond.to_pyarrow_filter()
[[('date', '>=', Timestamp('2000-01-01 00:00:00')),
  ('date', '<=', Timestamp('2001-01-31 00:00:00')),
  ('A', 'in', {'a1', 'a3'}),
  ('B', '=', 'b1')],
 [('C', 'not in', {'c3', 'c5'})]]

SQL Generation

>>> from condition.sql import render_sql
>>> sql = """
...     select *
...     from my_table
...     where {{where_condition}}
... """
>>> print(render_sql(sql, cond))

    select *
    from my_table
    where 
        (
                (date >= '2000-01-01 00:00:00'
                and date <= '2001-01-31 00:00:00'
                and A in ('a1','a3')
                and B = 'b1')
        or C not in ('c3','c5'))

Please see Usage Examples for detailed examples.

Installation

This project is distributed via pip. To get started:

pip install condition

To install jinja2 package used by condition.sql, do the following

pip install "condition[sql]"

To install all packages for development, do the following

pip install "condition[dev]"

Metadata

Release files for condition 1.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for condition 1.0.8
File Size Uploaded
condition-1.0.8.tar.gz 35.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for condition 1.0.8
File Interpreter ABI Platform
condition-1.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 50.8 kB

Release files / condition-1.0.8.tar.gz

Download URL condition-1.0.8.tar.gz
Size 35.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c5b96dd18622784777b4792e8dbc793e56d9d760039c29288b3c2c5706f0797c
BLAKE2b-256 checksum
How to use checksums
cc86469af68ee68581c51d4a3cf00afb4697e23087814f672be8c9d89d217892
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

Release files / condition-1.0.8-py3-none-any.whl

Download URL condition-1.0.8-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09f815d33b8a2b277f2afbb7aa341637e207a273b858e7dcc1c883c4030b7571
BLAKE2b-256 checksum
How to use checksums
ec5cc124e4cb5e8ef3da5e7c35d5d09a4d8535362e1c64b91c70b580bb642aa2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.13

Release history Release notifications | RSS feed

This release

1.0.8 This release

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page