Skip to content

Instantly share code, notes, and snippets.

@jj-github-jj
Created December 6, 2021 05:52
Show Gist options
  • Save jj-github-jj/044b50ad458689fa6bf9e8e27c285864 to your computer and use it in GitHub Desktop.
Save jj-github-jj/044b50ad458689fa6bf9e8e27c285864 to your computer and use it in GitHub Desktop.
tuple of tuples labview xy multiplot data to py class
from dataclasses import dataclass, is_dataclass
def nested_dataclass(*args, **kwargs):
def wrapper(cls):
cls = dataclass(cls, **kwargs)
original_init = cls.__init__
def __init__(self, *args, **kwargs):
for name, value in kwargs.items():
field_type = cls.__annotations__.get(name, None)
if is_dataclass(field_type) and isinstance(value, dict):
new_obj = field_type(**value)
kwargs[name] = new_obj
original_init(self, *args, **kwargs)
cls.__init__ = __init__
return cls
return wrapper(args[0]) if args else wrapper
from dataclasses import dataclass, is_dataclass
@dataclass
class xy_plot:
x:np.ndarray
y:np.ndarray
@nested_dataclass
class XYplots:
data:[xy_plot]
legend:[] #list
def xy_obj_from_tuple_of_tuples(s):
""" s is labview tuple of tuples represenation of xy plot
labview = win32com.client.Dispatch("Labview.Application")
VI = labview.getvireference(vi_path)
s=(VI.getcontrolvalue(control_name)) #returns array of clusters as tuple of tuples, no legend or other properties
control_name str is the name of the xy plot control or indicator
xy_obj[trace_index].x is x data, xy_obj[trace_index].y is ydata
"""
xy_obj=XYplots(data=[xy_plot(x=s[i][0],y=s[i][1]) for i in range(len(s))],legend=['trace'+str(i) for i in range(len(s))])
#modify xy_plots1.legend from another data
return(xy_obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment