try to cleanup stuff(3)

This commit is contained in:
2024-12-11 15:26:01 +01:00
parent 71a473d25d
commit 0fe59e9ad6
4 changed files with 195 additions and 58 deletions

View File

@@ -16,7 +16,7 @@ This contains a Widget to handle FixTargetFrames and fiducials, calculate final
import logging
_log=logging.getLogger(__name__)
import json, base64, yaml
import os, json, base64, yaml
import numpy as np
import pyqtUsrObj as UsrGO
import pyqtgraph as pg
@@ -47,6 +47,22 @@ class MyJsonEncoder(json.JSONEncoder):
return repr(obj)
return json.JSONEncoder.default(self, obj)
def iterencode(self, o, _one_shot=False):
list_lvl = 0
l=super().iterencode(o, _one_shot=_one_shot)
#l=tuple(l);print(''.join(l)) # helpful to debug
for s in l:
if s.startswith('['):
list_lvl += 1
if list_lvl > 0:
s = s[0]+s[1:].replace('\n', '').strip()
s = s.replace('\n', '').rstrip()
#self.item_separator):
#self.key_separator
if s.endswith(']'):
list_lvl -= 1
yield s
def MyJsonDecoder(dct):
if isinstance(dct, dict):
if '__class__' in dct:
@@ -173,28 +189,10 @@ class WndFixTarget(QWidget):
#bm_pos_eu=self._goBeamMarker._pos_eu
#bm_size_eu=self._goBeamMarker._size_eu
try:
#parse the parameters: 'key:value [,key:value]'
# as key value separator : and = are allowed
#examples:
#Fiducial:
# no param
#FixTarget(12.5x12.5), FixTarget(23.0x23.0), FixTarget(test):
# code_gen:2
# code_gen=3 | tmove=10 | twait=30
#Grid():
# size:(30, 20)
# cnt:(30, 22)
# fiducialSize:0.1
#SwissMX():
# ofs:(.2, .2)
# width:10
# fidScl:.02
# fiducial:((.1, .1), (.1, 2.7), (10.3, .1), (10.3, 2.7))
#SwissFEL():
# ofs:(.2, .2)
# width:10
# fidScl:.02
# fiducial:((.1, .1), (.1, 2.2), (10.3, .1), (10.3, 2.2))
#parse the parameters: as yaml string.
# allows : without space, allows () as []
# no {} to define a dictionary
# e.g. 'a:ggf,b:5,c:[5,6.1],d(8,9,3)'
param=param.replace(':', ': ') # allow gen:4 without space
param=yaml.safe_load(param) # "ofs":[10, 5],"width":200,"fidScl":0.5,"fiducial":[[18,7],[25,16],[70, 20]]
@@ -376,7 +374,10 @@ class WndFixTarget(QWidget):
#df = pd.DataFrame(data)
#df.to_csv(filename, float_format="%.6f")
#import numpy as np
ext=filename.rsplit('.',1)[1].lower()
base,ext=os.path.splitext(filename)
if not ext.lower():
ext='json'
filename=base+'.'+ext
try:
wnd=app._mainWnd
except AttributeError:
@@ -386,7 +387,7 @@ class WndFixTarget(QWidget):
grp=wnd._goTracked
data=grp.childItems()
if ext=='json':
if ext.lower()=='json':
with open(filename, 'w') as f:
json.dump(data, f,cls=MyJsonEncoder, indent=2)#separators=(',', ':')
else: