Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
** Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement! **
Version Handling¶
from packaging.version import Version, parsev1 = parse("1.0a5")
v1<Version('1.0a5')>v2 = Version("1.0")<Version('1.0')>v1 < v2Truev1.epoch0v1.release(1, 0)v1.pre('a', 5)v1.is_prereleaseTruev2.is_prereleaseFalseVersion("french toast")---------------------------------------------------------------------------
InvalidVersion Traceback (most recent call last)
<ipython-input-10-6e617289b19c> in <module>
----> 1 Version("french toast")
/usr/local/lib/python3.8/site-packages/packaging/version.py in __init__(self, version)
275 match = self._regex.search(version)
276 if not match:
--> 277 raise InvalidVersion("Invalid version: '{0}'".format(version))
278
279 # Store the parsed out pieces of the version
InvalidVersion: Invalid version: 'french toast'Version("1.0").postVersion("1.0").is_postreleaseFalseVersion("1.0.post0").post0Version("1.0.post0").is_postreleaseTruefrom packaging.specifiers import SpecifierSet
from packaging.version import Versionspec = SpecifierSet("")
spec<SpecifierSet('')>"1.0" in specTrueSpecifierSet("==1.0")<SpecifierSet('==1.0')>spec1 = SpecifierSet("~=1.0")
spec1<SpecifierSet('~=1.0')>spec2 = SpecifierSet(">=1.0")
spec2<SpecifierSet('>=1.0')>spec3 = SpecifierSet("~=1.0,>=1.0")
spec3<SpecifierSet('>=1.0,~=1.0')>Combine specifiers.
combined_spec = spec1 & spec2
combined_spec<SpecifierSet('>=1.0,~=1.0')>The combination of spec1 (~=1.0) and spec2 (>=1.0) is the same as spec3 (~=1.0,>=1.0).
combined_spec == spec3TrueImplicitly combine a string specifier.
combined_spec &= "!=1.1"
combined_spec<SpecifierSet('!=1.1,>=1.0,~=1.0')>Create a few versions to check for contains.
v1 = Version("1.0a5")
v1<Version('1.0a5')>v2 = Version("1.0")
v2<Version('1.0')>Check a version object to see if it falls within a specifier.
v1 in combined_specFalsev2 in combined_specTrueYou can doo the same with a string based version.
"1.4" in combined_specTrueFilter a list of versions to get only those which are contained within a specifier.vers = [v1, v2, "1.4"]
list(combined_spec.filter(vers))[<Version('1.0')>, '1.4']