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

(internal) Typing support #244

Merged
merged 25 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
221a26a
Some ignores for backports for typing
henryiii May 19, 2020
a2c0269
Fixing a bunch of unclosed file warnings
henryiii May 19, 2020
b2a0d1a
Starting on mypy support
henryiii May 20, 2020
94c8953
Adding MyPy support to PDGID
henryiii May 20, 2020
8a35e1b
Full mypy support added to pdgid/functions
henryiii May 20, 2020
f974b13
Adding pre-commit changes
henryiii May 20, 2020
3a39e72
Basic types for Particle (with several skips
henryiii May 20, 2020
525a2cf
Fix for extra __main__ being dumped in the wrong place
henryiii May 20, 2020
add8eaa
Fix for Python 2
henryiii May 20, 2020
5708dff
Fix for loading from zipfiles
henryiii May 20, 2020
811aea3
Make typing optional for the ZipApp
henryiii May 21, 2020
92cd7aa
Prepare to push ZipApp as well
henryiii May 21, 2020
83136bc
ZipApp requires Python 3.7+
henryiii May 21, 2020
4d3c14f
ZipApp compression and earlier Python 3 support
henryiii May 21, 2020
ea0cb70
Minor optimizations
henryiii May 21, 2020
74ba2b6
No need to skip a file that is not present
henryiii May 21, 2020
c9574e3
Change internal table to a set. 100x faster load, faster searches. Fi…
henryiii May 21, 2020
ca04905
Add GHA badge
henryiii May 21, 2020
f485f6d
Update docs/CHANGELOG.md
eduardo-rodrigues May 21, 2020
c4044c2
Update docs/CHANGELOG.md
eduardo-rodrigues May 21, 2020
4f317ca
Update docs/CHANGELOG.md
eduardo-rodrigues May 21, 2020
11407ea
Update docs/CHANGELOG.md
eduardo-rodrigues May 21, 2020
6d48a6c
Update docs/CHANGELOG.md
eduardo-rodrigues May 21, 2020
1f24a79
Remove old performance addition - not needed with sets!
henryiii May 21, 2020
c5f567b
Mention PDGID works on any SupportsInt
henryiii May 21, 2020
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: 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 @@ -73,12 +86,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 @@ -87,16 +98,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 .
eduardo-rodrigues marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 4 additions & 0 deletions 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
7 changes: 5 additions & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog
Version 0.10.0
--------------

May 18th, 2020
May 21th, 2020

- `Particle` class:
- Several improvements, in particular to better deal with nuclei
eduardo-rodrigues marked this conversation as resolved.
Show resolved Hide resolved
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
eduardo-rodrigues marked this conversation as resolved.
Show resolved Hide resolved
- 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
eduardo-rodrigues marked this conversation as resolved.
Show resolved Hide resolved
- 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
eduardo-rodrigues marked this conversation as resolved.
Show resolved Hide resolved
- Some initial work on static type hints
eduardo-rodrigues marked this conversation as resolved.
Show resolved Hide resolved

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
1 change: 1 addition & 0 deletions src/particle/data/__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/exceptions.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/geant/__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
Loading