Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ coverage.xml
*.cover
.hypothesis/
.pytest_cache/
prof/
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor personal annoyance.


# Translations
*.mo
Expand Down
2 changes: 1 addition & 1 deletion gemd/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.2.0"
__version__ = "2.2.1"
2 changes: 1 addition & 1 deletion gemd/demo/cake.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Bake a cake."""
from importlib_resources import files
from importlib.resources import files
from io import BytesIO
import random

Expand Down
2 changes: 1 addition & 1 deletion gemd/demo/strehlow_and_cook.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

def import_table(filename=SMALL_TABLE):
"""Return the deserialized JSON table."""
from importlib_resources import files
from importlib.resources import files
import json

return json.loads(files("gemd.demo").joinpath(filename).read_text(encoding='utf-8'))
Expand Down
58 changes: 23 additions & 35 deletions gemd/units/impl.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
"""Implementation of units."""
from deprecation import deprecated
import functools
from importlib_resources import files
from importlib.resources import files
import os
from pathlib import Path
import re
from tempfile import TemporaryDirectory
from typing import Union, List, Tuple, Generator, Any
try:
from typing import TypeAlias # Python 3.10+
except ImportError: # pragma nocover
from typing_extensions import TypeAlias # Python 3.9

from pint import UnitRegistry, register_unit_format
try: # Pint 0.23 migrated the location of this method, and augmented it
from pint.pint_eval import tokenizer
except ImportError: # pragma: no cover
from pint.compat import tokenizer
from pint.pint_eval import tokenizer
from tokenize import NAME, NUMBER, OP, ERRORTOKEN, TokenInfo
# alias the error that is thrown when units are incompatible
# this helps to isolate the dependence on pint
from pint.errors import DimensionalityError as IncompatibleUnitsError # noqa Import
from pint.errors import UndefinedUnitError, DefinitionSyntaxError # noqa Import
from pint.errors import DimensionalityError as IncompatibleUnitsError
from pint.errors import UndefinedUnitError, DefinitionSyntaxError
from pint.registry import GenericUnitRegistry

# Store directories so they don't get auto-cleaned until exit
_TEMP_DIRECTORY = TemporaryDirectory()
Expand Down Expand Up @@ -216,43 +218,29 @@ def _unmangle_scaling(input_string: str) -> str:
return input_string


try: # pragma: no cover
# Pint 0.23 modified the preferred way to derive a custom class
# https://pint.readthedocs.io/en/0.23/advanced/custom-registry-class.html
from pint.registry import GenericUnitRegistry
from typing_extensions import TypeAlias
# Standard approach to creating a custom registry class:
# https://pint.readthedocs.io/en/0.23/advanced/custom-registry-class.html

class _ScaleFactorUnit(UnitRegistry.Unit):
"""Child class of Units for generating units w/ clean scaling factors."""
class _ScaleFactorUnit(UnitRegistry.Unit):
"""Child class of Units for generating units w/ clean scaling factors."""

def __format__(self, format_spec):
result = super().__format__(format_spec)
return _unmangle_scaling(result)
def __format__(self, format_spec):
result = super().__format__(format_spec)
return _unmangle_scaling(result)

class _ScaleFactorQuantity(UnitRegistry.Quantity):
"""Child class of Quantity for generating units w/ clean scaling factors."""

pass

class _ScaleFactorRegistry(GenericUnitRegistry[_ScaleFactorQuantity, _ScaleFactorUnit]):
"""UnitRegistry class that uses _GemdUnits."""
class _ScaleFactorQuantity(UnitRegistry.Quantity):
"""Child class of Quantity for generating units w/ clean scaling factors."""

Quantity: TypeAlias = _ScaleFactorQuantity
Unit: TypeAlias = _ScaleFactorUnit
pass

except ImportError: # pragma: no cover
# https://pint.readthedocs.io/en/0.21/advanced/custom-registry-class.html
class _ScaleFactorUnit(UnitRegistry.Unit):
"""Child class of Units for generating units w/ clean scaling factors."""

def __format__(self, format_spec):
result = super().__format__(format_spec)
return _unmangle_scaling(result)
class _ScaleFactorRegistry(GenericUnitRegistry[_ScaleFactorQuantity, _ScaleFactorUnit]):
"""UnitRegistry class that uses _GemdUnits."""

class _ScaleFactorRegistry(UnitRegistry):
"""UnitRegistry class that uses _GemdUnits."""
Quantity: TypeAlias = _ScaleFactorQuantity
Unit: TypeAlias = _ScaleFactorUnit

_unit_class = _ScaleFactorUnit

_REGISTRY: _ScaleFactorRegistry = None # global requires it be defined in this scope

Expand Down
73 changes: 73 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[project]
name = "gemd"
dynamic = ["version"]
dependencies = [
"pint>=0.24.4,<0.25",
"deprecation>=2.1.0,<3",
"typing_extensions>=4.8,<5; python_version<'3.10'",
]
requires-python = ">=3.9"
authors = [
{name = "Citrine Informatics"}
]
description = "Python binding for Citrine's GEMD data model"
readme = "README.md"
license-files = ["LICENSE"]
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]

[project.urls]
Homepage = "http://github.com/CitrineInformatics/gemd-python"

[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.setuptools.dynamic]
version = {attr = "gemd.__version__.__version__"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah, this is a small but awesome little feature!


[tool.setuptools.packages.find]
where = ["."]
include = ["gemd"]
exclude = ["docs", "tests"]

[tool.setuptools.package-data]
"gemd.demo" = ["strehlow_and_cook.pif", "strehlow_and_cook_small.pif", "toothpick.jpg"]
"gemd.units" = ["citrine_en.txt", "constants_en.txt"]
"tests.units" = ["test_units.txt"]


[dependency-groups]
dev = [
"flake8==7.0.0",
"flake8-docstrings==1.7.0",
"numpy==1.24.4; python_version<'3.10'",
"pandas==2.0.3; python_version<'3.10'",
"numpy>=2.0.2,<=2.1.0; python_version>='3.10'",
"pandas==2.3.0; python_version>='3.10'",
"pytest==8.4.2",
"pytest-cov==7.0.0",
"derp==0.1.1",
"tomli>=2.2.1",
]
docs = [
"sphinx==5.0.0",
"sphinx-rtd-theme==1.0.0",
"sphinxcontrib-apidoc==0.3.0",
]

[tool.pytest.ini_options]
testpaths = [
"tests",
]

[tool.coverage.run]
omit = [
"gemd/demo/*",
]
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pint==0.24.4
deprecation==2.1.0
typing-extensions==4.8.0
importlib-resources==5.3.0
typing_extensions>=4.8,<5; python_version<'3.10'
5 changes: 3 additions & 2 deletions scripts/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ then python $REPO_DIR/scripts/validate_version_bump.py ||
fi

flake8 $REPO_DIR/gemd || exit 1;
derp $REPO_DIR $REPO_DIR/gemd/__version__.py || exit 1;
derp $REPO_DIR/gemd $REPO_DIR/gemd/__version__.py || exit 1;
pytest $QUIET $EXITFIRST --cov=$REPO_DIR/gemd \
--cov-report term-missing:skip-covered \
--cov-config=$REPO_DIR/tox.ini --no-cov-on-fail --cov-fail-under=100 \
--cov-config=$REPO_DIR/pyproject.toml \
--no-cov-on-fail --cov-fail-under=100 \
$REPO_DIR || exit 1;
67 changes: 0 additions & 67 deletions setup.py

This file was deleted.

4 changes: 2 additions & 2 deletions test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ numpy==1.24.4; python_version<'3.10'
pandas==2.0.3; python_version<'3.10'
numpy>=2.0.2,<=2.1.0; python_version>='3.10'
pandas==2.3.0; python_version>='3.10'
pytest==8.0.0
pytest-cov==4.1.0
pytest==8.4.2
pytest-cov==7.0.0
derp==0.1.1
2 changes: 1 addition & 1 deletion tests/units/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from contextlib import contextmanager
from deprecation import DeprecatedWarning
from importlib_resources import files
from importlib.resources import files
import re
from pint import UnitRegistry
import pytest
Expand Down
5 changes: 1 addition & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@ max-doc-length = 119
# D401: Imperative mood requirement basically gets in the way
ignore = D100,D104,D105,D107,D301,D401
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flake8 can't pull configuration from pyproject.toml, so it remains here.


[pytest]
testpaths = tests

[run]
omit = gemd/demo/*
omit = gemd/demo/*
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed in a follow up. It's required to pass tests common-gh-actions no longer references tox.ini.

Loading