主要代码展示
业务逻辑代码部分
"""
业务逻辑逻辑部分,多数据村粗的操作,在对应database时都封装、抽象为了业务逻辑需要的方法。
"""
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
conf_value.load_config('./config/config.ini')
self.conf_value = conf_value
self.database = DataBase(r'path')
self.database_info = self.database.get_database_info()
self.use_macs = self.database.get_use_macs()
self.init_ui()
self.add_signal()
@tryExcept
def init_ui(self):
省略...
cur_date = get_curdate()
self.pack_date.setText(cur_date)
last_packserial = self.database.get_last_packserial(cur_date)
if last_packserial is not None:
self.packSerial = last_packserial + 1
self.pack_serial.setText('%04d' % self.packSerial)
self.pack_info.setFocusPolicy(QtCore.Qt.NoFocus)
@tryExcept
def add_signal(self):
省略...
@tryExcept
def get_packinfo(self):
pack_serial = int(self.pack_serial.text())
pack_date = int(self.pack_date.text())
last_date = self.database.get_last_date()
last_packserial = self.database.get_last_packserial(last_date)
if pack_serial == last_packserial and last_date == pack_date:
cur_number = len(self.database.get_packinfo(pack_date, pack_serial))
self.cur_number.setValue(cur_number)
self.get_boxId()
handware_version = self.database.get_handware_version(pack_date, pack_serial)
self.hardware_version.setText(str(handware_version))
self.hardware_version.setDisabled(True)
packinfo = get_packinfo_head(self.boxId, handware_version, pack_date)
self.pack_info.setPlainText(packinfo)
pack_info = self.database.get_packinfo(pack_date, pack_serial)
for mac in pack_info:
self.pack_info.appendPlainText('%s' % mac)
else:
self.hardware_version.setDisabled(False)
self.pack_info.clear()
@tryExcept
def check(self):
if self.totalNumber <= self.curNumber:
省略...
return
if self.pack_date.isEnabled() is False:
mac = self.input_mac.text().strip()
if mac not in self.use_macs:
self.pack_info.appendPlainText('%s' % mac)
self.input_mac.clear()
pack_date = int(self.packDate)
pack_serial = int(self.packSerial)
self.database.record_packinfo(pack_date, pack_serial, self.hardVersion, mac)
cur_number = len(self.database.get_packinfo(pack_date, pack_serial))
self.cur_number.setValue(cur_number)
else:
省略...
@tryExcept
def get_boxId(self):
省略...
@tryExcept
def read_input(self):
self.get_boxId()
if int(self.packDate) < self.database.get_last_date():
省略...
return
last_packserail = self.database.get_last_packserial(self.packDate)
if int(self.packSerial) < 1 or (last_packserail is not None and int(self.packSerial) < last_packserail):
省略...
return
self.curNumber = int(self.cur_number.text())
self.totalNumber = int(self.total_number.text())
if VerifyInput.check_cur_number(self.curNumber, self.totalNumber) is False:
return
self.hardVersion = self.hardware_version.text().strip()
if self.hardware_version.isEnabled() is True:
packinfo = get_packinfo_head(self.boxId, self.hardVersion, self.packDate)
self.pack_info.setPlainText(packinfo)
self.disable_input()
@tryExcept
def disable_input(self):
省略...
@tryExcept
def enable_input(self):
省略...
if __name__ == '__main__':
app = QApplication(sys.argv)
myWindow = MainWindow()
myWindow.show()
sys.exit(app.exec())
数据存储部分
"""
根据业务逻辑实现的方法
get_use_macs
get_packinfo
get_handware_version
get_last_packserial
get_last_date
record_packinfo
"""
class DataBase:
def __init__(self, path):
self.path = path
with open(path) as f:
self._data = yaml.load(f)
if self._data is None:
self._data = {}
self._use_macs = set()
def get_database_info(self):
return self._data
def get_use_macs(self):
for pack_info in self._data.values():
for k in pack_info:
if k.startswith('serial'):
for mac in pack_info[k]:
self._use_macs.add(mac)
return self._use_macs
def get_packinfo(self, date, serial):
serail_name = 'serial%s' % serial
if date in self._data and serail_name in self._data[date]:
return self._data[date][serail_name]
else:
return None
def get_handware_version(self, date, serial):
ver_name = 'ver%s' % serial
return self._data[date][ver_name]
def get_last_packserial(self, date):
date = int(date)
packserial = []
if date in self._data:
for i in self._data[date].keys():
if i.startswith('serial'):
packserial.append(int(i.replace('serial', '')))
if len(packserial) != 0:
return max(packserial)
def get_last_date(self):
if self._data == {}:
return int(get_curdate())
return max(self._data.keys())
def write_mac(self, mac):
template = SPACE_8 + "- '%s'\n" % mac
with open(self.path, 'a+') as f:
f.write(template)
def write_packinfo(self, serial, ver, mac):
template = (SPACE_4 + "ver%s: '%s'\n" % (serial, ver))
template += (SPACE_4 + "serial%s:\n" % serial)
template += (SPACE_8 + "- '%s'\n" % mac)
with open(self.path, 'a+') as f:
f.write(template)
def write_headinfo(self, date, serial, ver, mac):
template = "%s:\n" % date
template += (SPACE_4 + "ver%s: '%s'\n" % (serial, ver))
template += (SPACE_4 + "serial%s:\n" % serial)
template += (SPACE_8 + "- '%s'\n" % mac)
with open(self.path, 'a+') as f:
f.write(template)
def record_packinfo(self, date, serial, ver, mac):
ver_name = 'ver%s' % serial
serail_name = 'serial%s' % serial
self._use_macs.add(mac)
print(self._data)
if date not in self._data:
self._data[date] = {ver_name: ver, serail_name: [mac]}
self.write_headinfo(date, serial, ver, mac)
elif date in self._data and serail_name not in self._data[date]:
self._data[date][ver_name] = ver
self._data[date][serail_name] = [mac]
self.write_packinfo(serial, ver, mac)
else:
self._data[date][serail_name].append(mac)
self.write_mac(mac)
print(self._data)
抽象封装理解
可以对目前的 DataBase 类抽象出一个上级父类,根据业务需求,在里面申明子类需要实现的方法,如:get_last_date
(返回存储数据中最后记录的日期),然后子类继承。根据使用不同的存储对象,如:文本、mysql等,在实现的返回中满足 get_last_date
业务同样的需求即可。在业务判断逻辑处理部分 self.database = DataBase(r'path')
,根据实际使用的存储对象简单替换,即可替换为如:mysql数据库存储。