~/datapy v0.2.0 on pypi

corey@mccarty.dev:~/datapy$

Datapy

An intuitive lightweight file based database and query tool.

Documentation · PyPI · Source

Installation

Datapy needs Python 3.10 or newer.

pip install datapy

Usage

Given people.csv:

name,age,job
Ann,49,engineer
Bob,7,student

Select every column with -sa and the file name:

$ datapy -sa people.csv
name,age,job
Ann,49,engineer
Bob,7,student

Narrow it down and sort it with -w (where) and -o (order):

$ datapy -sa people.csv -w 'age > 40' -o name
name,age,job
Ann,49,engineer

Choose columns with -s, by header name or by position ($1 is the first column):

$ datapy -s name,job people.csv -w 'age > 40'
name,job
Ann,engineer

$ datapy -s '$1' people.csv -w '$2 < 18'
name
Bob

Building a query from flags

datapy -sa FILE [-w CONDITION] [-g COLUMNS] [-o COLUMNS] [-l COUNT]
datapy -s COLUMNS FILE [-w CONDITION] [-g COLUMNS] [-o COLUMNS] [-l COUNT]
Flag Meaning Example
-s, --select choose columns -s name,job
-a, --all with -s, all columns -sa
-w, --where only rows matching a condition -w 'age > 40'
-o, --order sort -o name, -o 'age desc'
-g, --group one row per distinct value -s 'job, count(*)' -g job
-l, --limit at most this many rows -l 10

Each flag is followed by its value, and the flags can come in any order. Repeat -w to require several conditions.

Put each value in single quotes. Without them the shell treats > and < as redirects and expands * and $1 before datapy sees them. Use double quotes when the value contains text in single quotes:

datapy -sa people.csv -w "name = 'Ann'"

Pipes and large files

Leave the file out to read standard input. Results are written as csv, so datapy fits anywhere in a pipeline:

cat people.csv | datapy -sa -w 'age > 40' | head

Rows are read and written as they go. Picking columns and filtering rows takes a small, fixed amount of memory on a file of any size; sorting, grouping and counting use temporary disk space instead of memory. See large files, pipes and memory for details and measurements.

If you know SQL

-q takes everything from where to the end as one string, in place of -w, -g, -o and -l. Or pass a whole statement as a single argument:

datapy -sa people.csv -q 'where age > 40 order by name limit 5'
datapy 'select name, job from people.csv where age > 40'

datapy --help lists every flag, as does the command line reference.

From Python

Open a file in a with block and query it as often as you like. The file is read once, when it is opened:

import datapy

with datapy.open("people.csv") as people:
    print(people.columns)                      # ['name', 'age', 'job']

    for row in people.query("select name, age from people.csv where age > 40"):
        print(row["name"], row[1])             # by column name or position

    minors = people.query("select $1 from people where $2 < 18")
    print(len(minors))

Statements are the same ones the command line accepts. Inside the block the from clause can name the file by its path, its file name (people.csv) or just its name without the extension (people).

For a result too large to hold in memory, datapy.stream() hands the rows over one at a time:

with datapy.stream("select name from people.csv where age > 40") as rows:
    for row in rows:
        print(row["name"])

Problems with a file or a statement raise datapy.DatapyError. The Python API reference has the details.

What a query can do

One select statement against one file. The statement is run by SQLite, so where, order by, group by, limit and SQLite's built-in functions all work. A column is treated as numeric when every value in it is a number, so age > 40 compares numbers rather than text. That is decided from the first 1,000 rows; -t zip keeps a column as text.

See what a query can do for the full rules and limits.

Development

See CONTRIBUTING.md for setup, the code layout and the checks that run before a release. Changes are listed in CHANGELOG.md.

Want to contribute? Great!

This project was originated as a way to bring a community together for Hacktoberfest. I'm glad to have community involvement and help others to understand databases on a more basic level.

License

Apache 2.0