pytoolbox.comparison module¶
- class pytoolbox.comparison.SlotsEqualityMixin[source]¶
Bases:
object
Implement the comparison operators based on the slots. Both the name of the slots retrieved with
pytoolbox.types.get_slots()
and theirs values are tested for equality.
- pytoolbox.comparison.unified_diff(before: str, after: str, *, colorize: bool = True, **kwargs) str [source]¶
Colorization is not guaranteed (your environment may disable it). Use pytoolbox.console.toggle_colors appropriately to ensure it.
- class pytoolbox.comparison.Version(version: str)[source]¶
Bases:
_BaseVersion
This class abstracts handling of a project’s versions.
A
Version
instance is comparison aware and can be compared and sorted using the standard Python interfaces.>>> v1 = Version("1.0a5") >>> v2 = Version("1.0") >>> v1 <Version('1.0a5')> >>> v2 <Version('1.0')> >>> v1 < v2 True >>> v1 == v2 False >>> v1 > v2 False >>> v1 >= v2 False >>> v1 <= v2 True
- __init__(version: str) None [source]¶
Initialize a Version object.
- Parameters:
version – The string representation of a version which will be parsed and normalized before use.
- Raises:
InvalidVersion – If the
version
does not conform to PEP 440 in any way then this exception will be raised.
- property epoch: int¶
The epoch of the version.
>>> Version("2.0.0").epoch 0 >>> Version("1!2.0.0").epoch 1
- property release: Tuple[int, ...]¶
The components of the “release” segment of the version.
>>> Version("1.2.3").release (1, 2, 3) >>> Version("2.0.0").release (2, 0, 0) >>> Version("1!2.0.0.post0").release (2, 0, 0)
Includes trailing zeroes but not the epoch or any pre-release / development / post-release suffixes.
- property pre: Tuple[str, int] | None¶
The pre-release segment of the version.
>>> print(Version("1.2.3").pre) None >>> Version("1.2.3a1").pre ('a', 1) >>> Version("1.2.3b1").pre ('b', 1) >>> Version("1.2.3rc1").pre ('rc', 1)
- property post: int | None¶
The post-release number of the version.
>>> print(Version("1.2.3").post) None >>> Version("1.2.3.post1").post 1
- property dev: int | None¶
The development number of the version.
>>> print(Version("1.2.3").dev) None >>> Version("1.2.3.dev1").dev 1
- property local: str | None¶
The local version segment of the version.
>>> print(Version("1.2.3").local) None >>> Version("1.2.3+abc").local 'abc'
- property public: str¶
The public portion of the version.
>>> Version("1.2.3").public '1.2.3' >>> Version("1.2.3+abc").public '1.2.3' >>> Version("1.2.3+abc.dev1").public '1.2.3'
- property base_version: str¶
The “base version” of the version.
>>> Version("1.2.3").base_version '1.2.3' >>> Version("1.2.3+abc").base_version '1.2.3' >>> Version("1!1.2.3+abc.dev1").base_version '1!1.2.3'
The “base version” is the public version of the project without any pre or post release markers.
- property is_prerelease: bool¶
Whether this version is a pre-release.
>>> Version("1.2.3").is_prerelease False >>> Version("1.2.3a1").is_prerelease True >>> Version("1.2.3b1").is_prerelease True >>> Version("1.2.3rc1").is_prerelease True >>> Version("1.2.3dev1").is_prerelease True
- property is_postrelease: bool¶
Whether this version is a post-release.
>>> Version("1.2.3").is_postrelease False >>> Version("1.2.3.post1").is_postrelease True
- property is_devrelease: bool¶
Whether this version is a development release.
>>> Version("1.2.3").is_devrelease False >>> Version("1.2.3.dev1").is_devrelease True
- pytoolbox.comparison.satisfy_version_constraints(version: str | None, constraints: tuple[str, ...], *, default='<undefined>') bool [source]¶
Ensure given version fulfill the constraints (if any).
Constraints are given in the form ‘<operator> <version>’, Exemple:
>>> satisfy_version_constraints('v1.5.2', ['>= v1.5', '< v2']) True >>> satisfy_version_constraints('v0.7', ['>= v1.5', '< v2']) False >>> satisfy_version_constraints(None, ['>= v1.5', '< v2']) False >>> satisfy_version_constraints('main', ['!= main']) False >>> satisfy_version_constraints(None, ['== <undefined>']) True >>> satisfy_version_constraints(None, ['!= master'], default='master') False