Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dwd_synop
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Schultz
dwd_synop
Commits
6d91a555
Commit
6d91a555
authored
5 years ago
by
martin
Browse files
Options
Downloads
Patches
Plain Diff
First version of synop.py
parent
5becb2dd
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
synop.py
+71
-0
71 additions, 0 deletions
synop.py
with
71 additions
and
0 deletions
synop.py
0 → 100644
+
71
−
0
View file @
6d91a555
"""
SynopReader class
reads a nested JSON structure and flattens it for easier access
"""
import
json
from
collections
import
OrderedDict
class
SynopReader
():
def
__init__
(
self
,
filename
):
"""
initializes object with filename and reads the JSON file
"""
# ToDo: interpret URL as alternative to filename and access JSON from URL if filename startswith http
with
open
(
filename
,
'
r
'
)
as
f
:
content
=
f
.
read
()
self
.
filename
=
filename
self
.
content
=
json
.
loads
(
content
)
## print(self.content)
@staticmethod
def
condense
(
d
,
keys
=
None
):
"""
converts a
"
DWD dct
"
with key, value, units tags into dict entry key: (value, units)
:param d: the input dictionary
:param keys: existing key values (to avoid overwriting)
:return: dict with {key: (value, units)}
"""
print
(
d
)
key
=
d
.
get
(
'
key
'
,
'
undefined_key
'
)
value
=
d
.
get
(
'
value
'
)
units
=
d
.
get
(
'
units
'
)
# ToDo: improve this logic to make result more readable
if
keys
is
not
None
and
key
in
keys
:
suffix
=
'
123456789abcdefghijklmnopqrstuvwxyz
'
key_c
=
[
key
+
'
_
'
+
x
for
x
in
suffix
]
for
k
in
key_c
:
if
not
k
in
keys
:
key
=
key_c
break
return
{
key
:
(
value
,
units
)}
def
process_one
(
self
,
element
):
"""
browse through one part of the JSON structure and condense all information
:return: condensed dict
"""
res
=
OrderedDict
()
# check if current element is list
if
isinstance
(
element
,
list
):
for
item
in
element
:
res
.
update
(
self
.
process_one
(
item
))
return
res
# ToDo: implement solution for list
# assume current element is dict
return
self
.
condense
(
element
,
res
.
keys
())
def
process
(
self
):
"""
browse through the entire JSON structure and condense all information
:return: condensed dict
"""
res
=
self
.
process_one
(
self
.
content
)
print
(
res
)
if
__name__
==
"
__main__
"
:
TESTDATA
=
"
testdata/snippet_synop_dwd_opendata.json
"
SynopReader
(
TESTDATA
).
process
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment