Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Semantic Version Comparison for Python

Project description

semvercomp

🐍 Semantic Version Comparison for Python

Implementation of a Version object with comparison capabilities and tag validation following semver conventions.

Installation

pip install semvercomp

Usage

Classes

Version(major=0 : int, minor=0 : int, patch=0 : int, pre_release="" : str, patch="" : str, has_v=False : str)

Class Version represents a version tag following semver conventions. A version tag is structured the following way:

X.Y.Z-pre+build
Value Name Description
X Major Version when you make incompatible API changes
Y Minor Version when you add functionality in a backwards-compatible manner
Z Patch Version when you make backwards-compatible bug fixes
pre Pre Release Version pre-release tag
build Build Version build tag

Source: Semantic Versioning 2.0.0

  • Public Properties
Key Value Type
has_v bool Flag for preceding v or V in version tag
major int Major member
minor int Minor member
patch int Patch member
pre_release str Pre Release member
build str Build member
  • Instance of Version
from semvercomp.Version import Version

ver = Version(1, 0, 0, 'beta', '20191224')
print(str(ver))
# 1.0.0-beta+20191224
  • Creating a Version object from a version string:
from semvercomp.Version import Version

str_v = Version()
str_v.parse_version_number('v1.0.0-beta')
# str_v.major == 1
# str_v.minor == 0
# str_v.patch == 0
# str_v.has_v == True
# str_v.pre_release == 'beta'

Comparison of Version Objects

Version class implements __gt__, __lt__ and __eq__ built-in methods to implement comparison.

Equal

from semvercomp.Version import Version

a = Version(1, 0, 0)
b = Version(1, 0, 0)
print(a == b) # True

Greater

from semvercomp.Version import Version

a = Version(1, 1, 0)
b = Version(1, 0, 0)
print(a > b) # True

Lower

from semvercomp.Version import Version

a = Version(0, 1, 0)
b = Version(1, 0, 0)
print(a < b) # True

Validation

It is possible to validate and gather the different members of a version tag using validate version from semvercomp.validators.

validate_version(version: str): (parts: dict(), is_ok: bool)

Will return a tuple where, the first element is a dictionary with the properties of the given version tag destructured.

The second element in the tuple is a boolean flag that stands as the validation result.

from semvercomp.validators import validate_version

ver_str = 'v1.0.22'
(parts, is_ok) = validate_version(ver_str)
print(parts) # {'has_v': True, 'major': 1, 'minor': 0, 'patch': 22, 'pre_release': None, 'build': None}
print(is_ok) # True

Utils

to_version_list(coll: iterable): Version[]

Create an array of Version objects from an iterable of version tags as strings.

from semvercomp.utils import to_version_list

all = [
	'1.0.0-beta',
	'0.1.0+1234',
	'33.22.3'
]

versions = to_version_list(all)

Development

Requirements

Debugging

  • Debugging tests Tests for this package are written with pytest. The following json, is an example of the .vscode/settings.json:
{
	"python.pythonPath": /* Your Python Binary Address*/,
	"python.testing.pytestArgs": [
		"tests"
	],
	"python.testing.unittestEnabled": false,
	"python.testing.nosetestsEnabled": false,
	"python.testing.pytestEnabled": true
}

Testing

  • Running unit tests
# from repository root directory
pytest
  • Running test coverage
# from repository root directory
pytest --cov=semvercomp tests/

# or with html report
pytest --cov-report html --cov=semvercomp tests/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for semvercomp, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size semvercomp-0.1.1-py3-none-any.whl (7.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size semvercomp-0.1.1.tar.gz (5.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page