Create a gist now

Instantly share code, notes, and snippets.

@jul /version.py
Last active Aug 29, 2015

What would you like to do?
parser for my own reduced semantic version :)
"""Given a string of three comma separated values X.Y.Z
where
X = API change
Y = improvement
Z = bugfix
and where X, Y, Z is in the form figures followed by letters
where figures should be compared as decimal int and letters ... as letters
by convention letters are ascii lower case alphabet letters.
"""
import re
FIGURE = re.compile("(?P<N>[0-9]*)(?P<S>[a-z]*?)$")
ZERO = (0 ,'')
def parse_figure(s_fig):
high = int(FIGURE.match(s_fig.lower()).group("N") or 0)
low = FIGURE.match(s_fig.lower()).group("S") or ""
return (high, low)
to_tuple_version = lambda version_str: tuple(map(parse_figure, version_str.split(".")))
__t = to_tuple_version
superior = lambda a, b: to_tuple_version(a) > to_tuple_version(b)
is_in = lambda t, t2: to_tuple_version(t[:len(t2)])==to_tuple_version(t2)
def equal(st1,st2):
t1, t2 = map(to_tuple_version,(st1,st2))
pad1, pad2 =map(lambda t: max(len(t2), len(t1)) - len(t), (t1,t2))
return t1 + (ZERO, ) * pad1 == t2 + (ZERO, ) * pad2
sup_or_equal = lambda a, b: superior(a,b) or equal(a,b)
assert equal("1","1.0.0.0")
between = lambda a, b, c: superior(c,a) and sup_or_equal(a,b)
release_history = [
'0', '0.0.1', '0.0.1a', '0.1', '0.1.a',"0.1.ab","0.1.az",
"0.1.c", '0.1.9', '0.2', '1.0.0', '1.0.2','1.1.3', "2"
]
rev = [ i for i in reversed(release_history) ]
for i, el in enumerate(rev):
for el_inf in rev[i+1:]:
print "checking %r > %r" % ( el, el_inf)
assert superior(el, el_inf)
""" http://legacy.python.org/dev/peps/pep-0440/#examples"""
""" 3.1: version 3.1 or later, but not version 4.0 or later."""
assert is_in("3.1.2","3.1")
assert is_in("4.2.3a","3.1")==False
""" 3.1.2: version 3.1.2 or later, but not version 3.2.0 or later."""
def case2(v):
return superior(v,"3.1.2") and superior("3.2",v)
assert case2("3.3") == False
assert case2("3.1.3") == True
assert case2("3.1.3x") == True
assert case2("3.1.3x.3x") == True
assert case2("3.2.34a") == False
assert case2("4.3") == False
""" superior to 3.1.2 but avoid the 3.2 series"""
def case2(v):
return superior(v,"3.1.2") and not(is_in(v, "3.2"))
assert case2("3.3") == True
assert case2("3.2.34a") == False
assert case2("4.3") == True
""" == 3.1: specifically version 3.1 (or 3.1.0), excludes all pre-releases, post releases, developmental releases and any 3.1.x maintenance releases.
I don't do
"""
""" == 3.1.*: any version that starts with 3.1. Equivalent to the 3.1.0 compatible release clause.
is_in does the job
"""
""" 3.1.0, != 3.1.3: version 3.1.0 or later, but not version 3.1.3 and not version 3.2.0 or later."""
case3 = lambda v: equal(v,"3.1.0") or ( not(equal(v, "3.1.3")) and not(is_in(v,"3.2")) and superior(v,"3.1.0"))
assert case3("3.5")
assert case3("4.0.2")
assert case3("3.2.1")==False
assert case3("3.1.0")==True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment