Skip to content

Instantly share code, notes, and snippets.

@akaszynski
Last active February 4, 2020 21:05
Show Gist options
  • Save akaszynski/3624c52e32421e5e6b35561da2e0383c to your computer and use it in GitHub Desktop.
Save akaszynski/3624c52e32421e5e6b35561da2e0383c to your computer and use it in GitHub Desktop.
Check if a Python version string meets a minimum version
def version_tuple(v):
"""Converts a version string to a tuple"""
return tuple(map(int, (v.split("."))))
def meets_version(va, vb):
"""Check if a version string meets a minimum version.
Parameters
----------
va : str
Version string. For example ``'0.25.1'``.
va : str
Version string. For example ``'0.25.2'``.
Returns
-------
newer : bool
True if version ``va`` is greater or equal to version ``vb``.
Examples
--------
>>> meets_version('0.25.1', '0.25.2')
False
>>> meets_version('0.26.0', '0.25.2')
True
"""
va = version_tuple(va)
vb = version_tuple(vb)
for i in range(3):
if va[i] > vb[i]:
return True
elif va[i] < vb[i]:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment