Skip to content

Commit

Permalink
(internal) Typing support (#244)
Browse files Browse the repository at this point in the history
* Some ignores for backports for typing

* Fixing a bunch of unclosed file warnings

* Starting on mypy support

* Adding MyPy support to PDGID

* Full mypy support added to pdgid/functions

As a result, anything that supports __int__ can now be used in the
functions; this includes Particle directly!

* Adding pre-commit changes

* Basic types for Particle (with several skips

* Fix for extra __main__ being dumped in the wrong place

* Fix for Python 2

* Fix for loading from zipfiles

* Make typing optional for the ZipApp

* Prepare to push ZipApp as well

* ZipApp requires Python 3.7+

* ZipApp compression and earlier Python 3 support

* Minor optimizations

* No need to skip a file that is not present

* Change internal table to a set. 100x faster load, faster searches. Fixes #245

* Add GHA badge

* Update docs/CHANGELOG.md

* Update docs/CHANGELOG.md

* Update docs/CHANGELOG.md

* Update docs/CHANGELOG.md

* Update docs/CHANGELOG.md

* Remove old performance addition - not needed with sets!

* Mention PDGID works on any SupportsInt

Co-authored-by: Eduardo Rodrigues <eduardo.rodrigues@cern.ch>
  • Loading branch information
henryiii and eduardo-rodrigues authored May 21, 2020
1 parent 5748d73 commit 0abf3b2
Show file tree
Hide file tree
Showing 57 changed files with 443 additions and 224 deletions.
1 change: 0 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ omit =
# omit this single file
particle/particle/convert.py
particle/__main__.py

51 changes: 41 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ on:
- 'v*'

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- name: set PY
run: echo "::set-env name=PY::$(python --version --version | sha256sum | cut -d' ' -f1)"
- uses: actions/cache@v1
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- uses: pre-commit/action@v1.0.0

checks:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version:
- 2.7
- 3.6
- 3.5
- 3.8
name: Check Python ${{ matrix.python-version }}
steps:
Expand Down Expand Up @@ -88,12 +101,10 @@ jobs:
zipapp:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
with:
python-version: 3.8
python-version: 3.7

- name: Install wheel and sdist requirements
run: python -m pip install "setuptools>=42.0" "setuptools_scm[toml]>=3.4" "wheel"
Expand All @@ -102,16 +113,36 @@ jobs:
run: python setup.py sdist

- name: Install requirements
run: python -m pip install enum34 pathlib2 importlib_resources attrs hepunits tabulate --target src

- name: Move enum to prevent name clash
run: mv src/enum src/enum34
run: python -m pip install attrs hepunits tabulate importlib_resources --target src

- name: Make ZipApp
run: python -m zipapp -p "/usr/bin/env python" -o ../particle.pyz .
run: python -m zipapp -c -p "/usr/bin/env python3" -m "particle.__main__:main" -o ../particle.pyz .
working-directory: src

- uses: actions/upload-artifact@v1
with:
name: ZipApp
path: particle.pyz

- name: Create Release
id: create_release
uses: actions/create-release@v1
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false

- name: Upload Release Asset
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./particle.pyz
asset_name: particle.pyz
asset_content_type: application/zip
22 changes: 0 additions & 22 deletions .github/workflows/pre-commit.yml

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,3 @@ docs/_build
/src/particle/version.py
/.mypy_cache/*
/pip-wheel-metadata

16 changes: 13 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

repos:
- repo: https://github.com/psf/black
rev: 19.3b0
rev: 19.10b0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
rev: v3.1.0
hooks:
- id: check-added-large-files
args: ['--maxkb=1000']
Expand All @@ -15,7 +15,17 @@ repos:
- id: check-case-conflict
- id: check-symlinks
- id: check-yaml
- id: requirements-txt-fixer
- id: debug-statements
- id: end-of-file-fixer
- id: fix-encoding-pragma
- repo: https://github.com/mgedmin/check-manifest
rev: "0.39"
rev: "0.42"
hooks:
- id: check-manifest
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.770
hooks:
- id: mypy
files: src
additional_dependencies: [attrs==19.3.0]
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
graft src
graft src/particle

global-exclude .env*
global-exclude .git*
Expand Down
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
.. image:: https://zenodo.org/badge/DOI/10.5281/zenodo.2552429.svg
:target: https://doi.org/10.5281/zenodo.2552429

.. image:: https://github.com/scikit-hep/particle/workflows/CI/badge.svg
:alt: GitHub Actions status
:target: https://github.com/scikit-hep/particle/workflows/CI

.. image:: https://dev.azure.com/scikit-hep/particle/_apis/build/status/scikit-hep.particle?branchName=master
:alt: Build Status
:target: https://dev.azure.com/scikit-hep/particle/_build/latest?definitionId=1?branchName=master
Expand Down Expand Up @@ -97,7 +101,7 @@ Getting started: PDGIDs
>>> pid
<PDGID: 99999999 (is_valid==False)>
For convenience, all properties of the ``PDGID`` class are available as standalone functions:
For convenience, all properties of the ``PDGID`` class are available as standalone functions that work on any SupportsInt (including ``Particle``):

.. code-block:: python
Expand Down
9 changes: 6 additions & 3 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Changelog
Version 0.10.0
--------------

May 18th, 2020
May 21th, 2020

- `Particle` class:
- Several improvements, in particular to better deal with nuclei
- Several improvements, in particular to better deal with nuclei.
and diquarks.
- Speed of table loading improved.
- Particle enums extended for diquarks.
Expand All @@ -18,6 +18,7 @@ May 18th, 2020
- `PDGID` class:
- PDG ID functions extended to correctly and consistently deal
with nuclei.
- Functions now accept any int-like, including Particle objects.
- Data CSV files:
- Version 5 of package data files, with
- Diquarks added.
Expand All @@ -27,7 +28,7 @@ May 18th, 2020
time).
- Converter script adapted to add to the produced data files
particles not in the PDG data table, such as diquarks.
- Redesigned packaging system.
- Redesigned packaging system, GHA deployment.
- Miscellaneous:
- Files `*requirements.txt` removed from package - use
`pip install .[dev]` instead
Expand All @@ -36,6 +37,8 @@ May 18th, 2020
- Deprecation warning in `attr.s` fixed, requirement on minimal
version of `attr` added.
- Version tags now follow standard `v#.#.#` format.
- Some Python warnings fixed.
- Some initial work on static type hints.

Version 0.9.2
-------------
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ classifiers =
python_requires = >=2.7, !=3.0, !=3.1, !=3.2, !=3.3, !=3.4
install_requires =
enum34>=1.1; python_version<"3.4"
typing>=3.7; python_version<"3.5"
importlib_resources>=1.0; python_version<"3.7"
attrs>=19.2
hepunits>=1.1.0
hepunits>=1.2.0
packages = find:
include_package_data = True
package_dir =
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020, Eduardo Rodrigues and Henry Schreiner.
#
# Distributed under the 3-clause BSD license, see accompanying file LICENSE
Expand Down
3 changes: 0 additions & 3 deletions src/__main__.py

This file was deleted.

1 change: 1 addition & 0 deletions src/particle/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020, Eduardo Rodrigues and Henry Schreiner.
#
# Distributed under the 3-clause BSD license, see accompanying file LICENSE
Expand Down
120 changes: 64 additions & 56 deletions src/particle/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020, Eduardo Rodrigues and Henry Schreiner.
#
# Distributed under the 3-clause BSD license, see accompanying file LICENSE
Expand All @@ -13,59 +14,66 @@
import argparse
import sys

parser = argparse.ArgumentParser(
prog="particle", description="Particle command line display utility. Has two modes."
)

parser.add_argument(
"--version",
action="version",
version="%(prog)s {version}".format(version=__version__),
)

subparsers = parser.add_subparsers(help="Subcommands")

search = subparsers.add_parser(
"search",
help="Look up particles by PID or name (Ex.: python -m particle search D+ D-)",
)
search.add_argument("particle", nargs="+", help="Name(s) or ID(s)")

pdgid = subparsers.add_parser(
"pdgid", help="Print info from PID (Ex.: python -m particle pdgid 11 13)"
)
pdgid.add_argument("pdgid", nargs="+", help="ID(s)")

opts = parser.parse_args()

if "particle" in opts:
for cand in opts.particle:
if hasattr(cand, "decode"):
cand = cand.decode("utf-8")

try:
value = int(cand)
except ValueError:
value = 0

if value:
particles = [Particle.from_pdgid(value)]
else:
particles = Particle.from_string_list(cand)

if len(particles) == 0:
print("Particle", cand, "not found.")
sys.exit(1)
elif len(particles) == 1:
print(particles[0].describe())
else:
for particle in particles:
print(repr(particle))

print()

if "pdgid" in opts:
for value in opts.pdgid:
p = PDGID(value)
print(p)
print(PDGID(value).info())

def main():
parser = argparse.ArgumentParser(
prog="particle",
description="Particle command line display utility. Has two modes.",
)

parser.add_argument(
"--version",
action="version",
version="%(prog)s {version}".format(version=__version__),
)

subparsers = parser.add_subparsers(help="Subcommands")

search = subparsers.add_parser(
"search",
help="Look up particles by PID or name (Ex.: python -m particle search D+ D-)",
)
search.add_argument("particle", nargs="+", help="Name(s) or ID(s)")

pdgid = subparsers.add_parser(
"pdgid", help="Print info from PID (Ex.: python -m particle pdgid 11 13)"
)
pdgid.add_argument("pdgid", nargs="+", help="ID(s)")

opts = parser.parse_args()

if "particle" in opts:
for cand in opts.particle:
if hasattr(cand, "decode"):
cand = cand.decode("utf-8")

try:
value = int(cand)
except ValueError:
value = 0

if value:
particles = [Particle.from_pdgid(value)]
else:
particles = Particle.from_string_list(cand)

if len(particles) == 0:
print("Particle", cand, "not found.")
sys.exit(1)
elif len(particles) == 1:
print(particles[0].describe())
else:
for particle in particles:
print(repr(particle))

print()

if "pdgid" in opts:
for value in opts.pdgid:
p = PDGID(value)
print(p)
print(PDGID(value).info())


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions src/particle/converters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020, Eduardo Rodrigues and Henry Schreiner.
#
# Distributed under the 3-clause BSD license, see accompanying file LICENSE
Expand Down
1 change: 1 addition & 0 deletions src/particle/converters/bimap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020, Eduardo Rodrigues and Henry Schreiner.
#
# Distributed under the 3-clause BSD license, see accompanying file LICENSE
Expand Down
1 change: 1 addition & 0 deletions src/particle/converters/evtgen.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020, Eduardo Rodrigues and Henry Schreiner.
#
# Distributed under the 3-clause BSD license, see accompanying file LICENSE
Expand Down
1 change: 1 addition & 0 deletions src/particle/converters/geant.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020, Eduardo Rodrigues and Henry Schreiner.
#
# Distributed under the 3-clause BSD license, see accompanying file LICENSE
Expand Down
1 change: 1 addition & 0 deletions src/particle/converters/pythia.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020, Eduardo Rodrigues and Henry Schreiner.
#
# Distributed under the 3-clause BSD license, see accompanying file LICENSE
Expand Down
Loading

0 comments on commit 0abf3b2

Please sign in to comment.