为了账号安全,请及时绑定邮箱和手机立即绑定

python 数据结构零 之 python类

2019.07.11 21:23 363浏览

python 数据结构教程第零课
首先展示一些基本类的源码,供对python语言还不是很熟悉的同学学习了解,这里提供 Time类、Date类、Rational类、Student类、Person类、staff类源码
1.时间类

#时间值错误提示
class TimeValueError(ValueError):
    pass

#24小时 时间类
class Time:
    #类初始化
    def __init__(self,hours = 0,minutes = 0,seconds = 0):

        if hours > 24 or hours < 0 or type(hours) is not int:
            raise TimeValueError('Hours is wrong',hours)
        if minutes > 60 or minutes < 0 or type(minutes) is not int:
            raise TimeValueError('minutes is wrong',minutes)
        if seconds > 60 or seconds <0 or type(seconds) is not int:
            raise TimeValueError('seconds is wrong',seconds)
        self.__hours = hours
        self.__minutes = minutes
        self.__seconds = seconds

    #将时间转化为秒
    def __absoluteValue(self):
        return self.__hours * 3600 + self.__minutes * 60 + self.__seconds

    #返回小时
    def hours(self):
        return self.__hours

    #返回分钟
    def minutes(self):
        return self.__minutes

    #返回秒
    def seconds(self):
        return self.__seconds

    #支持两个时间相加
    def __add__(self,other):
        time = Time()
        time.__seconds = (self.__seconds + other.__seconds)%60
        s_add = (self.__seconds + other.__seconds)//60
        if s_add == 1:
            time.__minutes = (self.__minutes + other.__minutes + 1)%60  
        else:
            time.__minutes = (self.__minutes + other.__minutes)%60
        m_add = (self.__minutes + other.__minutes)//60
        if m_add == 1: 
            time.__hours = (self.__hours + other.__hours + 1)%24 
        else: time.__hours = (self.__hours + other.__hours)%24
        return time

    #支持两个时间相减
    def __sub__(self,other):
        time = Time()
        Error = self.__absoluteValue() - other.__absoluteValue()
        if Error < 0:
            raise TimeValueError('sub is wrong')
        time.__hours = Error // 3600
        time.__minutes = (Error - time.__hours * 3600) // 60
        time.__seconds = (Error - time.__hours * 3600 - time.__minutes * 60)
        return time

    #时间 == 符号的支持函数
    def __eq__(self,other):
        return self.__absoluteValue() == other.__absoluteValue()

    #时间 < 符号的支持函数
    def __lt__(self,other):
        return self.__absoluteValue() < other.__absoluteValue()

    #输出时间
    def showTime(self):
        print(str(self.__hours) + ':' + str(self.__minutes) + ':' + str(self.__seconds))

2.日期类

#时间值错误提示
class TimeValueError(ValueError):
    pass

#日期类
class Date:

    #类方法 判断某一年是否是闰年
    @classmethod
    def RY(cls,year):
        if (year % 4 == 0 or year % 400 == 0) and year % 100 != 0:
            return True
        else:
            return False

    #日期初始化    
    def __init__(self,year = 1,month = 1,day = 1):
        if year < 0 or type(year) is not int:
            raise TimeValueError('year is wrong',year)
        if month > 12 or month <= 0 or type(month) is not int:
            raise TimeValueError('month is wrong',month)
        if Date.RY(year) and month == 2:
            if day > 29 or day < 0 or type(day) is not int:
                raise TimeValueError('day is wrong',day)
        else:
            if day > 28 or day < 0 or type(day) is not int:
                raise TimeValueError('day is wrong',day)
        self.__year = year
        self.__month = month
        self.__day = day 

    #返回年
    def year(self):
        return self.__year

    #返回月
    def month(self):
        return self.__month

    #返回日
    def day(self):
        return self.__day

    #将日期转化为天
    def date_To_day(self):
        count = 0
        loop = int(self.__year)
        for i in range(loop):
            if Date.RY(i):
                count += 1
        if Date.RY(self.__year) and self.__month > 2:
            count += 1
        if self.__month == 1:
            return (self.__year - 1) * 365 + self.__day + count
        elif self.__month == 2:
            return (self.__year - 1) * 365 + self.__day + 31 + count
        elif self.__month == 3:
            return (self.__year - 1) * 365 + self.__day + 31 + 28 + count
        elif self.__month == 4:
            return (self.__year - 1) * 365 + self.__day + 31 * 2 + 28 + count
        elif self.__month == 5:
            return (self.__year - 1) * 365 + self.__day + 31 * 2 + 30 +28 + count
        elif self.__month == 6:
            return (self.__year - 1) * 365 + self.__day + 31 * 3 + 30 +28 + count        
        elif self.__month == 7:
            return (self.__year - 1) * 365 + self.__day + 31 * 3 + 30 * 2 +28 + count
        elif self.__month == 8:
            return (self.__year - 1) * 365 + self.__day + 31 * 4 + 30 * 2 +28 + count           
        elif self.__month == 9:
            return (self.__year - 1) * 365 + self.__day + 31 * 5 + 30 * 2 +28 + count
        elif self.__month == 10:
            return (self.__year - 1) * 365 + self.__day + 31 * 5 + 30 * 3 +28 + count     
        elif self.__month == 11:
            return (self.__year - 1) * 365 + self.__day + 31 * 6 + 30 * 3 +28 + count
        elif self.__month == 12:
            return (self.__year - 1) * 365 + self.__day + 31 * 6 + 30 * 4 +28 + count     
        else:
            return (self.__year - 1) * 365

    #类方法,将天转化为日期
    @classmethod
    def day_To_date(cls,day):
        Hundredsy_day = 365 * 400 + (400 / 4 - 400 / 100)
        Hundredy_day = 365 * 100 + (100 / 4 - 1)
        Foury_day = 365 * 4 + 1
        Y_day = 365
        y_400 = day // Hundredsy_day
        y_100 = (day - y_400 * Hundredsy_day) // Hundredy_day
        y_4 = (day - y_400 * Hundredsy_day - y_100 * Hundredy_day) // Foury_day
        if (day - y_400 * Hundredsy_day - y_100 * Hundredy_day - Foury_day * y_4) < 1460:
            Y_1 = (day - y_400 * Hundredsy_day - y_100 * Hundredy_day - Foury_day * y_4) // Y_day
        elif (day - y_400 * Hundredsy_day - y_100 * Hundredy_day - Foury_day * y_4) == 1460:
            Y_1 = (day - y_400 * Hundredsy_day - y_100 * Hundredy_day - Foury_day * y_4) // Y_day - 1
        day_remain = day - y_400 * Hundredsy_day  - y_100 * Hundredy_day - Foury_day * y_4  - Y_day * Y_1 
        date = Date()
        date.__year = y_400 * 400 + y_100 * 100 + y_4 * 4 + Y_1 + 1

        if Date.RY(date.__year):
            if day_remain <= 31:
                date.__month = 1
                date.__day = day_remain
            elif day_remain <= 60:
                date.__month = 2
                date.__day = day_remain - 31
            elif day_remain <= 91:
                date.__month = 3
                date.__day = day_remain - 60
            elif day_remain <= 121:
                date.__month = 4
                date.__day = day_remain - 91
            elif day_remain <= 152:
                date.__month = 5
                date.__day = day_remain - 121
            elif day_remain <= 182:
                date.__month = 6
                date.__day = day_remain - 152
            elif day_remain <= 213:
                date.__month = 7
                date.__day = day_remain - 182
            elif day_remain <= 244:
                date.__month = 8
                date.__day = day_remain - 213
            elif day_remain <= 274:
                date.__month = 9
                date.__day = day_remain - 244
            elif day_remain <= 305:
                date.__month = 10
                date.__day = day_remain - 374
            elif day_remain <= 335:
                date.__month = 11
                date.__day = day_remian - 305
            elif day_remain < 366:
                date.__month = 12
                date.__day = day_remain - 335
            else:
                print(day_remain)
        elif not Date.RY(date.__year):
            if day_remain <= 31:
                date.__month = 1
                date.__day = day_remain
            elif day_remain <= 59:
                date.__month = 2
                date.__day = day_remain - 31
            elif day_remain <= 90:
                date.__month = 3
                date.__day = day_remain - 59
            elif day_remain <= 120:
                date.__month = 4
                date.__day = day_remain - 90
            elif day_remain <= 151:
                date.__month = 5
                date.__day = day_remain - 120
            elif day_remain <= 181:
                date.__month = 6
                date.__day = day_remain - 151
            elif day_remain <= 212:
                date.__month = 7
                date.__day = day_remain - 181
            elif day_remain <= 243:
                date.__month = 8
                date.__day = day_remain - 212
            elif day_remain <= 273:
                date.__month = 9
                date.__day = day_remain - 243
            elif day_remain <= 304:
                date.__month = 10
                date.__day = day_remain - 373
            elif day_remain <= 334:
                date.__month = 11
                date.__day = day_remian - 304
            elif day_remain < 365:
                date.__month = 12
                date.__day = day_remain - 334
            else:
                print(day_remain)
        if day_remain == 0:
            date.__year -= 1
            date.__month = 12
            date.__day = 31
        return date

    #日期相减,返回天数
    def __sub__(self,other):
       return abs(self.date_To_day() - other.date_To_day())

    #日期相加,返回日期                    
    def plus(self,day):
        day = self.date_To_day() + day
        return Date.day_To_date(day) 

    #类方法,某一年的第n天日期 
    @classmethod         
    def num_date(cls,year,n):
        date = Date()
        date.__year = year
        day = date.date_To_day() + n -1
        a = Date.day_To_date(day)
        return a

    #类方法 ,某一日期,n天(可正可负)后日期
    @classmethod
    def adjust(cls,d,n):
        print('aaaa')
        day = d.date_To_day() + n
        print('bbbbb')
        return Date.day_To_date(day)

    #显示日期
    def show(self):
        print(str(int(self.__year))+'/'+str(int(self.__month))+'/'+str(int(self.__day)))

3.有理数类

#有理数类
class Rational(object):
    #计算两者的最大公约数
    @staticmethod
    def _gcd(m,n):
        if n == 0:
            m,n = n,m
        while m != 0:
            m,n = n % m,m
        return n

    #有理数初始化,分数形式
    def __init__(self,num,den = 1):
        if isinstance(num,float):
            (self._num,self._den) = num.as_integer_ratio()
        else:    
            sign = 1
            if num < 0:
                num,sign = -num,-sign
            if den < 0:
                den, sign = -den,-sign
            g = Rational._gcd(num,den)
            self._num = sign * (num // g)
            self._den = den // g

    静态方法,new用于实例化之前判断参数是否合理
    @staticmethod
    def __new__(cls,num,den = 1):
        if (not isinstance(num,float) and not isinstance(num,int)) or not isinstance(den,int):
            raise TypeError
        if den == 0:
            raise ZeroDivisionError
        return object.__new__(cls)

     返回分子   
    def num(self):
        return self._num
    返回坟墓
    def den(self):
        return self._den

    #有理数相加
    def __add__(self,another):
        den = self._den * another.den()
        num = (self._num * another.den + self._den * another.num())
        return Rational(num,den)

    #有理数相乘
    def __mul__(self,another):
        return Rational(self._num * another.num(),self._den * another.den())

    #有理数相除
    def __floordiv__(self,another):
        if another.num() == 0:
            raise ZeroDivisionError
        return Rational(self._num * another.den(),self._den * another.num())

    #有理数相减
    def __sub__(self,another):
        num = self._num * another.den() - another.num() * self._den
        den = self._den * another.den()
        return Rational(num,den)

    #有理数除法,返回float
    def __truediv__(self,another):
        if another.num() == 0:
            raise ZeroDivisionError
        if not isinstance(another,Rational):
            raise TypeError
        return (self._num * another.den())/(self._den * another.num())

    #取有理数余数
    def __mod__(self,another):
        if another.num() == 0:
            raise ZeroDivisionError
        if not isinstance(another,Rational):
            raise TypeError
        a = int(self / another)
        b = another * Rational(a)
        return (self - b)

    #转化为int
    def int(self):
        return int(self._num / self._den)

    #转化为float
    def float(self):
        return float(self._num / self._den)

    #以下是各种比较符号支持函数
    def __eq__(self,another):
        return self._num * another.den() == self._den * another.num()

    def __lt__(self,another):
        return self._num * another.den() < self._den * another.num()

    def __ne__(self,another):
        return self._num * another.den() != self._den * another.num()

    def __le__(self,another):
        return self._num * another.den() <= self._den * another.num()

    def __gt__(self,another):
        return self._num * another.den() > self._den * another.num()

    def __ge__(self,another):
        return self._num * another.den() >= self._den * another.num()

    #转化为字符串
    def __str__(self):
        return str(self._num) + '/' +str(self._den)

    #输出有理数
    def print(self):
        print(self._num,'/',self._den)

4.’人 ‘类

import datetime

class PersonTypeError(TypeError):
    pass

class PersonValueError(ValueError):
    pass

# ’人‘类
class Person:
    #产生与类相关的一个编号,每次实例化加1
    __num = 0

    #初始化
    def __init__(self,name,sex,birthday,ident):
        if not (isinstance(name,str) and sex in ('女','男')):
            raise PersonValueError(name,sex)
        try:
            birth = datetime.date(*birthday)
        except:
            raise PersonValueError('Wrong date',birthday)
        self._name = name
        self._sex = sex
        self._birthday = birth
        self._id = ident
        Person.__num += 1

    #返回基本信息
    def id(self):
        return self._id

    def name(self):
        return self._name

    def sex(self):
        return self._sex

    def birthday(self):
        return self._birthday

    def age(self):
        return datetime.date.today().year - self._birthday.year

    #设置名称
    def set_name(self,name):
        if not isinstance(name,str):
            raise PersonValueError('set_name',name)
        self._name = name

    # <符号支持
    def __lt__(self,another):
        if not isinstance(another,Person):
            raise PersonTypeError(another)
        return self._id < another._id

    #类方法,返回编号
    @classmethod
    def num(cls):
        return Person.__num

    #字符串化
    def __str__(self):
        return  ' '.join((self._id,self._name,self._sex,str(self._birthday)))

    #显示信息
    def details(self):
        return ','.join(('编号:' + self._id,'姓名:' + self._name,'性别:' + self._sex,'出生日期:' +str(self._birthday)))

5.学生类

import datetime

class PersonTypeError(TypeError):
    pass

class PersonValueError(ValueError):
    pass

 #学生类,继承与‘人’类
class Student(Person):
    _id_num = 0

    #类方法,产生学生ID
    @classmethod
    def _id_gen(cls):
        cls._id_num += 1
        year = datetime.date.today().year
        return '1{:04}{:05}'.format(year,cls._id_num)

    #初始化
    def __init__(self,name,sex,birthday,department):
        Person.__init__(self,name,sex,birthday,Student._id_gen())
        self._department = department
        self._enroll_date = datetime.date.today()
        self._courses = {}

    #给学生加课函数     
    def set_course(self,course_name):
        self._courses[course_name] = None

    #设置课程分数
    def set_score(self,course_name,score):
        if course_name not in self._courses:
            raise PersonValueError('No this course selected:',course_name)

        self._courses[course_name] = score

    #返回分数
    def scores(self):
        return [(cname,self._courses[cname]) for cname in self._courses]

    #返回学生信息
    def details(self):
        return ','.join((Person.details(self),'入学日期:' + str(self._enroll_date),'院系:' + self._department,'课程日期:' + str(self.scores())))

6.员工类

#员工类
class Staff(Person):
    _id_num = 0
    #获得ID
    @classmethod
    def _id_gen(cls,birthday):
        cls._id_num += 1
        birth_year = datetime.date(*birthday).year
        return '0{:04}{:05}'.format(birth_year,cls._id_num)

    #初始化
    def __init__(self,name,sex,birthday,entry_date = None):

        super().__init__(name,sex,birthday,Staff._id_gen(birthday))

        if entry_date:
            try:
                self._entry_date = datetime.date(*entry_date)
            except:
                raise PersonValueError('Wrong date',entry_date)

        else:
            self._entry_date = datetime.date.today()
        self._salary = 1720
        self._department = '未定'
        self._position  = '未定'

    #设置工资    
    def set_salary(self,amount):
        if not type(amount) is int:
            raise TypeError
        self._salary = amount

    #设置职位
    def set_position(self,amount):
        self._position = amount

    #显示信息    
    def details(self):
        return ','.join((super().details(),'入职日前:' + str(self._entry_date),'院系:' + self._department,'职位:' + self._position,'工资:' + str(self._salary)))
    #设置部门
    def set_department(self,apart):
        self._department = apart
点击查看更多内容

本文原创发布于慕课网 ,转载请注明出处,谢谢合作

0人点赞

若觉得本文不错,就分享一下吧!

评论

相关文章推荐

正在加载中
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消