Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48f92c6

Browse files
committed
Improve documentation with examples of how to use the builtin max and min. Keep the old way of doing for reference.
1 parent 81a84ae commit 48f92c6

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

docs/usage.rst

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,27 +483,53 @@ That gives you the following possibilities to express your condition:
483483
.. _sec_max_min:
484484

485485
Getting Minimum and Maximum of Multiple Versions
486-
-------------------------------------------
486+
------------------------------------------------
487487
.. versionchanged:: 2.10.2
488488
The functions :func:`semver.max_ver` and :func:`semver.min_ver` are deprecated in
489489
favor of their builtin counterparts :func:`max` and :func:`min`.
490490

491491
Since :class:`semver.VersionInfo` implements :func:`__gt__()` and :func:`__lt__()`, it can be used with builtins requiring
492492

493-
.. code-block:: python
494-
495-
>>> str(max(semver.VersionInfo.parse("1.0.0"), semver.VersionInfo.parse("2.0.0")))
496-
'2.0.0'
497-
>>> str(min(semver.VersionInfo.parse("1.0.0"), semver.VersionInfo.parse("2.0.0")))
498-
'1.0.0'
499-
500493
.. code-block:: python
501494
502495
>>> max([semver.VersionInfo(0, 1, 0), semver.VersionInfo(0, 2, 0), semver.VersionInfo(0, 1, 3)])
503496
VersionInfo(major=0, minor=2, patch=0, prerelease=None, build=None)
504497
>>> min([semver.VersionInfo(0, 1, 0), semver.VersionInfo(0, 2, 0), semver.VersionInfo(0, 1, 3)])
505498
VersionInfo(major=0, minor=1, patch=0, prerelease=None, build=None)
506499
500+
Incidentally, using :func:`map`, you can get the min or max version of any number of versions of the same type
501+
(convertible to :class:`semver.VersionInfo`).
502+
503+
For example, here are the maximum and minimum versions of a list of version strings:
504+
505+
.. code-block:: python
506+
507+
>>> str(max(map(semver.VersionInfo.parse, ['1.1.0', '1.2.0', '2.1.0', '0.5.10', '0.4.99'])))
508+
'2.1.0'
509+
>>> str(min(map(semver.VersionInfo.parse, ['1.1.0', '1.2.0', '2.1.0', '0.5.10', '0.4.99'])))
510+
'0.4.99'
511+
512+
And the same can be done with tuples:
513+
514+
.. code-block:: python
515+
516+
>>> max(map(lambda v: semver.VersionInfo(*v), [(1, 1, 0), (1, 2, 0), (2, 1, 0), (0, 5, 10), (0, 4, 99)]))).to_tuple()
517+
(2, 1, 0)
518+
>>> min(map(lambda v: semver.VersionInfo(*v), [(1, 1, 0), (1, 2, 0), (2, 1, 0), (0, 5, 10), (0, 4, 99)]))).to_tuple()
519+
(0, 4, 99)
520+
521+
For dictionaries, it is very similar to finding the max version tuple: see :ref:`_sec.convert.versions`.
522+
523+
The Old Way
524+
^^^^^^^^^^^
525+
526+
.. code-block:: python
527+
528+
>>> semver.max_ver("1.0.0", "2.0.0")
529+
'2.0.0'
530+
>>> semver.min_ver("1.0.0", "2.0.0")
531+
'1.0.0'
532+
507533
508534
Dealing with Invalid Versions
509535
-----------------------------

0 commit comments

Comments
 (0)