Skip to content

Instantly share code, notes, and snippets.

@artnez
Created April 2, 2014 16:01
Show Gist options
  • Save artnez/9937119 to your computer and use it in GitHub Desktop.
Save artnez/9937119 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
#
# For Keri. There really isn't a generalizable way to go about this because the
# input has very specific rules. But here's a pretty typical way to do it.
from re import compile, split
RE_DELIMETERS = compile(r'[\/\s]+')
RE_NUMERIC = compile(r'^[0-9]+')
RE_BINARY_OPS = compile(r'^[\+\-]+')
def parse(string):
values = split(RE_DELIMETERS, string)
result = []
for value in values:
if RE_NUMERIC.match(value):
result.append(bool(int(value)))
elif RE_BINARY_OPS.match(value):
result.extend([op == '+' for op in value])
else:
raise ValueError('Unrecognized value: %s' % value)
return tuple(result)
assert parse('103 234 0 1') == (True, True, False, True)
assert parse('103') == (True,)
assert parse('+') == (True,)
assert parse('+/-') == (True, False)
assert parse('++--++') == (True, True, False, False, True, True)
assert parse('53/0 -+ 0/0/10/++/--') == \
(True, False, False, True, False, False, True, True, True, False, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment