1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
Comparing Versions
==================
To compare two versions depends on your type:
* **Two strings**
Use :func:`semver.compare <semver._deprecated.compare>`::
>>> semver.compare("1.0.0", "2.0.0")
-1
>>> semver.compare("2.0.0", "1.0.0")
1
>>> semver.compare("2.0.0", "2.0.0")
0
The return value is negative if ``version1 < version2``, zero if
``version1 == version2`` and strictly positive if ``version1 > version2``.
* **Two** :class:`~semver.version.Version` **instances**
Use the specific operator. Currently, the operators ``<``,
``<=``, ``>``, ``>=``, ``==``, and ``!=`` are supported::
>>> v1 = Version.parse("3.4.5")
>>> v2 = Version.parse("3.5.1")
>>> v1 < v2
True
>>> v1 > v2
False
* **A** :class:`~semver.version.Version` **type and a** :func:`tuple` **or** :func:`list`
Use the operator as with two :class:`~semver.version.Version` types::
>>> v = Version.parse("3.4.5")
>>> v > (1, 0)
True
>>> v < [3, 5]
True
The opposite does also work::
>>> (1, 0) < v
True
>>> [3, 5] > v
True
* **A** :class:`~semver.version.Version` **type and a** :func:`str`
You can use also raw strings to compare::
>>> v > "1.0.0"
True
>>> v < "3.5.0"
True
The opposite does also work::
>>> "1.0.0" < v
True
>>> "3.5.0" > v
True
However, if you compare incomplete strings, you get a :py:exc:`python:ValueError` exception::
>>> v > "1.0"
Traceback (most recent call last):
...
ValueError: 1.0 is not valid SemVer string
* **A** :class:`~semver.version.Version` **type and a** :func:`dict`
You can also use a dictionary. In contrast to strings, you can have an "incomplete"
version (as the other parts are set to zero)::
>>> v > dict(major=1)
True
The opposite does also work::
>>> dict(major=1) < v
True
If the dictionary contains unknown keys, you get a :py:exc:`python:TypeError` exception::
>>> v > dict(major=1, unknown=42)
Traceback (most recent call last):
...
TypeError: ... got an unexpected keyword argument 'unknown'
Other types cannot be compared.
If you need to convert some types into others, refer to :ref:`sec.convert.versions`.
The use of these comparison operators also implies that you can use builtin
functions that leverage this capability; builtins including, but not limited to: :func:`max`, :func:`min`
(for examples, see :ref:`sec_max_min`) and :func:`sorted`.
|