python: more Layer Architecture and its Implementation in Python

2023-11-12

sql server:

--学生表
DROP TABLE DuStudentList
GO
create table DuStudentList
(
     StudentId INT IDENTITY(1,1) PRIMARY KEY,
     StudentName nvarchar(50),
     StudentNO varchar(50),                    --学号
     StudentBirthday datetime                  --学生生日     
  
)
go

model

"""
StudentListInfo.py
学生类
date 2023-06-16
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
"""
 
import datetime
from datetime import date
import sys
import os
import Common
 
class StudentList(object):
    """
    学生实体类
    """
 
 
 
    def __init__(self,StudentId:int,StudentName:str, StudentNO:str,StudentBirthday:datetime.datetime):
        """
 
        :param StudentName:
        :param StudentNO:
        :param StudentBirthday:
        """
        self._StudentName=StudentName
        self._StudentNO=StudentNO
        self._StudentBirthday=StudentBirthday
        self._StudentId=StudentId
        self._age=0 #date.today().year-StudentBirthday.year #date.today().year-StudentBirthday.year #Common.Commond.calculateAge(date(StudentBirthday.year,StudentBirthday.month,StudentBirthday.day))
 
    def __init__(self,StudentId:int,StudentName:str, StudentNO:str,StudentBirthday:datetime.datetime,age:int):
        """
 
        :param StudentName:
        :param StudentNO:
        :param StudentBirthday:
        """
        self._StudentName=StudentName
        self._StudentNO=StudentNO
        self._StudentBirthday=StudentBirthday
        self._StudentId=StudentId
        self._age=age #date.today().year-StudentBirthday.year #date.today().year-StudentBirthday.year #Common.Commond.calculateAge(date(StudentBirthday.year,StudentBirthday.month,StudentBirthday.day))
 
    def __del__(self):
        """
 
        :return:
        """
        print(f"{self._StudentName}")
 
    def setStudentName(self,StudentName):
        """
 
        :param StudentName:
        :return:
        """
        self._StudentName = StudentName
 
 
    def getStudentName(self):
        """
 
        :return:
        """
        return self._StudentName
 
    def setStudentNO(self,StudentNO):
        """
 
        :param StudentNO:
        :return:
        """
        self._StudentNO=StudentNO
 
 
    def getStudentNO(self):
        """
 
        :return:
        """
        return self._StudentNO
 
    def setStudentId(self,StudentId):
        """
 
        :param StudentId:
        :return:
        """
        self._StudentId=StudentId
 
 
    def getStudentId(self):
        """
 
        :return:
        """
        return  self._StudentId
 
    def setStudentBirthday(self,StudentBirthday):
        """
 
        :param StudentBirthday:
        :return:
        """
        self._StudentBirthday = StudentBirthday
        dage =date.today().year-StudentBirthday.year# Common.Commond.calculate_age(StudentBirthday)
        self._age=dage
 
 
    def getStudentBirthday(self):
        """
 
        :return:
        """
        return self._StudentBirthday
 
    def setAge(self,age):
        """
 
        :param age:
        :return:
        """
        dage=1 #Common.Commond.calculate_age(StudentBirthday)
        self._age = age
 
 
    def getAge(self):
        """
 
        :return:
        """
        return self._age
 
    def __str__(self):
        """
 
        :return:
        """
        return f"{self._StudentId},{self._StudentName},{self._StudentNO},{self._StudentBirthday}{self._age}"

DAL

"""
StudentDALListDAL.py
数据业务处理层 Data Access Layer (DAL)
SQL Server 数据库操作
date 2023-06-21
edit: Geovin Du,geovindu, 涂聚文
ide: PyCharm 2023.1 python 11
"""
 
import os
import sys
from pathlib import Path
import re
import pymssql  #sql server
import Model.StudentListInfo
import UtilitieDB.MsSQLHelper
import Interface.IStudentList
 
class StudentDal(Interface.IStudentList.IStudentList):
    """
    数据业务处理层  学生
    数据库连接可以放在这里,通过配置读取数据连接参数
    """
 
    def __init__(self):
        """
        构造函数,方法
        :param strserver:
        :param struser:
        :param strpwd:
        :param strdatabase:
        """
        self._strserver = ""
        self._struser = ""
        self._strpwd = ""
        self._strdatabase =""
 
    def selectSql(self):
        """
        查询数据 self._strserver, self._struser, self._strpwd, self._strdatabase
        :return:
        """
 
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper()
        row=myms.execute('select *,DATEDIFF(hour,StudentBirthday,GETDATE())/8766 as Age from DuStudentList;')
        return row
 
    def selectSqlOrder(self,order:str)->list:
        """
 
        :param order:  studenName desc/asc
        :return:
        """
        students=[]
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper()
        strsql=f"select * from DuStudentList order by {order};"
        row=myms.execute(f'select *,DATEDIFF(hour,StudentBirthday,GETDATE())/8766 as Age from DuStudentList order by {order};')
        return row
 
    def selectIdSql(self,StudentId:int):
        """
 
        :param StudentId: 主键ID
        :return:
        """
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper()
        row=myms.execute(f'select * from DuStudentList where StudentId={StudentId};')
        return row
    def selectProc(self):
        """
        存储过程
        :return:
        """
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper()
        args = ()
        row = myms.executeCallProc("proc_Select_StudentListAll",args)
        return row
 
    def selectIdProc(self,StudentId:int):
        """
        存储过程
        :param StudentId: 主键ID
        :return:
        """
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper()
        args = (StudentId,)
        row = myms.executeCallProc('dbo.proc_Select_StudentList', args)
        return row
 
    def addSql(self,info:Model.StudentListInfo.StudentList):
        """
        添加,要考虑添加返回ID值
        :param info:学生实体类
        :return:
        """
        myms=UtilitieDB.MsSQLHelper.MsSqlHelper();
        column=("StudentName","StudentNO","StudentBirthday")
        vales=[info.getStudentName(),info.getStudentNO(),info.getStudentBirthday()]
        myms.insertByColumnaAndValues("dbo.DuStudentList",column,vales)
 
    def addProc(self,info:Model.StudentListInfo.StudentList):
        """
        添加,要考虑添加返回ID值
        :param info:学生实体类
        :return:
        """
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper();
        args=(info.getStudentName(),info.getStudentNO(),info.getStudentBirthday())
        myms.insertCallProc("dbo.proc_Insert_StudentList",args)
 
    def addOutProc(self,info:Model.StudentListInfo.StudentList):
        """
        添加,要考虑添加返回ID值
        :param info:学生实体类
        :return: 返回增加的学生的ID
        """
        id=0
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper();
        outid = pymssql.output(int)
        args = (info.getStudentName(), info.getStudentNO(), info.getStudentBirthday(),outid)
        print(args)
        id=myms.insertOutCallProc("dbo.proc_Insert_StudentListOutput", args)
        return id
    def editSql(self,info:Model.StudentListInfo.StudentList):
        """
 
        :param info:学生实体类
        :return:
        """
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper();
        args = {"StudentName":f"{info.getStudentName()}","StudentNO":f"{info.getStudentNO()}","StudentBirthday":f"{info.getStudentBirthday()}"}  #"StudentId":6
        where = f"StudentId={info.getStudentId()}" #
        #print(args,where)
        myms.updateByKeyValues("DuStudentList",where,args)
 
    def editProc(self, info: Model.StudentListInfo.StudentList):
        """
 
        :param info: 学生实体类
        :return:
        """
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper();
        args = (info.getStudentId(),info.getStudentName(),info.getStudentNO(),info.getStudentBirthday())
        myms.updateProc("[dbo].[proc_Update_StudentList]",args)
 
 
    def delSql(self,StudentId:int):
        """
        sql语句删除
        :param StudentId: 主键ID
        :return:
        """
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper();
        where={f"StudentId":StudentId}
        myms.deleteByKeyValues("DuStudentList",where)
 
    def delProc(self, studentId):
        """
        删除 存储过程 删除多个ID,后面增加
        :param StudentId: 主键ID
        :return:
        """
        myms = UtilitieDB.MsSQLHelper.MsSqlHelper();
        args =studentId
        myms.deleteProc("dbo.proc_Delete_StudentList",args)

IDAL

"""
IStudentList.py
接口层 Interface Data Access Layer
IDAL(Interface Data Access Layer)DAL的接口层
date 2023-06-19
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
"""
from __future__ import annotations
from abc import ABC, abstractmethod
import os
import sys
import Model.StudentListInfo
 
class IStudentList(ABC):
    """
 
    """
 
    @classmethod
    def __subclasshook__(cls, subclass):
        return (hasattr(subclass, 'load_data_source') and
                callable(subclass.load_data_source) and
                hasattr(subclass, 'extract_text') and
                callable(subclass.extract_text) or
                NotImplemented)
 
    @abstractmethod
    def selectSql(self):
        """
 
        :return:
        """
        pass
 
    @abstractmethod
    def selectSqlOrder(self, order: str) -> list:
        """
 
        :param order:
        :return:
        """
        pass
 
    @abstractmethod
    def selectIdSql(self, StudentId: int):
        """
 
        :param StudentId:
        :return:
        """
        pass
 
    @abstractmethod
    def selectProc(self):
        """
 
        :return:
        """
        pass
 
    @abstractmethod
    def selectIdProc(self, StudentId: int):
        """
 
        :param StudentId:
        :return:
        """
        pass
 
    @abstractmethod
    def addSql(self, info: Model.StudentListInfo.StudentList):
        """
 
        :param info:
        :return:
        """
        pass
 
    @abstractmethod
    def addProc(self, info: Model.StudentListInfo.StudentList):
        """
 
        :param info:
        :return:
        """
        pass
 
    @abstractmethod
    def addOutProc(self, info: Model.StudentListInfo.StudentList):
        """
 
        :param info:
        :return:
        """
        pass
 
    @abstractmethod
    def editSql(self, info: Model.StudentListInfo.StudentList):
        """
 
        :param info:
        :return:
        """
        pass
 
    @abstractmethod
    def editProc(self, info: Model.StudentListInfo.StudentList):
        """
 
        :param info:
        :return:
        """
        pass
 
    @abstractmethod
    def delSql(self, StudentId: int):
        """
 
        :param StudentId:
        :return:
        """
        pass
 
    @abstractmethod
    def delProc(self, studentId):
        """
 
        :param studentId:
        :return:
        """
        pass

BLL

"""
StudentListBLL.py
业务层 Business Logic Layer (BLL)
date 2023-06-19
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
"""
 
import os
import sys
from pathlib import Path
import re
import pymssql  #sql server
from datetime import date
import DAL.StudentListDAL
#import DAL.ConfigDAL
import Model.StudentListInfo
import Interface.IStudentList
import Factory.AbstractFactory
 
class StudentBll(object):
    """
    学生信息操作业务类
    """
 
 
    dal=Factory.AbstractFactory.AbstractFactory.createStudentList
    #dal =DAL.StudentListDAL.StudentDal() #Factory.AbstractFactory.AbstractFactory.createStudentList()#
    """
    类属性 操作DAL
    """
    def __init__(self):
        """
 
        """
        self._name = "geovindu"
 
 
 
    def __del__(self):
        print(f"{self._name}挂失了")
 
    def selectSql(cls)->list:
        """
        元组数据
        :return: list 学生列表
        """
        students = []
 
 
        data = cls.dal().selectSql()
        stus = list(data)  # 如C# 强制转换
        '''
        for a in data:
            for i in a:
                print("II",i[0],i[1],i[2],i[3],i[4])
        '''
        #print(stus)
        for ii in stus:
            for i in ii:
                students.append(Model.StudentListInfo.StudentList(i[0],i[1],i[2],i[3],i[4]))
        return students
 
    def selectSqlOrder(cls, order: str)->list:
        """
        元组数据
        :param order: studenName desc/asc
        :return:
        """
        studentsorder = []
        students=[]
        data = cls.dal().selectSqlOrder(order)
        (studentsorder) = data # 如C# 强制转换
        '''
        for i in range(len(studentsorder)):
            print("rrr",type(studentsorder[i]))
            for duobj in studentsorder[i]:
                print(type(duobj))
                print(duobj)
        '''
        for obj in studentsorder:
            for i in obj:
                students.append(Model.StudentListInfo.StudentList(i[0], i[1], i[2], i[3],i[4]))
        return students
 
 
    def selectIdSql(cls,StudentId:int)->list:
        """
 
        :param StudentId:学生ID
        :return:
        """
        students = []
        data = cls.dal().selectIdSql(StudentId)
        students=data
        for ii in students:
            for i in ii:
                students.append(Model.StudentListInfo.StudentList(i[0],i[1],i[2],i[3]))
        return students
 
 
    def selectProc(cls):
        """
 
        :return:
        """
        students=[]
        data = cls.dal().selectProc()
        for i in data:
            students.append(Model.StudentListInfo.StudentList(i[0],i[1],i[2],i[3],i[4]))
        return students
 
    def selectIdProc(cls,StudentId:int)->list:
        """
 
        :param StudentId:
        :return:
        """
        students = []
        data = cls.dal().selectIdProc(StudentId)
        for i in data:
            students.append(Model.StudentListInfo.StudentList(i[0],i[1],i[2],i[3]))
        return students
 
    def addSql(cls,info:Model.StudentListInfo.StudentList):
        """
 
        :param info:学生实体类
        :return:
        """
        cls.dal.addSql(info)
 
    def addProc(cls,info:Model.StudentListInfo.StudentList):
        """
 
        :param info:学生实体类
        :return:
        """
        #print(info)
        cls.dal().addProc(info)
 
    def addOutProc(cls,info:Model.StudentListInfo.StudentList)->int:
        """
 
        :param info: 学生实体类
        :return: 返回增加的学生ID
        """
        print(info)
        return cls.dal().addOutProc(info)
 
    def editSql(cls,info:Model.StudentListInfo.StudentList):
        """
 
        :param info:学生实体类
        :return:
        """
        #print(info)
        cls.dal().editSql(info)
 
    def editProc(cls, info: Model.StudentListInfo.StudentList):
        """
 
        :param info:学生实体类
        :return:
        """
        cls.dal().editProc(info)
 
    def delSql(cls, StudentId: int):
        """
 
        :param StudentId:
        :return:
        """
        cls.dal().delSql(StudentId)
 
    def delProc(cls, StudentId):
        """
 
        :param StudentId:
        :return:
        """
        cls.dal().delProc(StudentId)

UI

"""
StudentUI.py
读文件类
date 2023-06-24
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
 
"""
 
import datetime
import sys
import os
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
from ttkbootstrap import Style  # pip install ttkbootstrap
import random
import Model.StudentListInfo
import BLL.StudentListBLL
 
class StudentUi(object):
 
    global tree
    stubll = BLL.StudentListBLL.StudentBll()
 
    def __init__(self):
        self.name="geovindu"
 
    def __del__(self):
        print(f"{self.name}")
 
    def delete(cls):
        #global tree
        curItem = cls.tree.focus()
        val=cls.tree.item(curItem)['values'][0]  #id
        print(val)
        print(cls.tree.selection())
        cls.tree.delete(cls.tree.selection())
        cls.stubll.delSql(val)  #需要删除关联的数据才可以删除
 
        #cls.stubll.delSql()
 
    def main(cls):
        """
        窗体绑定数据
        :return:
        """
 
 
        style=Style(theme='darkly') #定义窗口样式
        window=style.master
        window.title("学生管理")
        # win = Tk()
        screenWidth = window.winfo_screenwidth()
        screenHeight = window.winfo_screenheight()
        width=100
        height=600
        x=int((screenWidth-width)/2)
        y=int((screenHeight-height)/2)
        window.geometry('{}x{}+{}+{}'.format(width,height,x,y))
        #Treeview 控件
        cls.tree=ttk.Treeview(master=window,style='success.Treeview',height=25,show='headings')
        cls.tree.pack()
        #定义列
        cls.tree['columns']=("StudentId","StudentName","StudentNO","StudentBirthday","Age")
        #设置列属性,列不显示
        cls.tree.column("StudentId",width=150,minwidth=100,anchor=S)
        cls.tree.column("StudentName", width=150, minwidth=100, anchor=S)
        cls.tree.column("StudentNO", width=150, minwidth=100, anchor=S)
        cls.tree.column("StudentBirthday", width=150, minwidth=100, anchor=S)
        cls.tree.column("Age", width=150, minwidth=100, anchor=S)
        #设置表头
        cls.tree.heading("StudentId",text="序号")
        cls.tree.heading("StudentName", text="姓名")
        cls.tree.heading("StudentNO", text="学号")
        cls.tree.heading("StudentBirthday", text="出生日期")
        cls.tree.heading("Age", text="年龄")
        # stubll = BLL.StudentListBLL.StudentBll()
        geovindu = cls.stubll.selectSqlOrder("Age asc")  # list()
        #treeView控件绑定数据
        i=1
        for Model.StudentListInfo.StudentList in geovindu:
            cls.tree.insert("",i,text="2",values=(Model.StudentListInfo.StudentList.getStudentId(),Model.StudentListInfo.StudentList.getStudentName(),Model.StudentListInfo.StudentList.getStudentNO(),Model.StudentListInfo.StudentList.getStudentBirthday(),Model.StudentListInfo.StudentList.getAge()))
            i+=1
        #删除按钮
        ttk.Button(window,text="删除",style='success,TButton',command=cls.delete).pack(side='left',padx=5,pady=10)
        window.mainloop()

输出“

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python: more Layer Architecture and its Implementation in Python 的相关文章

随机推荐

  • C++运算符重载(简单易懂)

    一 运算符重载 对已有的运算符进行重新定义 赋予另一种功能以适用不同的数据类型 1 加号运算符 重载 创建类 Person class Person public 1 成员函数重载 号 本质 Person p3 p1 operator p2
  • 结构体(struct)继承——[C++语言中]

    在C 语言中 struct对C语言中的strcut进行了扩充 已经不仅仅是一个包含不同数据类型的数据结构体了 在C 语言中 strcut可以包含成员函数 可以实现继承 可以实现多态 在C 语言中 结构体struct与类class的最本质区别
  • JavaScript设计模式(二)——简单工厂模式、抽象工厂模式、建造者模式

    个人简介 个人主页 前端杂货铺 学习方向 主攻前端方向 正逐渐往全干发展 个人状态 研发工程师 现效力于中国工业软件事业 人生格言 积跬步至千里 积小流成江海 推荐学习 前端面试宝典 Vue2 Vue3 Vue2 3项目实战 Node js
  • Git查看和编辑配置并设置默认编辑器为VSCode

    1 查看已有配置 使用命令 git config list show origin 查看已有的配置 file D Git etc gitconfig diff astextplain textconv astextplain file D
  • 如何剪辑视频?方法来了,零基础也能学会!

    视频怎么剪辑呀 刚刚用录屏软件录制了一段视频 但是录进去了很多不需要的画面 需要进行修改 可是不知道视频怎么剪辑 有没有人知道剪辑视频的方法 推荐一下 剪辑视频是一门重要的技能 无论是在日常生活中还是工作中 都需要用到 从个人Vlog到专业
  • 给我写一段nginx代码

    server listen 80 server name example com location root var www example index index html index htm
  • (Struts2学习篇)Struts2标签库(表单标签)

    一 jsp页面代码 index jsp
  • CSS实现DIV的水平与垂直居中

    使用CSS样式实现DIV的水平与垂直居中 1 使用 div 标签的 align 属性实现水平居中 HTML中的 div 标签的 align 属性用于规定 div 元素中的内容的水平对齐方式 所有浏览器都支持 align 属性 语法 div
  • prometheus中常用的查询

    prometheus server 可以通过HTTPAPI的方式进行查询 官网链接https prometheus io docs prometheus latest querying basics 我这边主要用到的是实时查询 当然prom
  • Mybatis注意小笔记

    映射文件中的namespace是用于绑定Dao接口的 即面向接口编程 1 接口式编程 原生 Dao gt DaoImpl mybatis Mapper gt xxMapper xml 2 SqlSession代表和数据库的一次会话 用完必须
  • Leetcode617. 合并二叉树(C语言)

    Leetcode617 合并二叉树 C语言 数据结构 树 算法与数据结构参考 题目 给定两个二叉树 将它们中的一个覆盖到另一个上时 两个二叉树的一些节点便会重叠 如果两个节点重叠 那么将他们的值相加作为节点合并后的新值 否则不为 NULL
  • 想学设计模式、想搞架构设计,先学学 UML 系统建模吧

    UML 系统建模 1 概述 1 1 课程概述 汇集 UML 及其相关的一些话题 回顾 UML 相关的符号与概念 以电商订单相关业务为例 借助 UML 完成系统建模 将 UML 变成提升建模效率 表达架构思想的工具 1 2 什么是 UML U
  • 二分查找C++实现

    非递归方式代码 区间范围 left right 左闭右闭 时间复杂度为O logn include
  • Debian 安装 ldac

    需要将系统本来的PulseAudio ALSA JACK同时更换到 pipewire 如果没有支持LDAC aptX的设备不建议折腾 系统 Debian 11 Gnome 声卡 Intel HDA 蓝牙 支持蓝牙5 0 1 更换PulseA
  • 终止代码:DRIVER_IRQL_NOT_LESS_OR_EQUAL 失败的操作:CH341S64.SYS

    终止代码 DRIVER IRQL NOT LESS OR EQUAL 失败的操作 CH341S64 SYS Python串口程序致使电脑蓝屏自动重启 CH430驱动有问题 Python串口程序致使电脑蓝屏自动重启 最近在做一个项目 其中有一
  • Redis入门——Key、五大数据类型图文详解

    文章目录 1 Redis简介 2 Redis常见命令 3 Redis Key关键字 4 五大数据类型简介 4 1 String 字符串 4 2 List 列表 4 3 Set 4 3 Hash 4 4 ZSet 1 Redis简介 Redi
  • 什么是索引?MySQL常见的几种索引类型和原理

    来源 素文宅博客 地址 https blog yoodb com yoodb article detail 1536 在关系数据库中 索引是一种单独的 物理的对数据库表中一列或多列的值进行排序的一种存储结构 它是某个表中一列或若干列值的集合
  • Java---JUC并发篇(多线程详细版)

    Java 多线程 1 并发基础 线程篇 1 1 java线程状态及线程状态之间的转化 1 2 操作系统层面有5种状态 2 线程池的核心参数 7个核心参数 3 sleep与wait方法对比 4 lock锁与synchronized锁区别 5
  • docker启动容器及启动一个挂载数据卷的容器

    在docker运行容器前可以先使用docker images列出本地镜像 docker运行容器前需要本地存在对应的镜像 如果本地不存在该镜像 Docker会从镜像仓库下载此镜像 先创建 usr share nginx html 目录 创建数
  • python: more Layer Architecture and its Implementation in Python

    sql server 学生表 DROP TABLE DuStudentList GO create table DuStudentList StudentId INT IDENTITY 1 1 PRIMARY KEY StudentName