Python API reference
Query delimited text files with SQL.
This package is the entry point for Python code. Everything a script needs is importable from here; modules whose names start with an underscore are internal and may change without notice.
import datapy
with datapy.open("people.csv") as people: people.query("select name from people.csv where age > 40")
The command line entry point is datapy.cli, which uses only the names
exported here.
datapy.open(path, *, header: bool = True, delimiter: str = ',', text = ()) -> DataFile
Open a file for querying. Use it in a with block.
Returns a DataFile; see that class for what header, delimiter and
text mean.
datapy.query(statement: str, *, header: bool = True, delimiter: str = ',', text = ()) -> Result
Run one select statement against the file named in its from clause.
A shortcut for opening the file, running a single query and closing it.
The whole result is held in memory; use datapy.stream() for large
results. header, delimiter and text mean the same as for
datapy.open().
datapy.stream(statement: str, *, header: bool = True, delimiter: str = ',', text = ()) -> Stream
Run one select statement and return a Stream of its rows.
The file named in the from clause is read as you iterate, so a statement
that only picks columns and filters rows (select, where, limit)
runs in a small, fixed amount of memory however large the file is, and
stops reading as soon as you stop iterating. A statement that needs every
row first (order by, group by, count(*), a subquery...) is loaded
into a temporary database on disk and then streamed from there.
header, delimiter and text mean the same as for datapy.open().
datapy.DataFile(path, *, header: bool = True, delimiter: str = ',', text = ())
A delimited text file opened for querying.
Usually created with datapy.open() in a with block. The file is read
once when opened, into a temporary database on disk rather than into
memory, so any number of queries can then be run against it, including
ones that sort, group or count. The file itself is never modified.
path: the file to open, or"-"to read standard input.header: when true (the default) the first line holds the column names. When false the first line is data and columns are only available by position, as$1,$2...delimiter: the single character that separates fields.text: columns to keep as text even if their values look like numbers, given by name or position:text=["zip", "$4"].
A column is treated as a number when every value in the first 1,000 rows
is one. Use text for a column that only looks numeric at the start.
Attributes:
path: the path the file was opened with.columns: the column names, in order.closed: true once the file has been closed.
DataFile.close()
Release the loaded data.
Called automatically at the end of a with block. Closing twice is
harmless; querying a closed file raises DatapyError.
DataFile.query(statement: str) -> Result
Run a select statement against this file and return a Result.
Columns can be referenced by header name or by position ($1 is the
first column). The from clause may name the file by the path it was
opened with, by its file name (people.csv) or by its name without
the extension (people).
The whole result is held in memory. For a result too large for that,
use stream().
DataFile.stream(statement: str) -> Stream
Run a select statement and return a Stream of its rows.
Takes the same statements as query(), but hands the rows over one
at a time instead of collecting them.
datapy.Result(columns: list[str], rows: list[sqlite3.Row])
The rows returned by a query, all held in memory.
Iterate over it to get the rows, or use len() for the row count. Each
row can be indexed by position (row[0]) or by column name
(row["name"]).
columns: the column names, in order.rows: the rows, as a list.
datapy.Stream
The rows of a query, produced one at a time as you iterate.
Unlike a Result it can be iterated only once and has no length, but it
never holds more than a batch of rows in memory. Use it in a with block,
or call close(), if you stop before the last row.
columns: the column names, in order.streamed: true when the file is being read as you iterate; false when the statement needed the whole file to be loaded first (sorting, grouping, counting and the like).
Stream.close()
Stop reading and release the file. Closing twice is harmless.
datapy.DatapyError
Raised for any problem with a statement or the file it queries.
Catch this one type to handle a missing or malformed file, an invalid
statement, or a statement that is not a select.