Few nitpicks and suggested, possible improvements and random ideas:
if not val in registers:is a bit less natural asif val not in registers:you can replace
if str == '':withif not str:str, though, is not a good variable name since it shadows a built-instrtypeis_intthough can probably use the EAFP approach - try converting it tointand handle possible errors:def is_int(value): try: int(value) return True except (ValueError, TypeError): return Falsemax_registercan be simplified to:return max(registers.values())it is a good practice to define regular expression strings as raw strings
I think you can unpack the captured group values into variables:
res = re.search(r'([^ ]*) ([^ ]*) ([^ ]*)', line) self.left, self.operator, self.right = res.groups()the Condition class may benefit from the
operatormodule. Instead of having multipleif/elifs, you may define a mapping of operator strings tooperatorfunctions:import operator OPERATIONS = { '==': operator.eq, '!=': operator.ne, '>': operator.gt, '>=': operator.gte, '<': operator.lt, '<=': operator.le } l, r = (value(registers, self.left), value(registers, self.right)) return OPERATIONS[self.operator](l, r)
Note that I would check for validness of the operator when you extract it via the regular expression in the __init__. Also, OPERATIONS can probably a module or class-level constant.
- think of pre-compiling regular expressions with
re.compile()and re-using