%s
----- end (%s)
+---- end ( %s )
'''
Use a Boolean value to select one of two items.
::
- A B False choice
+ A B false choice
----------------------
A
- A B True choice
+ A B true choice
---------------------
B
Use a Boolean value to select one of two items from a sequence.
::
- [A B] False select
+ [A B] false select
------------------------
A
- [A B] True select
+ [A B] true select
-----------------------
B
v = frame
try: frame = stack.pop()
except IndexError:
- raise ParseError('Extra closing bracket.')
+ raise ParseError('Extra closing bracket.') from None
frame.append(list_to_stack(v))
- elif tok == 'true':
- frame.append(True)
- elif tok == 'false':
- frame.append(False)
- elif isinstance(tok, Snippet):
- frame.append(tok)
+ elif tok == 'true': frame.append(True)
+ elif tok == 'false': frame.append(False)
+ elif isinstance(tok, Snippet): frame.append(tok)
else:
- try:
- thing = int(tok)
- except ValueError:
- thing = Symbol(tok)
+ try: thing = int(tok)
+ except ValueError: thing = Symbol(tok)
frame.append(thing)
- if stack:
- raise ParseError('Unclosed bracket.')
+ if stack: raise ParseError('Unclosed bracket.')
return list_to_stack(frame)
from collections import namedtuple
from re import compile as RE
+
Snippet = namedtuple('Snippet', 'sha offset length')
-_fmt = '{%s %i %i}'
+
+
pat = (
'{'
'\s*'
'\s*'
'}'
)
+
+
_PAT = RE(pat)
def to_string(snip):
- return _fmt % _ts(*snip)
+ return _ts(*snip)
+
def _ts(sha, offset, length):
- return sha.decode('ascii'), offset, length
+ return f'{{{sha.decode("ascii")} {offset} {length}}}'
+
def from_string(text):
m = _PAT.match(text)
raise ValueError
return _fs(**m.groupdict())
+
def _fs(sha, offset, length):
return Snippet(sha.encode('ascii'), int(offset), int(length))