From 6cd0607d4475c03e1647dee6a3ac153c6fabd523 Mon Sep 17 00:00:00 2001 From: Mohamed ElKalioby Date: Thu, 30 Jan 2025 17:12:07 +0300 Subject: [PATCH] v0.90.0 --- .github/workflows/workflow.yml | 3 +- README.md | 1 + USAGE.md | 81 ++++++++++++++++++++++++++++++++++ leopards/Query.py | 10 ++--- setup.py | 7 ++- tox.ini | 19 ++++++++ 6 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 USAGE.md create mode 100644 tox.ini diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index e3e4e69..98f34c0 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -22,6 +22,7 @@ jobs: - "3.10" - "3.11" - "3.12" + - "3.13" steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - name: Checkout code @@ -35,4 +36,4 @@ jobs: pip install pytest pytest-cov - name: Run tests run: | - pytest --cov=leopards tests/ + pytest --cov=leopards tests/ --cov-report term-missing diff --git a/README.md b/README.md index fea3167..9ca350b 100644 --- a/README.md +++ b/README.md @@ -207,4 +207,5 @@ Thanks for [Asma Tahir](https://github.com/tahirasma) for Pandas stats. ## Tutorials +* [Usage with different file types](https://dev.to/mkalioby/leopards-with-different-file-types-1d3) * [Work on CSV Files with Leopards](https://dev.to/mkalioby/working-with-csv-by-leopards-5bmd) diff --git a/USAGE.md b/USAGE.md new file mode 100644 index 0000000..de6d82e --- /dev/null +++ b/USAGE.md @@ -0,0 +1,81 @@ +# Usage + +This document covers how to use leopards with different file types + +## CSV + +`DictReader` from `csv` module can be used to read csv files as dictionaries as shown below. + +```python +import csv +from leopards import Q + +data = csv.DictReader(open("data.csv")) +res = Q(data, {"age__gt": 15}) +``` + +## TSV +`DictReader` from `csv` module can be used to read tsv files as dictionaries as shown below. + +```python +import csv +from leopards import Q + +data = csv.DictReader(open("data.csv"), delimiter="\t") +res = Q(data, {"age__gt": 15}) +``` + +## JSON +`json.load` can be used to read json files as dictionaries as shown below. + +```python + +import json +from leopards import Q + +data = json.load(open("data.json")) +res = Q(data, {"age__gt": 15}) +``` + +## XLS + +`xlrd` library can be used to read xls files as dictionaries as shown below. + +```python +import xlrd +from leopards import Q + +wb = xlrd.open_workbook("data.xls") +sh = wb.sheets()[0] +keys = sh.row_values(0) +data =[] +for n in range(1, sh.nrows): + data.append({key: sh.row_values(n)[n2] for n2, key in enumerate(keys)}) +res = Q(data, {"age__gt": 15}) +``` + +## ClickHouse +'clickhouse_driver' library can be used to read data from ClickHouse as dictionaries as shown below. +```python +import clickhouse_connect +client = clickhouse_connect.get_client( host='localhost', username='default',password='' ) +rows = client.execute("SELECT * FROM TABLE") +data = rows.named_results() +res = Q(data, {"age__gt": 15}) +``` + +## MySQL + +`mysql-client` library can be used to read data from MySQL as dictionaries as shown below. + +```python +import MySQLdb +from MySQLdb.cursors import DictCursor +from leopards import Q + +db=MySQLdb.connect(user='root',password='PASS', database="db", cursorclass=DictCursor) +cursor = db.cursor() +cursor.execute("SELECT * FROM TABLE") +data = cursor.fetchall() +res = Q(data, {"age__gt": 15}) +``` \ No newline at end of file diff --git a/leopards/Query.py b/leopards/Query.py index 82feb79..9356c22 100644 --- a/leopards/Query.py +++ b/leopards/Query.py @@ -53,7 +53,7 @@ def Count(iterable:list, cols:list=None, col_name:str='count'): for item in iterable: if type(item) is not dict: - item=item.__dict__ + item=item.__dict__ # pragma: no cover if cols: d={k:v for k,v in item.items() if k in cols} k = ":".join(d.values()) @@ -80,7 +80,7 @@ def Max(iterable:list, col_name:str, cols:list=None, dtype=str): for item in iterable: if type(item) is not dict: - item=item.__dict__ + item=item.__dict__ # pragma: no cover if cols: d={k:v for k,v in item.items() if k in cols} k = ":".join(d.values()) @@ -112,7 +112,7 @@ def Min(iterable:list, col_name:str, cols:list=None, dtype=str): for item in iterable: if type(item) is not dict: - item=item.__dict__ + item=item.__dict__ # pragma: no cover if cols: d={k:v for k,v in item.items() if k in cols} k = ":".join(d.values()) @@ -141,7 +141,7 @@ def Sum(iterable:list, col_name:str, cols:list=None): for item in iterable: if type(item) is not dict: - item=item.__dict__ + item=item.__dict__ # pragma: no cover if cols: d={k:v for k,v in item.items() if k in cols} k = ":".join(d.values()) @@ -167,7 +167,7 @@ def Avg(iterable:list, col_name:str, cols:list=None): for item in iterable: if type(item) is not dict: - item=item.__dict__ + item=item.__dict__ # pragma: no cover if cols: d={k:v for k,v in item.items() if k in cols} k = ":".join(d.values()) diff --git a/setup.py b/setup.py index 6323e4d..194aaf1 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='leopards', - version='0.20.1', + version='0.9.0', description='Allows filtering & aggregation iterable of dictionary by another dictionary. Much faster than pandas', long_description=open("README.md").read(), long_description_content_type="text/markdown", @@ -21,7 +21,8 @@ include_package_data=True, zip_safe=False, # because we're including static files classifiers=[ - "Development Status :: 4 - Beta", + #"Development Status :: 4 - Beta", + "Development Status :: 5 - Production", "Intended Audience :: Developers", "Operating System :: OS Independent", "Programming Language :: Python", @@ -31,6 +32,8 @@ "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", "Topic :: Software Development :: Libraries :: Python Modules", ] ) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..611f835 --- /dev/null +++ b/tox.ini @@ -0,0 +1,19 @@ +[tox] +envlist= + py37, + py38, + py39 + py310 + py311 + py312 + py313 + + +[testenv] +deps = + +allowlist_externals = + pytest + +commands = + pytest --cov=leopards tests/ --cov-report term-missing