Skip to content

Commit

Permalink
chore: modernize build backend and require Python 3.8+
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Oct 18, 2024
1 parent a1de861 commit e5b37d5
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
name: artifact
path: dist

- uses: pypa/gh-action-pypi-publish@v1.10.3
- uses: pypa/gh-action-pypi-publish@release/v1
7 changes: 3 additions & 4 deletions .github/workflows/pythontests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:

runs-on: ubuntu-20.04
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -50,7 +49,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: '3.9'
python-version: '3.x'
- uses: pre-commit/[email protected]
with:
extra_args: --all-files --hook-stage manual
41 changes: 1 addition & 40 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ ci:
# Is still tested in the GitHub CI, so save to just disable here
skip: [mypy]

default_language_version:
# force all unspecified python hooks to run python3.9
# Latest version that can easily be isntalled on Ubuntu 20.04 LTS
python: python3.9

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: "v5.0.0"
Expand All @@ -23,40 +18,12 @@ repos:
- id: requirements-txt-fixer
- id: trailing-whitespace

- repo: https://github.com/PyCQA/isort
rev: "5.13.2"
hooks:
- id: isort
name: isort (python)

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.7.0"
hooks:
- id: ruff
args: [--fix, --show-fixes]

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: python-use-type-annotations

- repo: https://github.com/asottile/setup-cfg-fmt
rev: "v2.6.0"
hooks:
- id: setup-cfg-fmt

- repo: https://github.com/asottile/pyupgrade
rev: "v3.18.0"
hooks:
- id: pyupgrade
args: ["--py36-plus"]

- repo: https://github.com/pycqa/flake8
rev: "7.1.1"
hooks:
- id: flake8
exclude: (docs/conf.py)|(tests/.*)
additional_dependencies: [flake8-bugbear, flake8-print]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.11.2"
Expand All @@ -65,9 +32,3 @@ repos:
files: histoprint
args: []
additional_dependencies: [numpy, types-click, uhi, rich]

- repo: https://github.com/mgedmin/check-manifest
rev: "0.50"
hooks:
- id: check-manifest
stages: [manual]
8 changes: 0 additions & 8 deletions MANIFEST.in

This file was deleted.

33 changes: 16 additions & 17 deletions histoprint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np

import histoprint as hp
import histoprint.formatter as formatter
from histoprint import formatter


@click.command()
Expand Down Expand Up @@ -124,27 +124,26 @@ def histoprint(infile, **kwargs):
try:
data_handle.seek(0)
_histoprint_txt(data_handle, **kwargs)
exit(0)
return
except ValueError:
pass

# Try to interpret file as CSV file
try:
data_handle.seek(0)
_histoprint_csv(data_handle, **kwargs)
exit(0)
raise SystemExit(0)
except ImportError:
click.echo("Cannot try CSV file format. Pandas module not found.", err=True)

# Try to interpret file as ROOT file
try:
_histoprint_root(infile, **kwargs)
exit(0)
return
except ImportError:
pass

click.echo("Could not interpret file format.", err=True)
exit(1)
raise click.FileError(infile, "Could not interpret the file format")


def _bin_edges(kwargs, data):
Expand Down Expand Up @@ -180,7 +179,7 @@ def _histoprint_txt(infile, **kwargs):
except Exception as e:
click.echo("Error interpreting the cut string:", err=True)
click.echo(e, err=True)
exit(1)
raise SystemExit(1) from None

# Interpret field numbers
fields = kwargs.pop("fields", [])
Expand All @@ -189,12 +188,12 @@ def _histoprint_txt(infile, **kwargs):
fields = [int(f) for f in fields]
except ValueError:
click.echo("Fields for a TXT file must be integers.", err=True)
exit(1)
raise SystemExit(1) from None
try:
data = data[fields]
except KeyError:
click.echo("Field out of bounds.", err=True)
exit(1)
raise SystemExit(1) from None

# Interpret bins
bins = _bin_edges(kwargs, data)
Expand Down Expand Up @@ -222,7 +221,7 @@ def _histoprint_csv(infile, **kwargs):
except Exception as e:
click.echo("Error interpreting the cut string:", err=True)
click.echo(e, err=True)
exit(1)
raise SystemExit(1) from None

# Interpret field numbers/names
fields = list(kwargs.pop("fields", []))
Expand All @@ -231,7 +230,7 @@ def _histoprint_csv(infile, **kwargs):
data = data[fields]
except KeyError:
click.echo("Unknown column name.", err=True)
exit(1)
raise SystemExit(1) from None

# Get default columns labels
if kwargs.get("labels", ("",)) == ("",):
Expand Down Expand Up @@ -276,7 +275,7 @@ def _histoprint_root(infile, **kwargs):
if len(fields) == 0:
click.echo("Must specify at least one field for ROOT files.", err=True)
click.echo(F.keys(), err=True)
exit(1)
raise SystemExit(1)

# Get default columns labels
if kwargs.get("labels", ("",)) == ("",):
Expand Down Expand Up @@ -317,11 +316,11 @@ def _histoprint_root(infile, **kwargs):
f"Could not find key '{key}'. Possible values: {branch.keys()}",
err=True,
)
exit(1)
raise SystemExit(1) from None
# Has `arrays` method?
if hasattr(branch, "arrays"):
# Found it
path = "/".join(splitfield[i + 1:])
path = "/".join(splitfield[i + 1 :])
if branch in trees:
tree_fields[trees.index(branch)].append(
{"label": label, "path": path}
Expand Down Expand Up @@ -350,7 +349,7 @@ def _histoprint_root(infile, **kwargs):
except up.KeyInFileError as e:
click.echo(e, err=True)
click.echo(f"Possible keys: {tree.keys()}", err=True)
exit(1)
raise SystemExit(1) from None

# Cut on values
if cut is not None:
Expand All @@ -359,11 +358,11 @@ def _histoprint_root(infile, **kwargs):
except up.KeyInFileError as e:
click.echo(e, err=True)
click.echo(f"Possible keys: {tree.keys()}", err=True)
exit(1)
raise SystemExit(1) from None
except Exception as e:
click.echo("Error interpreting the cut string:", err=True)
click.echo(e, err=True)
exit(1)
raise SystemExit(1) from None

for i in range(len(d)):
d[i] = d[i][index]
Expand Down
29 changes: 14 additions & 15 deletions histoprint/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def substitute_character(char, compose):
subs = {
"|": "\u2502",
"=": "\u2550",
"#": "\u256A",
"#": "\u256a",
"\\": "\u20e5",
"/": "\u20eb",
"X": "\u20e5\u20eb",
Expand Down Expand Up @@ -293,18 +293,17 @@ def format_bin(self, top, bottom, counts, width=1):
Hixel(s, fg, bg, self.use_color, self.compose)
for _ in range(h)
]
# Overlay histograms
elif h > len(line):
for hix in line:
hix.add(s, fg, bg)
line += [
Hixel(s, fg, bg, self.use_color, self.compose)
for _ in range(h - len(line))
]
else:
# Overlay histograms
if h > len(line):
for hix in line:
hix.add(s, fg, bg)
line += [
Hixel(s, fg, bg, self.use_color, self.compose)
for _ in range(h - len(line))
]
else:
for hix in line[:h]:
hix.add(s, fg, bg)
for hix in line[:h]:
hix.add(s, fg, bg)

for hix in line:
bin_string += hix.render()
Expand Down Expand Up @@ -527,15 +526,15 @@ def summarize(self, counts, top, bottom, legend_only=False):
# Second line: Total
summary += " " * pad + "Tot:"
for c, w in zip(counts, label_widths):
tot = np.sum(c)
tot = float(np.sum(c))
summary += f" {tot: .2e}" + " " * (w - 10)
summary += "\n"

# Third line: Average
summary += " " * pad + "Avg:"
for c, w in zip(counts, label_widths):
try:
average = np.average(bin_values, weights=c)
average = float(np.average(bin_values, weights=c))
except ZeroDivisionError:
average = np.nan
summary += f" {average: .2e}" + " " * (w - 10)
Expand All @@ -545,7 +544,7 @@ def summarize(self, counts, top, bottom, legend_only=False):
summary += " " * pad + "Std:"
for c, w in zip(counts, label_widths):
try:
average = np.average(bin_values, weights=c)
average = float(np.average(bin_values, weights=c))
std = np.sqrt(np.average((bin_values - average) ** 2, weights=c))
except ZeroDivisionError:
std = np.nan
Expand Down
2 changes: 1 addition & 1 deletion histoprint/rich.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from rich.text import Text

import histoprint.formatter as formatter
from histoprint import formatter

__all__ = ["RichHistogram"]

Expand Down
1 change: 1 addition & 0 deletions histoprint/version.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version: str
13 changes: 9 additions & 4 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import nox

ALL_PYTHONS = ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
nox.needs_version = ">=2024.4.15"
nox.options.default_venv_backend = "uv|virtualenv"

nox.options.sessions = ["lint", "tests"]
ALL_PYTHONS = [
c.split()[-1]
for c in nox.project.load_toml("pyproject.toml")["project"]["classifiers"]
if c.startswith("Programming Language :: Python :: 3.")
]


@nox.session
Expand All @@ -16,7 +21,7 @@ def lint(session):
session.run("pre-commit", "run", "--all-files", *session.posargs)


@nox.session(python=ALL_PYTHONS, reuse_venv=True)
@nox.session(python=ALL_PYTHONS)
def tests(session):
"""
Run the unit and regular tests.
Expand All @@ -25,7 +30,7 @@ def tests(session):
session.run("pytest", "-s", *session.posargs)


@nox.session
@nox.session(default=False)
def build(session):
"""
Build an SDist and wheel.
Expand Down
Loading

0 comments on commit e5b37d5

Please sign in to comment.