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]
- property data: dict
- 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]]
- 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
- 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]]
- 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.
- property version: str
Matcher
- class json_as_db.core.matcher.Condition(key: str, value: Any, operator: Operator)[source]
Bases:
Comparator
- 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]
- 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.