原型和补丁(prototype and patch):一种开发方案,编写一个程序的初稿,测试,发现错误时修正它们。
设计开发(designed development):一种开发方案,需要对问题有更高层次的理解,比增量开发或原型开发更有计划性。
纯函数(pure function):一种不修改任何作为参数传入的对象的函数。大部分纯函数是有返回值的(fruitful)。
修改器(modifier):一种修改一个或多个作为参数传入的对象的函数。大部分修改器没有返回值;即返回 None 。
函数式编程风格(functional programming style):一种程序设计风格,大部分函数为纯函数。
不变式(invariant):在程序执行过程中总是为真的条件。
断言语句(assert statement):一种检查条件是否满足并在失败的情况下抛出异常的语句。

 

# -*- coding: utf-8 -*-
"""
Created on Sat Feb 24 10:16:17 2018

@author: gzwuqiang3
"""

import sys
print(sys.getdefaultencoding())


class Time:
"""Represents the time of day.

attributes:hous,minute,second
"""

time = Time()
time.hour = 11
time.minute = 29
time.second = 30
#print(time.second)

def add_time(t1,t2):
  sum = Time()
  sum.hour = t1.hour + t2.hour
  sum.miunte = t1.minute + t2.minute
  sum.second = t1.second + t2.second

  if sum.second >= 60:
    sum.secon -= 60
    sum.miunte += 1
  if sum.miunte >= 60:
    sum.miunte -=60
    sum.hour += 1

  return sum

start = Time()
start.hour = 9
start.minute = 45
start.second = 0

duration = Time()
duration.hour = 1
duration.minute = 35
duration.second = 0

done = add_time(start, duration)
print(done)

def increment(time,seconds):
  time.second += seconds
  if time.second >= 60:
    time.second -=60
    time.miunte += 1
  if time.minute >= 60:
    time.minute -=60
    time.hour +=1

print(increment)

#把 Time 对象转成整数的函数
def time_to_int(seconds):
  minutes = time.hour * 60 + time.minute
  seconds = minutes * 60 + time.second
  print(seconds)
#print(time_to_int)
# 一个把整数转换为 Time 对象
def int_to_time(seconds):
  time = Time()
  minutes,time.second = divmod(seconds, 60)
  time.hour, time.minute = divmod(minutes,60)
  return time

#
def add_time(t1, t2):
  seconds = time_to_int(t1) + time_to_int(t2)
  return int_to_time(seconds)


def valid_time(time):
  if time.hour < 0 or time.minute < 0 or time.second < 0 :
    return False
  if time.hour >= 60 or time.minute >= 60 or time.second >= 60:
    return False
  return True

def add_time(t1,t2):
  if not valid_time(t1) or not valid_time(t2):
    raise ValueError('invalid Time object in add_time')
  seconds = time_to_int(t1) + time_to_int(t2)
  return int_to_time(seconds)

#assert语句,检查一个给定的不变式并在失败的情况下抛出异常:
def add_time(t1,t2):
  assert valid_time(t1) and valid_time(t2)
  seconds = time_to_int(t1) + time_to_int(t2)
  return int_to_time(seconds)