Skip to content

Commit c7f2b0f

Browse files
add support for list data. AtsushiSakai#31 (AtsushiSakai#32)
* add support for list data. AtsushiSakai#31 * add test cases. * add test for nested json.
1 parent 55e9218 commit c7f2b0f

File tree

5 files changed

+177
-2
lines changed

5 files changed

+177
-2
lines changed

dat/dictionary.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"glossary": {
3+
"title": "example glossary",
4+
"GlossDiv": {
5+
"title": "S",
6+
"GlossList": {
7+
"GlossEntry": {
8+
"ID": "SGML",
9+
"SortAs": "SGML",
10+
"GlossTerm": "Standard Generalized Markup Language",
11+
"Acronym": "SGML",
12+
"Abbrev": "ISO 8879:1986",
13+
"GlossDef": {
14+
"para": "A meta-markup language, used to create markup languages such as DocBook.",
15+
"GlossSeeAlso": ["GML", "XML", "TOML", "YAML"],
16+
"GlossSeeAlso2": [1, 2, 3, 4],
17+
"GlossSeeAlso3": ["5", 2.3, "3", null, true]
18+
},
19+
"GlossSee": "マークアップ"
20+
}
21+
}
22+
}
23+
},
24+
"Link": "https://searchmicroservices.techtarget.com/definition/SGML-Standard-Generalized-Markup-Language"
25+
}

dat/list.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"date": "2017-01-01",
4+
"title": "Automated question answering system using ontology and semantic role",
5+
"doc_id": "258cc0fd7973def42234f3db5ffdfd2a",
6+
"score": 1.0
7+
},
8+
{
9+
"date": "2011-01-01",
10+
"title": "A new fuzzy-based method to weigh the related concepts in semantic focused web crawlers",
11+
"doc_id": "71ae01c53f4eeb7d402f5bcfbce36f9f",
12+
"score": 1.0
13+
}
14+
]

dat/nested.json

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"glossary": {
3+
"title": "example glossary",
4+
"GlossDiv": {
5+
"title": "S",
6+
"GlossList": {
7+
"GlossEntry": {
8+
"ID": "SGML",
9+
"SortAs": "SGML",
10+
"GlossTerm": "Standard Generalized Markup Language",
11+
"Acronym": "SGML",
12+
"Abbrev": "ISO 8879:1986",
13+
"GlossDef": {
14+
"para": "A meta-markup language, used to create markup languages such as DocBook.",
15+
"GlossSeeAlso": [
16+
"GML",
17+
"XML",
18+
"TOML",
19+
"YAML"
20+
],
21+
"GlossSeeAlso2": [
22+
1,
23+
2,
24+
3,
25+
4
26+
],
27+
"GlossSeeAlso3": [
28+
"5",
29+
2.3,
30+
"3",
31+
null,
32+
true
33+
]
34+
},
35+
"GlossSee": "マークアップ"
36+
}
37+
}
38+
}
39+
},
40+
"List": [
41+
{
42+
"date": "2017-01-01",
43+
"title": "Automated question answering system using ontology and semantic role",
44+
"doc_id": "258cc0fd7973def42234f3db5ffdfd2a",
45+
"score": 1.0
46+
},
47+
{
48+
"date": "2011-01-01",
49+
"title": "A new fuzzy-based method to weigh the related concepts in semantic focused web crawlers",
50+
"doc_id": "71ae01c53f4eeb7d402f5bcfbce36f9f",
51+
"score": 1.0
52+
}
53+
],
54+
"Link": "https://searchmicroservices.techtarget.com/definition/SGML-Standard-Generalized-Markup-Language"
55+
}

pyjsonviewer/pyjsonviewer.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def find(self, search_text):
164164
self.collapse_all(None)
165165
for item_id in self.get_all_children(self.tree):
166166
item_text = self.tree.item(item_id, 'text')
167+
item_text = str(item_text)
167168
if search_text.lower() in item_text.lower():
168169
self.tree.see(item_id)
169170

@@ -225,8 +226,12 @@ def delete_all_nodes(self):
225226

226227
def insert_nodes(self, data):
227228
parent = ""
228-
for (key, value) in data.items():
229-
self.insert_node(parent, key, value)
229+
if isinstance(data, list):
230+
for index, value in enumerate(data):
231+
self.insert_node(parent, index, value)
232+
elif isinstance(data, dict):
233+
for (key, value) in data.items():
234+
self.insert_node(parent, key, value)
230235

231236
def open_github_page(self):
232237
self.open_url("https://github.com/AtsushiSakai/PyJSONViewer")

tests/test_pyjsonviewer.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""Unit tests for Pyjsonviewer."""
5+
6+
import sys
7+
import traceback
8+
sys.path.insert(0, "../pyjsonviewer")
9+
import tkinter as tk
10+
from pyjsonviewer import JSONTreeFrame
11+
12+
13+
def test_dictionary_file():
14+
"""
15+
should read dictionary file okay
16+
"""
17+
# given
18+
root: Tk = tk.Tk()
19+
app = JSONTreeFrame(root, json_path="../dat/dictionary.json")
20+
21+
# when
22+
children = app.get_all_children(app.tree)
23+
# print([app.tree.item(item_id, 'text') for item_id in children])
24+
25+
# then
26+
assert len(children) == 31
27+
28+
29+
def test_list_file():
30+
"""
31+
should read list file okay
32+
"""
33+
# given
34+
root: Tk = tk.Tk()
35+
app = JSONTreeFrame(root, json_path="../dat/list.json")
36+
37+
# when
38+
children = app.get_all_children(app.tree)
39+
# print([app.tree.item(item_id, 'text') for item_id in children])
40+
41+
# then
42+
assert len(children) == 18
43+
44+
45+
def test_nested_file():
46+
"""
47+
should read list file okay
48+
"""
49+
# given
50+
root: Tk = tk.Tk()
51+
app = JSONTreeFrame(root, json_path="../dat/nested.json")
52+
53+
# when
54+
children = app.get_all_children(app.tree)
55+
print([app.tree.item(item_id, 'text') for item_id in children])
56+
57+
# then
58+
assert len(children) == 33
59+
60+
61+
def test_search():
62+
"""
63+
search should not break in case of numeric fields
64+
"""
65+
# given
66+
root: Tk = tk.Tk()
67+
app = JSONTreeFrame(root, json_path="../dat/list.json")
68+
69+
70+
try:
71+
x = app.find("fuzzy")
72+
except AttributeError as err:
73+
# AttributeError: 'int' object has no attribute 'lower'
74+
assert False
75+
assert True
76+

0 commit comments

Comments
 (0)