Skip to content

Commit

Permalink
v0.90.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalioby committed Jan 30, 2025
1 parent add8866 commit 6cd0607
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
81 changes: 81 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -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})
```
10 changes: 5 additions & 5 deletions leopards/Query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
]
)
19 changes: 19 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6cd0607

Please sign in to comment.