API (the verschemes package)

The bases and default version implementation (verschemes module)

verschemes module

This module can be used to manage and enforce rules for version numbering.

verschemes.DEFAULT_FIELD_TYPE

The type used for fields with no type explicitly specified.

alias of int

class verschemes.SegmentField[source]

Bases: verschemes._SegmentField

The definition of an atomic portion of a version segment value.

This is an immutable set of metadata defining the parameters of a field in a SegmentDefinition.

The attributes can be set only in the constructor by name or position and can be accessed by attribute name (preferred) or item index:

  1. type (default: int) is the underlying type of the field value.

    Only int and str have been tested.

  2. name (default: “value”) is the name of the field, which must be unique among all of the fields in a segment.

    Each segment will often have only one field, so the name is defaulted to something quite generic, but it must be explicitly set to ensure uniqueness when the SegmentDefinition contains multiple fields.

    If the field name is specified, it must be a valid Python identifier that does not start with an underscore. This field can then be identified by this name in the segment value of a Version subclass if the corresponding segment definition contains multiple fields, because the value is a subclass of namedtuple named Segment that has fields corresponding to the segment definition’s fields.

  3. re_pattern (default: “[0-9]+”) is the regular expression pattern that the string representation of the field value must match.

  4. render (default: str) is the function that takes the underlying value and returns its appropriate string representation. Note that in Python 2 this str is from the future package and is similar to unicode.

verschemes.DEFAULT_SEGMENT_FIELD = SegmentField(type=<class 'int'>, name='value', re_pattern='[0-9]+', render=<class 'str'>)

The default SegmentField instance.

verschemes.DEFAULT_SEGMENT_SEPARATOR = '.'

The separator used for segments with no separator explicitly specified.

class verschemes.SegmentDefinition[source]

Bases: verschemes._SegmentDefinition

The definition of a version segment.

This is an immutable set of metadata defining the parameters of a segment.

The attributes can be set only in the constructor by name or position and can be accessed by attribute name (preferred) or item index:

  1. optional (default: False) indicates whether the segment may be excluded from rendering and whether its value is allowed to be unspecified even if the segment has no default.

  2. default (default: None) is the implied value of the segment when the value is unspecified (or None).

  3. separator (default: ”.”) is the string within the version’s string representation that comes just before the segment value(s).

    This value is ignored for the first segment in a version and also not rendered when all optional segments before it are unspecified.

  4. fields (default: a singular SegmentField instance) is the sequence of metadata (SegmentField instances) for the field(s) in the segment.

  5. name (default: None) is an optional name for the segment.

    If the segment name is specified, it must be a valid Python identifier that does not start with an underscore. This segment can then be identified by this name in the Version subclass in which it is used:

    1. The constructor and the Version.replace method will accept this name as a keyword argument, overriding the positional argument if also specified.
    2. There will be a read-only property to access this segment’s value if the name is not already used in the class, so don’t use a name that matches an existing attribute like ‘render’ if you want to use this property as an alternative to index access.
  6. separator_re_pattern (default: None) is an optional regular expression pattern that matches all allowed input separators.

    If it is set, then a string representation given to the Version subclass constructor will allow any input matching this regular expression as the separator for this segment, but the separator attribute is the normal form used when rendering the version.

    If it is not set, then the separator attribute is the only acceptable literal input form in addition to being the normal form.

    This value is ignored for the first segment in a version.

re_pattern[source]

The regular expression pattern for the segment’s possible string representations.

This is generated from the re_patterns of its field(s).

required[source]

Whether a value for this segment is required.

It is required if it is not optional and has no default value.

validate_value(value)[source]

Validate the given value and return the value as the inner type.

The given value may be:

  • a field value if there is only one field,
  • a tuple of field values, or
  • a string that shall be parsed by the fields’ regular expressions.

The result will be:

  • a field value if there is only one field or
  • a Segment (namedtuple subclass) instance whose attributes and values correspond to the fields’ names and values.
render(value)[source]

Return the given segment value as a string.

verschemes.DEFAULT_SEGMENT_DEFINITION = SegmentDefinition(optional=False, default=None, separator='.', fields=(SegmentField(type=<class 'int'>, name='value', re_pattern='[0-9]+', render=<class 'str'>),), name=None, separator_re_pattern=None)

The default SegmentDefinition instance.

class verschemes.Version[source]

Bases: builtins.tuple

A class whose instances adhere to the version scheme it defines.

The versioning rules are defined in SEGMENT_DEFINITIONS, which may be overridden in a subclass to make it more specific to the version scheme represented by the subclass.

The concept is that the class represents a particular set of versioning rules (i.e., a version scheme), and the instances of that class are version identifiers that follow those rules.

Pass the constructor either a version identifier as a str or the individual segment values in order corresponding to the SEGMENT_DEFINITIONS defined by the class (or any number of integers if using the default implementation). Optional segments and segments with default values may be passed a value of None or excluded from the end.

As of version 1.1, keyword arguments may also be given to the constructor when the keywords match the segments’ names. Any keyword arguments for segments also specified positionally will override the positional arguments.

The instance’s segment values can be accessed via standard indexing, slicing, and keys. For example, instance[0] returns the value of the instance’s first segment, instance[3:5] returns a tuple containing the values of the instance’s fourth and fifth segments, and since version 1.1 instance['name'] returns the value of the segment with name ‘name’.

As of version 1.1, individual segment values can also be accessed via properties if the segment was given a name and the name does not conflict with an existing attribute of the class.

The segment values acquired from indexing, slicing, keywords, and properties are cooked according to the segment definition (basically returning the default if the raw value is None). The raw values can be accessed with get_raw_item().

SEGMENT_DEFINITIONS = None

The parameters for segments in this version.

This can only be set as a class attribute and is validated/captured during class creation, after which it can be read as an attribute of the class. It cannot be modified within an existing class. It cannot be accessed directly from an instance.

If not using the default, this must be set to an iterable of SegmentDefinitions, one element for each segment, at least one of which must be non-optional.

The default (empty sequence) is special in that it allows for a variable and logically infinite number of segments, each following the rules of the default SegmentDefinition. This means that the default implementation supports the most common version scheme of an arbitrary number of integer segments separated by dots.

get_raw_item(item=None)[source]

Return the raw segment value (or values if item is a slice).

This is an alternative to __getitem__() (i.e., bracket item retrieval) and named-segment properties, which cook the value(s) according to their segment definition(s).

render(exclude_defaults=True, include_callbacks=(), exclude_callbacks=())[source]

Render the version into a string representation of normal form.

Pass False (or equivalent) for the exclude_defaults argument to stop the default behavior of excluding optional segments that have a default value but have not been explicitly set.

If there was only one way to render a version, then this method would not exist, and its implementation would be in __str__. There is, however, only one default way, which is done when __str__ is called, and that is to call this method with its default arguments.

There could be many ways to render a version, depending on its complexity and features. The base class implements rendering with only one simple argument (exclude_defaults) and two complex arguments (include_callbacks and exclude_callbacks). The two complex arguments (i.e., callback arguments) allow for future versions and subclasses to provide additional simple arguments. (Keep reading if this interests you.)

The signature of this method should be considered the most volatile in the project. The callback arguments should never be passed by position to keep the code prepared for injection of additional simple arguments in the base implementation that are more likely to be passed by position.

Callback structure

The callback arguments are sequences of metadata describing how the simple arguments are processed. The metadata may just be a function or a sequence consisting of a function and any additional arguments. Each callback function requires the ‘version’ (or ‘self’ if its a method) argument and the ‘index’ argument. The “additional” arguments mentioned above follow the required arguments in the callback function’s signature.

Callback processing

The functions in include_callbacks can return True (or equivalent) to force the segment identified by the ‘index’ argument to be included in the rendering. If no “include” callbacks force the inclusion of the segment, then the functions in exclude_callbacks can return True (or equivalent) to exclude the segment of the version identified by the ‘index’ argument in the rendering. Any segment with a value (i.e., not None) that is not excluded by this process will be rendered in the result. The exclude_defaults argument is an example of a simple argument whose affect is implemented via exclude_callbacks with the _render_exclude_defaults_callback method.

replace(**kwargs)[source]

Return a copy of this version with the given segments replaced.

Each keyword argument can either be an underscore (‘_’) followed by the numeric segment index or the segment name. Each identified segment is replaced with the argument’s value. Segment name arguments take precedence over underscore-index arguments.

validate()[source]

Override this in subclasses that require intersegment validation.

Raise an appropriate exception if validation fails.

Included implementations

Python

verschemes.python module

The Python verschemes module implements standard Python versioning.

class verschemes.python.PythonMajorVersion[source]

Bases: verschemes.Version

A major Python version.

This version scheme has one segment that identifies the major Python version, which is only incremented for really major changes in the language. It is the base for the more detailed Python version classes and is mostly only useful itself for comparison. For example:

>>> python2 = PythonMajorVersion(2)
>>> my_python_version = PythonVersion('2.7.1')
>>> assert(my_python_version.major_version == python2)
major_version[source]

Return a new PythonMajorVersion with the object’s values.

This is mainly useful in subclasses.

major

The ‘major’ segment value.

class verschemes.python.PythonMinorVersion[source]

Bases: verschemes.python.PythonMajorVersion

A minor Python version.

This version scheme has two segments that identify the minor Python version, which is incremented for less earth-shattering changes in the language than a major version increment. It is the base for the more detailed Python version classes and is mostly only useful itself for comparison. For example:

>>> python33 = PythonMinorVersion(3, 3)
>>> my_python_version = PythonVersion(3, 4, 2)
>>> assert(my_python_version.minor_version > python33)
minor_version[source]

Return a new PythonMinorVersion with the object’s values.

This is mainly useful in subclasses.

minor

The ‘minor’ segment value.

class verschemes.python.PythonMicroVersion[source]

Bases: verschemes.python.PythonMinorVersion

A micro Python version.

This version scheme has three segments that identify the micro Python version, which is incremented for each bugfix release (see PEP 6). It is the base for the more detailed Python version classes and is mostly only useful itself for comparison. For example:

>>> python301 = PythonMicroVersion(3, 0, 1)
>>> my_python_version = PythonVersion(3, 0, 1, ('b', 1))
>>> assert(my_python_version.micro_version == python301)
micro_version[source]

Return a new PythonMicroVersion with the object’s values.

This is mainly useful in subclasses.

micro

The ‘micro’ segment value.

class verschemes.python.PythonVersion[source]

Bases: verschemes.python.PythonMicroVersion

A complete, specific Python version.

This version scheme has four segments that identify a specific version of Python. See the link in the module documentation for details about the Python version scheme.

is_nondevelopment[source]

Whether this version represents a non-development release.

This simply says whether it is equivalent to its micro_version; that is, whether the SUFFIX-index value is None.

>>> assert(PythonVersion('3.4.1').is_nondevelopment)
>>> assert(not PythonVersion('3.4.1c1').is_nondevelopment)
>>> assert(not PythonVersion('3.4.1+').is_nondevelopment)
is_release[source]

Whether this version represents a release.

This simply says whether the SUFFIX-index value is not ‘+’. A ‘+’ indicates that it is an unreleased version, built directly from the Subversion trunk; anything else is a release (be it development or non-development).

>>> assert(PythonVersion('3.4.1').is_release)
>>> assert(PythonVersion('3.4.1c1').is_release)
>>> assert(not PythonVersion('3.4.1+').is_release)
suffix

The ‘suffix’ segment value.

PEP 440

verschemes.pep440 module

The PEP 440 verschemes module implements standard PEP 440 public versioning. PEP 440 local versions are not supported by this module; they are just public versions with a hyphen and a numeric version (as implemented by the defaults in the base verschemes.Version class) appended.

This implementation is limited to six release-number segments, which should handle any reasonable scheme.

class verschemes.pep440.Pep440Version[source]

Bases: verschemes.Version

A PEP 440 public version.

The version scheme has an optional epoch segment followed by up to six release-number segments (named ‘releaseX’, where X is 1-6) followed by the optional pre_release, post_release, and development segments.

SEGMENT_DEFINITIONS contains the following named segments in order:

  • epoch: optional=True, default=0

  • release1: default=0, separator=’!’

  • release2: optional=True, default=0

  • release3: optional=True, default=0

  • release4: optional=True, default=0

  • release5: optional=True, default=0

  • release6: optional=True, default=0

  • pre_release: optional=True, separator=’‘, separator_re_pattern=’[.-]?’
    • field ‘level’: type=str (case-insensitive input)
      • normal form ‘a’ accepts ‘a’ or ‘alpha’
      • normal form ‘b’ accepts ‘b’ or ‘beta’
      • normal form ‘c’ accepts ‘c’ or ‘rc’
    • field ‘serial’: type=int_empty_zero, re_pattern=’[0-9]*’

  • post_release: optional=True, separator=’.post’, separator_re_pattern=’[.-]?post’
    • field: type=int_empty_zero, re_pattern=’[0-9]*’
  • development: optional=True, separator=’.dev’, separator_re_pattern=’[.-]?dev’
    • field: type=int_empty_zero, re_pattern=’[0-9]*’
is_release[source]

Whether this version represents a final release.

Return True if all of the ‘pre_release’, ‘post_release’, and ‘development’ segments have no value.

render(exclude_defaults=True, include_callbacks=(), exclude_callbacks=(), min_release_segments=1)[source]

Override to provide the min_release_segments option.

development

The ‘development’ segment value.

epoch

The ‘epoch’ segment value.

post_release

The ‘post_release’ segment value.

pre_release

The ‘pre_release’ segment value.

release1

The ‘release1’ segment value.

release2

The ‘release2’ segment value.

release3

The ‘release3’ segment value.

release4

The ‘release4’ segment value.

release5

The ‘release5’ segment value.

release6

The ‘release6’ segment value.

PostgreSQL

verschemes.postgresql module

The PostgreSQL verschemes module implements standard PostgreSQL versioning.

class verschemes.postgresql.PgMajorVersion[source]

Bases: verschemes.Version

A major PostgreSQL version.

This version scheme has two segments that identify the major PostgreSQL version, which includes new features and requires a dump/reload of the database or use of the pg_upgrade module. It is the base for the more detailed PostgreSQL version classes and is mostly only useful itself for comparison. For example:

>>> pg83 = PgMajorVersion(8.3)
>>> my_version = PgVersion('8.3.4')
>>> assert(my_version.major_version == pg83)
major_version[source]

Return a new PgMajorVersion with the object’s values.

This is mainly useful in subclasses.

major1

The ‘major1’ segment value.

major2

The ‘major2’ segment value.

class verschemes.postgresql.PgVersion[source]

Bases: verschemes.postgresql.PgMajorVersion

A complete, specific PostgreSQL version.

This version scheme has three segments that identify a specific release of PostgreSQL.

minor

The ‘minor’ segment value.

X.org

verschemes.xorg module

The X.org verschemes module implements the standard X.org version number scheme.

An added rule is to extend the patch number’s pre-full-release value (99) to the minor number as well when preparing for a [major].0 full release.

class verschemes.xorg.XorgVersion[source]

Bases: verschemes.Version

validate()[source]

Override for version scheme validations.

is_release[source]

Whether the version identifies a release.

is_full_release[source]

Whether the version identifies a full release.

is_pre_full_release[source]

Whether the version is between feature freeze and a full release.

is_bugfix_release[source]

Whether the version identifies a bug-fix release.

is_development[source]

Whether the version is a non-release prior to feature freeze.

is_branch_start[source]

Whether the version is the start of a release branch.

is_release_candidate[source]

Whether the version identifies a release candidate.

release_candidate[source]

The release candidate number if it is a release candidate.

stable_branch_suffix[source]

The suffix of the stable branch name if not in development.

major

The ‘major’ segment value.

minor

The ‘minor’ segment value.

patch

The ‘patch’ segment value.

snapshot

The ‘snapshot’ segment value.

Table Of Contents

Previous topic

Installation

Next topic

Examples

This Page