Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test matrix and fix tests #1

Merged
merged 6 commits into from
Sep 17, 2024
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
17 changes: 17 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Static analysis

on:
pull_request:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
- uses: pre-commit/[email protected]
13 changes: 7 additions & 6 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ on:
- "main"
paths:
- '**.py'
- 'pyproject.toml'
pull_request:
branches:
- "main"
paths:
- '**.py'
- 'pyproject.toml'

# jobs:
# call-standard-unit-tests:
# uses: ionworks/.github/workflows/unit_tests.yml@main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
pytest-linux:
pytest:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.11"]
python-version: ["3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest]

steps:
- uses: actions/checkout@v3
Expand All @@ -38,4 +39,4 @@ jobs:
pip install -e ".[dev]"
- name: Test with pytest
run: |
pytest
pytest
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
build
dist
dist
.venv
.env
venv
env
.idea
.vscode
__pycache__
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ci:
autoupdate_commit_msg: "chore: update pre-commit hooks"
autofix_commit_msg: "style: pre-commit fixes"

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.3"
hooks:
- id: ruff
args: [--fix, --show-fixes]
types_or: [python, pyi, jupyter]
- id: ruff-format
types_or: [python, pyi, jupyter]
32 changes: 20 additions & 12 deletions iwutil/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from . import save

import matplotlib.pyplot as plt
import numpy as np
from functools import singledispatch
Expand Down Expand Up @@ -70,26 +69,35 @@ def read_df(file):


@read_df.register
def _(file: str | Path):
file_extension = Path(file).suffix[1:]
def _(file: str):
return iwutil_file_path_helper(file)


@read_df.register
def _(file: Path):
return iwutil_file_path_helper(file)


@read_df.register
def _(file: pd.DataFrame):
return file


def iwutil_file_path_helper(file_name: str | Path):
file_extension = Path(file_name).suffix[1:]

if file_extension == "csv":
return pd.read_csv(file)
return pd.read_csv(file_name)
elif file_extension in ["xls", "xlsx"]:
return pd.read_excel(file)
return pd.read_excel(file_name)
elif file_extension == "json":
return pd.read_json(file)
return pd.read_json(file_name)
elif file_extension == "parquet":
return pd.read_parquet(file)
return pd.read_parquet(file_name)
else:
raise ValueError(f"Unsupported file type: {file_extension}")


@read_df.register
def _(file: pd.DataFrame):
return file


def copyfile(src, dst):
"""
Copy a file from src to dst, creating the parent directory if it does not exist
Expand Down
6 changes: 3 additions & 3 deletions iwutil/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ def parquet(df, filename):
df.to_parquet(filename)


def fig(fig, filename):
def fig(fig_to_save, filename):
"""
Save fig to a file

Parameters
----------
fig : matplotlib.figure.Figure
fig_to_save : matplotlib.figure.Figure
Figure to save
filename : str
Full path and name of the file to save
"""
create_folder(filename)
fig.savefig(filename)
fig_to_save.savefig(filename)
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ description = "Public utility functions for Ionworks code."
authors = [{ name = "Ionworks Technologies Inc", email = "[email protected]" }]
readme = { file = "README.md", content-type = "text/markdown" }
license = { file = "LICENSE.md" }
version = "0.3.1"
requires-python = ">=3.10"
version = "0.3.2"
dependencies = [
"matplotlib",
"pandas[parquet,feather]",
Expand Down
59 changes: 59 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Exclude a variety of commonly ignored directories.
exclude = [
"__init__.py",
"docs",
".eggs",
".git",
".mypy_cache",
".pytype",
".ruff_cache",
".venv",
"__pypackages__",
"_build",
"build",
"dist",
"node_modules",
"venv",
]

line-length = 88
indent-width = 4

target-version = "py39"

[lint]
select = [
"W",
"E",
"F",
"B",
"A",
"U",
"SLF",
]
ignore = [
"E501", # Line length check conflicted with the formatter
"B018", # Useless expression checks were a problem for notebooks
"B905", # Requires zip to have explict strict flag. Enable in the future
]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

3 changes: 3 additions & 0 deletions tests/test_save_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
def test_save_read_df(file_format, path_format):
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

df_read = pd.DataFrame()
if file_format == "df":
df_read = iwutil.read_df(df)
else:
Expand All @@ -28,6 +29,8 @@ def test_save_read_df(file_format, path_format):
iwutil.save.parquet(df, file)
elif file_format == "json":
iwutil.save.json(df.to_dict(orient="list"), file)
else:
raise NotImplementedError(f"Test does not cover format: {file_format}")

df_read = iwutil.read_df(file)
assert df.equals(df_read)
Loading