Minor cleanup.

This commit is contained in:
Simon Forman
2022-03-22 21:24:07 -07:00
parent 7dbb3c69ed
commit 4827aa467e
3 changed files with 22 additions and 21 deletions
+10 -3
View File
@@ -1,8 +1,10 @@
from collections import namedtuple
from re import compile as RE
Snippet = namedtuple('Snippet', 'sha offset length')
_fmt = '{%s %i %i}'
pat = (
'{'
'\s*'
@@ -14,14 +16,18 @@ pat = (
'\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)
@@ -29,5 +35,6 @@ def from_string(text):
raise ValueError
return _fs(**m.groupdict())
def _fs(sha, offset, length):
return Snippet(sha.encode('ascii'), int(offset), int(length))