serialix#
Subpackages#
- serialix.formats package
- Module contents
- Submodules
- serialix.formats.json module
JSON_Format
JSON_Format.clear()
JSON_Format.commit()
JSON_Format.copy()
JSON_Format.create_file()
JSON_Format.delete_file()
JSON_Format.dictionary
JSON_Format.dictionary_default
JSON_Format.file_exists()
JSON_Format.get()
JSON_Format.is_file_exist()
JSON_Format.items()
JSON_Format.keys()
JSON_Format.parser_read_kwargs
JSON_Format.parser_write_kwargs
JSON_Format.pop()
JSON_Format.popitem()
JSON_Format.read_file_as_dict()
JSON_Format.refresh()
JSON_Format.reload()
JSON_Format.reset_to_defaults()
JSON_Format.setdefault()
JSON_Format.update()
JSON_Format.values()
JSON_Format.write_dict_to_file()
- serialix.formats.yaml module
YAML_Format
YAML_Format.clear()
YAML_Format.commit()
YAML_Format.copy()
YAML_Format.create_file()
YAML_Format.delete_file()
YAML_Format.dictionary
YAML_Format.dictionary_default
YAML_Format.file_exists()
YAML_Format.get()
YAML_Format.is_file_exist()
YAML_Format.items()
YAML_Format.keys()
YAML_Format.parser_read_kwargs
YAML_Format.parser_write_kwargs
YAML_Format.pop()
YAML_Format.popitem()
YAML_Format.read_file_as_dict()
YAML_Format.refresh()
YAML_Format.reload()
YAML_Format.reset_to_defaults()
YAML_Format.setdefault()
YAML_Format.update()
YAML_Format.values()
YAML_Format.write_dict_to_file()
- serialix.formats.toml module
TOML_Format
TOML_Format.clear()
TOML_Format.commit()
TOML_Format.copy()
TOML_Format.create_file()
TOML_Format.delete_file()
TOML_Format.dictionary
TOML_Format.dictionary_default
TOML_Format.file_exists()
TOML_Format.get()
TOML_Format.is_file_exist()
TOML_Format.items()
TOML_Format.keys()
TOML_Format.parser_read_kwargs
TOML_Format.parser_write_kwargs
TOML_Format.pop()
TOML_Format.popitem()
TOML_Format.read_file_as_dict()
TOML_Format.refresh()
TOML_Format.reload()
TOML_Format.reset_to_defaults()
TOML_Format.setdefault()
TOML_Format.update()
TOML_Format.values()
TOML_Format.write_dict_to_file()
- serialix.langs package
Submodules#
serialix.serialix module#
- class serialix.serialix.Serialix(file_format: str | Type[BaseLang] | object, file_path: str, default_dictionary: dict = {}, auto_file_creation: bool = True, force_overwrite_file: bool = False, parser_write_kwargs: dict = {}, parser_read_kwargs: dict = {}, ignore_inheritance_check=False)#
Bases:
object
serialix
unified instance generator for any officially supported language.This class should be used for creation of the basic
serialix
object for one of the officially supported languages. Currently supported languages:json
,yaml
,toml
- Parameters:
file_format – Format of data serialization language to be used. Can be class of language parser with
serialix
implementation via.core.BaseLang
inheritance or string -json
,yaml
(oryml
),toml
(ortml
)file_path – Path to preferred local file destination. If the file does not exist at the specified path, it will be created
default_dictionary – Default local file path
str
ordict
that will be used for local file start values, defaults to{}
(empty dict)auto_file_creation – Automatic local file creation on object initialization, defaults to True
force_overwrite_file – Whether the file needs to be overwritten if it already exists, defaults to False
parser_write_kwargs – Pass custom arguments to parser’s write to local file action, defaults to
{}
(empty dict)parser_read_kwargs – Pass custom arguments to parser’s read from local file action, defaults to
{}
(empty dict)ignore_inheritance_check – Disable the “inherited from
BaseLang
” check for passed tofile_format
argument class
- Returns:
Instance of
.formats.*
, depending on the file format provided- Raises:
ValueError – If provided data type in argument
default_dictionary
can’t be represented as pathstr
ordict
ValueError – If provided data in argument
file_format
is not one of the supported languages
New in version 2.1.0.
serialix.core module#
- class serialix.core.BaseLang(file_path: str, default_dictionary: dict = {}, auto_file_creation: bool = True, force_overwrite_file: bool = False, parser_write_kwargs: dict = {}, parser_read_kwargs: dict = {})#
Bases:
object
Base data serialization API implementation.
This class is not intended to be used directly. Instead, it should be inherited by a class, that provides the support for serialization language. Inherited class must overwrite the
_core__read_file_to_dict()
and_core__write_dict_to_file()
static methods with real implementation of their functionality that described in the docstring of each method.- Parameters:
file_path – Path to preferred local file destination. If the file does not exist at the specified path, it will be created
default_dictionary – Default local file path
str
ordict
that will be used for local file start values, defaults to{}
(empty dict)auto_file_creation – Automatic local file creation on object initialization, defaults to True
force_overwrite_file – Whether the file needs to be overwritten if it already exists, defaults to False
parser_write_kwargs – Pass custom arguments to parser’s write to local file action, defaults to
{}
(empty dict)parser_read_kwargs – Pass custom arguments to parser’s read from local file action, defaults to
{}
(empty dict)
- Raises:
ValueError – If provided data type in argument
default_dictionary
can’t be represented as pathstr
ordict
Note
Methods
.clear()
,.fromkeys()
,.get()
,.items()
,.keys()
,values()
,pop()
,popitem()
,setdefault()
,update()
are bound to the attributedictionary
, so executing:>>> this_object.update({'check': True})
Is equal to:
>>> this_object.dictionary.update({'check': True})
- __parsed_dict = {}#
- __parser_read_kwargs = {}#
- __parser_write_kwargs = {}#
- _core__read_file_to_dict(file_path: str) dict #
Template for reading local file as dictionary
- Parameters:
file_path – Path to local file
- Returns:
Parsed local file dictionary
- Raises:
NotImplementedError – If method was not implemented directly in inherited class
- _core__write_dict_to_file(file_path: str, dictionary: dict)#
Template for dumping dictionary to local file
- Parameters:
file_path – Path to local file
dictionary – Dictionary which will be written in
file_path
- Raises:
NotImplementedError – If method was not implemented directly in inherited class
- clear()#
Remove all keys from this object’s
dictionary
. No changes will be commited to local file without manual.commit()
call
- commit()#
Commit all changes from object to local file
- copy(deep_mode=True) dict #
Get the copy of this object keys and values. This method uses the recursive copy function by default that will remove all references to original dictionary.
- Parameters:
deep_mode – Use the recursive copy function to remove all references to original dictionary. Disabling this will lead to saving the references of the nested dictionaries to the copy object, defaults to True.
- Returns:
dictionary
copy
- create_file() bool #
Create new local file from default dictionary
- Returns:
Was the file created successfully
- delete_file() bool #
Delete local file
- Returns:
Was the file removed. False will be returned only if the local file does not exist at the time of deletion.
- property dictionary: dict#
Full access to the object’s dictionary attribute. Contains local file data parsed to dictionary
- Returns:
dictionary
attribute
- property dictionary_default: dict#
Full access to the object’s default dictionary. Contains dictionary with default keys that was specified in
default_dictionary
argument.- Returns:
default dictionary
- file_exists() bool #
Check local file existence
- Returns:
Does the file exist
- get(key, default=None) Any #
Get key from object’s
dictionary
- Parameters:
key – Key name
default – Default value, if key was not found, defaults to None
- Returns:
Value of requested
key
, ordefault
value if key wasn’t found.
- is_file_exist() bool #
Deprecated in
2.3.0
, use.file_exists()
method instead. This method will be removed in3.0.0
release.Check local file existence.
- Returns:
Does the file exist
- items() list #
Get items of the object’s
dictionary
- Returns:
Items of the
dictionary
((key, value) pairs)
- keys() list #
Get keys of the object’s
dictionary
- Returns:
Keys of the
dictionary
(, key)
- property parser_read_kwargs: dict#
Arguments that will be passed to parser on read from file action. This property can be modified. Note that only
dict
type is allowed.- Returns:
already specified arguments
- property parser_write_kwargs: dict#
Arguments that will be passed to parser on write to file action. This property can be modified. Note that only
dict
type is allowed.- Returns:
already specified arguments
- pop(key: str, default=None) Any #
Pop key from object’s
dictionary
- Parameters:
key – Key name
default – Default value, if key was not found, defaults to None
- Returns:
Value of requested
key
, ordefault
value if key wasn’t found, defaults to None.
- popitem() Any #
Pop item from object’s
dictionary
in LIFO order.- Parameters:
key – Key name
default – Default value, if key was not found, defaults to None
- Returns:
Value of requested
key
, ordefault
value if key wasn’t found, defaults to None.
- read_file_as_dict() dict #
Read local file bound to this object as dictionary
- Returns:
Parsed to dictionary local file
- refresh(safe_mode=True) bool #
Refresh object’s
dictionary
values from local file. Note that this method does not remove user-added keys, it will only add non existent keys and modify the already existing keys.- Parameters:
safe_mode – Provides the recursive merge of dictionary from local file to object’s
dictionary
. This option prevents object’s nested dictionaries to be overwritten by local files, but is much slower than simpledict.update()
method call. So if you don’t care about nested dictionaries be overwritten, you can disable this feature to boost the execution speed- Returns:
Status of local file read action. If file does not exist -
False
will be returned.
- reload() bool #
Reset all not commited changes made to object’s
dictionary
to values from local file- Returns:
Status of local file read action. If file does not exist -
False
will be returned.
- reset_to_defaults()#
Reset the object’s
dictionary
to values fromdictionary_default
attribute. Note that local file will stay untouched.
- setdefault(key: str, default=None) Any #
If key is in the object’s
dictionary
, return its value. If not, insert key with a value ofdefault
and returndefault
- Parameters:
key – Name of the key
default – Default value, defaults to None
- Returns:
If key is in the dictionary, return its value, else: returns
defalut
- update(dictionary: dict)#
Update object’s
dictionary
with another dictionary- Parameters:
dictionary – Dictionary, that will be merged to
dictionary
- values() list #
Get values of the object’s
dictionary
- Returns:
Values of the
dictionary
(, value)
- write_dict_to_file(dictionary: dict)#
Write dict from
dictionary
argument to local file bound to this object- Parameters:
dictionary – Dictionary that should be written to file
- serialix.core.create_directories(path_to_use: str, path_is_dir=False)#
Create all directories from path
- Parameters:
path_to_use – The path to be created
path_is_dir – Is
path_to_use
ends with directory, defaults to False
- serialix.core.deprecation_mark(deprecation_note: str) None #
Function used to mark something deprecated in this package
- Parameters:
deprecation_note – What and when was deprecated message, that will be collected by loggers
- serialix.core.recursive_dicts_merge(merge_from: dict, merge_to: dict) dict #
This function will recursively merge
merge_from
dictionary tomerge_to
. Merging with this function, instead of thedict.update()
method prevents from keys removal of nested dictionaries.- Parameters:
merge_from – Dictionary to merge keys from
merge_to – Dictionary to merge keys to
- Returns:
Dictionary with all
merge_from
keys merged intomerge_to
Note
merge_from
andmerge_to
dictionaries will not be modified in process of execution. Function will get their copies and work with them.
serialix.__init__#
Here is the main initialization code that makes it easier to access the main features of the other sub-modules