The utils.py module#

Summary#

VersionMeta

Metaclass for version comparison.

SemanticVersion

Class for semantic versioning.

mystr

Custom class to hold strings for versioning.

myint

Custom class to hold integers for versioning.

VersionNumber

Class for version comparison.

sanitize_version_string

Sanitize a version string number by adding additional zeros.

sanitize_version_tuple

Sanitize a version number by adding additional zeros.

version_string_as_tuple

Convert a semantic version string into a tuple.

version_tuple_as_string

Convert a semantic version tuple into a string.

server_meets_version

Check if server meets the required version.

requires_version

Ensure the method called matches a certain version.

valid_version_string

Check if version string is valid.

valid_semantic_version

Check if a semantic version is valid.

Description#

A module containing various utilities.

Module detail#

utils.sanitize_version_string(version_string)#

Sanitize a version string number by adding additional zeros.

Parameters:
version_stringstr

A string representing a semantic version.

Returns:
str

A string representing a semantic version.

Examples

>>> sanitize_version_string("0")
'0.0.0'
>>> sanitize_version_string("1.2")
'1.2.0'
>>> sanitize_version_string("0.3.4")
'0.3.4'
utils.sanitize_version_tuple(version_tuple)#

Sanitize a version number by adding additional zeros.

Parameters:
version_tupletuple

A tuple representing a semantic version.

Returns:
tuple

A tuple representing a semantic version.

Examples

>>> sanitize_version_tuple((0,))
(0, 0, 0)
>>> sanitize_version_tuple((1, 2))
(1, 2, 0)
>>> sanitize_version_tuple((0, 3, 4))
(0, 3, 4)
utils.version_string_as_tuple(version_string)#

Convert a semantic version string into a tuple.

Parameters:
version_stringstr

A string representing a semantic version.

Returns:
version_tupletuple

A tuple representing a semantic version.

Examples

>>> version_string_as_tuple("0.3.4")
(0, 3, 4)
>>> version_string_as_tuple("1.2")
(1, 2, 0)
utils.version_tuple_as_string(version_tuple)#

Convert a semantic version tuple into a string.

Parameters:
version_tupletuple

A tuple representing a semantic version.

Returns:
str

A string representing a semantic version.

Examples

>>> version_tuple_as_string((0, 3, 4))
'0.3.4'
>>> version_tuple_as_string((1, 2))
'1.2.0'
utils.server_meets_version(server_version, required_version)#

Check if server meets the required version.

Parameters:
server_versionstr, tuple, obj

A string or tuple representing the server version. If it is an object different from the previous ones, it must have a ‘_server_version’ attribute.

required_versionstr, tuple

A string or tuple representing the version to be meet.

Returns:
bool

True if server version meets required version, False if not.

Raises:
ValueError

If the ‘server_version’ object does not have ‘_server_version’ attribute.

Examples

>>> server_version, required_version = "1.2.0", "1.3.0"
>>> server_meets_version(server_version, required_version)
False
>>> server_version, required_version = (1, 2, 0), (1, 3, 0)
>>> server_meets_version(server_version, required_version)
False
>>> server_version, required_version = "2.3.0", "1.3.0"
>>> server_meets_version(server_version, required_version)
True
>>> server_version, required_version = (2, 3, 0), (1, 3, 0)
>>> server_meets_version(server_version, required_version)
True
>>> server_version, required_version = (0, 0, 0), (0, 0, 0)
>>> server_meets_version(server_version, required_version)
True
>>> class MyServer:
        def __init__(self):
            self._server_version = "1.2.0"
>>> server = MyServer()
>>> server_version, required_version = server, "1.3.0"
>>> server_meets_version(server, required_version)
utils.requires_version(version, VERSION_MAP=None)#

Ensure the method called matches a certain version.

Parameters:
versionstr, tuple

A string or tuple representing the minimum required version.

VERSION_MAPdict, optional

A dictionary relating server version and ANSYS unified install version.

Raises:
AttributeError

Decorated class method requires _server_version attribute.

VersionError

Decorated class method is not supported by server version.

utils.valid_version_string(version)#

Check if version string is valid.

utils.valid_semantic_version(iterable)#

Check if a semantic version is valid.