Skip to content

Commit

Permalink
WIP: update old code: CI, python version, pre-commit and unittests (#36)
Browse files Browse the repository at this point in the history
* fix: CI not working with mamba

* ci: use miniforge instead

* enh: move to Python 3.6-3.10

* enh: add dependabot.yml

* enh: add a few pre-commits, including black formatter

* enh: update pre-commit

* refactor: remove old Python 2 code, update tests

* refactor: remove old Python 2 code, update tests
  • Loading branch information
jonas-eschle authored May 21, 2024
1 parent 58007ec commit f1cf41a
Show file tree
Hide file tree
Showing 28 changed files with 1,737 additions and 1,331 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions monthly
interval: "monthly"
26 changes: 17 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: Python package
name: unittests

on: [ push, pull_request ]
on:
push:
tags:
- "*"
branches:
- master
- "ci/*"
pull_request:

jobs:
unittests:
Expand All @@ -9,33 +16,34 @@ jobs:
run:
shell: "bash -l {0}"
strategy:
fail-fast: false
matrix:
python-version:
- 3.6
- 3.7
- 3.8
- 3.9
- 3.10
name: Tests for Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v2
- uses: conda-incubator/setup-miniconda@v2
with:
miniforge-variant: Mambaforge
use-mamba: true
mamba-version: "*"
channels: conda-forge,defaults
channel-priority: true
python-version: ${{ matrix.python-version }}
activate-environment: formulate-env

- name: Install ROOT
run: |
conda config --add channels conda-forge
conda config --set channel_priority strict
conda install root -y
mamba install root -y
- name: Install test dependencies
run: |
python -m pip install --upgrade pip
conda install coveralls -y
conda install pytest-cov -y
mamba install coveralls -y
mamba install pytest-cov -y
pip install -e .[dev,test]
- name: Test with pytest
Expand Down
54 changes: 51 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
rev: v4.2.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
Expand All @@ -12,10 +12,58 @@ repos:
- id: mixed-line-ending
- id: requirements-txt-fixer
- id: trailing-whitespace
- id: fix-encoding-pragma

- repo: https://github.com/mgedmin/check-manifest
rev: "0.46"
rev: "0.48"
hooks:
- id: check-manifest
stages: [ manual ]

- repo: https://github.com/myint/docformatter
rev: v1.4
hooks:
- id: docformatter
args: [ -r, --in-place, --wrap-descriptions, '120', --wrap-summaries, '120', -- ]

- repo: https://github.com/mattlqx/pre-commit-sign
rev: v1.1.3
hooks:
- id: sign-commit

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
hooks:
- id: python-use-type-annotations
- id: python-check-mock-methods
- id: python-no-eval
- id: rst-directive-colons

- repo: https://github.com/asottile/pyupgrade
rev: v2.32.1
hooks:
- id: pyupgrade
args: [ --py36-plus ]

- repo: https://github.com/asottile/setup-cfg-fmt
rev: v1.20.1
hooks:
- id: setup-cfg-fmt
args: [--max-py-version=3.10]

- repo: https://github.com/roy-ht/pre-commit-jupyter
rev: v1.2.1
hooks:
- id: jupyter-notebook-cleanup

# TODO: for Python 3.7+
# - repo: https://github.com/sondrelg/pep585-upgrade
# rev: 'v1.0'
# hooks:
# - id: upgrade-type-hints
# args: [ '--futures=true' ]

- repo: https://github.com/ambv/black
rev: 22.3.0
hooks:
- id: black
language_version: python3
11 changes: 6 additions & 5 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Licensed under a 3-clause BSD style license, see LICENSE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import pytest


def pytest_addoption(parser):
parser.addoption("--run-slow", action="store_true",
default=False, help="Enable running slow tests")
parser.addoption(
"--run-slow",
action="store_true",
default=False,
help="Enable running slow tests",
)


def pytest_collection_modifyitems(config, items):
Expand Down
38 changes: 21 additions & 17 deletions formulate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
# Licensed under a 3-clause BSD style license, see LICENSE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from pyparsing import ParserElement

from .backends import from_auto, from_numexpr, to_numexpr
from .backends import from_root, to_root
from .expression import ExpressionComponent, SingleComponent, Expression, Variable, NamedConstant, UnnamedConstant
from .expression import (
ExpressionComponent,
SingleComponent,
Expression,
Variable,
NamedConstant,
UnnamedConstant,
)
from .parser import ParsingException
from .version import version as __version__


__all__ = [
'ExpressionComponent',
'SingleComponent',
'Expression',
'Variable',
'NamedConstant',
'UnnamedConstant',
'ParsingException',
'from_auto',
"ExpressionComponent",
"SingleComponent",
"Expression",
"Variable",
"NamedConstant",
"UnnamedConstant",
"ParsingException",
"from_auto",
# numexpr
'from_numexpr',
'to_numexpr',
"from_numexpr",
"to_numexpr",
# ROOT
'from_root',
'to_root',
'__version__',
"from_root",
"to_root",
"__version__",
]


Expand Down
29 changes: 14 additions & 15 deletions formulate/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# Licensed under a 3-clause BSD style license, see LICENSE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys
Expand All @@ -11,18 +8,20 @@


def parse_args(args):
parser = argparse.ArgumentParser(description='Convert between different types of formulae')
parser = argparse.ArgumentParser(
description="Convert between different types of formulae"
)

from_group = parser.add_mutually_exclusive_group(required=True)
from_group.add_argument('--from-root')
from_group.add_argument('--from-numexpr')
from_group.add_argument("--from-root")
from_group.add_argument("--from-numexpr")

to_group = parser.add_mutually_exclusive_group(required=True)
to_group.add_argument('--to-root', action='store_true')
to_group.add_argument('--to-numexpr', action='store_true')
to_group.add_argument('--variables', action='store_true')
to_group.add_argument('--named-constants', action='store_true')
to_group.add_argument('--unnamed-constants', action='store_true')
to_group.add_argument("--to-root", action="store_true")
to_group.add_argument("--to-numexpr", action="store_true")
to_group.add_argument("--variables", action="store_true")
to_group.add_argument("--named-constants", action="store_true")
to_group.add_argument("--unnamed-constants", action="store_true")

args = parser.parse_args(args)
if args.from_root is not None:
Expand All @@ -37,16 +36,16 @@ def parse_args(args):
elif args.to_numexpr:
result = to_numexpr(expression)
elif args.variables:
result = '\n'.join(sorted(expression.variables))
result = "\n".join(sorted(expression.variables))
elif args.named_constants:
result = '\n'.join(sorted(expression.named_constants))
result = "\n".join(sorted(expression.named_constants))
elif args.unnamed_constants:
result = '\n'.join(sorted(expression.unnamed_constants))
result = "\n".join(sorted(expression.unnamed_constants))
else:
raise NotImplementedError()

return result


if __name__ == '__main__':
if __name__ == "__main__":
print(parse_args(sys.argv[1:]))
Loading

0 comments on commit f1cf41a

Please sign in to comment.