zoukankan      html  css  js  c++  java
  • 《Python核心编程》第二版第407页第十三章练习 续四 Python核心编程答案自己做的


    这是自己做的练习,可能有错误,欢迎讨论和各种优化重构方案。
    根据反馈,或者code review,对本篇文章答案或者相关内容的更新补充,一般会被添加在本篇博客的评论中。
    尽量保证每题的答案代码是完整的,不仅仅是函数或者类,打开Python 2.7的IDLE,将代码完整拷贝进去,就能调试运行。

    13-7.
    数据类。提供一个time模块的接口,允许用户按照自己给定时间的格式,比如:“MM/DD/YY”、“MM/DD/YYYY”、“DD/MM/YY”、“DD/MM/YYYY”、“Mon DD, YYYY”或是标准的Unix日期格式“Day Mon DD, HH:MM:SS YYYY”来查看日期。你的类应该维护一个日期值,并用给定的时间创建一个实例。如果没有给出时间值,程序执行时会默认采用当前的系统时间。还包括另外一些方法。
    update()    按照给定时间或是默认的当前系统时间修改数据值
    display()    以代表时间格式的字符串做参数,并按照给定时间的格式显示:

    'MDY' -> MM/DD/YY
    'MDYY'-> MM/DD/YYYY
    'DMY' -> DD/MM/YY
    'DMYY' -> DD/MM/YYYY
    'MODYY' -> Mon DD, YYYY
    如果没有提供任何时间格式,默认使用系统时间或ctime()的格式。附加题:把这个类和练习6-15结合起来。

    【答案】
    代码如下:

    class MyTime(object):
        'My Time Class for Ex 13-7'
        
        def __init__(self, timeValue, timeString):
            self.timeValue = timeValue
            self.timeString = timeString
            
        def update(self, timeValue, timeString):
            self.timeValue = timeValue
            self.timeString = timeString
            print 'Time updated success.\n'
            
        def display(self, displaytype):
            
            day = self.timeValue.tm_mday
            if day <= 9: day2 = '0' + str(day)
            else: day2 = str(day)
            
            months = self.timeValue.tm_mon
            if months <= 9: months2 = '0' + str(months)
            else: months2 = str(months)
            
            year = self.timeValue.tm_year
            year2 = str(year)[2:]
            year4 = str(year)
            
            timeStringSplit = self.timeString.split()
                    
            if displaytype == 'MDY':
                return months2 + '/' + day2 + '/' + year2
            elif displaytype == 'MDYY':
                return months2 + '/' + day2 + '/' + year4
            elif displaytype == 'DMYY':
                return day2 + '/' + months2 + '/' + year2        
            elif displaytype == 'DMY':
                return day2 + '/' + months2 + '/' + year4
            elif displaytype == 'MODYY':
                return timeStringSplit[0] + ' ' + timeStringSplit[2] + ', ' + timeStringSplit[4]
            else:
                return self.timeString
    
        
    import time
    from time import sleep
    
    timeValue = time.localtime(time.time())
    timeString = time.ctime(time.time())
    print 'Current time and date is: ', timeString, '\n'
    CurrentTime = MyTime(timeValue, timeString)
    
    sleep(3)
    timeValue = time.localtime(time.time())
    timeString = time.ctime(time.time())
    CurrentTime.update(timeValue, timeString)
    
    print "'MDY'   -> MM/DD/YY ...... ", CurrentTime.display('MDY')
    print "'MDYY'  -> MM/DD/YYYY .... ", CurrentTime.display('MDYY')
    print "'DMY'   -> DD/MM/YY ...... ", CurrentTime.display('DMY')
    print "'DMYY'  -> DD/MM/YYYY .... ", CurrentTime.display('DMYY')
    print "'MODYY' -> Mon DD,YYYY ... ", CurrentTime.display('MODYY')
    print "Nothing Defined .......... ", CurrentTime.display('NothingDefined')

    【执行结果】

    Current time and date is:  Fri Sep 14 10:40:47 2012
    # http://www.cnblogs.com/balian

    Time updated success.

    'MDY'   -> MM/DD/YY ......  09/14/12
    'MDYY'  -> MM/DD/YYYY ....  09/14/2012
    'DMY'   -> DD/MM/YY ......  14/09/2012
    'DMYY'  -> DD/MM/YYYY ....  14/09/12
    'MODYY' -> Mon DD,YYYY ...  Fri 14, 2012
    Nothing Defined ..........  Fri Sep 14 10:40:50 2012

    【未完】
    附加题没有做。

  • 相关阅读:
    在WCF中使用Flag Enumerations
    WCF开发教程资源收集
    [转]WCF 4 安全性和 WIF 简介
    Asp.Net Web API 2 官网菜鸟学习系列导航[持续更新中]
    Asp.Net Web API 2第十八课——Working with Entity Relations in OData
    Asp.Net Web API 2第十七课——Creating an OData Endpoint in ASP.NET Web API 2(OData终结点)
    Asp.Net Web API 2第十六课——Parameter Binding in ASP.NET Web API(参数绑定)
    Asp.Net Web API 2第十五课——Model Validation(模型验证)
    函数 生成器 生成器表达式
    函数的进阶
  • 原文地址:https://www.cnblogs.com/balian/p/2687324.html
Copyright © 2011-2022 走看看