support.semver – Semantic Versioning Support

This module defines functions and classes for a semantic versioning (aka SemVer) support.

The parser match the semantic versioning specification 2.0.0.

Public Classes

This module has only one public class.

SemVer

Objects reference

This section details the objects defined in this module.

class support.semver.SemVer(version_string)

Bases: object

SemVer parser class.

Parameters

version_string (str) – The version string matching the semantic versioning specification. Otherwise a ValueError exception is raised.

Raises
  • TypeError – Parameters type mismatch.

  • ValueError – Version string do not match the specification rules.

major

get the major version number.

Type

property

minor

get the minor version number.

Type

property

patch

get the patch version number.

Type

property

unstable

indicate if the version is unstable.

Type

property

Special Methods

This class has a number of special methods, listed below in alphabetical order, to make the version comparison.

__eq__

__ne__

__gt__

__repr__

__lt__

Using SemVer…

The main purpose of this class is to compute comparison between version identifier as described in semantic versioning specification. So the using is limited to create class instance with the version identifier string and use the comparison operator as shown in the below example.

>>> import lapptrack.cots.semver as semver
>>> v1 = semver.SemVer("1.0.0")
>>> v2 = semver.SemVer("2.0.0")
>>> v1 < v2
True
>>> v1 = semver.SemVer("1.0.0-alpha")
>>> v2 = semver.SemVer("1.0.0-beta")
>>> v1 > v2
False
>>> v1 = semver.SemVer("1.0.0")
>>> v2 = semver.SemVer("1.0.0")
>>> v1 < v2
False
>>> v1 == v2
True
__eq__(other)

Rich comparison method, return self == other.

__gt__(other)

Rich comparison method, return self > other.

__init__(version_string)
__lt__(other)

Rich comparison method, return self < other.

__ne__(other)

Rich comparison method, return self != other.

__repr__()

Compute the string representation of the SemVer object.

Returns

the version string which can be used to recreate the

SemVer object.

Return type

str

_parse(version_string)

Parse the version string.

Parameters

version_string (str) – The version string matching the semantic versioning specification. Otherwise a ValueError exception is raised.

Raises
  • TypeError – Parameters type mismatch.

  • ValueError – Version string do not match the specification rules.

__hash__ = None
__weakref__

list of weak references to the object (if defined)

property major

Get the major version number.

Returns

The version number with only the major number.

Return type

str

property minor

Get the minor version number.

Returns

The version number with the major and the minor number .

Return type

str

property patch

Get the patch version number.

Returns

The version number with the major, the minor and the patch

number.

Return type

str

property unstable

Indicate if the version is unstable.

Returns

True if the version is unstable.

Return type

bool

Functions reference

This section details the specific functions used in this module.

support.semver._comp_version(version1, version2)

Compare two version identifiers limited to the major, minor and patch fields.

The rule # 11 specify the precedence rules for comparing version identifiers (i.e. 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1).

Parameters
  • version1 (list) – The list of fields from the version string, constituted by only ASCII digits [0-9], as that order: major, minor, patch.

  • version2 (list) – same as version1.

Returns

-1, 0 or 1 as below

  • -1: version1 is lower than version2

  • 0: version1 is equal to version2

  • 1: version1 is greater than version2

Return type

int

Raises

TypeError – Parameters type mismatch.

support.semver._comp_prerelease(prerelease1, prerelease2)

Compare two pre-release version identifiers.

The rule # 11 specify the precedence rules for comparing pre-release version identifiers (i.e. 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0).

Parameters
  • prerelease1 (list) – The list of fields from the pre-release identifier, constituted by only ASCII alphanumerics and hyphen [0-9A-Za-z-]. The list may be empty.

  • prerelease2 (list) – Same as prerelease1.

Returns

-1, 0 or 1 as below

  • -1: prerelease1 is lower than prerelease2

  • 0: prerelease1 is equal to prerelease2

  • 1: prerelease1 is greater than prerelease2

Return type

int

Raises

TypeError – Parameters type mismatch.

support.semver._compstr(string1, string2)

Compare two string either numerically or lexically.

If the two string are constituted of only digits, there are compared numerically and strings with letters or hyphens are compared lexically in ASCII sort order. So numeric string always have lower precedence than non-numeric string.

Example

‘2’ < ‘11’``
‘ab’ < ‘abc’, ‘ab’ < ‘ac’, ‘AC’ < ‘ac’
‘12’ < ‘abc’
Parameters
  • string1 (str) – A string constituted by only ASCII alphanumerics and hyphen [0-9A-Za-z-].

  • string2 (str) – Same as string1

Returns

-1, 0 or 1 as below

  • -1: string1 is lower than string2

  • 0: string1 is equal to string2

  • 1: string1 is greater than string1

Return type

int

Raises

TypeError – Parameters type mismatch.