dblite  1.3.0
Simple query interface for SQL databases
dblite.util Namespace Reference

Classes

class  StaticTzInfo
 datetime.tzinfo class representing a constant offset from UTC. More...
 

Functions

def factory (ctor, data)
 Returns object constructed with data dictionary. More...
 
def is_dataobject (obj)
 Returns whether input is a data object: namedtuple, or has attributes or slots. More...
 
def is_namedtuple (obj)
 Returns whether input is a namedtuple class or instance. More...
 
def json_dumps (data, indent=2, sort_keys=True)
 Returns JSON string, with datetime types converted to ISO-8601 strings (in UTC if no timezone set), sets converted to lists, and Decimal objects converted to float or int. More...
 
def json_loads (s)
 Returns deserialized JSON, with datetime/date strings converted to objects. More...
 
def keyvalues (obj, namefmt=None)
 Returns a list of keys and values, or [given object] if not applicable. More...
 
def load_modules ()
 Returns db engines loaded from file directory, as {name: module}. More...
 
def nameify (val, namefmt=None, parent=None)
 Returns value as table or column name string. More...
 
def parse_datetime (s)
 Tries to parse string as ISO8601 datetime, returns input on error. More...
 

Variables

 UTC = StaticTzInfo("UTC", StaticTzInfo.ZERO)
 UTC timezone singleton. More...
 

Function Documentation

◆ factory()

def dblite.util.factory (   ctor,
  data 
)

Returns object constructed with data dictionary.

Parameters
ctorcallable like a class, declared args are matched case-insensitively for positional arguments if keyword argument invocation fails
datadata dictionary with string keys
Returns
(result, [error strings])

Definition at line 58 of file util.py.

◆ is_dataobject()

def dblite.util.is_dataobject (   obj)

Returns whether input is a data object: namedtuple, or has attributes or slots.

Definition at line 74 of file util.py.

◆ is_namedtuple()

def dblite.util.is_namedtuple (   obj)

Returns whether input is a namedtuple class or instance.

Definition at line 87 of file util.py.

◆ json_dumps()

def dblite.util.json_dumps (   data,
  indent = 2,
  sort_keys = True 
)

Returns JSON string, with datetime types converted to ISO-8601 strings (in UTC if no timezone set), sets converted to lists, and Decimal objects converted to float or int.

Returns None if data is None.

Definition at line 98 of file util.py.

◆ json_loads()

def dblite.util.json_loads (   s)

Returns deserialized JSON, with datetime/date strings converted to objects.

Returns original input if loading as JSON failed.

Definition at line 116 of file util.py.

◆ keyvalues()

def dblite.util.keyvalues (   obj,
  namefmt = None 
)

Returns a list of keys and values, or [given object] if not applicable.

Parameters
objmapping or namedtuple or list|set|tuple or object with attributes or slots
namefmtfunction(key) to apply on extracted keys, if any
Returns
[(key, value)] if available, else original argument as list if list/set/tuple, else list with a single item

Definition at line 148 of file util.py.

◆ load_modules()

def dblite.util.load_modules ( )

Returns db engines loaded from file directory, as {name: module}.

Definition at line 171 of file util.py.

◆ nameify()

def dblite.util.nameify (   val,
  namefmt = None,
  parent = None 
)

Returns value as table or column name string.

Parameters
vala primitive like string, or a named object like a class, or a class property or member or data descriptor
namefmtfunction(name) to apply on name extracted from class or object, if any
parentthe parent class object if value is a class member or property
Returns
string

Definition at line 194 of file util.py.

◆ parse_datetime()

def dblite.util.parse_datetime (   s)

Tries to parse string as ISO8601 datetime, returns input on error.

Supports "YYYY-MM-DD[ T]HH:MM:SS(.micros)?(Z|[+-]HH(:MM)?)?". All returned datetimes are timezone-aware, falling back to UTC.

Definition at line 211 of file util.py.

Variable Documentation

◆ UTC

dblite.util.UTC = StaticTzInfo("UTC", StaticTzInfo.ZERO)

UTC timezone singleton.

Definition at line 47 of file util.py.