xpybuild.utils.stringutils

Utility functions for manipulating strings, such as xpybuild.utils.stringutils.compareVersions.

compareVersions

xpybuild.utils.stringutils.compareVersions(v1: str, v2: str) → int[source]

Compares two alphanumeric dotted version strings to see which is more recent.

Example usage:

if compareVersions(thisversion, '1.2.alpha-3') > 0:
        ... # thisversion is newer than 1.2.alpha-3 

The comparison algorithm ignores case, and normalizes separators ./-/_ so that '1.alpha2'=='1Alpha2'. Any string components are compared lexicographically with other strings, and compared to numbers strings are always considered greater.

Parameters
  • v1 – A string containing a version number, with any number of components.

  • v2 – A string containing a version number, with any number of components.

Returns

an integer > 0 if v1>v2, an integer < 0 if v1<v2, or 0 if they are semantically the same.

>>> compareVersions('10-alpha5.dev10', '10alpha-5-dEv_10') == 0 # normalization of case and separators
True
>>> compareVersions('1.2.0', '1.2')
0
>>> compareVersions('1.02', '1.2')
0
>>> compareVersions('1.2.3', '1.2') > 0
True
>>> compareVersions('1.2', '1.2.3')
-1
>>> compareVersions('10.2', '1.2')
1
>>> compareVersions('1.2.text', '1.2.0') # letters are > numbers
1
>>> compareVersions('1.2.text', '1.2') # letters are > numbers 
1
>>> compareVersions('10.2alpha1', '10.2alpha')
1
>>> compareVersions('10.2dev', '10.2alpha') # letters are compared lexicographically
1
>>> compareVersions('', '')
0
>>> compareVersions('1', '')
1

formatTimePeriod

xpybuild.utils.stringutils.formatTimePeriod(secs)[source]

Format a time period to a short display string.