<commit_before>from pyrpl import RedPitaya
from pyrpl.redpitaya_modules import NotReadyError
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np



APP = QtGui.QApplication.instance()
if APP is None:
    APP = QtGui.QApplication(["pyrpl_gui"])

def property_factory(module_widget, prop):
    if hasattr(module_widget.module, prop + 's'):
        new_prop = ComboProperty(prop, module_widget)
    elif hasattr(module_widget.module, prop[:-1] + 's'): # for instance inputs for input1
        new_prop = ComboProperty(prop, module_widget, prop[:-1] + 's')
    elif isinstance(getattr(module_widget.module, prop), bool):
        new_prop = BoolProperty(prop, module_widget)
    else:
        new_prop = NumberProperty(prop, module_widget)
    return new_prop

class BaseProperty(object):
    def __init__(self, name, module_widget):
        self.module_widget = module_widget
        self.name = name
        self.layout_v = QtGui.QVBoxLayout()
        self.label = QtGui.QLabel(name)
        self.layout_v.addWidget(self.label)
        self.module = self.module_widget.module
        self.set_widget()
        self.layout_v.addWidget(self.widget)
        self.module_widget.property_layout.addLayout(self.layout_v)
        self.module_widget.property_watch_timer.timeout.\
                                connect(self.update_widget)
    
    def update_widget(self):
        self.widget.blockSignals(True)
        self.update()
        self.widget.blockSignals(False)

class NumberProperty(BaseProperty):
    def set_widget(self):
        self.widget = QtGui.QDoubleSpinBox()
        self.widget.setDecimals(4)
        self.widget.setSingleStep(0.01)
        self.widget.valueChanged.connect(self.write)
    
    def write(self):
        setattr(self.module, self.name, self.widget.value())
        self.module_widget.property_changed.emit()

    def update(self):
        self.widget.setValue(float(getattr(self.module, self.name)))
        
            
class ComboProperty(BaseProperty):
    def __init__(self, name, module_widget, defaults=None):
        if defaults is not None:
            self.defaults = defaults
        else:
            self.defaults = name + 's'
        super(ComboProperty, self).__init__(name, module_widget)

    def set_widget(self):
        self.widget = QtGui.QComboBox()
        self.widget.addItems(map(str, self.options))
        self.widget.currentIndexChanged.connect(self.write)
    
    @property
    def options(self):
        return getattr(self.module, self.defaults)
    
    def write(self):
        setattr(self.module, self.name, str(self.widget.currentText()))
        self.module_widget.property_changed.emit()
        
    def update(self):
        index = self.options.index(getattr(self.module, self.name))
        self.widget.setCurrentIndex(index)
    
class BoolProperty(BaseProperty):
    def set_widget(self):
        self.widget = QtGui.QCheckBox()<commit_msg>resolves bug with numeric parameters refreshing<commit_after>from pyrpl import RedPitaya
from pyrpl.redpitaya_modules import NotReadyError
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np



APP = QtGui.QApplication.instance()
if APP is None:
    APP = QtGui.QApplication(["pyrpl_gui"])

def property_factory(module_widget, prop):
    if hasattr(module_widget.module, prop + 's'):
        new_prop = ComboProperty(prop, module_widget)
    elif hasattr(module_widget.module, prop[:-1] + 's'): # for instance inputs for input1
        new_prop = ComboProperty(prop, module_widget, prop[:-1] + 's')
    elif isinstance(getattr(module_widget.module, prop), bool):
        new_prop = BoolProperty(prop, module_widget)
    else:
        new_prop = NumberProperty(prop, module_widget)
    return new_prop

class BaseProperty(object):
    def __init__(self, name, module_widget):
        self.module_widget = module_widget
        self.name = name
        self.layout_v = QtGui.QVBoxLayout()
        self.label = QtGui.QLabel(name)
        self.layout_v.addWidget(self.label)
        self.module = self.module_widget.module
        self.set_widget()
        self.layout_v.addWidget(self.widget)
        self.module_widget.property_layout.addLayout(self.layout_v)
        self.module_widget.property_watch_timer.timeout.\
                                connect(self.update_widget)
    
    def update_widget(self):
        self.widget.blockSignals(True)
        self.update()
        self.widget.blockSignals(False)

class NumberProperty(BaseProperty):
    def set_widget(self):
        self.widget = QtGui.QDoubleSpinBox()
        self.widget.setDecimals(4)
        self.widget.setSingleStep(0.01)
        self.widget.valueChanged.connect(self.write)
    
    def write(self):
        setattr(self.module, self.name, self.widget.value())
        self.module_widget.property_changed.emit()

    def update(self):
        if not self.widget.isActiveWindow():
            self.widget.setValue(float(getattr(self.module, self.name)))
        
            
class ComboProperty(BaseProperty):
    def __init__(self, name, module_widget, defaults=None):
        if defaults is not None:
            self.defaults = defaults
        else:
            self.defaults = name + 's'
        super(ComboProperty, self).__init__(name, module_widget)

    def set_widget(self):
        self.widget = QtGui.QComboBox()
        self.widget.addItems(map(str, self.options))
        self.widget.currentIndexChanged.connect(self.write)
    
    @property
    def options(self):
        return getattr(self.module, self.defaults)
    
    def write(self):
        setattr(self.module, self.name, str(self.widget.currentText()))
        self.module_widget.property_changed.emit()
        
    def update(self):
        index = self.options.index(getattr(self.module, self.name))
        self.widget.setCurrentIndex(index)
    
class BoolProperty(BaseProperty):
    def set_widget(self):
        self.widget = QtGui.QCheckBox()
{'fraction_non_alphanumeric': 0.10396785534907081, 'fraction_numerical': 0.002343880796919471, 'mean_word_length': 2.8867924528301887, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2721814', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1782, 'n_words': 335}