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
|
.. _sec_creating_subclasses_from_versioninfo:
Creating Subclasses from Version
================================
If you do not like creating functions to modify the behavior of semver
(as shown in section :ref:`sec_dealing_with_invalid_versions`), you can
also create a subclass of the :class:`Version <semver.version.Version>` class.
For example, if you want to output a "v" prefix before a version,
but the other behavior is the same, use the following code:
.. literalinclude:: semverwithvprefix.py
:language: python
:lines: 4-
The derived class :class:`SemVerWithVPrefix` can be used like
the original class. Additionally, you can pass "incomplete"
version strings like ``v2.3``:
.. code-block:: python
>>> v1 = SemVerWithVPrefix.parse("v1.2.3")
>>> assert str(v1) == "v1.2.3"
>>> print(v1)
v1.2.3
>>> v2 = SemVerWithVPrefix.parse("v2.3")
>>> v2 > v1
True
>>> bad = SemVerWithVPrefix.parse("1.2.4")
Traceback (most recent call last):
...
ValueError: '1.2.4': not a valid semantic version tag. Must start with 'v' or 'V'
|