Metadata-Version: 2.1
Name: condition
Version: 1.0.1
Summary: A pythonic way to construct conditions for pandas dataframe query and sql
Home-page: https://gitlab.com/wyzhao/condition
Author: Weiyang Zhao
Author-email: wyzhao@gmail.com
License: UNKNOWN
Project-URL: Docs, https://condition.readthedocs.io/en/latest/
Project-URL: Source, https://gitlab.com/wyzhao/condition
Project-URL: Bug Reports/Issues, https://gitlab.com/wyzhao/condition/issues
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Utilities
Description-Content-Type: text/markdown
Requires-Dist: pandas
Provides-Extra: all_extras
Requires-Dist: jinja2 ; extra == 'all_extras'
Requires-Dist: pandas ; extra == 'all_extras'
Requires-Dist: coverage ; extra == 'all_extras'
Requires-Dist: pyarrow ; extra == 'all_extras'
Requires-Dist: pytest ; extra == 'all_extras'
Requires-Dist: pytest-cov ; extra == 'all_extras'
Requires-Dist: prospector ; extra == 'all_extras'
Requires-Dist: mypy ; extra == 'all_extras'
Requires-Dist: sphinx ; extra == 'all_extras'
Requires-Dist: ipython ; extra == 'all_extras'
Requires-Dist: rst2pdf ; extra == 'all_extras'
Requires-Dist: sphinx-rtd-theme ; extra == 'all_extras'
Requires-Dist: graphviz ; extra == 'all_extras'
Requires-Dist: sphinx-copybutton ; extra == 'all_extras'
Provides-Extra: dev
Requires-Dist: pandas ; extra == 'dev'
Requires-Dist: coverage ; extra == 'dev'
Requires-Dist: jinja2 ; extra == 'dev'
Requires-Dist: pyarrow ; extra == 'dev'
Requires-Dist: pytest ; extra == 'dev'
Requires-Dist: pytest-cov ; extra == 'dev'
Requires-Dist: prospector ; extra == 'dev'
Requires-Dist: mypy ; extra == 'dev'
Requires-Dist: sphinx ; extra == 'dev'
Requires-Dist: ipython ; extra == 'dev'
Requires-Dist: rst2pdf ; extra == 'dev'
Requires-Dist: sphinx-rtd-theme ; extra == 'dev'
Requires-Dist: graphviz ; extra == 'dev'
Requires-Dist: sphinx-copybutton ; extra == 'dev'
Provides-Extra: sql
Requires-Dist: jinja2 ; extra == 'sql'

# Condition
[![PyPI version](https://badge.fury.io/py/condition.svg)](https://badge.fury.io/py/condition)
[![PyPI](https://img.shields.io/pypi/pyversions/condition.svg)](https://pypi.org/project/condition/)
[![Docs](https://readthedocs.org/projects/condition/badge/?version=latest)](https://condition.readthedocs.io/en/latest/?badge=latest)
[![pipeline status](https://gitlab.com/wyzhao/condition/badges/master/pipeline.svg)](https://gitlab.com/wyzhao/condition/commits/master)
[![coverage report](https://gitlab.com/wyzhao/condition/badges/master/coverage.svg)](https://gitlab.com/wyzhao/condition/commits/master)

## Project Description

This project provides a pythonic way to construct a condition object. The condition
object can later used to query pandas Dataframe, filter pyarrow partitions or 
to generate where conditions in SQL. 
It takes care of formating and syntax for you.

#### A Quick Example


```python 

>>> 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())
>>> 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

```python 

>>> 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](https://condition.readthedocs.io/en/latest/usage.html) 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]
```



