toydb package

Submodules

toydb.Database module

class toydb.Database.Database(name: str = 'db.tdb', path: str = '.')

Bases: object

createTable(table_name: str, schema: Dict[str, toydb.dtypes.DType], if_not_exists: bool = False)

Create a new DB table.

Parameters
  • table_name – Name of new table

  • schema – A dictionary mapping from column names to their datatypes. Note: Text datatypes need their max lengths specified.

  • if_not_exists – If True and the table already exists, it won’t be overwritten, otherwise it will.

delete(table_name: str, where: Callable[[dict], bool])

Delete rows from a table in the database where callable where evaluates to true.

Similar to the SQL DELETE FROM command.

Parameters
  • table_name – Table in the database

  • where – Callable that gets a row from the table as an argument (as a dict, mapping from column name to value) and should return True if that row should be deleted and False otherwise.

dropTable(table_name: str)

Delete a table from the database.

Parameters

table_name – Table in database

getTableColumns(table_name: str) → List[str]

Get a list of column names in a table.

Parameters

table_name – Name of existing table in DB

getTableSchema(table_name: str) → Dict[str, toydb.dtypes.DType]

Get the schema for table table_name.

Parameters

table_name – Name of existing table in DB

Returns

dict mapping from str column name to dtype.DType

insert(table_name: str, row: Union[Sequence[Any], Dict[str, Any]])

Add a new row of data into a table.

Parameters
  • table_name – Table in the database

  • row – Row of data to add to table

insertMany(table_name: str, rows: Iterable[Union[Sequence[Any], Dict[str, Any]]])

Insert multiple rows of data into a table.

Parameters
  • table_name – Name of table in database

  • rows – Iterable of rows to add to table

listTables() → List[str]

Get a list of Database table names.

Returns

List of DB table names

classmethod new(name: str = 'db.tdb', path: str = '.')

Create a new database.

Parameters

filename – Path to the new databasae

Returns

New Database instance

printSchema(table_name: str)

Print a table’s schema of column names and dtypes.

Parameters

table_name – Existing table in the database.

query(from_: str, select: List[Union[str, Dict[str, Callable]]] = '*', where=None, limit: Optional[int] = None)

Query a database using SQL(-ish) syntax.

Parameters
  • select – Columns to select

  • from – DB table to select from

  • where – Conditionally filter results with a callable function

  • limit – Limit the number of results

remove()

Deletes a database folder and all subdirectories.

toydb.RowStruct module

class toydb.RowStruct.RowStruct(columns: List[str], types: List[toydb.dtypes.DType], endian: str = '>')

Bases: object

pack(row: Union[List[Any], Dict[str, Any]]) → bytes

Encodes data from row to a byte string that can be written to the table file, per the RowStruct’s format string.

Wrapper around python’s struct.pack, which handles reordering columns (if necessary – if row is a dict), as well as NA values.

Parameters

row – Row of data to be written to table.

Returns

Byte string encoding of row.

unpack(data: bytes) → List[Any]

Decodes a byte encoding of a row of data from the table file.

Wrapper around python’s struct.unpack, which handles NA values and strings (which are padded in the encoding process).

Parameters

data – byte encoding of row data

Returns

Row data in list form

toydb.dtypes module

class toydb.dtypes.DType(name: str, value: str, default: Optional[Any] = None, has_numeric_value: bool = False)

Bases: object

getLength()
subtype(n: int)

Creates a new DType with the same format character, but with a prefix number. Useful when creating

Parameters

n – Prefix number prepended to self.value. With strings, it represents the max length of the string.

validate(val) → bool

Validates the input’s type.

Parameters

val – Value being

Returns

Is val a valid instance of this dtype?

class toydb.dtypes.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: json.encoder.JSONEncoder

Subclass of json.JSONEncoder that is able to encode dtype.DType when calling json.dump().

default(obj: dict)

Handles decoding string data types as DType objects.

toydb.dtypes.get_type_from_string(fmt_str: str)toydb.dtypes.DType

Get dtype from struct format string.

Parameters

fmt_str – format character with possible preceding count.

Returns

Matching dtype for format string

toydb.dtypes.get_type_from_value(value: Union[int, float, bool, str])toydb.dtypes.DType

Guess the DType matching value.

Parameters

value – Value to find matching dtype

Returns

DType matching value

toydb.dtypes.validate(value: Union[int, float, bool, str], dtype: toydb.dtypes.DType) → bool

Checks that value’s type matches dtype.

Parameters
  • value – Value being checked

  • dtype – DType to check against

Returns

Is value of type DType?

toydb.exceptions module

exception toydb.exceptions.BaseError

Bases: Exception

exception toydb.exceptions.SchemaError

Bases: toydb.exceptions.BaseError

toydb.util module

toydb.util.iter_limit(itr: Iterable, limit: int) → Generator

Generator function that limits the results.

Parameters
  • itr – Iterable to limit

  • limit – Max number of values to yield from itr. Must be a positive integer.

Yields

Up to limit values from itr.

toydb.util.md5(text: str) → str

md5 hash function.

Used for creating filenames based on table_names.

Parameters

text – String to hash

Returns

Hex digest of text

Module contents

toydb.create_db(dbname='database.tdb', path='.')

Create a new database

toydb.delete_db(db: toydb.Database.Database)

Delete a database