serialix#

Subpackages#

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 (or yml), toml (or tml)

  • 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 or dict 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 to file_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 path str or dict

  • 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 or dict 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 path str or dict

Note

Methods .clear(), .fromkeys(), .get(), .items(), .keys(), values(), pop(), popitem(), setdefault(), update() are bound to the attribute dictionary, 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, or default 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 in 3.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, or default 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, or default 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 simple dict.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 from dictionary_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 of default and return default

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 to merge_to. Merging with this function, instead of the dict.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 into merge_to

Note

merge_from and merge_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

serialix.meta module#