首页 > 编程语言 >python: How to Create a Python Package

python: How to Create a Python Package

时间:2023-06-23 09:11:25浏览次数:45  
标签:return stu python Create param How ._ self def

上篇博文的两个类文件,拖着一个创建好的包名Model中,有些代码会自己生成变化

"""
StudentScoreInfo.py
学生成绩类
date 2023-06-16
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
"""
import datetime
import sys
import os

class StudentScore(object):
    """
    学生成绩类
    """

    def __init__(self,ScoreId:int,StudentId:int,CourseId:int,Score:float):
        """
        构造函数(方法)
        :param ScoreId: ID
        :param StudentId:学生ID
        :param CourseId:课程ID
        :param Score:成绩ID
        """
        self._StudentId = StudentId
        self._CourseId = CourseId
        self._Score = Score
        self._ScoreId = ScoreId

    '''
    def __del__(self):
        """

        :return:
        """
        print(f"{self._StudentId}")
    '''
    def setStudentId(self,StudentId):
        """

        :param StudentId:
        :return:
        """
        self._StudentId=StudentId

    def getStudentId(self):
        """

        :return:
        """
        return self._StudentId

    def setCourseId(self,CourseId):
        """

        :param CourseId:
        :return:
        """
        self._CourseId=CourseId

    def getCourseId(self):
        """

        :return:
        """
        return self._CourseId

    def setScore(self,Score):
        """

        :param Score:
        :return:
        """
        self._Score=Score

    def getScore(self):
        """

        :return:
        """
        return self._Score

    def setScoreId(self,ScoreId):
        """

        :param ScoreId:
        :return:
        """
        self._ScoreId=ScoreId

    def getScoreId(self):
        """

        :return:
        """
        return  self._ScoreId

    def __str__(self):
        """

        :return:
        """
        return  f"{self._ScoreId},{self._StudentId},{self._Score},{self._CourseId}"


"""
StudentScoreListInfo.py
学生成绩视图类
date 2023-06-21
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11
"""
from Model import StudentScoreInfo


class StudentScoreList(StudentScoreInfo.StudentScore):  #继承学生成绩实体类,也可以继承多个类
    """
    学生成绩视图类 ,ScoreId:int,StudentId:int,CourseId:int,Score:float,CourseName:str,StudentNO:str,StudentName:str,StudentBirthday:datetime.datetime,Age:int
    ,ScoreId:int,StudentId:int,CourseId:int,Score:float,  ,CourseName:str,StudentNO:str,StudentName:str,StudentBirthday:datetime.datetime,Age:int
    """
    def __init__(self): #,CourseName:str,StudentNO:str,StudentName:str,StudentBirthday:datetime.datetime,Age:int,totalScore:float
        """

        :param CourseName:课程名称
        :param StudentNO:学生编号
        :param StudentName:学生姓名
        :param StudentBirthday:出生日期
        :param Age: 年龄
        :param totalScore: 总成绩
        """
        #super().__init__(ScoreId, StudentId,CourseId,Score)
        self._CourseName=""#CourseName
        self._StudentNO=""#StudentNO
        self._StudentName=""#StudentName
        self._StudentBirthday=""#StudentBirthday
        self._Age=0#Age

        #self._totalScore =0#totalScore

    '''
    def setScoreId(self,ScoreId):
        self._ScoreId=ScoreId

    def getScoreId(self):
        return self._ScoreId

    def setStudentId(self,StudentId):
        self._StudentId=StudentId

    def getStudentId(self):
        return self._StudentId

    def setCourseId(self,CourseId):
        self._CourseId=CourseId

    def getCourseId(self):
        return self._CourseId

    def setScore(self,Score):
        self._Score=Score

    def getScore(self):
        return self._Score

    '''
    def setCourseName(self,CourseName):
        """

        :param CourseName:
        :return:
        """
        self._CourseName=CourseName

    def getCourseName(self):
        """

        :return:
        """
        return self._CourseName

    def setStudentNO(self,StudentNO):
        """

        :param StudentNO:
        :return:
        """
        self._StudentNO=StudentNO

    def getStudentNO(self):
        """

        :return:
        """
        return self._StudentNO

    def setStudentName(self,StudentName):
        """

        :param StudentName:
        :return:
        """
        self._StudentName=StudentName

    def getStudentName(self):
        """

        :return:
        """
        return self._StudentName

    def setStudentBirthday(self,StudentBirthday):
        """

        :param StudentBirthday:
        :return:
        """
        self._StudentBirthday=StudentBirthday

    def getStudentBirthday(self):
        """

        :return:
        """
        return self._StudentBirthday

    def setAge(self,Age):
        """

        :param Age:
        :return:
        """
        self._Age=Age

    def getAge(self):
        """

        :return:
        """
        return self._Age
    '''
    def settotalScore(self,totalScore):
        """

        :param totalScore:
        :return:
        """
        self._totalScore=totalScore

    def gettotalScore(self):
        """

        :return:
        """
        return self._totalScore
    '''
    def __str__(self):
        return f"{self.getScoreId()},{self.getStudentId()},{self.getScore()},{self._StudentNO},{self._StudentName},{self._CourseName},{self._StudentBirthday},{self._Age}"

  

"""

__init__.py
创建实体类包
date 2023-06-23
edit: Geovin Du,geovindu, 涂聚文
ide:  PyCharm 2023.1 python 11

"""
# __init__.py
#from .StudentScoreListInfo import Model  https://www.blog.pythonlibrary.org/2021/09/23/python-101-how-to-create-a-python-package/ 参考无效
#from .StudentScoreInfo import Model
from Model import StudentScoreListInfo
from Model import StudentScoreInfo

#StudentScoreInfo.py  参考 Python CookBook 无效
#StudentScoreListInfo.py

  

调用:

import Model.StudentScoreListInfo

    #1
    #stu=StudentScoreListInfo.StudentScoreList(1,2,1,95,'语文','002','geovindu','2007-08-05',15)
    #print(stu.getStudentName())
    #2
    studens=[]
    stu = Model.StudentScoreListInfo.StudentScoreList()
    stu.setScoreId(1)
    stu.setStudentId(2)
    stu.setCourseId(1)
    stu.setScore(95)
    stu.setCourseName('语文')
    stu.setStudentNO('002')
    stu.setStudentName('geovindu')
    stu.setStudentBirthday('2007-08-05')
    stu.setAge(15)
    studens.append(stu)
    stu = Model.StudentScoreListInfo.StudentScoreList()
    stu.setScoreId(1)
    stu.setStudentId(2)
    stu.setCourseId(1)
    stu.setScore(95)
    stu.setCourseName('语文')
    stu.setStudentNO('002')
    stu.setStudentName('涂聚文')
    stu.setStudentBirthday('2007-08-05')
    stu.setAge(15)
    studens.append(stu)
    for i in studens:
        print(i)

  

输出:

1,2,95,002,geovindu,语文,2007-08-05,15
1,2,95,002,涂聚文,语文,2007-08-05,15

  

 

标签:return,stu,python,Create,param,How,._,self,def
From: https://www.cnblogs.com/geovindu/p/17498730.html

相关文章

  • 如何使用cpython
    这个的学习主要是因为在运行目标检测的代码时总是会出现下面的错误:fromCython.BuildimportcythonizeModuleNotFoundError:Nomodulenamed'Cython'----------------------------------------ERROR:Commanderroredoutwithexitstatus1:pythonset......
  • 使用pyinstaller打包python为exe文件
    将Python图形界面程序打包成可执行文件,可以在没有Python环境的电脑上运行,可以使用PyInstaller或者cx_Freeze等工具。使用PyInstaller打包Python程序首先,在命令行中安装PyInstaller:pipinstallpyinstaller在命令行中进入Python程序所在目录。输入以下命令:pyinstaller--onef......
  • Python json 字符串插入变量值
    在json字符串中,对某个key插入一个变量值而非固定的值可以照以下的例子来实现:beep_status="ON"f"{{\"Beep\":\"{beep_status}\"}}"在json字符串外套一层{}和f,在取变量时通过{beep_status}来取。......
  • 使用 python 插入 sql 字符串时将对象转换为元组插入占位符
    sql插入的值肯定是动态的,通过%s占位符插入变量:definsert_cover(data:pojos.Cover):sql="insertintocovers(temp,accel_x,accel_y,accel_z,cover_status,date,name,sno)values(%s,%s,%s,%s,%s,%s,%s,%s)"exeSql(sql,tuple(data))data是一个......
  • Python开发系列课程(13) - 文件和异常
    文件和异常在实际开发中,常常需要对程序中的数据进行持久化操作,而实现数据持久化最直接简单的方式就是将数据保存到文件中。说到“文件”这个词,可能需要先科普一下关于文件系统的知识,对于这个概念,维基百科上给出了很好的诠释,这里不再浪费笔墨。在Python中实现文件的读写操作其实非常......
  • Python开发系列课程(0) - 公告
    这里我们先为大家普及一下编程语言的基本知识。日常生活中有很多任务都可以通过计算机来解决,我们可以通过编程来控制计算机工作,当然编程可能对很多人来说并不是一件容易的事情,尤其是当你从C或者C++这样的语言开始起步的时候。今天你也可能会听到很多类似于“每个人都应该学习编程......
  • python实现批量重命名与批量删除
    python实现文件批量重命名这里我是用正则表达式,把文件名包含“[中间是任意字母或者数字]”替换成空字符串importosimportre#指定目录路径directory=r"D:\\音乐"#匹配的正则表达式pattern=r"\[.+?\]"#遍历目录下的文件forfilenameinos.listdir(directory):......
  • 安装Python
    转载请注明来源:http://www.eword.name/Author:ewordEmail:[email protected]专题目录安装Python一、查询是否安装了Python及安装路径#查看当前Python版本python--versionPython2.7.16#查看当前所有Python版本路径appledeMBP:~apple$whichpython2.7/usr/lo......
  • 安装Python
    转载请注明来源:http://www.eword.name/Author:ewordEmail:[email protected]专题目录安装Python一、查询是否安装了Python及安装路径#查看当前Python版本python--versionPython2.7.16#查看当前所有Python版本路径appledeMBP:~apple$whichpython2.7/usr/lo......
  • MacBook 搭建python开发环境
    转载请注明来源:http://www.eword.name/Author:ewordEmail:[email protected]专题目录MacBook搭建python开发环境一、需要安装的软件安装Python安装pip安装Virtualenv安装VSCodeVSCode安装Python扩展插件安装PyCharm二、IDE搭配VSCode+Python+pip+Virtuale......