Skip to content

Commit

Permalink
Nano-Utils 1.0.0 (#17)
Browse files Browse the repository at this point in the history
* Updated the development status from Beta to Production/Stable.
  • Loading branch information
BvB93 authored Jun 12, 2020
1 parent 2c94d83 commit 7666cf8
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 76 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.6, 3.7, 3.8]
env:
OS: ${{ matrix.os }}
PYTHON: ${{ matrix.python-version }}

steps:
- uses: actions/checkout@v2
Expand All @@ -33,7 +36,7 @@ jobs:
shell: bash -l {0}
run: |
pip show numpy
NUMPY_GE_1_20=$(numpy_ge_1_20)
NUMPY_GE_1_20=$(python ./numpy_ge_1_20.py)
if [[ $NUMPY_GE_1_20 == 'FALSE' ]]; then
pip install git+https://github.com/numpy/numpy-stubs@master
fi
Expand All @@ -54,3 +57,4 @@ jobs:
with:
file: ./coverage.xml
name: codecov-umbrella
env_vars: OS,PYTHON
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ All notable changes to this project will be documented in this file.
This project adheres to `Semantic Versioning <http://semver.org/>`_.


1.0.0
*****
* Updated the development status from Beta to Production/Stable.


0.4.3
*****
* Added the ``nanoutils.testing_utils`` module;
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


################
Nano-Utils 0.4.3
Nano-Utils 1.0.0
################
Utility functions used throughout the various nlesc-nano repositories.

Expand Down
2 changes: 1 addition & 1 deletion codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ coverage:
status:
project:
default:
target: 0
target: 80
patch:
default:
target: 0
2 changes: 1 addition & 1 deletion nanoutils/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""The **Nano-Utils** version."""

__version__ = '0.4.3'
__version__ = '1.0.0'
57 changes: 1 addition & 56 deletions nanoutils/numpy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
"""

import warnings
from math import factorial, nan
from typing import TYPE_CHECKING, Optional, Union, Iterable
from itertools import combinations
from collections import abc

from .utils import raise_if, construct_api_doc, VersionInfo
from .utils import raise_if, construct_api_doc

try:
import numpy as np
Expand Down Expand Up @@ -234,58 +233,4 @@ def fill_diagonal_blocks(array: ndarray, i: int, j: int, val: float = nan) -> No
j0 += j


def numpy_ge_1_20() -> None:
r"""Print :code:`"\nnumpy >= 1.20: TRUE"` if the :mod:`numpy` version is 1.20 or higher; print :code:`"\nnumpy >= 1.20: FALSE"` otherwise.
:code:`"\nnumpy >= 1.20: ERROR"` will be printed if an exception is encountered.
Examples
--------
.. code:: python
>>> import numpy as np
>>> from nanoutils.numpy_utils import numpy_ge_1_20
>>> np.__version__ # doctest: +SKIP
'1.17.3'
>>> numpy_ge_1_20() # doctest: +SKIP
FALSE
A command line example.
.. code:: bash
>>> pip show numpy # doctest: +SKIP
Name: numpy
Version: 1.17.3
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
...
>>> numpy_ge_1_20 # doctest: +SKIP
FALSE
""" # noqa: E501
if NUMPY_EX is not None:
warning1 = ImportWarning(str(NUMPY_EX))
warning1.__cause__ = NUMPY_EX
warnings.warn(warning1)
return

__version__ = getattr(np, '__version__', None)
try:
version = VersionInfo.from_str(__version__)
except Exception as ex:
warning2 = RuntimeWarning(f"Failed to parse the NumPy version: {__version__!r}")
warning2.__cause__ = ex
warnings.warn(warning2)
return

if version.major > 1 or (version.major == 1 and version.minor >= 20):
print("TRUE")
else:
print("FALSE")


__doc__ = construct_api_doc(globals())
6 changes: 3 additions & 3 deletions nanoutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ def split_dict(dct, preserve_order=False, *, keep_keys=None, disgard_keys=None):
""" # noqa: E501
if keep_keys is disgard_keys is None:
raise TypeError("'keep_keys' and 'disgard_keys' cannot both be unspecified")
elif keep_keys is not None:
iterable = _keep_keys(dct, keep_keys, preserve_order)
elif disgard_keys is not None:
elif keep_keys is None:
iterable = _disgard_keys(dct, disgard_keys, preserve_order)
elif disgard_keys is None:
iterable = _keep_keys(dct, keep_keys, preserve_order)
else:
raise TypeError("'keep_keys' and 'disgard_keys' cannot both be specified")

Expand Down
62 changes: 62 additions & 0 deletions numpy_ge_1_20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

r"""Print :code:`"\nnumpy >= 1.20: TRUE"` if the :mod:`numpy` version is 1.20 or higher; print :code:`"\nnumpy >= 1.20: FALSE"` otherwise.
:code:`"\nnumpy >= 1.20: ERROR"` will be printed if an exception is encountered.
Examples
--------
.. code:: bash
>>> pip show numpy # doctest: +SKIP
Name: numpy
Version: 1.17.3
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
...
>>> python ./numpy_ge_1_20.py # doctest: +SKIP
FALSE
""" # noqa: E501

import warnings
from typing import Optional

from nanoutils import VersionInfo

try:
import numpy as np
NUMPY_EX: Optional[ImportError] = None
except ImportError as ex:
NUMPY_EX = ex


def main() -> None:
"""Execute the script."""
__version__: Optional[str] = getattr(np, '__version__', None)

try:
major, minor, micro, *_ = __version__.split('.') # type: ignore
version = VersionInfo(major=int(major), minor=int(minor), micro=int(micro))
except Exception as ex:
warning = RuntimeWarning(f"Failed to parse the NumPy version: {__version__!r}")
warning.__cause__ = ex
warnings.warn(warning)
return

if version.major > 1 or (version.major == 1 and version.minor >= 20):
print("TRUE")
else:
print("FALSE")


if __name__ == '__main__':
if NUMPY_EX is not None:
_warning = ImportWarning(str(NUMPY_EX))
_warning.__cause__ = NUMPY_EX
warnings.warn(_warning)
del _warning
else:
main()
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ source = nanoutils

[tool:pytest]
flake8-max-line-length = 100
testpaths = nanoutils docs tests
testpaths = nanoutils docs numpy_ge_1_20.py tests
addopts = --tb=short --doctest-glob='*.py' --doctest-glob='*.rst' --cache-clear --flake8 --pydocstyle --cov=nanoutils --cov-report xml --cov-report term --cov-report html --doctest-modules --mypy

# Define `python setup.py build_sphinx`
Expand Down
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
'libraries'
],
classifiers=[
'Development Status :: 4 - Beta',
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
Expand All @@ -72,9 +72,6 @@
'Topic :: Software Development :: Libraries',
'Typing :: Typed'
],
entry_points={
'console_scripts': ['numpy_ge_1_20=nanoutils.numpy_utils:numpy_ge_1_20']
},
python_requires='>=3.6',
install_requires=[
'typing_extensions>=3.7.4; python_version<"3.8"',
Expand Down
12 changes: 5 additions & 7 deletions tests/test_sphinx.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
"""Test the :mod:`sphinx` documentation generation."""

import shutil
from os.path import join, isdir
from os.path import join
from sphinx.application import Sphinx

from nanoutils import delete_finally

SRCDIR = CONFDIR = 'docs'
OUTDIR = join('tests', 'test_files', 'build')
DOCTREEDIR = join('tests', 'test_files', 'build', 'doctrees')


@delete_finally(OUTDIR)
def test_sphinx_build() -> None:
"""Test :meth:`sphinx.application.Sphinx.build`."""
app = Sphinx(SRCDIR, CONFDIR, OUTDIR, DOCTREEDIR,
buildername='html', warningiserror=True)
try:
app.build(force_all=True)
finally:
if isdir(OUTDIR):
shutil.rmtree(OUTDIR)
app.build(force_all=True)
43 changes: 42 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Tests for :mod:`nanoutils.utils`."""

import sys
from inspect import isclass
from functools import partial

from assertionlib import assertion
from nanoutils import set_docstring, get_importable, VersionInfo, version_info
from nanoutils import set_docstring, get_importable, VersionInfo, version_info, split_dict


def test_set_docstring() -> None:
Expand Down Expand Up @@ -51,3 +52,43 @@ def test_version_info() -> None:
assertion.assert_(VersionInfo.from_str, '0.1.2.3.4', exception=ValueError)

assertion.isinstance(version_info, VersionInfo)


def test_split_dict() -> None:
"""Test :func:`nanoutils.split_dict`."""
dct1 = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
dct2 = dct1.copy()
dct3 = dct1.copy()
dct4 = dct1.copy()
dct5 = dct1.copy()
dct6 = dct1.copy()
dct7 = dct1.copy()
dct8 = dct1.copy()

ref1 = {1: 1, 2: 2}
ref2 = {3: 3, 4: 4, 5: 5}

split_dict(dct1, keep_keys=[1, 2])
split_dict(dct2, keep_keys={1, 2})
split_dict(dct3, keep_keys=iter({1, 2}))
split_dict(dct4, keep_keys=[1, 2], preserve_order=True)
assertion.eq(dct1, ref1)
assertion.eq(dct2, ref1)
assertion.eq(dct3, ref1)
assertion.eq(dct4, ref1)
if sys.version_info[1] > 6:
assertion.eq(list(dct4.keys()), [1, 2])

split_dict(dct5, disgard_keys=[1, 2])
split_dict(dct6, disgard_keys={1, 2})
split_dict(dct7, disgard_keys=iter({1, 2}))
split_dict(dct8, disgard_keys=[1, 2], preserve_order=True)
assertion.eq(dct5, ref2)
assertion.eq(dct6, ref2)
assertion.eq(dct7, ref2)
assertion.eq(dct8, ref2)
if sys.version_info[1] > 6:
assertion.eq(list(dct8.keys()), [3, 4, 5])

assertion.assert_(split_dict, {}, exception=TypeError)
assertion.assert_(split_dict, {}, keep_keys=[], disgard_keys=[], exception=TypeError)

0 comments on commit 7666cf8

Please sign in to comment.