myObject = SomeClass(this, that, other)
"""How to use SomeClass: myObject = SomeClass(this, that, other) """
; set the colorscheme and userlevel [colorscheme] background=red foreground=blue title=green [userlevel] login=2 title=1
import string txt = open( 'hypothetical.ini').read() sects = string.split(txt, '[') for sect in sects: # do something with sect, like get its name # (the stuff up to ']') and read its assignments
for line in open( 'hypothetical.ini').readlines(): if line[0] == '[': current_section = line(1:-2) elif line[0] == ';': pass # ignore comments else : apply_value(current_section, line)
global state, blocks, bl_num, newblock #-- Initialize the globals state = "HEADER" blocks = [""] bl_num = 0 newblock = 1 for line in fhin.readlines(): if state == "HEADER": # blank line means new block of header if blankln.match(line): newblock = 1 elif textln.match(line): startText(line) elif codeln.match(line): startCode(line) else : if newblock: startHead(line) else : blocks[bl_num] = blocks[bl_num] + line elif state == "TEXT": # blank line means new block of text if blankln.match(line): newblock = 1 elif headln.match(line): startHead(line) elif codeln.match(line): startCode(line) else : if newblock: startText(line) else : blocks[bl_num] = blocks[bl_num] + line elif state == "CODE": # blank line does not change state if blankln.match(line): blocks[bl_num] = blocks[bl_num] + line elif headln.match(line): startHead(line) elif textln.match(line): startText(line) else : blocks[bl_num] = blocks[bl_num] + line else : raise ValueError, "unexpected input block state: "+state
from
string
import
upper
class
StateMachine
:
def
__init__
(self):
self.handlers = {}
self.startState = None
self.endStates = []
def
add_state
(self, name, handler, end_state=0):
name = upper(name)
self.handlers[name] = handler
if
end_state:
self.endStates.append(name)
def
set_start
(self, name):
self.startState = upper(name)
def
run
(self, cargo):
try
:
handler = self.handlers[self.startState]
except
:
raise
"InitializationError",
"must call .set_start() before .run()"
if
not
self.endStates:
raise
"InitializationError",
"at least one state must be an end_state"
while
1:
(newState, cargo) = handler(cargo)
if
upper(newState)
in
self.endStates:
break
else
:
handler = self.handlers[upper(newState)]
from statemachine import StateMachine def ones_counter (val): print "ONES State: ", while 1: if val <= 0 or val >= 30: newState = "Out_of_Range" ; break elif 20 <= val < 30: newState = "TWENTIES"; break elif 10 <= val < 20: newState = "TENS"; break else : print " @ %2.1f+" % val, val = math_func(val) print " >>" return (newState, val) def tens_counter (val): print "TENS State: ", while 1: if val <= 0 or val >= 30: newState = "Out_of_Range"; break elif 1 <= val < 10: newState = "ONES"; break elif 20 <= val < 30: newState = "TWENTIES"; break else : print " #%2.1f+" % val, val = math_func(val) print " >>" return (newState, val) def twenties_counter (val): print "TWENTIES State:", while 1: if val <= 0 or val >= 30: newState = "Out_of_Range"; break elif 1 <= val < 10: newState = "ONES"; break elif 10 <= val < 20: newState = "TENS"; break else : print " *%2.1f+" % val, val = math_func(val) print " >>" return (newState, val) def math_func (n): from math import sin return abs(sin(n))*31 if __name__== "__main__": m = StateMachine() m.add_state( "ONES", ones_counter) m.add_state( "TENS", tens_counter) m.add_state( "TWENTIES", twenties_counter) m.add_state( "OUT_OF_RANGE", None, end_state=1) m.set_start( "ONES") m.run(1)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有