母親節
母親節(Mother’s Day),是一個感謝母親的節日。這個節日的傳統起源於古希臘;而現代的母親節起源於美國。日期每年5月的第二個星期日。今年的母親節在2017年5月14日。
以下是使用python3 中的線程和協程實現了成長的過程,表達對媽媽的愛,歡迎提交PR,也歡迎大家拍磚。
先來看一下效果圖:
【代碼地址】(https://github.com/navcat/motherDay)
代碼
#!/usr/bin/python
#coding=utf-8
import time, random
import threading
from functools import wraps
# 孕期,你懂的
PREGNANCY = [
'今天,媽媽的體溫38°,比平常高了0.5°',
'媽媽吐得很厲害,是你搗的鬼嗎?',
'媽媽看到了你的手指,很可愛,不過看上去還有點像鴨蹼',
'你身高8cm,但是已經有指紋了',
'醫生說看到你在吸吮自己的大拇指,我想知道,那是什麼味道',
'眉毛和眼皮都長出來了,天生就是愛運動',
'媽媽看到你的大腦在長大,我想,你一定是個聰明的孩子',
'你偶爾會張開雙眼,似乎看到什麼,又似乎沒有看到',
'媽媽看到你越來越強壯,很開心',
'哇喔,你來了,50cm,你很開心,可媽媽很痛',
]
# 你要什麼,你懂的
WHAT_I_WANTS = [
'AD鈣奶',
'哇哈哈',
'棒棒糖',
'皮卡丘',
'學鋼琴',
'自行車',
'上北大'
]
# 母愛無窮,你懂的
WHAT_MOTHER_CARES = [
'給你打了點生活費',
'天冷了,多加衣',
'天熱了,買點衣服吧',
'都10點了,還不起牀',
'快點,要遲到了',
'有點出息行不',
'媽媽想你了...'
]
def coroutine(func):
''' 協程裝飾器,調用一次next,進入等待 '''
@wraps(func)
def wrapper(*args, **kwargs):
g = func(*args, **kwargs)
next(g)
return g
return wrapper
class Person(object):
''' 人 '''
def __init__(self):
self.age = 0
class Growing(object):
def __init__(self, monther, child, condition):
self.monther = monther
self.child = child
self.condition = condition
def grow_up(self):
''' 年齡增長,此處,我和母親的年齡同時增長 '''
try:
self.condition.acquire()
# 隨機增長年齡
age = random.randint(1,3)
# age = 1
self.monther.age += age
self.child.age += age
finally:
self.condition.release()
def time_lapse(self):
''' 時間流逝 '''
try:
time.sleep(random.randint(1, 3))
except InterruptedError:
pass
class MotherGrowing(Growing):
def __init__(self, *args, **kwargs):
super(MotherGrowing, self).__init__(*args, **kwargs)
self.monther.age = 22
def growing(self):
''' 媽媽在變老 '''
self.grow_up()
if self.age > 60:
print('<<<<<<<<<<<<<<媽媽老了>>>>>>>>>')
# print('----->媽媽【{0}】歲了'.format(self.age))
def pregnant(self):
''' 孕期 '''
self.age = 22
print('---那一年,媽媽%d 歲----' % self.age)
# 成長週期
for week, desc in enumerate(PREGNANCY):
print("[第{0}月] {1}".format(week + 1, desc))
# 這裏是漫長的等待
self.time_lapse()
@coroutine
def from_child(self):
''' 你要什麼,媽媽就給你什麼 '''
while True:
something = (yield)
self.time_lapse()
print('[媽{0}歲, 我{1}歲]>>你說要:[{2}], 媽媽給你 【{2}】'.format(
self.monther.age, self.child.age, something))
def to_child(self, cg):
''' 對孩子的關懷 '''
care = cg.from_monther()
max_count = 0
while max_count < 20:
index = random.randint(0, len(WHAT_MOTHER_CARES) - 1)
# 不間斷的關懷
care.send(WHAT_MOTHER_CARES[index])
max_count += 1
# 媽媽在變老
self.growing()
def say(self):
print('{0}歲媽媽對{1}歲孩子說,你是好樣的!'.format(
self.monther.age,
self.child.age))
class ChildGrowing(Growing):
def __init__(self, *args, **kwargs):
super(ChildGrowing, self).__init__(*args, **kwargs)
def growing(self):
''' 我長大了 '''
self.grow_up()
if self.child.age > 18:
print('<<<<<<<<<<<<<<我長大了>>>>>>>>>')
# print('-----<我【{0}】歲了'.format(self.age))
def to_mother(self, mg):
''' 向媽媽索取 '''
say = mg.from_child()
max_count = 0
while max_count < 20:
index = random.randint(0, len(WHAT_I_WANTS) - 1)
# 不間斷索取
say.send(WHAT_I_WANTS[index])
max_count += 1
# 我在成長
self.growing()
@coroutine
def from_monther(self):
''' 來自媽媽的愛 '''
while True:
care = (yield)
self.time_lapse()
print('[我{0}歲, 媽{1}歲]<<媽媽說:{2}'.format(
self.child.age,
self.monther.age,
care))
def say(self):
print('{0}歲的我對{1}歲的媽說:媽,您辛苦了,母親節快樂!'.format(
self.child.age,
self.monther.age
))
def main():
monther = Person()
me = Person()
condition = threading.Condition()
g_mother = MotherGrowing(monther, me, condition)
g_mother.pregnant()
g_me = ChildGrowing(monther, me, condition)
# 媽媽線程
t_mothor = threading.Thread(target=g_mother.to_child, args=(g_me,))
# 我的線程
t_me = threading.Thread(target=g_me.to_mother, args=(g_mother, ))
t_mothor.start()
t_me.start()
t_mothor.join()
t_me.join()
# 我說
g_me.say()
# 媽媽說
g_mother.say()
if __name__ == '__main__':
main()
祝福
祝福親愛的母親,您辛苦啦!
如果可以,送一束康乃馨給她吧!