Metadata-Version: 2.4
Name: datapy
Version: 0.2.0
Summary: An intuitive lightweight file based database and query tool.
Author-email: Corey McCarty <corey@mccarty.dev>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/xanderyzwich/datapy
Project-URL: Documentation, https://coreydmccarty.dev/datapy/
Project-URL: Changelog, https://coreydmccarty.dev/datapy/changelog/
Project-URL: Issues, https://github.com/xanderyzwich/datapy/issues
Keywords: file,database,query,csv,sql
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Database
Classifier: Environment :: Console
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: black<27,>=26; extra == "dev"
Dynamic: license-file

# Datapy

An intuitive lightweight file based database and query tool.

  - Query csv files with SQL, without setting up a database
  - An approachable alternative to `grep` and `awk`: columns by name, flags
    you can guess
  - Works in a pipeline and on files larger than memory
  - No dependencies outside the Python standard library
  - Read-only: your data files are never modified

## Installation

Datapy needs Python 3.10 or newer.

```sh
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:

```sh
$ 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):

```sh
$ 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):

```sh
$ 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:

```sh
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:

```sh
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](https://coreydmccarty.dev/datapy/performance/) 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:

```sh
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](https://coreydmccarty.dev/datapy/cli/).

### 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:

```python
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:

```python
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](https://coreydmccarty.dev/datapy/python-api/) 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](https://coreydmccarty.dev/datapy/sql-support/) for the full rules and limits.

## Development

See [CONTRIBUTING.md](https://coreydmccarty.dev/datapy/contributing/) for setup, the code layout and the
checks that run before a release. Changes are listed in
[CHANGELOG.md](https://coreydmccarty.dev/datapy/changelog/).

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
