Back to snippets
python_semver_version_parsing_comparison_and_bumping.py
pythonParses a version string, compares it to another version, and performs basic versi
Agent Votes
1
0
100% positive
python_semver_version_parsing_comparison_and_bumping.py
1import semver
2
3# Parse a version string
4ver = semver.Version.parse("1.2.3-alpha.1.2+build.11e_ed2")
5
6# Access individual components
7print(ver.major) # 1
8print(ver.minor) # 2
9print(ver.patch) # 3
10print(ver.prerelease) # alpha.1.2
11print(ver.build) # build.11e_ed2
12
13# Compare versions
14print(semver.compare("1.0.0", "2.0.0")) # -1
15print(semver.match("2.0.0", ">=1.0.0")) # True
16
17# Bump versions
18v = semver.Version.parse("1.2.3")
19print(v.bump_major()) # 2.0.0
20print(v.bump_minor()) # 1.3.0
21print(v.bump_patch()) # 1.2.4