On Sunday 18 September 2005 21:52, Angus Leeming wrote:
>
> def get_version_info():
>     version_re = re.compile("([0-9])\.([0-9])")
>
>     match = version_re.match(sys.version)
>     if match == None:
>         error("Unable to extract version info from 'sys.version'")
>
>     return int(match.group(1)), int(match.group(2))
>
> major, minor = get_version_info()
> if os.name == 'nt' and (major < 2 or minor < 3):
>     error("This script requires Python 2.3 or newer.")

  I like this version more:

import sys

def get_version_info():
    try:
        version = sys.version_info[:3]
    except:
        # sys.version_info was introduced with 2.0.0
        # just consider it 1.5.2, as it enough for this function purposes
        version = (1,5,2)

    return version

if os.name == "nt" and get_version_info() < (2,3,0):
    error("This script requires Python 2.3 or newer.")

  I have used exceptions as they are pretty fast in python by design. No need 
to use strings unless needed, it is possible to compare tuples (the 
lexicographical order applies here), and sometimes when requiring a given 
version we need to go to the micro level.

  For this function to be completely accurate I could use the string 
comparisons that you have used, also there is no need to test for the failure 
of the match because if this fails we have bigger problems. :-)

-- 
José Abílio

Reply via email to