MCPcopy Create free account
hub / github.com/ActiveState/code / parse

Function parse

recipes/Python/361168_Vpython_example/recipe-361168.py:215–247  ·  view source on GitHub ↗

Parses the user input, starting with the number of blocks

()

Source from the content-addressed store, hash-verified

213 print self.box.pos
214
215def parse():
216 """Parses the user input, starting with the number of blocks"""
217 # Get the number of blocks
218 while 1:
219 num = sys.stdin.readline()
220 try:
221 num = int(num[:-1])
222 except Exception, e:
223 if num.lower() == 'quit\n': return
224 else: continue
225 else: break
226 # Create the objects
227 stacks = Stacks(num)
228 arm = Arm(stacks)
229 # Now parse each line of input ignoring dumb commands
230 while 1:
231 line = sys.stdin.readline().lower()[:-1] # Remove the enter at the end and convert to lowercase
232 if line == 'quit':
233 arm.quit()
234 break
235 else:
236 words = line.split()
237 if len(words) != 4: continue
238 if words[0] not in ('move', 'pile'): continue
239 try:
240 a = int(words[1])
241 b = int(words[3])
242 except:
243 continue
244 if words[2] not in ('onto', 'over'): continue
245 command = words[0] + words[2].capitalize()
246 func = getattr(arm, command)
247 func(a,b)
248
249def test1():
250 s = RealStacks(10)

Callers 1

test2Function · 0.70

Calls 8

quitMethod · 0.95
StacksClass · 0.85
ArmClass · 0.85
capitalizeMethod · 0.80
funcFunction · 0.50
readlineMethod · 0.45
lowerMethod · 0.45
splitMethod · 0.45

Tested by

no test coverage detected