diffjson package¶
Subpackages¶
Submodules¶
diffjson.branch module¶
-
class
diffjson.branch.BranchBase(data, parent)[source]¶ Bases:
objectTemplate Class for branch base. All Branch and Leaf class must be inherit this class.
- Args:
- data(any json data): Json format data under current branch.
dict, list, str, int, float, bool
parent(BranchBase): Parent branch of this branch.
_childbranches(dict or list): Child branches, wrapped by attributes.
- Attributes:
parent(BranchBase): Parent branch of this branch.
childnodenames(list): List of NodeName for childbranches. (getter)
childbranches(list): List of childbranches. (getter)
descendants(list): List of descendant branches. (getter)
rootbranch(RootBranch): RootBranch of this branch. (getter)
nodename(NodenameBase): Nodename of this branch. (getter)
locationpath(LocationPath): LocationPath of this branch. (getter)
- Note:
Not allow to access directory because data types are different for all branch types.
-
check_predicates(predicates)[source]¶ Check self branch matches predicates or not.
- Args
predicates(Predicates): List of Arg objects.
- Returns:
Boolean: Predicates match for the branch or not.
-
childbranch(nodename)[source]¶ Return child branch with target nodename if exist
- Args:
nodename(NodenameBase): Child nodename.
- Returns:
- BranchBase: Target Child branch.
Return NullBranch if not exist.
-
property
childbranches¶ Return all child branches.
- Returns:
list: List of all child branches.
-
property
childnodenames¶ Nodenames of branches.
- Returns:
list: List of branch nodenames.
-
property
descendants¶ Return all descendants branches.
- Returns:
list: List of all descendants branches in flat format.
-
dump()[source]¶ Return json data of child branches.
- Returns:
Any json data: Dict format child branches.
-
isat(locationpath_str)[source]¶ Check this branch is at target locationpath
- Args:
locationpath_str(str): Check target.
- Returns:
bool
-
property
locationpath¶ Extract location Path.
- Returns:
LocationPath: LocationPath of this branch.
-
property
nodename¶ Nodename for self.
- Returns:
NodenameBase: nodename.
- Note:
Nodename is unknown for branch, only parent knows.
-
raw_search(locationpath)[source]¶ Search child branches by locationpath.
- Args:
locationpath(LocationPath): Search LocationPath
- Returns:
List: List of matched objects.
-
property
rootbranch¶ Return root branch.
- Returns:
RootBranch: Root of this branch.
-
search(locationpath_string, details=False)[source]¶ Search target path and get json data list.
- Args:
locationpath_string(str): XPath format search string.
- details(bool): Return searched path with value,
default: False(value only).
- Returns:
- list: List of json data at target path (details=False).
With details True, list of set like {target_path(str), dict or list at target path}.
- Node:
This method must be implemented in RootBranch only.
-
class
diffjson.branch.ChildBranch(data, parent)[source]¶ Bases:
diffjson.branch.BranchBaseClass for child data in json data tree.
- Note:
All child branch classes inherits this class.
-
append(data)[source]¶ Append new data to target DictBranch.
- Args:
- data(any json data): Json format data under current branch.
dict, list, str, int, float, bool
- Returns:
- LocationPath or None: LocationPath of new branch.
Return None if target nodename already exist.
- Note:
Must be implemented for ListBranch only.
-
insert(index, data)[source]¶ Insert new data to target DictBranch.
- Args:
nodepath_string(str): XPath format search string.
- data(any json data): Json format data under current branch.
dict, list, str, int, float, bool
- Returns:
- LocationPath or None: LocationPath of new branch.
Return None if target nodename already exist.
- Note:
Must be implemented for ListBranch only.
-
pop(nodename)[source]¶ Pop target BranchBase.
- Args:
nodename(NodeName): Pop Target
- Returns:
BranchBase
-
set(nodename, data)[source]¶ Set data to existing branch.
- Args:
nodepath(Nodename): Nodename for set target.
- data(any json data): Json format data under current branch.
dict, list, str, int, float, bool
-
setdefault(nodename, data)[source]¶ Add new data to target DictBranch if not exists.
Keep current data if already exists.”
- Args:
nodepath_string(str): XPath format search string.
- data(any json data): Json format data under current branch.
dict, list, str, int, float, bool
- Returns:
- LocationPath or None: LocationPath of new branch.
Return None if target nodename already exist.
- Note:
Must be implemented for DictBranch only.
-
class
diffjson.branch.DictBranch(data, parent)[source]¶ Bases:
diffjson.branch.ChildBranchClass for node data in json data tree.
-
dump()[source]¶ Return json data of child branches.
- Returns:
Any json data: Dict format child branches.
-
setdefault(nodename, data)[source]¶ Add new data to target DictBranch if not exists.
Keep current data if already exists.”
- Args:
nodepath_string(str): XPath format search string.
- data(any json data): Json format data under current branch.
dict, list, str, int, float, bool
- Returns:
- LocationPath or None: LocationPath of new branch.
Return None if target nodename already exist.
- Note:
Must be implemented for DictBranch only.
-
-
class
diffjson.branch.Leaf(data, parent)[source]¶ Bases:
diffjson.branch.ChildBranchClass for edge node in json data tree.
- Args:
data(any json edge data): Edge data. parent(ChildBranch): Parent branch of this leaf.
- Attributes:
data(str or int or float or bool): Edge value.
-
class
diffjson.branch.ListBranch(data, parent)[source]¶ Bases:
diffjson.branch.ChildBranchClass for List type branch.
-
dump()[source]¶ Return json data of child branches.
- Returns:
Any json data: Dict format child branches.
-
insert(index, data)[source]¶ Insert new data to target DictBranch.
- Args:
nodepath_string(str): XPath format search string.
- data(any json data): Json format data under current branch.
dict, list, str, int, float, bool
- Returns:
- LocationPath or None: LocationPath of new branch.
Return None if target nodename already exist.
- Note:
Must be implemented for ListBranch only.
-
-
class
diffjson.branch.NullBranch(parent=None)[source]¶ Bases:
diffjson.branch.ChildBranchDummy branch for not existing branch.
-
class
diffjson.branch.RootBranch(data)[source]¶ Bases:
diffjson.branch.BranchBaseClass for root data in json data tree.
- Note:
This class wraps child branches and provides methods for users. Have only one child branch with “/” nodename.
-
append(locationpath_string, data)[source]¶ Append new data to target ListBranch.
- Args:
data(any json data): Json format data under current branch.
- Returns:
list: List of locationpaths where data was changed.
-
dump()[source]¶ Return json data of child branches.
- Returns:
Any json data: Dict format child branches.
-
insert(locationpath_string, index, data)[source]¶ Insert new data to target ListBranch.
- Args:
locationpath_string(str): XPath format search string.
index(int): XPath format search string.
data(any json data): Json format data under current branch.
- Returns:
list: List of locationpaths where data was changed.
-
property
locationpath¶ Extract location Path.
- Returns:
LocationPath: LocationPath of this branch.
-
pop(locationpath_string)[source]¶ Pop BranchBase.
- Args:
locationpath_string(str): XPath format search string.
- Returns:
list: List of dumped Branches at target locationpath_string.
-
remove(locationpath_string)[source]¶ Remove BranchBase.
- Args:
locationpath_string(str): XPath format search string.
- Note:
Don’t delete self, only delete link from parent.
-
set(locationpath_string, data)[source]¶ Update existing branch.
- Args:
locationpath_string(str): XPath format search string.
- data(any json data): Json format data under current branch.
dict, list, str, int, float, bool
- Returns:
list: List of locationpaths where data was changed.
-
setdefault(locationpath_string, nodename_string, data)[source]¶ Add new data to target DictBranch.
- Args:
locationpath_string(str): XPath format search string.
nodename_string(str): Nodename for new node.
- data(any json data): Json format data under current branch.
dict, list, str, int, float, bool
- Returns:
list: List of locationpaths where data was changed.
diffjson.diff module¶
-
class
diffjson.diff.DiffBranchBase(data, parent)[source]¶ Bases:
diffjson.branch.BranchBaseTemplate Class for DiffBranchBases.
- Args:
listed_branches(list): Listed RootBranches to compare.
- Note:
DiffBranchBase expand all input RootBranches to have same branches.
DiffBranchBases are dict style multi dimensional branches.
Use various dump and export methods to get various output data styles.
-
class
diffjson.diff.DiffChildBranch(data, parent)[source]¶ Bases:
diffjson.diff.DiffBranchBaseClass for diff child branch.
-
class
diffjson.diff.DiffMultiBranch(listed_branches, parent)[source]¶ Bases:
diffjson.diff.DiffChildBranchClass for diff child branch for multi branch diff.
- Args:
listed_branches(list): Branches to compare. parent(BranchBase): Parent of DiffMultiBranch
- Attributes:
parent(BranchBase): Parent of DiffMultiBranch
-
dump(hide_unchanged=False)[source]¶ Dump diff data.
- Note:
For this version, dump is dump_as_structure. This might be reviewed.
-
class
diffjson.diff.DiffRootBranch(listed_branches, nodenamemasks=None, reportmode=None)[source]¶ Bases:
diffjson.diff.DiffBranchBaseClass for root of diff branch.
- Args:
listed_branches(list): Listed RootBranches compare.
- nodenamemasks(dict): Dict of locationpath_string, lambda.
This list will be passed from root to child. Child find it’s own mask if exist in the list.
- Note:
This class wraps child branches and provides methods for users.
Have only one child branch with “/” nodename.
-
dump(**kwargs)[source]¶ Dump diff data.
- Args:
**kwargs: Dump args, passed to child branches.
- Returns:
- dict: List format diff result.
{<path>: [<value of 1st branch>, <value of 2nd branch>, ,,]}
- Note:
Cannot inherit dump from Branch to ignore disturbing hierarchical output.
-
dump_as_table(hide_unchanged=False)[source]¶ Dump diff result as table format.
- Args:
- hide_unchanged(bool, optional): Stop to output unchanged branch,
default False
- Returns:
list
-
export_csv(fname, hide_unchanged=False, emphasis=False)[source]¶ Dump diff result as list format.
- Args:
fname(str): Export filename.
hide_unchanged(bool): Hide unchanged branch. Default: False
emphasis(bool): Emphasis changed point. Default: False
- Returns:
- list: List of following formats.
{<nodepath>: [data of each branch]}
-
search(locationpath_string, details=False, **kwargs)[source]¶ Search target path with search nodepath string.
- Args:
locationpath_string(str): XPath format search string.
- details(bool, option): Return searched path with value,
default: False(value only).
- dump_mode(str, option): DiffTwoBranch only.
‘all’: Return all branch. ‘added’: Return added branches, including it’s children. ‘bulk_added’: Return root of added branches. ‘removed’: Return removed branches, including it’s children. ‘bulk_removed’: Return root of removed branches. ‘changed’: Return changed branch only.
- Returns:
dict or list: dict or list format JSON data.
- Node:
This method must be implemented in RootBranch only.
-
class
diffjson.diff.DiffTwoBranch(listed_branches, parent)[source]¶ Bases:
diffjson.diff.DiffMultiBranchClass for diff two branches. This class has some extra method only for 2 branch diff.
- Args:
listed_branches(list): Branches to compare. parent(BranchBase): Parent of DiffMultiBranch
- Attributes:
parent(BranchBase): Parent of DiffMultiBranch
-
dump(hide_unchanged=False, dump_mode='all')[source]¶ Dump diff data.
- Note:
For this version, dump is dump_as_structure. This might be reviewed.
-
dump_as_slice(hide_unchanged=False, dump_mode='all')[source]¶ Dump diff result of target branch. Not include children. Target branch only.
- Args:
hide_unchanged(bool): Stop to output unchanged branch.
- dump_mode(str): Select dump target.
If hide_unchanged=True, ignored. ‘all’: Dump all branch. ‘added’: Dump added branch. Include added root and children. ‘bulk_added’: Dump added root branch, ignore children of them. ‘removed’: Dump removed branch. Include added root and children. ‘bulk_removed’: Dump removed root branch, ignore children of them. ‘changed’: Dump changed branch only.
-
dump_as_structure(hide_unchanged=False, dump_mode='all')[source]¶ Dump branch data at this branch.
- Args:
hide_unchanged(bool): Stop to output unchanged branch.
- dump_mode(str): Select dump target.
If hide_unchanged=True, ignored. ‘all’: Dump all branch. ‘added’: Dump added branch. Include added root and children. ‘bulk_added’: Dump added root branch, ignore children of them. ‘removed’: Dump removed branch. Include added root and children. ‘bulk_removed’: Dump removed root branch, ignore children of them. ‘changed’: Dump changed branch only.
-
is_bulk_added()[source]¶ Check this branch is root of added branch or not.
- Returns:
bool
- Note:
If parent branch is added branch, this branch is not root. But if parent is DiffRootBranch, this branch is added root.
-
is_bulk_removed()[source]¶ Check this branch is root of removed branch or not.
- Returns:
bool
- Note:
If parent branch is removed branch, this branch is not root. But if parent is DiffRootBranch, this branch is removed root.