JSONManipulator package

Submodules

JSONManipulator.core module

The JSONManipulator’s core module with all the classes and functions.

class JSONManipulator.core.AddKey(full_path)

Bases: object

A class to add a new key to each object in the JSON file.

Args:
full_path (str): the full path to the JSON file.
Raises:

FileNotFoundError: if the JSON file is not found by full_path.

IsADirectoryError: if full_path is to a directory, not to the JSON file.

add_key() → None

Add a key to each object in the JSON file, optionally with a description, with a default value.

class JSONManipulator.core.AddObject(full_path)

Bases: object

A class to add a new object to the JSON file.

Args:
full_path (str): the full path to the JSON file.
add_object() → None

Add an object to the JSON file.

class JSONManipulator.core.ChangeAllValues(full_path, value='')

Bases: JSONManipulator.core.ChangeValue

A class to change values of all objects in the JSON file.

Args:

value (str): a redundant parameter, exists as mandatory in the parent class.

full_path (str): the full path to the JSON file.

Raises:

FileNotFoundError: if the JSON file is not found by full_path.

IsADirectoryError: if full_path is to a directory, not to the JSON file.

change_all_values() → None

Change the values of all objects in the JSON file.

class JSONManipulator.core.ChangeValue(value, full_path, levenshtein=1.0, key=None, desc=None)

Bases: JSONManipulator.core.GetInformation

A child class of GetInformation to change values of found dictionary(-ies) simultaneously.

Args:

key (str): to find the object by the key in the JSON file.

desc (str): to find the object by the description.

full_path (str): the full path to the JSON file.

value (str): the value of key/desc which will be used to find the object(s).

levenshtein (float): the similarity of the elicited objects to the input. By default, seeks 100% similarity.

Raises:

exceptions.NoKeyAndDesc: if neither key nor desc is entered.

FileNotFoundError: if the JSON file is not found by full_path.

IsADirectoryError: if full_path is to a directory, not to the JSON file.

change_one_object(start_dictionary) → None

Change user-chosen values from the object - start_dictionary.

change_several_objects(start_list_dictionaries) → None

Change several objects from start_list_dictionaries simultaneously.

change_value() → None

Inheritably call get_information(), then call additional in-class functions to change values of the object(s).

desc
static format_several_objects(user_option, dictionary_container) → List[Dict[KT, VT]]

Process user_option and take user-chosen objects from dictionary_container.

Args:
user_option (str): numbers of chosen dictionaries, for example, “2-4,10”. dictionary_container (list): the list to take dictionaries from.
Returns:
List[Dict]: the list of dictionaries which values it is necessary to change.
full_path
static if_clauses(type_value, changed_dictionary, key_in_initial_dict, desc, new_value) → None

Set new values to the objects in the JSON file.

key
levenshtein
value
class JSONManipulator.core.DeleteObject(value, full_path, levenshtein=1.0, key=None, desc=None)

Bases: JSONManipulator.core.ChangeValue

A class to delete found objects.

Args:

key (str): to find the object by the key in the JSON file.

desc (str): to find the object by the description.

full_path (str): the full path to the JSON file.

value (str): the value of key/desc which will be used to find the object(s).

levenshtein (float): the similarity of the elicited objects to the input. By default, seeks 100% similarity.

Raises:

exceptions.NoKeyAndDesc: if neither key nor desc is entered.

FileNotFoundError: if the JSON file is not found by full_path.

IsADirectoryError: if full_path is to a directory, not to the JSON file.

delete_object() → None

Take the list of objects, after a user chose ones to delete, call execute_delete() function.

desc
execute_delete(dict_container) → None

Delete redundant objects.

full_path
key
levenshtein
value
class JSONManipulator.core.GetInformation(value, full_path, levenshtein=1.0, key=None, desc=None)

Bases: object

The base class to retrieve the information about desired object(s).

Args:

key (str): to find the object by the key in the JSON file.

desc (str): to find the object by the description.

full_path (str): the full path to the JSON file.

value (str): the value of key/desc which will be used to find the object(s).

levenshtein (float): the similarity of the elicited objects to the input. By default, seeks 100% similarity.

Raises:

exceptions.NoKeyAndDesc: if neither key nor desc is entered.

FileNotFoundError: if the JSON file is not found by full_path.

IsADirectoryError: if full_path is to a directory, not to the JSON file.

static cap_sentence(string) → bool

Check if string looks like a person’s first, middle or/and last names.

Returns:

True: if the entered data is about a person.

False: else.

desc
full_path
get_information() → List[Dict[KT, VT]]

Process the user’s parameters and the JSON file’s values, then call additional in-class functions to analyse data and output objects, accordingly.

Returns:

List[Dict]: if GetInformation is a parent class - for list of dictionaries manipulations.

None: else.

key
levenshtein
levenshtein_calc(dictionary_value, dictionary) → None

Compare processed object.value and dictionary_value, and if the similarity is higher than object.levenshtein - append to the list for the further output.

output_dict_container
output_for_key_and_value() → None

Process object.output_dict_container from levenshtein_calc(), beautify the output of the objects.

value
JSONManipulator.core.set_up(full_path) → None

Configure the initial JSON file. Add descriptions for the keys in the JSON file for the further more readable retrieval.

Args:
full_path (str): the path to the desired file.
Raises:

FileNotFoundError: if the JSON file is not found by full_path.

IsADirectoryError: if full_path is to a directory, not to the JSON file.

exception.NotSupportedJSONFile: if the JSON file is not supported by the package.

JSONManipulator.exceptions module

The JSONManipulator’s exceptions.

exception JSONManipulator.exceptions.NoKeyAndDesc

Bases: Exception

Raised when a user has entered neither key nor desc.

exception JSONManipulator.exceptions.NotSupportedJSONFile

Bases: Exception

Raised when the JSON file’s structure does not confront to [{…},{…},…].

Module contents

NAME
JSONManipulator.
DESCRIPTION
A Python library to manipulate objects in JSON files.
PACKAGE CONTENTS

set_up(full_path): initially set up the JSON file.

GetInformation(value, full_path, levenshtein=1.0, key=None, desc=None): retrieve information about particular objects.

ChangeValue(value, full_path, levenshtein=1.0, key=None, desc=None): change values of particular objects in the JSON file.

AddObject(full_path): add a new object to the JSON file.

DeleteObject(value, full_path, levenshtein=1.0, key=None, desc=None): delete particular objects in the JSON file.

AddKey(full_path): add a new key to each object in the JSON file.

ChangeAllValues(value, full_path): change values of all objects in the JSON file.