Skip to content

Instantly share code, notes, and snippets.

@martynjarvis
Created January 26, 2015 14:54
Show Gist options
  • Save martynjarvis/f34af9ada6ccd2556795 to your computer and use it in GitHub Desktop.
Save martynjarvis/f34af9ada6ccd2556795 to your computer and use it in GitHub Desktop.
Class to represent a iteration number
"""
Class to represent a iteration number as a tuple
ie, it-39.1-130956 is represented as (39,1,130956)
"""
import re
import collections
def cast_parts(parts):
for part in parts:
try:
yield int(part)
except ValueError:
yield part
except TypeError:
yield 0
class Version(collections.namedtuple('Version', ['major', 'minor', 'change'])):
__slots__ = ()
def __new__(cls, version):
m = re.search(r'it-([0-9]+)\.([0-9]+)(?:-([A-Z0-9]+))?', version)
self = cls._make(cast_parts(m.groups()))
return self
# test:
a = Version('it-39.1-130956')
b = Version('it-36.2')
# can now do comparisons
assert(a>b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment