Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pycomsdk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
com
pycomsdk
Commits
35017770
Commit
35017770
authored
May 01, 2020
by
Anton Pershin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support of serialization of objects into json files
parent
ef5254e8
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
2 deletions
+22
-2
comaux.py
comsdk/comaux.py
+22
-2
No files found.
comsdk/comaux.py
View file @
35017770
...
@@ -6,8 +6,10 @@ from copy import deepcopy
...
@@ -6,8 +6,10 @@ from copy import deepcopy
import
importlib
import
importlib
from
abc
import
ABC
,
abstractmethod
from
abc
import
ABC
,
abstractmethod
from
typing
import
Optional
,
List
,
Tuple
,
Type
,
Any
from
typing
import
Optional
,
List
,
Tuple
,
Type
,
Any
import
json
import
numpy
as
np
import
numpy
as
np
from
jsons
import
JsonSerializable
ArrayItemGetter
=
collections
.
namedtuple
(
'ArrayItemGetter'
,
[
'key_path_to_array'
,
'i'
])
ArrayItemGetter
=
collections
.
namedtuple
(
'ArrayItemGetter'
,
[
'key_path_to_array'
,
'i'
])
...
@@ -368,7 +370,7 @@ def parse_datafile(path, data_names, transform_funcs, cols_to_parse=[]):
...
@@ -368,7 +370,7 @@ def parse_datafile(path, data_names, transform_funcs, cols_to_parse=[]):
for
i
,
data_name
in
enumerate
(
data_names
):
for
i
,
data_name
in
enumerate
(
data_names
):
val
=
tmp
[
cols_to_parse
[
i
]]
val
=
tmp
[
cols_to_parse
[
i
]]
data
[
data_name
]
.
append
(
transform_funcs
[
i
](
val
))
data
[
data_name
]
.
append
(
transform_funcs
[
i
](
val
))
return
data
return
{
name
:
np
.
array
(
array_
)
for
name
,
array_
in
data
.
items
()}
def
parse_timed_numdatafile
(
path
):
def
parse_timed_numdatafile
(
path
):
'''
'''
...
@@ -391,7 +393,7 @@ def parse_timed_numdatafile(path):
...
@@ -391,7 +393,7 @@ def parse_timed_numdatafile(path):
for
i
,
val
in
enumerate
(
tmp
[
1
:]):
for
i
,
val
in
enumerate
(
tmp
[
1
:]):
timed_data
[
i
]
=
float
(
val
)
timed_data
[
i
]
=
float
(
val
)
data
.
append
(
timed_data
)
data
.
append
(
timed_data
)
return
time
,
data
return
time
,
np
.
array
(
data
)
def
write_datafile
(
path
,
data
):
def
write_datafile
(
path
,
data
):
keys
=
list
(
data
.
keys
())
keys
=
list
(
data
.
keys
())
...
@@ -418,6 +420,7 @@ def print_pretty_dict(d):
...
@@ -418,6 +420,7 @@ def print_pretty_dict(d):
for
k
,
v
in
d
.
items
():
for
k
,
v
in
d
.
items
():
print
(
'{}: {}'
.
format
(
k
,
v
))
print
(
'{}: {}'
.
format
(
k
,
v
))
def
raise_exception_if_arguments_not_in_keywords_or_none
(
argument_names
,
kwargs
)
->
None
:
def
raise_exception_if_arguments_not_in_keywords_or_none
(
argument_names
,
kwargs
)
->
None
:
for
arg
in
argument_names
:
for
arg
in
argument_names
:
if
arg
not
in
kwargs
:
if
arg
not
in
kwargs
:
...
@@ -426,6 +429,7 @@ def raise_exception_if_arguments_not_in_keywords_or_none(argument_names, kwargs)
...
@@ -426,6 +429,7 @@ def raise_exception_if_arguments_not_in_keywords_or_none(argument_names, kwargs)
if
kwargs
[
arg
]
is
None
:
if
kwargs
[
arg
]
is
None
:
raise
ValueError
(
'Keywords "{}" must not be None'
.
format
(
arg
))
raise
ValueError
(
'Keywords "{}" must not be None'
.
format
(
arg
))
def
take_value_if_not_none
(
value
,
default
=
None
,
transform
=
str
)
->
Any
:
def
take_value_if_not_none
(
value
,
default
=
None
,
transform
=
str
)
->
Any
:
if
value
is
None
:
if
value
is
None
:
if
default
is
None
:
if
default
is
None
:
...
@@ -434,3 +438,19 @@ def take_value_if_not_none(value, default=None, transform=str) -> Any:
...
@@ -434,3 +438,19 @@ def take_value_if_not_none(value, default=None, transform=str) -> Any:
return
default
return
default
else
:
else
:
return
transform
(
value
)
return
transform
(
value
)
def
dump_to_json
(
obj
:
JsonSerializable
,
path_to_jsons
:
str
=
'jsons'
)
->
None
:
filename
=
'{}.{}.json'
.
format
(
type
(
obj
)
.
__module__
,
type
(
obj
)
.
__name__
)
filename
=
os
.
path
.
join
(
path_to_jsons
,
filename
)
obj_as_dict
=
obj
.
json
with
open
(
filename
,
'w'
)
as
f
:
json
.
dump
(
obj_as_dict
,
f
,
indent
=
4
)
def
load_from_json
(
cls
:
Type
[
JsonSerializable
],
path_to_jsons
:
str
=
'jsons'
)
->
JsonSerializable
:
filename
=
'{}.{}.json'
.
format
(
cls
.
__module__
,
cls
.
__name__
)
filename
=
os
.
path
.
join
(
path_to_jsons
,
filename
)
with
open
(
filename
,
'r'
)
as
f
:
obj_as_dict
=
json
.
load
(
f
)
return
cls
.
from_json
(
obj_as_dict
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment