3.16. Getting Minimum and Maximum of Multiple VersionsΒΆ
Changed in version 2.10.2: The functions semver.max_ver()
and semver.min_ver()
are deprecated in
favor of their builtin counterparts max()
and min()
.
Since Version
implements
__gt__()
and
__lt__()
, it can be used with builtins requiring:
>>> max([Version(0, 1, 0), Version(0, 2, 0), Version(0, 1, 3)])
Version(major=0, minor=2, patch=0, prerelease=None, build=None)
>>> min([Version(0, 1, 0), Version(0, 2, 0), Version(0, 1, 3)])
Version(major=0, minor=1, patch=0, prerelease=None, build=None)
Incidentally, using map()
, you can get the min or max version of any number of versions of the same type
(convertible to Version
).
For example, here are the maximum and minimum versions of a list of version strings:
>>> max(['1.1.0', '1.2.0', '2.1.0', '0.5.10', '0.4.99'], key=Version.parse)
'2.1.0'
>>> min(['1.1.0', '1.2.0', '2.1.0', '0.5.10', '0.4.99'], key=Version.parse)
'0.4.99'
And the same can be done with tuples:
>>> max(map(lambda v: Version(*v), [(1, 1, 0), (1, 2, 0), (2, 1, 0), (0, 5, 10), (0, 4, 99)])).to_tuple()
(2, 1, 0, None, None)
>>> min(map(lambda v: Version(*v), [(1, 1, 0), (1, 2, 0), (2, 1, 0), (0, 5, 10), (0, 4, 99)])).to_tuple()
(0, 4, 99, None, None)
For dictionaries, it is very similar to finding the max version tuple: see Converting a Version instance into Different Types.