Skip to content

Instantly share code, notes, and snippets.

@tony
Created September 5, 2022 16:47
Show Gist options
  • Select an option

  • Save tony/05c66199c003aaf712a39b5dbc103eba to your computer and use it in GitHub Desktop.

Select an option

Save tony/05c66199c003aaf712a39b5dbc103eba to your computer and use it in GitHub Desktop.

Revisions

  1. tony created this gist Sep 5, 2022.
    1 change: 1 addition & 0 deletions INSTRUCTIONS
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Run `python -m doctest example.py`
    20 changes: 20 additions & 0 deletions example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    from packaging.specifiers import InvalidSpecifier, SpecifierSet
    from packaging.version import Version


    def is_allowed_version(spec: str, version: str) -> bool:
    """Check `spec` satisfies `version` or not.
    This obeys PEP-440 specifiers:
    https://peps.python.org/pep-0440/#version-specifiers
    Some examples:
    >>> is_allowed_version('3.3', '<=3.5')
    True
    >>> is_allowed_version('3.3', '<=3.2')
    False
    >>> is_allowed_version('3.3', '>3.2, <4.0')
    True
    """
    return Version(version) in SpecifierSet(spec)