diffjson package

Submodules

diffjson.branch module

class diffjson.branch.BranchBase(data, parent)[source]

Bases: object

Template 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.

is_child()[source]

Check this branch is child or not.

Returns:

bool

is_root()[source]

Check this branch is root or not.

Returns:

bool

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.

Search child branches by locationpath.

Args:

locationpath(LocationPath): Search LocationPath

Returns:

List: List of matched objects.

raw_set(nodename, childbranch)[source]
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.BranchBase

Class 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.

is_child()[source]

Check this branch is child or not.

Returns:

bool

is_root()[source]

Check this branch is root or not.

Returns:

bool

pop(nodename)[source]

Pop target BranchBase.

Args:

nodename(NodeName): Pop Target

Returns:

BranchBase

remove(nodename)[source]

Remove target BranchBase.

Args:

nodename(NodeName): Pop Target

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.ChildBranch

Class 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.ChildBranch

Class 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.

dump()[source]

Return json data of child branches.

Returns:

Any json data: Dict format child branches.

class diffjson.branch.ListBranch(data, parent)[source]

Bases: diffjson.branch.ChildBranch

Class 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.

raw_append(data)[source]
class diffjson.branch.NullBranch(parent=None)[source]

Bases: diffjson.branch.ChildBranch

Dummy branch for not existing branch.

dump()[source]

Return json data of child branches.

Returns:

Any json data: Dict format child branches.

class diffjson.branch.RootBranch(data)[source]

Bases: diffjson.branch.BranchBase

Class 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.

is_child()[source]

Check this branch is child or not.

Returns:

bool

is_root()[source]

Check this branch is root or not.

Returns:

bool

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.branch.generate_branch(data)[source]

Generate Branch.

Args:
data(Any): Json format data.

dict, list, str, int, float, bool

Returns:

BranchRoot with branch formed data.

diffjson.diff module

class diffjson.diff.DiffBranchBase(data, parent)[source]

Bases: diffjson.branch.BranchBase

Template 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.

check_predicates(predicates)[source]

Seal predicate search.

Note:

Raise exception when predicate is used in raw_search method.

is_unchanged()[source]

Check this branch and children are not changed or not.

Returns:

bool

class diffjson.diff.DiffChildBranch(data, parent)[source]

Bases: diffjson.diff.DiffBranchBase

Class for diff child branch.

is_child()[source]

Check this branch is child or not.

Returns:

bool

is_root()[source]

Check this branch is root or not.

Returns:

bool

class diffjson.diff.DiffMultiBranch(listed_branches, parent)[source]

Bases: diffjson.diff.DiffChildBranch

Class 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.

dump_as_slice(hide_unchanged=False)[source]

Dump diff result of target branch. Not include children. Target branch only.

Args:
hide_unchanged(bool, optional): Stop to output unchanged branch,

default False

dump_as_structure(hide_unchanged=False)[source]

Dump branch data at this branch.

Args:
hide_unchanged(bool, optional): Stop to output unchanged branch,

default False

Returns:

list: List of dumped data for each diffed branch.

class diffjson.diff.DiffRootBranch(listed_branches, nodenamemasks=None, reportmode=None)[source]

Bases: diffjson.diff.DiffBranchBase

Class 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]}

is_child()[source]

Check this branch is child or not.

Returns:

bool

is_root()[source]

Check this branch is root or not.

Returns:

bool

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.DiffMultiBranch

Class 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_added()[source]

Check this branch is added or not.

Returns:

bool

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.

is_changed()[source]

Check this branch and children are changed or not.

Returns:

bool

Note:

This method is only for purely changed branch. No need for bulk because children of changed branch are added or deleted.

is_removed()[source]

Check this branch and children are removed or not.

Returns:

bool

diffjson.diff.diff_branches(listed_branches)[source]

Diff branches.

Args:

listed_branches(list): List of branches to compare.

Returns:

DiffRootBranch: Diff result.

diffjson.exceptions module

Module contents