Skip to content

Commit 8cf55be

Browse files
committed
refactor(core): refactored json encoder and decoder.
1 parent 8c4c045 commit 8cf55be

File tree

7 files changed

+18
-22
lines changed

7 files changed

+18
-22
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Passed with test_instrument_driver version v0.1.0 tested on pyscan version v0.5.1 at 2024-06-18 14:48:00
1+
Passed with test_instrument_driver version v0.1.0 tested on pyscan version v0.5.3 at 2024-07-05 12:02:04
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Passed with test_voltage version v0.1.0 tested on pyscan version v0.5.1 at 2024-06-18 14:48:00
1+
Passed with test_voltage version v0.1.0 tested on pyscan version v0.5.3 at 2024-07-05 12:02:04

pyscan/general/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# objects
22
from .item_attribute import ItemAttribute
3-
from .json_encoder import CustomJSONEncoder
3+
from .json_encoder import PyscanJSONEncoder
44

55
# methods
66
from .d_range import drange
@@ -12,4 +12,4 @@
1212
from .set_difference import set_difference
1313
from .stack_or_append import stack_or_append
1414
from .get_pyscan_version import get_pyscan_version
15-
from .json_decoder import CustomJSONDecoder
15+
from .json_decoder import PyscanJSONDecoder

pyscan/general/json_decoder.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33

44

5-
class CustomJSONDecoder(json.JSONDecoder):
5+
class PyscanJSONDecoder(json.JSONDecoder):
66
def __init__(self, *args, **kwargs):
77
super().__init__(object_hook=self.item_attribute_object_hook, *args, **kwargs)
88

@@ -21,10 +21,7 @@ def item_attribute_object_hook(self, data):
2121
ItemAttribute
2222
'''
2323
if type(data) is dict:
24-
new_data = ItemAttribute()
25-
26-
for key, value in data.items():
27-
new_data[key] = value
24+
new_data = ItemAttribute(data)
2825
else:
2926
new_data = data
3027

pyscan/general/json_encoder.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pathlib import Path, WindowsPath
77

88

9-
class CustomJSONEncoder(json.JSONEncoder):
9+
class PyscanJSONEncoder(json.JSONEncoder):
1010
"""
1111
A custom JSON encoder subclass that extends json.JSONEncoder to handle additional Python data types
1212
not supported by the default JSON encoder. This includes handling of custom objects, numpy data types,
@@ -36,23 +36,22 @@ def default(self, obj, debug=False):
3636
# keys_to_skip = {'logger', 'expt_thread', 'data_path', 'instrument', 'module_id_string', 'spec'}
3737

3838
if type(obj) is type:
39-
return f"<class '{obj.__name__}'>"
39+
return obj.__name__
4040
elif isinstance(obj, (InstrumentDriver, ItemAttribute)):
4141
if debug is True:
4242
print(f"obj {obj} was instance of InstrumentDriver and or ItemAttribute.")
4343
return obj.__dict__
44-
elif isinstance(obj, range):
44+
elif isinstance(obj, (range, tuple)):
4545
if debug is True:
46-
print(f"obj {obj} was instance of range.")
46+
print(f"obj {obj} was instance of {type(obj)}.")
4747
return list(obj)
4848
# Handle numpy integers
49-
elif isinstance(obj, (np.integer, np.int_, np.intc, np.intp, np.int8, np.int16, np.int32, np.int64,
50-
np.uint8, np.uint16, np.uint32, np.uint64)):
49+
elif isinstance(obj, np.integer):
5150
if debug is True:
5251
print(f"Object {obj} is a numpy integer, converting to int.")
5352
return int(obj)
5453
# Handle numpy floating values
55-
elif isinstance(obj, (np.floating, np.float16, np.float32, np.float64)):
54+
elif isinstance(obj, np.floating):
5655
if debug is True:
5756
print(f"Object {obj} is a numpy floating value, converting to float.")
5857
return float(obj)

pyscan/measurement/abstract_experiment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pyscan.general import (ItemAttribute,
99
is_list_type)
1010
from pyscan.measurement.scans import PropertyScan, RepeatScan
11-
from pyscan.general.json_encoder import CustomJSONEncoder
11+
from pyscan.general.json_encoder import PyscanJSONEncoder
1212

1313

1414
class AbstractExperiment(ItemAttribute):
@@ -227,8 +227,8 @@ def save_metadata(self):
227227
save_name = str(save_path.absolute())
228228

229229
with h5py.File(save_name, 'a') as f:
230-
f.attrs['runinfo'] = json.dumps(self.runinfo, cls=CustomJSONEncoder)
231-
f.attrs['devices'] = json.dumps(self.devices, cls=CustomJSONEncoder)
230+
f.attrs['runinfo'] = json.dumps(self.runinfo, cls=PyscanJSONEncoder)
231+
f.attrs['devices'] = json.dumps(self.devices, cls=PyscanJSONEncoder)
232232

233233
def start_thread(self):
234234
'''Starts experiment as a background thread, this works in conjunction with live plot

pyscan/measurement/load_experiment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55
from pathlib import Path
66
from pyscan.general import ItemAttribute
7-
from pyscan.general.json_decoder import CustomJSONDecoder
7+
from pyscan.general.json_decoder import PyscanJSONDecoder
88

99

1010
def load_experiment(file_name):
@@ -56,9 +56,9 @@ def load_experiment(file_name):
5656
expt = ItemAttribute()
5757

5858
f = h5py.File('{}'.format(file_name), 'r')
59-
expt.runinfo = json.loads(f.attrs['runinfo'], cls=CustomJSONDecoder)
59+
expt.runinfo = json.loads(f.attrs['runinfo'], cls=PyscanJSONDecoder)
6060

61-
expt.devices = json.loads(f.attrs['devices'], cls=CustomJSONDecoder)
61+
expt.devices = json.loads(f.attrs['devices'], cls=PyscanJSONDecoder)
6262

6363
for key, value in f.items():
6464
expt[key] = (f[key][:]).astype('float64')

0 commit comments

Comments
 (0)