Skip to content

Commit

Permalink
safe_version
Browse files Browse the repository at this point in the history
  • Loading branch information
loriab committed Sep 20, 2024
1 parent dabbb9c commit e8579a9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Misc.
+++++

* added warnings to dummy files models/results.py etc. classes are rerouted to v1 so downstream can run w/o alteration with `from qcelemental.models.procedures import OptimizationInput`
* copied in pkg_resources.safe_version code as follow-up to Eric switch to packaging as both nwchem and gamess were now working. the try_harder_safe_version might be even bettter


0.28.0 / 2024-06-21
Expand Down
14 changes: 11 additions & 3 deletions qcelemental/tests/test_importing.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ def test_parse_version():
assert str(v) == "5.3.1"


def test_safe_version():
v = qcel.util.safe_version("5.3.1")
assert v == "5.3.1"
@pytest.mark.parametrize(
"inp,out",
[
("5.3.1", "5.3.1"),
("30 SEP 2023 (R2)", "30.SEP.2023.-R2-"),
("7.0.0+N/A", "7.0.0-N-A"),
],
)
def test_safe_version(inp, out):
v = qcel.util.safe_version(inp)
assert v == out
16 changes: 13 additions & 3 deletions qcelemental/util/importing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import shutil
import sys
from typing import TYPE_CHECKING, List, Union
Expand Down Expand Up @@ -132,14 +133,23 @@ def which(

def safe_version(version) -> str:
"""
Package resources is a very slow load
Convert an arbitrary string to a standard version string by pkg_resources definition.
"""
return str(parse_version(version))
# from https://github.com/pypa/setuptools/blob/main/pkg_resources/__init__.py
# original function deprecated and never one-to-one replaced
from packaging.version import InvalidVersion, Version

try:
# normalize the version
return str(Version(version))
except InvalidVersion:
version = version.replace(" ", ".")
return re.sub("[^A-Za-z0-9.]+", "-", version)


def parse_version(version) -> "Version":
"""
Package resources is a very slow load
Legitimate version
"""
from packaging.version import parse

Expand Down

0 comments on commit e8579a9

Please sign in to comment.