Last active
February 4, 2020 21:05
-
-
Save akaszynski/3624c52e32421e5e6b35561da2e0383c to your computer and use it in GitHub Desktop.
Check if a Python version string meets a minimum version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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