Utilities

dbtools.util.dict_to_dtypes(data, order=None)[source]

Parses data types from a dictionary or list of dictionaries.

The dictionary keys will be paired with the types of the corresponding values, e.g.:

(key, type(data[key]))

If there are multiple dictionaries that have the same keys, the value types should be the same across dictionaries (with the exception of NoneType).

For example:

dict_to_dtypes(data=[
    {'name': 'apple', 'fruit': True, 'tree': True},
    {'name': 'tomato', 'fruit': True, 'tree': False},
    {'name': 'cucumber', 'fruit': False, 'tree': False}
])

will return:

[('fruit', bool), ('name', str), ('tree', bool)]
Parameters :

data : dictionary or list of dictionaries

Data to extract names and dtypes from.

order : list of strings (optional)

The order in which to return the dtypes in, by key. If None, the dtypes will be sorted alphabetically by key.

Returns :

dtypes : list of 2-tuples

Each tuple in the list has the form (key, dtype)

dbtools.util.sql_execute(db, cmd, fetchall=False, verbose=False)[source]

Execute a SQL command cmd in database db.

Parameters :

db : string

Path to the SQLite database.

cmd : string or list

Command to be executed. Specifically, these are parameters to be passed to sqlite3.Cursor.execute. See:

http://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor.execute

fetchall : bool (optional)

Fetch the result of the command, and return it.

verbose : bool (optional)

Print the command that is run.

Returns :

result : list or None

The result of the executed command, if fetchall is True.

This Page