Modules

Database

class json_as_db.core.database.Database(*arg, **kwargs)[source]

Bases: dict

add(item: Union[Any, List[Any]]) Union[str, List[str]][source]
Parameters

item (Union[Any, List[Any]]) – Object(s) to add to database

Returns

Automatically generated ID of added item

Return type

Union[str, List[str]]

all() List[Any][source]

Provide all items in database.

Returns

All items as list

Return type

List[Any]

clear() None[source]

Clear all items. This method updates timestamp in metadata.

commit() None[source]

Save its states and all items at that time.

count() int[source]
Returns

indicates the count of all data

Return type

int

property data: dict
drop() int[source]
Returns

indicates the count of dropped items

Return type

int

find(func: Callable[[...], bool]) List[str][source]

Returns array of IDs that satisfies the provided testing function.

Parameters

func (Callable[..., bool]) – A function to execute for each items in database. It will call func(value) to determine boolean.

Returns

array with id of found items

Return type

List[str]

get(key: Union[str, List[str]], default=None) Union[Any, List[Any]][source]

Get objects by given IDs when list is given. When single string is given, returns single object by given key

Parameters
  • key (str | List[str]) – single key or list-like

  • default (Any, optional) – default value if not exists. Defaults to None.

Returns

single object or list-like

Return type

Any | List[Any]

Examples

>>> db.get('kcbPuqpfV3YSHT8YbECjvh')
{...}
>>> db.get(['kcbPuqpfV3YSHT8YbECjvh'])
[{...}]
>>> db.get(['kcbPuqpfV3YSHT8YbECjvh', 'jmJKBJBAmGESC3rGbSb62T'])
[{...}, {...}]
has(key: Union[str, List[str]]) Union[bool, List[bool]][source]

performs to determine whether has key

Parameters

key (Union[str, List[str]]) – to find with string(s) as key

Returns

boolean or array of boolean.

Return type

Union[bool, List[bool]]

items() a set-like object providing a view on D's items[source]
keys() a set-like object providing a view on D's keys[source]
load(path: str, file_args: dict = {'encoding': 'utf-8', 'mode': 'r'}, json_args: dict = {}) Database[source]

Load database object from a file

Parameters
  • path (str) – a string containing a file name to load.

  • file_args (dict, optional) – keyword arguments for file open(**kwagrs). Defaults to dict( mode=”r”, encoding=”utf-8”, ).

  • json_args (dict, optional) – keyword arguments for json.loads(**kwargs). Defaults to dict().

Raises

AttributeError – when JSON file does not contain keys and values to read into valid Database object.

Returns

itself

Return type

Database

property metadata: dict
modify(id: Union[str, List[str]], value: Union[Any, List[Any]]) Union[Any, List[Any]][source]
Parameters
  • id (str | List[str]) – id(s) to modify

  • value (Any | List[Any]) – value(s) to modify

Raises

ValueError – type or length is not matched

Returns

Modified value(s)

Return type

Any | List[Any]

remove(key: Union[str, List[str]]) Union[Any, List[Any]][source]
Parameters

key (Union[str, List[str]]) – ID(s) to remove from database

Returns

removed items

Return type

Union[Any, List[Any]]

rollback() None[source]

Restore all states and items from latest commit.

save(path: str, file_args: dict = {'encoding': 'utf-8', 'mode': 'w'}, json_args: dict = {'sort_keys': True}, make_dirs: bool = False) None[source]

Save database object into a file as JSON format

Parameters
  • path (str) – a string containing a file name to save.

  • file_args (dict, optional) – keyword arguments for file open(**kwagrs). Defaults to dict( mode=”w+”, encoding=”utf-8”, ).

  • json_args (dict, optional) – keyword arguments for json.dumps(**kwargs). Defaults to dict( sort_keys=True, ).

  • make_dirs (bool, optional) – create non-exists directories in given path. Defaults to False.

update(mapping: Union[dict, tuple] = (), **kwargs) None[source]

Note that this method overrides database itself.

values() an object providing a view on D's values[source]
property version: str

Matcher

class json_as_db.core.matcher.Comparator[source]

Bases: object

evaluate(other: dict) bool[source]
class json_as_db.core.matcher.Condition(key: str, value: Any, operator: Operator)[source]

Bases: Comparator

copy() Condition[source]
evaluate(item: dict) bool[source]
key: str
operator: Operator
value: Any
class json_as_db.core.matcher.Conditions(lvalue: Union[Condition, Conditions], rvalue: Union[Condition, Conditions], operator: Operator)[source]

Bases: Comparator

copy() Conditions[source]
evaluate(other: dict) bool[source]
invert() Conditions[source]

According boolean algebra that satisfies De Morgan’s laws In case of (a & b).invert(), it returns having the meaning of (not a) or (not b) that is equivalent to (a and b) in logical.

class json_as_db.core.matcher.Key[source]

Bases: str