from pyparsing import Word, OneOrMore, Optional, Group, Suppress, alphanums
class Gate:
def __init__(self):
self.is_open = False
def __str__(self):
return 'open' if self.is_open else 'closed'
def open(self):
print('opening the gate')
self.is_open = True
def close(self):
print('closing the gate')
self.is_open = False
class Garage:
def __init__(self):
self.is_open = False
def __str__(self):
return 'open' if self.is_open else 'closed'
def open(self):
print('opening the garage')
self.is_open = True
def close(self):
print('closing the garage')
self.is_open = False
class Aircondition:
def __init__(self):
self.is_on = False
def __str__(self):
return 'on' if self.is_on else 'off'
def turn_on(self):
print('turning on the aircondition')
self.is_on = True
def turn_off(self):
print('turning off the aircondition')
self.is_on = False
class Heating:
def __init__(self):
self.is_on = False
def __str__(self):
return 'on' if self.is_on else 'off'
def turn_on(self):
print('turning on the heating')
self.is_on = True
def turn_off(self):
print('turning off the heating')
self.is_on = False
class Boiler:
def __init__(self):
self.temperature = 180 # in fahrenheit
def __str__(self):
return 'boiler temperature: {}'.format(self.temperature)
def increase_temperature(self, amount):
print("increasing the boiler's temperature by {} degrees".format(amount))
self.temperature += amount
def decrease_temperature(self, amount):
print("decreasing the boiler's temperature by {} degrees".format(amount))
self.temperature -= amount
class Fridge:
def __init__(self):
self.temperature = 35 # in fahrenheit
def __str__(self):
return 'fridge temperature: {}'.format(self.temperature)
def increase_temperature(self, amount):
print("increasing the fridge's temperature by {} degrees".format(amount))
self.temperature += amount
def decrease_temperature(self, amount):
print("decreasing the fridge's temperature by {} degrees".format(amount))
self.temperature -= amount
def main():
word = Word(alphanums)
command = Group(OneOrMore(word))
token = Suppress("->")
device = Group(OneOrMore(word))
argument = Group(OneOrMore(word))
event = command + token + device + Optional(token + argument)
gate = Gate()
garage = Garage()
airco = Aircondition()
heating = Heating()
boiler = Boiler()
fridge = Fridge()
tests = ('open -> gate',
'close -> garage',
'turn on -> aircondition',
'turn off -> heating',
'increase -> boiler temperature -> 20 degrees',
'decrease -> fridge temperature -> 6 degree')
open_actions = {'gate':gate.open, 'garage':garage.open, 'aircondition':airco.turn_on,
'heating':heating.turn_on, 'boiler temperature':boiler.increase_temperature,
'fridge temperature':fridge.increase_temperature}
close_actions = {'gate':gate.close, 'garage':garage.close, 'aircondition':airco.turn_off,
'heating':heating.turn_off, 'boiler temperature':boiler.decrease_temperature,
'fridge temperature':fridge.decrease_temperature}
for t in tests:
if len(event.parseString(t)) == 2: # no argument
cmd, dev = event.parseString(t)
cmd_str, dev_str = ' '.join(cmd), ' '.join(dev)
if 'open' in cmd_str or 'turn on' in cmd_str:
open_actions[dev_str]()
elif 'close' in cmd_str or 'turn off' in cmd_str:
close_actions[dev_str]()
elif len(event.parseString(t)) == 3: # argument
cmd, dev, arg = event.parseString(t)
cmd_str, dev_str, arg_str = ' '.join(cmd), ' '.join(dev), ' '.join(arg)
num_arg = 0
try:
num_arg = int(arg_str.split()[0]) # extract the numeric part
except ValueError as err:
print("expected number but got: '{}'".format(arg_str[0]))
if 'increase' in cmd_str and num_arg > 0:
open_actions[dev_str](num_arg)
elif 'decrease' in cmd_str and num_arg > 0:
close_actions[dev_str](num_arg)
if __name__ == '__main__':
main()
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Kasampalis -- Mastering Python Design Patterns -- 2015 -- code.7z (92个子文件)
9324OS_12_code
interpreter.py 4KB
old
interpreter.py 4KB
interpreter.py~ 4KB
interpreter.py~ 4KB
9324OS_08_code
old
mvc.py~ 1KB
mvc.py 1KB
mvc.py~ 1KB
mvc.py 1KB
9324OS_02_code
builder.py~ 4KB
builder.py 4KB
computer-builder.py 1KB
pizza.py~ 4KB
fluent_builder.py~ 518B
fluent_builder.py 750B
apple-factory.py 810B
9324OS_09_code
lazy.py~ 978B
proxy.py~ 1KB
lazy.py 978B
proxy.py 1KB
9324OS_06_code
old
facade.py~ 989B
facade.py 2KB
facade.py~ 2KB
facade.py 3KB
9324OS_07_code
old
flyweight.py 1KB
flyweight.py~ 1KB
flyweight.py 2KB
flyweight.py~ 1KB
9324OS_14_code
state.py~ 3KB
old
state.py~ 3KB
test-fsm.py~ 1KB
state.py 3KB
test-fsm.py~ 1KB
state.py 3KB
9324OS_05_code
old
mymath.py~ 899B
fibonacci_naive.py~ 263B
mymath.py 1009B
fibonacci.py~ 339B
fibonacci.py 339B
fibonacci_naive.py 263B
mymath.py 1009B
fibonacci.py 339B
fibonacci_naive.py 263B
9324OS_15_code
langs.py 536B
old
langs.py 536B
strategy.py~ 1KB
strategy.py 2KB
strategy.py~ 2KB
strategy.py 1KB
9324OS_10_code
chain.py~ 1KB
old
chain.py~ 1KB
chain.py 1KB
chain.py 1KB
9324OS_03_code
clone.py 392B
9324OS_11_code
command.py 2KB
old
command.py 2KB
command.py~ 2KB
first-class.py~ 535B
command.2.py~ 2KB
first-class.py 516B
command.py~ 2KB
first-class.py 516B
9324OS_04_code
adapter.py~ 792B
external.py~ 412B
external.py 411B
old
adapter.py~ 843B
external.py~ 412B
external.py 411B
adapter.py 770B
adapter.py 770B
9324OS_16_code
old
graph-template.py~ 2KB
graph.py~ 2KB
graph.py 2KB
template.py 600B
graph-template.py 2KB
template.py~ 600B
9324OS_13_code
observer.py~ 2KB
old
observer.py~ 2KB
note 50B
observer.py 2KB
test.py 412B
observer.py 2KB
9324OS_01_code
a.py~ 2KB
factory_method.py~ 2KB
abstract_factory.py 2KB
data
donut.json 1KB
person.xml 1KB
old
abstract_factory.py 2KB
data
donut.json 1KB
person.xml 1KB
abstract_factory.py~ 2KB
factory_method.py 2KB
factory_method.py 2KB
共 92 条
- 1
资源评论
小九不懂SAP
- 粉丝: 2555
- 资源: 510
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功