dbetto package¶
- class dbetto.AttrsDict(value=None)¶
Bases:
dictAccess dictionary items as attributes.
Examples
>>> d = AttrsDict({"key1": {"key2": 1}}) >>> d.key1.key2 1 >>> d1 = AttrsDict() >>> d1["a"] = 1 >>> d1.a 1
- group(label)¶
Group dictionary according to a label.
This is equivalent to
map()with unique set toFalse.- Parameters:
label (
str) – name (key) at which the new label can be found. If nested in dictionaries, use.to separate levels, e.g.level1.level2.label.- Return type:
Examples
>>> d = AttrsDict({ ... "a": { ... "type": "A", ... "data": 1 ... }, ... "b": { ... "type": "A", ... "data": 2 ... }, ... "c": { ... "type": "B", ... "data": 3 ... }, ... }) >>> d.group("type").keys() dict_keys(['A', 'B']) >>> d.group("type").A.values() dict_values([{'type': 'A', 'data': 1}, {'type': 'A', 'data': 2}]) >>> d.group("type").B.values() dict_values([{'type': 'B', 'data': 3}]) >>> d.group("type").A.map("data")[1] {'type': 'A', 'data': 1}
See also
- map(label, unique=True)¶
Remap dictionary according to an alternative unique label.
Loop over keys in the first level and search for key named label in their values. If label is found and its value newid is unique, create a mapping between newid and the first-level dictionary obj. If label is of the form
key.label,labelwill be searched in a dictionary keyed bykey. If the label is unique a dictionary of dictionaries will be returned, if not unique and unique is false, a dictionary will be returned where each entry is a dictionary of dictionaries keyed by an arbitrary integer.- Parameters:
- Return type:
Examples
>>> d = AttrsDict({ ... "a": { ... "id": 1, ... "group": { ... "id": 3, ... }, ... "data": "x" ... }, ... "b": { ... "id": 2, ... "group": { ... "id": 4, ... }, ... "data": "y" ... }, ... }) >>> d.map("id")[1].data == "x" True >>> d.map("group.id")[4].data == "y" True
Note
No copy is performed, the returned dictionary is made of references to the original objects.
Warning
The result is cached internally for fast access after the first call. If the dictionary is modified, the cache gets cleared.
- class dbetto.Props¶
Bases:
objectClass to handle overwriting of dictionaries in cascade order
- static add_to(props_a, props_b)¶
- static read_from(sources, subst_pathvar=False, trim_null=False)¶
- static subst_vars(props, var_values=None, ignore_missing=False)¶
- static trim_null(props_a)¶
- class dbetto.TextDB(path, lazy=False, hidden=False)¶
Bases:
objectA simple text file database.
The database is represented on disk by a collection of text files arbitrarily scattered in a filesystem. Subdirectories are also
TextDBobjects. In memory, the database is represented as anAttrsDict.Currently supported file formats are JSON and YAML.
Tip
For large databases, a basic “lazy” mode is available. In this case, no global scan of the filesystem is performed at initialization time. Once a file is queried, it is also cached in the internal store for faster access. Caution, this option is for advanced use (see warning message below).
Warning
A manual call to
scan()is needed before most class methods (e.g. iterating on the database files) can be properly used.Examples
>>> from dbetto import TextDB >>> jdb = TextDB("path/to/dir") >>> jdb["file1.json"] # is a dict >>> jdb["file1.yaml"] # is a dict >>> jdb["file1"] # also works >>> jdb["dir1"] # TextDB instance >>> jdb["dir1"]["file1"] # nested file >>> jdb["dir1/file1"] # also works >>> jdb.dir1.file # keys can be accessed as attributes
- group(label)¶
Group dictionary according to a second unique label.
See also
Warning
If the database is lazy, you must call
scan()in advance to populate it, otherwise groupings cannot be created.
- map(label, unique=True)¶
Remap dictionary according to a second unique label.
See also
Warning
If the database is lazy, you must call
scan()in advance to populate it, otherwise mappings cannot be created.
- on(timestamp, pattern=None, system='all')¶
Query database in time[, file pattern, system].
A (only one) valid validity file (YAML, JSON, JSONL and other file types supported) must exist in the directory to specify a validity mapping. This functionality relies on the
catalog.Catalogclass.The YAML specification is documented at this link.
The special
$_string is expanded to the directory containing the text files.
- dbetto.str_to_datetime(value)¶
Convert a string in the format %Y%m%dT%H%M%SZ to
datetime.datetime.
Submodules¶
dbetto.attrsdict module¶
- class dbetto.attrsdict.AttrsDict(value=None)¶
Bases:
dictAccess dictionary items as attributes.
Examples
>>> d = AttrsDict({"key1": {"key2": 1}}) >>> d.key1.key2 1 >>> d1 = AttrsDict() >>> d1["a"] = 1 >>> d1.a 1
- group(label)¶
Group dictionary according to a label.
This is equivalent to
map()with unique set toFalse.- Parameters:
label (
str) – name (key) at which the new label can be found. If nested in dictionaries, use.to separate levels, e.g.level1.level2.label.- Return type:
Examples
>>> d = AttrsDict({ ... "a": { ... "type": "A", ... "data": 1 ... }, ... "b": { ... "type": "A", ... "data": 2 ... }, ... "c": { ... "type": "B", ... "data": 3 ... }, ... }) >>> d.group("type").keys() dict_keys(['A', 'B']) >>> d.group("type").A.values() dict_values([{'type': 'A', 'data': 1}, {'type': 'A', 'data': 2}]) >>> d.group("type").B.values() dict_values([{'type': 'B', 'data': 3}]) >>> d.group("type").A.map("data")[1] {'type': 'A', 'data': 1}
See also
- map(label, unique=True)¶
Remap dictionary according to an alternative unique label.
Loop over keys in the first level and search for key named label in their values. If label is found and its value newid is unique, create a mapping between newid and the first-level dictionary obj. If label is of the form
key.label,labelwill be searched in a dictionary keyed bykey. If the label is unique a dictionary of dictionaries will be returned, if not unique and unique is false, a dictionary will be returned where each entry is a dictionary of dictionaries keyed by an arbitrary integer.- Parameters:
- Return type:
Examples
>>> d = AttrsDict({ ... "a": { ... "id": 1, ... "group": { ... "id": 3, ... }, ... "data": "x" ... }, ... "b": { ... "id": 2, ... "group": { ... "id": 4, ... }, ... "data": "y" ... }, ... }) >>> d.map("id")[1].data == "x" True >>> d.map("group.id")[4].data == "y" True
Note
No copy is performed, the returned dictionary is made of references to the original objects.
Warning
The result is cached internally for fast access after the first call. If the dictionary is modified, the cache gets cleared.
dbetto.catalog module¶
- class dbetto.catalog.Catalog(entries)¶
Bases:
CatalogImplementation of the YAML metadata validity specification.
The legacy JSONL specification is also supported.
- static build_catalog(propstream, mode_default='append', suppress_duplicate_check=False)¶
Build a Catalog object from a validity file/stream
- static get(value)¶
- static get_files(catalog_file, timestamp, category='all')¶
Helper function to get the files for a given timestamp and category
- static read_from(file_name)¶
Read from a validity file and build a Catalog object
- valid_for(timestamp, system='all', allow_none=False)¶
Get the valid entries for a given timestamp and system
- class dbetto.catalog.Props¶
Bases:
objectClass to handle overwriting of dictionaries in cascade order
- static add_to(props_a, props_b)¶
- static read_from(sources, subst_pathvar=False, trim_null=False)¶
- static subst_vars(props, var_values=None, ignore_missing=False)¶
- static trim_null(props_a)¶
dbetto.textdb module¶
- class dbetto.textdb.TextDB(path, lazy=False, hidden=False)¶
Bases:
objectA simple text file database.
The database is represented on disk by a collection of text files arbitrarily scattered in a filesystem. Subdirectories are also
TextDBobjects. In memory, the database is represented as anAttrsDict.Currently supported file formats are JSON and YAML.
Tip
For large databases, a basic “lazy” mode is available. In this case, no global scan of the filesystem is performed at initialization time. Once a file is queried, it is also cached in the internal store for faster access. Caution, this option is for advanced use (see warning message below).
Warning
A manual call to
scan()is needed before most class methods (e.g. iterating on the database files) can be properly used.Examples
>>> from dbetto import TextDB >>> jdb = TextDB("path/to/dir") >>> jdb["file1.json"] # is a dict >>> jdb["file1.yaml"] # is a dict >>> jdb["file1"] # also works >>> jdb["dir1"] # TextDB instance >>> jdb["dir1"]["file1"] # nested file >>> jdb["dir1/file1"] # also works >>> jdb.dir1.file # keys can be accessed as attributes
- group(label)¶
Group dictionary according to a second unique label.
See also
Warning
If the database is lazy, you must call
scan()in advance to populate it, otherwise groupings cannot be created.
- map(label, unique=True)¶
Remap dictionary according to a second unique label.
See also
Warning
If the database is lazy, you must call
scan()in advance to populate it, otherwise mappings cannot be created.
- on(timestamp, pattern=None, system='all')¶
Query database in time[, file pattern, system].
A (only one) valid validity file (YAML, JSON, JSONL and other file types supported) must exist in the directory to specify a validity mapping. This functionality relies on the
catalog.Catalogclass.The YAML specification is documented at this link.
The special
$_string is expanded to the directory containing the text files.
dbetto.time module¶
- dbetto.time.datetime_to_str(value)¶
Convert a
datetime.datetimeobject to a string in the format %Y%m%dT%H%M%SZ.
- dbetto.time.str_to_datetime(value)¶
Convert a string in the format %Y%m%dT%H%M%SZ to
datetime.datetime.
- dbetto.time.unix_time(value)¶
Convert a string in the format %Y%m%dT%H%M%SZ or datetime object to Unix time value
dbetto.utils module¶
- dbetto.utils.float_representer(dumper, value)¶
- dbetto.utils.load_dict(fname, ftype=None)¶
Load a text file as a Python dict.