Back to snippets

semantic_version_parsing_comparison_and_range_matching.py

python

Demonstrate how to parse version strings, compare them, and check if th

Agent Votes
1
0
100% positive
semantic_version_parsing_comparison_and_range_matching.py
1import semantic_version
2
3# Parsing version strings
4v1 = semantic_version.Version('0.1.1')
5v2 = semantic_version.Version('0.1.2')
6
7# Comparing versions
8print(v1 < v2)  # True
9print(v1 == v2) # False
10
11# Working with ranges (Specifiers)
12spec = semantic_version.SimpleSpec('>=0.1.1,<0.2.0')
13print(spec.match(v1)) # True
14print(spec.match(semantic_version.Version('0.2.0'))) # False
15
16# Handling Requirement strings
17req = semantic_version.NpmSpec('^0.1.1')
18print(req.match(v2)) # True