Skip to content

Commit

Permalink
pre-commit and clangformat (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDembinski committed Aug 20, 2022
1 parent 5f93840 commit d8f1d3a
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 78 deletions.
76 changes: 76 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# To use:
#
# pre-commit run -a
#
# Or:
#
# pre-commit install # (runs every time you commit in git)
#
# To update this file:
#
# pre-commit autoupdate
#
# See https://github.com/pre-commit/pre-commit

repos:
# Standard hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
args: ["--allow-multiple-documents"]
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
- id: sort-simple-yaml
- id: file-contents-sorter
- id: trailing-whitespace
exclude: ^doc/_static/.*.svg

# Python formatting
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black

# Python docstring formatting
- repo: https://github.com/pycqa/pydocstyle
rev: 6.1.1
hooks:
- id: pydocstyle
files: src/iminuit/[^_].*\.py

# Python linter (Flake8)
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8

# C++ formatting
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v13.0.1
hooks:
- id: clang-format

# CMake formatting
- repo: https://github.com/cheshirekow/cmake-format-precommit
rev: v0.6.13
hooks:
- id: cmake-format
additional_dependencies: [pyyaml]
types: [file]
files: (\.cmake|CMakeLists.txt)(.in)?$

# Python type checking
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.971'
hooks:
- id: mypy
additional_dependencies: [numpy]
args: [src]
pass_filenames: false
13 changes: 6 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
)
endif()

set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ version selection")
set(CMAKE_CXX_STANDARD
11
CACHE STRING "C++ version selection")
set(CMAKE_CXX_STANDARD_REQUIRED ON) # optional, ensure standard is supported
set(CMAKE_CXX_EXTENSIONS OFF) # optional, keep compiler extensions off

Expand All @@ -32,11 +34,8 @@ if(MSVC)
target_compile_options(_core PRIVATE /bigobj)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
else()
target_compile_options(_core PRIVATE
-fvisibility=hidden
-Wall
-Wno-self-assign-overloaded
)
target_compile_options(_core PRIVATE -fvisibility=hidden -Wall
-Wno-self-assign-overloaded)
endif()
set_target_properties(_core PROPERTIES VISIBILITY_INLINES_HIDDEN ON)
target_compile_definitions(_core PRIVATE HEPMC3_HEPEVT_NMXHEP=10000)
target_compile_definitions(_core PRIVATE HEPMC3_HEPEVT_NMXHEP=10000)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ PIP_ONLY_BINARY = ":all:"
# to match numpy, we use manylinux2010 for cp36 to cp39
select = "cp3?-*"
manylinux-x86_64-image = "manylinux2010"
manylinux-i686-image = "manylinux2010"
manylinux-i686-image = "manylinux2010"
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ max-line-length = 90
ignore = E203

[pydocstyle]
convention = numpy
convention = numpy
53 changes: 23 additions & 30 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using ReaderRootTreePtr = std::shared_ptr<ReaderRootTree>;
using ReaderRootPtr = std::shared_ptr<ReaderRoot>;
#endif

void register_io(py::module &m) {
void register_io(py::module& m) {

// this class is here to simplify unit testing of Readers and Writers
py::class_<std::stringstream>(m, "stringstream")
Expand All @@ -43,66 +43,59 @@ void register_io(py::module &m) {

py::class_<ReaderAscii, ReaderAsciiPtr>(m, "ReaderAscii")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<std::stringstream &>()) METH(read_event, ReaderAscii)
.def(py::init<std::stringstream&>()) METH(read_event, ReaderAscii)
METH(failed, ReaderAscii) METH(close, ReaderAscii);

py::class_<ReaderAsciiHepMC2, ReaderAsciiHepMC2Ptr>(m, "ReaderAsciiHepMC2")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<std::stringstream &>()) METH(read_event, ReaderAsciiHepMC2)
.def(py::init<std::stringstream&>()) METH(read_event, ReaderAsciiHepMC2)
METH(failed, ReaderAsciiHepMC2) METH(close, ReaderAsciiHepMC2);

py::class_<ReaderLHEF, ReaderLHEFPtr>(m, "ReaderLHEF")
.def(py::init<const std::string>(), "filename"_a)
// This will be enabled a bit later: .def(py::init<std::stringstream&>())
METH(read_event, ReaderLHEF) METH(failed, ReaderLHEF)
METH(close, ReaderLHEF);
METH(read_event, ReaderLHEF) METH(failed, ReaderLHEF) METH(close, ReaderLHEF);

py::class_<ReaderHEPEVT, ReaderHEPEVTPtr>(m, "ReaderHEPEVT")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<std::stringstream &>())
.def(py::init<std::stringstream&>())
.def("read_event", (bool(ReaderHEPEVT::*)(GenEvent&)) & ReaderHEPEVT::read_event)
.def("read_event",
(bool(ReaderHEPEVT::*)(GenEvent &)) & ReaderHEPEVT::read_event)
.def("read_event",
(bool(ReaderHEPEVT::*)(GenEvent &, bool)) & ReaderHEPEVT::read_event)
(bool(ReaderHEPEVT::*)(GenEvent&, bool)) & ReaderHEPEVT::read_event)
METH(failed, ReaderHEPEVT) METH(close, ReaderHEPEVT);

py::class_<WriterAscii>(m, "WriterAscii")
.def(py::init<const std::string &, GenRunInfoPtr>(), "filename"_a,
.def(py::init<const std::string&, GenRunInfoPtr>(), "filename"_a,
"run"_a = nullptr)
.def(py::init<std::stringstream &, GenRunInfoPtr>(), "ostringstream"_a,
"run"_a = nullptr, py::keep_alive<1, 2>())
METH(write_event, WriterAscii) METH(write_run_info, WriterAscii)
METH(failed, WriterAscii) METH(close, WriterAscii)
PROP(precision, WriterAscii);
.def(py::init<std::stringstream&, GenRunInfoPtr>(), "ostringstream"_a,
"run"_a = nullptr, py::keep_alive<1, 2>()) METH(write_event, WriterAscii)
METH(write_run_info, WriterAscii) METH(failed, WriterAscii)
METH(close, WriterAscii) PROP(precision, WriterAscii);

py::class_<WriterAsciiHepMC2>(m, "WriterAsciiHepMC2")
.def(py::init<const std::string &, GenRunInfoPtr>(), "filename"_a,
.def(py::init<const std::string&, GenRunInfoPtr>(), "filename"_a,
"run"_a = nullptr)
.def(py::init<std::stringstream &, GenRunInfoPtr>(), "ostringstream"_a,
.def(py::init<std::stringstream&, GenRunInfoPtr>(), "ostringstream"_a,
"run"_a = nullptr, py::keep_alive<1, 2>())
METH(write_event, WriterAsciiHepMC2)
METH(write_run_info, WriterAsciiHepMC2)
METH(failed, WriterAsciiHepMC2) METH(close, WriterAsciiHepMC2)
PROP(precision, WriterAsciiHepMC2);
METH(write_event, WriterAsciiHepMC2) METH(write_run_info, WriterAsciiHepMC2)
METH(failed, WriterAsciiHepMC2) METH(close, WriterAsciiHepMC2)
PROP(precision, WriterAsciiHepMC2);

py::class_<WriterHEPEVT>(m, "WriterHEPEVT")
.def(py::init<const std::string &>(), "filename"_a)
METH(write_event, WriterHEPEVT) METH(failed, WriterHEPEVT)
METH(close, WriterHEPEVT);
.def(py::init<const std::string&>(), "filename"_a) METH(write_event, WriterHEPEVT)
METH(failed, WriterHEPEVT) METH(close, WriterHEPEVT);

#ifdef HEPMC3_ROOTIO

py::class_<ReaderRootTree, ReaderRootTreePtr>(m, "ReaderRootTree")
.def(py::init<const std::string>(), "filename"_a)
.def(py::init<const std::string, const std::string, const std::string>(),
"filename"_a, "treename"_a, "branchname"_a)
METH(read_event, ReaderRootTree) METH(failed, ReaderRootTree)
METH(close, ReaderRootTree);
"filename"_a, "treename"_a, "branchname"_a) METH(read_event, ReaderRootTree)
METH(failed, ReaderRootTree) METH(close, ReaderRootTree);

py::class_<ReaderRoot, ReaderRootPtr>(m, "ReaderRoot")
.def(py::init<const std::string>(), "filename"_a)
METH(read_event, ReaderRoot) METH(failed, ReaderRoot)
METH(close, ReaderRoot);
.def(py::init<const std::string>(), "filename"_a) METH(read_event, ReaderRoot)
METH(failed, ReaderRoot) METH(close, ReaderRoot);

#endif
}
18 changes: 8 additions & 10 deletions src/pybind.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@
namespace py = pybind11;
using namespace py::literals;

template <class R, class C, class... Args,
class _T = typename std::conditional<std::is_const<C>::value,
R (C::*)(Args...) const,
R (C::*)(Args...)>::type>
template <
class R, class C, class... Args,
class _T = typename std::conditional<
std::is_const<C>::value, R (C::*)(Args...) const, R (C::*)(Args...)>::type>
_T overload_cast(_T x) {
return x;
}

#define FUNC(name) m.def(#name, name)
#define PROP_RO(name, cls) .def_property_readonly(#name, &cls::name)
#define PROP_ROS(name, cls) .def_property_readonly_static(#name, &cls::name)
#define PROP_RO_OL(name, cls, rval) \
#define PROP_RO_OL(name, cls, rval) \
.def_property_readonly(#name, overload_cast<rval, const cls>(&cls::name))
#define PROP(name, cls) .def_property(#name, &cls::name, &cls::set_##name)
#define PROP_OL(name, cls, rval) \
.def_property(#name, overload_cast<rval, const cls>(&cls::name), \
&cls::set_##name)
#define PROP_OL(name, cls, rval) \
.def_property(#name, overload_cast<rval, const cls>(&cls::name), &cls::set_##name)
#define METH(name, cls) .def(#name, &cls::name)
#define METH_OL(name, cls, rval, args) \
.def(#name, (rval(cls::*)(args)) & cls::name)
#define METH_OL(name, cls, rval, args) .def(#name, (rval(cls::*)(args)) & cls::name)

#endif
3 changes: 1 addition & 2 deletions src/pyhepmc/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def __next__(self):
raise StopIteration
return evt


_Iter.next = _Iter.__next__
next = __next__


def _enter(self):
Expand Down
48 changes: 21 additions & 27 deletions tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,27 @@ def test_dot(tmpdir, evt): # noqa
d = view.to_dot(evt)
assert str(d) == (
"""digraph "event 1" {
node [shape=point]
-1
-2
-3
-4
in_0 [style=invis]
in_0 -> -1 [label="p
7 GeV"]
in_1 [style=invis]
in_1 -> -2 [label="p
7 GeV"]
-1 -> -3 [label="d
0.032 GeV"]
-2 -> -3 [label="u~
0.058 GeV"]
-3 -> -4 [label="W-
0.086 GeV"]
out_0 [style=invis]
-3 -> out_0 [label="gamma
0.0042 GeV"]
out_1 [style=invis]
-4 -> out_1 [label="d
0.03 GeV"]
out_2 [style=invis]
-4 -> out_2 [label="u~
0.056 GeV"]
node [shape=point]
-1
-2
-3
-4
in_0 [style=invis]
in_0 -> -1 [label="p\n7 GeV"]
in_1 [style=invis]
in_1 -> -2 [label="p\n7 GeV"]
-1 -> -3 [label="d\n0.032 GeV"]
-2 -> -3 [label="u~\n0.058 GeV"]
-3 -> -4 [label="W-\n0.086 GeV"]
out_0 [style=invis]
-3 -> out_0 [label="gamma\n0.0042 GeV"]
out_1 [style=invis]
-4 -> out_1 [label="d\n0.03 GeV"]
out_2 [style=invis]
-4 -> out_2 [label="u~\n0.056 GeV"]
}
"""
""".replace(
" ", "\t"
)
)
# d.render(view=True)

0 comments on commit d8f1d3a

Please sign in to comment.