working json document type
This commit is contained in:
@@ -35,8 +35,9 @@ This contains a Widget to handle FixTargetFrames and fiducials, calculate final
|
||||
import logging
|
||||
_log=logging.getLogger(__name__)
|
||||
|
||||
import os, pickle, json, yaml,base64
|
||||
import json, base64 #, os, pickle, yaml
|
||||
import numpy as np
|
||||
import pyqtUsrObj as UsrGO
|
||||
import pyqtgraph as pg
|
||||
from PyQt5.QtCore import Qt, QFileInfo, pyqtSignal, pyqtSlot
|
||||
from PyQt5.QtWidgets import (
|
||||
@@ -75,29 +76,31 @@ class MyJsonEncoder(json.JSONEncoder):
|
||||
#return obj.tolist()
|
||||
elif isinstance(obj, set):
|
||||
return list(obj)
|
||||
if type(obj) not in (dict,list,str,int):
|
||||
_log.error('dont know how to json')
|
||||
return repr(obj)
|
||||
elif type(obj) not in (dict,list,str,int):
|
||||
try:
|
||||
return obj.obj2json(self)
|
||||
except AttributeError:
|
||||
_log.error('dont know how to json')
|
||||
return repr(obj)
|
||||
return json.JSONEncoder.default(self, obj)
|
||||
|
||||
def MyJsonDecoder(dct):
|
||||
if isinstance(dct, dict) and '__ndarray__' in dct:
|
||||
if isinstance(dct, dict):
|
||||
if '__class__' in dct:
|
||||
cls=dct.pop('__class__')
|
||||
cls=UsrGO.__dict__[cls]
|
||||
obj=cls.__new__(cls)
|
||||
obj.__init__(**dct)
|
||||
#try:
|
||||
# obj.json2obj(dct)
|
||||
#except AttributeError:
|
||||
# obj.__init__(**dct)
|
||||
return obj
|
||||
elif '__ndarray__' in dct:
|
||||
data = base64.b64decode(dct['__ndarray__'])
|
||||
return np.frombuffer(data[:-1], dct['dtype']).reshape(dct['shape'])
|
||||
return dct
|
||||
|
||||
def yaml_repr_numpy(dumper, data):
|
||||
return dumper.represent_scalar(u'!np_array', u'%s' % repr(data))
|
||||
yaml.add_representer(np.ndarray, yaml_repr_numpy)
|
||||
|
||||
def yaml_cnstr_numpy(loader, node):
|
||||
value = loader.construct_scalar(node)
|
||||
a=eval(value[6:-1])
|
||||
return np.array(a)
|
||||
yaml.add_constructor(u'!np_array', yaml_cnstr_numpy)
|
||||
|
||||
|
||||
|
||||
|
||||
class MarkerDelegate(QItemDelegate):
|
||||
def createEditor(self, parent, option, index):
|
||||
@@ -263,8 +266,8 @@ class WndFixTarget(QWidget):
|
||||
#data_folder = cfg.value("folders/last_prelocation_folder", def_folder)
|
||||
data_folder=''
|
||||
if filename is None:
|
||||
filename, _ = QFileDialog.getOpenFileName(self,"Load yaml data file",None,
|
||||
"yaml files (*.yaml);;json files (*.json);;pickle files (*.pkl);;text files (*.txt);;all files (*)",)
|
||||
filename, _ = QFileDialog.getOpenFileName(self,"Load data file",None,
|
||||
"json files (*.json);;yaml files (*.yaml);;pickle files (*.pkl);;text files (*.txt);;all files (*)",)
|
||||
if not filename: # cancelled dialog
|
||||
return
|
||||
|
||||
@@ -350,8 +353,8 @@ class WndFixTarget(QWidget):
|
||||
#data_folder = settings.value("folders/last_prelocation_folder")
|
||||
data_folder=''
|
||||
if filename is None:
|
||||
filename, _ = QFileDialog.getSaveFileName(self,"Soad yaml data file",data_folder,
|
||||
"yaml files (*.yaml);;json files (*.json);;pickle files (*.pkl);;text files (*.txt);;all files (*)",)
|
||||
filename, _ = QFileDialog.getSaveFileName(self,"Save data file",data_folder,
|
||||
"json files (*.json);;yaml files (*.yaml);;pickle files (*.pkl);;text files (*.txt);;all files (*)",)
|
||||
if not filename:
|
||||
return
|
||||
|
||||
@@ -370,7 +373,7 @@ class WndFixTarget(QWidget):
|
||||
yaml.dump(self._data, f)
|
||||
elif ext=='json':
|
||||
with open(filename, 'w') as f:
|
||||
json.dump(self._data, f,cls=MyJsonEncoder)
|
||||
json.dump(self._data, f,cls=MyJsonEncoder, indent=2)#separators=(',', ':')
|
||||
print(self._data)
|
||||
|
||||
def delete_selected(self):
|
||||
@@ -725,7 +728,6 @@ void itemSelectionChanged()
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
import pyqtUsrObj as UsrGO
|
||||
|
||||
class Monster(yaml.YAMLObject):
|
||||
yaml_tag = u'!Monster'
|
||||
|
||||
Reference in New Issue
Block a user