diggrtoolbox.treeexplore package

Submodules

diggrtoolbox.treeexplore.treeexplore module

Getting data structures to work with, sometimes is hard, especially, when you need to find specific information in nested jsons and no schema is provided, or the data and its changing fast.

Author: F. Rämisch <raemisch@ub.uni-leipzig.de> Copyright: 2018, Universitätsbibliothek Leipzig License: GNU General Public License v3

class diggrtoolbox.treeexplore.treeexplore.TreeExplore(tree, tab_symbol=' ')[source]

Bases: object

TreeExplore provides easy to use methods to explore complex data structures obtained e.g. from online REST-APIs. As data structures behind often grew over the years, the internal structure of these objects to be obtained often is not logical.

By providing a full text search and a show method, this tool can be helpful when first investigating, what information is to be found in the data and what is its structure.

Example:
>>> import diggrtoolbox as dt
>>> test_dict = {'id' : 123456789,
>>>              'data' : {'name' : 'diggr project',
>>>                        'city' : 'Leipzig',
>>>                        'field': 'Video Game Culture'},
>>>              'references':[{'url' : 'http://diggr.link',
>>>                             'name' : 'diggr website'},
>>>                             {'url' : 'http://ub.uni-leipzig.de',
>>>                              'name' : 'UBL website'}]}
>>> tree = dt.TreeExplore(test_dict)
>>> results = tree.search("leipzig")
Search-Term: leipzig
Route: references, 1, url,
Embedding: 'http://ub.uni-leipzig.de'
>>> print(results)
[{'embedding': 'http://ub.uni-leipzig.de',
  'route': ['references', 1, 'url'],
  'unique_in_embedding': False,
  'term': 'leipzig'}]

Note

Currently the search is case sensitive only!

find(term)[source]
find_key(key)[source]
find_value(value)[source]

Wrapper for the _search function to ease access to a nonprinting search function.

Parameters:term (str, int, float) – the term/object to be found in the tree.
search(term)[source]

Wrapper for the _search function, stripping all the parameters not to be used by the end user.

Parameters:term (str, int, float) – the term/object to be found in the tree.
show(tree=None, indent=0)[source]

Visualizes the whole tree. If no tree-like structure (dict/list/both) is given, the self.tree is used. This function is called recursively with the nested subtrees.

Parameters:
  • tree (dict, list) – The tree to be shown.
  • indent (int) – Current indentation level of this tree
show_search_result(result)[source]

Displays a search result together with its embedding and path.

Parameters:result (dict) – the result dict generated by _prepare_search_result

diggrtoolbox.treeexplore.treehash module

TreeHash is a Function enabling the user to compare nested dicts and lists by generating a hash.

diggrtoolbox.treeexplore.treehash.treehash(var)[source]

Returns the hash of any dict or list, by using a string conversion via the json library.

Module contents

class diggrtoolbox.treeexplore.TreeExplore(tree, tab_symbol=' ')[source]

Bases: object

TreeExplore provides easy to use methods to explore complex data structures obtained e.g. from online REST-APIs. As data structures behind often grew over the years, the internal structure of these objects to be obtained often is not logical.

By providing a full text search and a show method, this tool can be helpful when first investigating, what information is to be found in the data and what is its structure.

Example:
>>> import diggrtoolbox as dt
>>> test_dict = {'id' : 123456789,
>>>              'data' : {'name' : 'diggr project',
>>>                        'city' : 'Leipzig',
>>>                        'field': 'Video Game Culture'},
>>>              'references':[{'url' : 'http://diggr.link',
>>>                             'name' : 'diggr website'},
>>>                             {'url' : 'http://ub.uni-leipzig.de',
>>>                              'name' : 'UBL website'}]}
>>> tree = dt.TreeExplore(test_dict)
>>> results = tree.search("leipzig")
Search-Term: leipzig
Route: references, 1, url,
Embedding: 'http://ub.uni-leipzig.de'
>>> print(results)
[{'embedding': 'http://ub.uni-leipzig.de',
  'route': ['references', 1, 'url'],
  'unique_in_embedding': False,
  'term': 'leipzig'}]

Note

Currently the search is case sensitive only!

find(term)[source]
find_key(key)[source]
find_value(value)[source]

Wrapper for the _search function to ease access to a nonprinting search function.

Parameters:term (str, int, float) – the term/object to be found in the tree.
search(term)[source]

Wrapper for the _search function, stripping all the parameters not to be used by the end user.

Parameters:term (str, int, float) – the term/object to be found in the tree.
show(tree=None, indent=0)[source]

Visualizes the whole tree. If no tree-like structure (dict/list/both) is given, the self.tree is used. This function is called recursively with the nested subtrees.

Parameters:
  • tree (dict, list) – The tree to be shown.
  • indent (int) – Current indentation level of this tree
show_search_result(result)[source]

Displays a search result together with its embedding and path.

Parameters:result (dict) – the result dict generated by _prepare_search_result
diggrtoolbox.treeexplore.treehash(var)[source]

Returns the hash of any dict or list, by using a string conversion via the json library.