gpt4 book ai didi

不带 distutils.version 的 Python 版本列表

转载 作者:行者123 更新时间:2023-11-30 22:05:19 24 4
gpt4 key购买 nike

如何在不使用 distutils 的情况下对版本列表进行排序?

distutils.version 对版本号的长度有限制,所以我得到了

严格版本

msg": "invalid version number '11.2.0.4.v4'" 

宽松版本

"results": null 

未排序的结果

["11.2.0.4.v4", "11.2.0.4.v5", "11.2.0.4.v6", "11.2.0.4.v7", "11.2.0.4.v8", 
"11.2.0.4.v9", "11.2.0.4.v10", "11.2.0.4.v11", "11.2.0.4.v12",
"11.2.0.4.v13", "11.2.0.4.v14", "11.2.0.4.v15", "11.2.0.4.v16",
"11.2.0.4.v17", "12.1.0.2.v1", "12.1.0.2.v2", "12.1.0.2.v3", "12.1.0.2.v4",
"12.1.0.2.v5", "12.1.0.2.v6", "12.1.0.2.v7", "12.1.0.2.v8", "12.1.0.2.v9",
"12.1.0.2.v10", "12.1.0.2.v11", "12.1.0.2.v12", "12.1.0.2.v13"]

相关代码

try:
params = dict()
params['Engine'] = module.params.get('engine_name')
results = client.describe_db_engine_versions(**params)
versions = [z['EngineVersion'] for z in results['DBEngineVersions']]
return versions.sort(key=StrictVersion)
except Exception as e:
module.fail_json(msg=str(e))

最佳答案

setuptools有一个更好的:

>>> from pkg_resources import parse_version
>>> max(versions, key=parse_version)
'12.1.0.2.v13'
>>> sorted(versions, key=parse_version)
['11.2.0.4.v4',
'11.2.0.4.v5',
'11.2.0.4.v6',
'11.2.0.4.v7',
'11.2.0.4.v8',
'11.2.0.4.v9',
'11.2.0.4.v10',
'11.2.0.4.v11',
'11.2.0.4.v12',
'11.2.0.4.v13',
'11.2.0.4.v14',
'11.2.0.4.v15',
'11.2.0.4.v16',
'11.2.0.4.v17',
'12.1.0.2.v1',
'12.1.0.2.v2',
'12.1.0.2.v3',
'12.1.0.2.v4',
'12.1.0.2.v5',
'12.1.0.2.v6',
'12.1.0.2.v7',
'12.1.0.2.v8',
'12.1.0.2.v9',
'12.1.0.2.v10',
'12.1.0.2.v11',
'12.1.0.2.v12',
'12.1.0.2.v13']

关于不带 distutils.version 的 Python 版本列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53052162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com