From 6b90b42e54b757eb57b26953d7947aef0fccac06 Mon Sep 17 00:00:00 2001 From: Tim Paine <3105306+timkpaine@users.noreply.github.com> Date: Tue, 6 Feb 2024 14:30:16 -0500 Subject: [PATCH] Refactor and test examples Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com> --- .gitignore | 6 +-- MANIFEST.in | 4 +- csp/examples/e_03_numba_simple_example.py | 52 ------------------- .../mystruct/__init__.py | 0 csp/tests/test_examples.py | 26 ++++++++++ .../1_basics}/e_01_trade_pnl.py | 6 +-- .../1_basics}/e_02_show_graph.py | 14 ++--- .../2_intermediate}/e_13_feedback.py | 0 .../2_intermediate}/e_15_dynamic.py | 0 .../2_intermediate}/e_19_exprtk_example.py | 0 .../2_intermediate}/e_20_stats.py | 0 .../2_intermediate}/e_21_numpy_stats.py | 0 .../e_06_parquet_writer_example.py | 0 .../3_using_adapters}/e_08_kafka.py | 0 .../e_11_websocket_output.py | 0 .../e_14_generic_push_adapter.py | 0 .../e_14_user_adapters_01_pullinput.py | 0 ...ser_adapters_02_adaptermanager_siminput.py | 0 .../e_14_user_adapters_03_pushinput.py | 0 ...er_adapters_04_adaptermanager_pushinput.py | 0 .../e_14_user_adapters_05_outputadapter.py | 0 ..._adapters_06_adaptermanager_inputoutput.py | 0 .../5_cpp/1_cpp_node}/CMakeLists.txt | 0 .../5_cpp/1_cpp_node}/README.md | 0 .../5_cpp/1_cpp_node}/piglatin.cpp | 0 .../5_cpp/1_cpp_node}/piglatin/__init__.py | 0 .../5_cpp/1_cpp_node}/piglatin/__main__.py | 0 .../5_cpp/1_cpp_node}/pyproject.toml | 0 .../5_cpp/1_cpp_node}/setup.py | 0 .../5_cpp/2_cpp_node_with_struct}/.gitignore | 0 .../2_cpp_node_with_struct}/CMakeLists.txt | 0 .../5_cpp/2_cpp_node_with_struct}/README.md | 0 .../mystruct}/__init__.py | 0 .../mystruct/__main__.py | 0 .../2_cpp_node_with_struct}/mystruct/node.py | 0 .../mystruct/struct.py | 0 .../2_cpp_node_with_struct}/pyproject.toml | 0 .../5_cpp/2_cpp_node_with_struct}/setup.py | 0 .../5_cpp/2_cpp_node_with_struct}/struct.cpp | 0 .../6_advanced}/e_12_caching_example.py | 0 .../e_99_csp_nand_computer.py | 0 .../99_developer_tools}/e_22_profiling.py | 0 examples/README.md | 3 ++ 43 files changed, 44 insertions(+), 67 deletions(-) delete mode 100644 csp/examples/e_03_numba_simple_example.py delete mode 100644 csp/examples/e_24_cpp_struct_example/mystruct/__init__.py create mode 100644 csp/tests/test_examples.py rename {csp/examples => examples/1_basics}/e_01_trade_pnl.py (95%) rename {csp/examples => examples/1_basics}/e_02_show_graph.py (68%) rename {csp/examples => examples/2_intermediate}/e_13_feedback.py (100%) rename {csp/examples => examples/2_intermediate}/e_15_dynamic.py (100%) rename {csp/examples => examples/2_intermediate}/e_19_exprtk_example.py (100%) rename {csp/examples => examples/2_intermediate}/e_20_stats.py (100%) rename {csp/examples => examples/2_intermediate}/e_21_numpy_stats.py (100%) rename {csp/examples => examples/3_using_adapters}/e_06_parquet_writer_example.py (100%) rename {csp/examples => examples/3_using_adapters}/e_08_kafka.py (100%) rename {csp/examples => examples/3_using_adapters}/e_11_websocket_output.py (100%) rename {csp/examples => examples/4_writing_adapters}/e_14_generic_push_adapter.py (100%) rename {csp/examples => examples/4_writing_adapters}/e_14_user_adapters_01_pullinput.py (100%) rename {csp/examples => examples/4_writing_adapters}/e_14_user_adapters_02_adaptermanager_siminput.py (100%) rename {csp/examples => examples/4_writing_adapters}/e_14_user_adapters_03_pushinput.py (100%) rename {csp/examples => examples/4_writing_adapters}/e_14_user_adapters_04_adaptermanager_pushinput.py (100%) rename {csp/examples => examples/4_writing_adapters}/e_14_user_adapters_05_outputadapter.py (100%) rename {csp/examples => examples/4_writing_adapters}/e_14_user_adapters_06_adaptermanager_inputoutput.py (100%) rename {csp/examples/e_23_cpp_example => examples/5_cpp/1_cpp_node}/CMakeLists.txt (100%) rename {csp/examples/e_23_cpp_example => examples/5_cpp/1_cpp_node}/README.md (100%) rename {csp/examples/e_23_cpp_example => examples/5_cpp/1_cpp_node}/piglatin.cpp (100%) rename {csp/examples/e_23_cpp_example => examples/5_cpp/1_cpp_node}/piglatin/__init__.py (100%) rename {csp/examples/e_23_cpp_example => examples/5_cpp/1_cpp_node}/piglatin/__main__.py (100%) rename {csp/examples/e_23_cpp_example => examples/5_cpp/1_cpp_node}/pyproject.toml (100%) rename {csp/examples/e_23_cpp_example => examples/5_cpp/1_cpp_node}/setup.py (100%) rename {csp/examples/e_24_cpp_struct_example => examples/5_cpp/2_cpp_node_with_struct}/.gitignore (100%) rename {csp/examples/e_24_cpp_struct_example => examples/5_cpp/2_cpp_node_with_struct}/CMakeLists.txt (100%) rename {csp/examples/e_24_cpp_struct_example => examples/5_cpp/2_cpp_node_with_struct}/README.md (100%) rename {csp/examples => examples/5_cpp/2_cpp_node_with_struct/mystruct}/__init__.py (100%) rename {csp/examples/e_24_cpp_struct_example => examples/5_cpp/2_cpp_node_with_struct}/mystruct/__main__.py (100%) rename {csp/examples/e_24_cpp_struct_example => examples/5_cpp/2_cpp_node_with_struct}/mystruct/node.py (100%) rename {csp/examples/e_24_cpp_struct_example => examples/5_cpp/2_cpp_node_with_struct}/mystruct/struct.py (100%) rename {csp/examples/e_24_cpp_struct_example => examples/5_cpp/2_cpp_node_with_struct}/pyproject.toml (100%) rename {csp/examples/e_24_cpp_struct_example => examples/5_cpp/2_cpp_node_with_struct}/setup.py (100%) rename {csp/examples/e_24_cpp_struct_example => examples/5_cpp/2_cpp_node_with_struct}/struct.cpp (100%) rename {csp/examples => examples/6_advanced}/e_12_caching_example.py (100%) rename {csp/examples => examples/98_just_for_fun}/e_99_csp_nand_computer.py (100%) rename {csp/examples => examples/99_developer_tools}/e_22_profiling.py (100%) create mode 100644 examples/README.md diff --git a/.gitignore b/.gitignore index d1ce08725..7c508dbdd 100644 --- a/.gitignore +++ b/.gitignore @@ -78,6 +78,9 @@ lib-cov coverage junit.xml +# Temp files +tmp*.* + # Documentation docs/_build/ /site @@ -100,9 +103,6 @@ csp/include/ csp/lib/ *.so *.tsbuildinfo -csp/extension -csp/nbextension -csp/labextension # Jupyter / Editors .ipynb_checkpoints diff --git a/MANIFEST.in b/MANIFEST.in index c31db9b64..e4601d238 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -17,10 +17,12 @@ prune _skbuild include vcpkg.json prune vcpkg -# IDE / VCS files +# IDE / VCS / docs files prune .git prune .github prune .vscode +prune ci +prune examples exclude .clang-format exclude .gitattributes exclude .gitignore diff --git a/csp/examples/e_03_numba_simple_example.py b/csp/examples/e_03_numba_simple_example.py deleted file mode 100644 index 79b4f3cc0..000000000 --- a/csp/examples/e_03_numba_simple_example.py +++ /dev/null @@ -1,52 +0,0 @@ -from datetime import datetime, timedelta -from typing import TypeVar - -import csp -from csp import ts - -node_t = csp.numba_node -# node_t = csp.node -T = TypeVar("T") - - -@node_t -def add_val(input: ts["T"], val_to_add: "~T") -> ts["T"]: - if csp.ticked(input) and csp.valid(input): - res = input + val_to_add - return res - - -@node_t -def my_prod(input1: ts["T"], input2: ts["T"]) -> ts["T"]: - if csp.ticked(input1, input2) and csp.valid(input1, input2): - return input1 * input2 - - -@node_t(state_types={"accum": "T"}) -# @node_t(debug_print=True) -def cum_sum(input: ts["T"]) -> ts["T"]: - with csp.state(): - accum = 0 - - if csp.ticked(input) and csp.valid(input): - accum += input // 10000 - return accum - - -@csp.graph -def my_graph(): - my_ts = csp.timer(timedelta(microseconds=100000), 100) - my_ts2 = add_val(my_ts, 100) - - accum_prod = cum_sum(my_prod(my_ts2, my_ts2)) - - sampled_s = csp.sample(csp.timer(timedelta(seconds=3600)), accum_prod) - - csp.add_graph_output("sampled", sampled_s) - - -if __name__ == "__main__": - s = datetime.now() - g = csp.run(my_graph, starttime=datetime(2020, 3, 1, 9, 30), endtime=timedelta(hours=0, minutes=390)) - e = datetime.now() - print((e - s).total_seconds()) diff --git a/csp/examples/e_24_cpp_struct_example/mystruct/__init__.py b/csp/examples/e_24_cpp_struct_example/mystruct/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/csp/tests/test_examples.py b/csp/tests/test_examples.py new file mode 100644 index 000000000..1949fa264 --- /dev/null +++ b/csp/tests/test_examples.py @@ -0,0 +1,26 @@ + +import importlib +import os.path +import pytest +import sys + +EXAMPLES_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "examples")) + +# make examples importable without keeping in source tree +sys.path.append(EXAMPLES_ROOT) + +def _get_module(folder, filename): + # Don't fail during test gathering + try: + return importlib.import_module(f"{folder}.{filename.replace('.py', '')}") + except BaseException: + return None + +def _get_modules_to_test(folder): + return [(file, _get_module(folder, file)) for file in os.listdir(os.path.join(EXAMPLES_ROOT, "1_basics")) if file.endswith(".py")] + +class TestExamples: + @pytest.mark.parametrize("filename,module", _get_modules_to_test("1_basics")) + def test_1_basics(self, filename, module): + assert module.main + module.main() diff --git a/csp/examples/e_01_trade_pnl.py b/examples/1_basics/e_01_trade_pnl.py similarity index 95% rename from csp/examples/e_01_trade_pnl.py rename to examples/1_basics/e_01_trade_pnl.py index 8919b2b35..f06541a2d 100644 --- a/csp/examples/e_01_trade_pnl.py +++ b/examples/1_basics/e_01_trade_pnl.py @@ -93,11 +93,7 @@ def my_graph(): def main(): start = datetime(2020, 1, 1) - show_graph = False - if show_graph: - csp.showgraph.show_graph(my_graph) - else: - csp.run(my_graph, starttime=start) + csp.run(my_graph, starttime=start) if __name__ == "__main__": diff --git a/csp/examples/e_02_show_graph.py b/examples/1_basics/e_02_show_graph.py similarity index 68% rename from csp/examples/e_02_show_graph.py rename to examples/1_basics/e_02_show_graph.py index 671a4963d..5fe2c686f 100644 --- a/csp/examples/e_02_show_graph.py +++ b/examples/1_basics/e_02_show_graph.py @@ -25,10 +25,12 @@ def my_graph(): csp.print("spread2", s2) +def main(): + # open in graphviz viewer + # csp.show_graph(my_graph) + # or output to file + csp.show_graph(my_graph, graph_filename="tmp.png") + csp.run(my_graph, starttime=datetime(2020, 3, 1), endtime=timedelta(seconds=10)) + if __name__ == "__main__": - csp.show_graph(my_graph) - realtime = True - if realtime: - csp.run(my_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=10), realtime=True) - else: - csp.run(my_graph, starttime=datetime(2020, 3, 1), endtime=timedelta(seconds=10)) + main() diff --git a/csp/examples/e_13_feedback.py b/examples/2_intermediate/e_13_feedback.py similarity index 100% rename from csp/examples/e_13_feedback.py rename to examples/2_intermediate/e_13_feedback.py diff --git a/csp/examples/e_15_dynamic.py b/examples/2_intermediate/e_15_dynamic.py similarity index 100% rename from csp/examples/e_15_dynamic.py rename to examples/2_intermediate/e_15_dynamic.py diff --git a/csp/examples/e_19_exprtk_example.py b/examples/2_intermediate/e_19_exprtk_example.py similarity index 100% rename from csp/examples/e_19_exprtk_example.py rename to examples/2_intermediate/e_19_exprtk_example.py diff --git a/csp/examples/e_20_stats.py b/examples/2_intermediate/e_20_stats.py similarity index 100% rename from csp/examples/e_20_stats.py rename to examples/2_intermediate/e_20_stats.py diff --git a/csp/examples/e_21_numpy_stats.py b/examples/2_intermediate/e_21_numpy_stats.py similarity index 100% rename from csp/examples/e_21_numpy_stats.py rename to examples/2_intermediate/e_21_numpy_stats.py diff --git a/csp/examples/e_06_parquet_writer_example.py b/examples/3_using_adapters/e_06_parquet_writer_example.py similarity index 100% rename from csp/examples/e_06_parquet_writer_example.py rename to examples/3_using_adapters/e_06_parquet_writer_example.py diff --git a/csp/examples/e_08_kafka.py b/examples/3_using_adapters/e_08_kafka.py similarity index 100% rename from csp/examples/e_08_kafka.py rename to examples/3_using_adapters/e_08_kafka.py diff --git a/csp/examples/e_11_websocket_output.py b/examples/3_using_adapters/e_11_websocket_output.py similarity index 100% rename from csp/examples/e_11_websocket_output.py rename to examples/3_using_adapters/e_11_websocket_output.py diff --git a/csp/examples/e_14_generic_push_adapter.py b/examples/4_writing_adapters/e_14_generic_push_adapter.py similarity index 100% rename from csp/examples/e_14_generic_push_adapter.py rename to examples/4_writing_adapters/e_14_generic_push_adapter.py diff --git a/csp/examples/e_14_user_adapters_01_pullinput.py b/examples/4_writing_adapters/e_14_user_adapters_01_pullinput.py similarity index 100% rename from csp/examples/e_14_user_adapters_01_pullinput.py rename to examples/4_writing_adapters/e_14_user_adapters_01_pullinput.py diff --git a/csp/examples/e_14_user_adapters_02_adaptermanager_siminput.py b/examples/4_writing_adapters/e_14_user_adapters_02_adaptermanager_siminput.py similarity index 100% rename from csp/examples/e_14_user_adapters_02_adaptermanager_siminput.py rename to examples/4_writing_adapters/e_14_user_adapters_02_adaptermanager_siminput.py diff --git a/csp/examples/e_14_user_adapters_03_pushinput.py b/examples/4_writing_adapters/e_14_user_adapters_03_pushinput.py similarity index 100% rename from csp/examples/e_14_user_adapters_03_pushinput.py rename to examples/4_writing_adapters/e_14_user_adapters_03_pushinput.py diff --git a/csp/examples/e_14_user_adapters_04_adaptermanager_pushinput.py b/examples/4_writing_adapters/e_14_user_adapters_04_adaptermanager_pushinput.py similarity index 100% rename from csp/examples/e_14_user_adapters_04_adaptermanager_pushinput.py rename to examples/4_writing_adapters/e_14_user_adapters_04_adaptermanager_pushinput.py diff --git a/csp/examples/e_14_user_adapters_05_outputadapter.py b/examples/4_writing_adapters/e_14_user_adapters_05_outputadapter.py similarity index 100% rename from csp/examples/e_14_user_adapters_05_outputadapter.py rename to examples/4_writing_adapters/e_14_user_adapters_05_outputadapter.py diff --git a/csp/examples/e_14_user_adapters_06_adaptermanager_inputoutput.py b/examples/4_writing_adapters/e_14_user_adapters_06_adaptermanager_inputoutput.py similarity index 100% rename from csp/examples/e_14_user_adapters_06_adaptermanager_inputoutput.py rename to examples/4_writing_adapters/e_14_user_adapters_06_adaptermanager_inputoutput.py diff --git a/csp/examples/e_23_cpp_example/CMakeLists.txt b/examples/5_cpp/1_cpp_node/CMakeLists.txt similarity index 100% rename from csp/examples/e_23_cpp_example/CMakeLists.txt rename to examples/5_cpp/1_cpp_node/CMakeLists.txt diff --git a/csp/examples/e_23_cpp_example/README.md b/examples/5_cpp/1_cpp_node/README.md similarity index 100% rename from csp/examples/e_23_cpp_example/README.md rename to examples/5_cpp/1_cpp_node/README.md diff --git a/csp/examples/e_23_cpp_example/piglatin.cpp b/examples/5_cpp/1_cpp_node/piglatin.cpp similarity index 100% rename from csp/examples/e_23_cpp_example/piglatin.cpp rename to examples/5_cpp/1_cpp_node/piglatin.cpp diff --git a/csp/examples/e_23_cpp_example/piglatin/__init__.py b/examples/5_cpp/1_cpp_node/piglatin/__init__.py similarity index 100% rename from csp/examples/e_23_cpp_example/piglatin/__init__.py rename to examples/5_cpp/1_cpp_node/piglatin/__init__.py diff --git a/csp/examples/e_23_cpp_example/piglatin/__main__.py b/examples/5_cpp/1_cpp_node/piglatin/__main__.py similarity index 100% rename from csp/examples/e_23_cpp_example/piglatin/__main__.py rename to examples/5_cpp/1_cpp_node/piglatin/__main__.py diff --git a/csp/examples/e_23_cpp_example/pyproject.toml b/examples/5_cpp/1_cpp_node/pyproject.toml similarity index 100% rename from csp/examples/e_23_cpp_example/pyproject.toml rename to examples/5_cpp/1_cpp_node/pyproject.toml diff --git a/csp/examples/e_23_cpp_example/setup.py b/examples/5_cpp/1_cpp_node/setup.py similarity index 100% rename from csp/examples/e_23_cpp_example/setup.py rename to examples/5_cpp/1_cpp_node/setup.py diff --git a/csp/examples/e_24_cpp_struct_example/.gitignore b/examples/5_cpp/2_cpp_node_with_struct/.gitignore similarity index 100% rename from csp/examples/e_24_cpp_struct_example/.gitignore rename to examples/5_cpp/2_cpp_node_with_struct/.gitignore diff --git a/csp/examples/e_24_cpp_struct_example/CMakeLists.txt b/examples/5_cpp/2_cpp_node_with_struct/CMakeLists.txt similarity index 100% rename from csp/examples/e_24_cpp_struct_example/CMakeLists.txt rename to examples/5_cpp/2_cpp_node_with_struct/CMakeLists.txt diff --git a/csp/examples/e_24_cpp_struct_example/README.md b/examples/5_cpp/2_cpp_node_with_struct/README.md similarity index 100% rename from csp/examples/e_24_cpp_struct_example/README.md rename to examples/5_cpp/2_cpp_node_with_struct/README.md diff --git a/csp/examples/__init__.py b/examples/5_cpp/2_cpp_node_with_struct/mystruct/__init__.py similarity index 100% rename from csp/examples/__init__.py rename to examples/5_cpp/2_cpp_node_with_struct/mystruct/__init__.py diff --git a/csp/examples/e_24_cpp_struct_example/mystruct/__main__.py b/examples/5_cpp/2_cpp_node_with_struct/mystruct/__main__.py similarity index 100% rename from csp/examples/e_24_cpp_struct_example/mystruct/__main__.py rename to examples/5_cpp/2_cpp_node_with_struct/mystruct/__main__.py diff --git a/csp/examples/e_24_cpp_struct_example/mystruct/node.py b/examples/5_cpp/2_cpp_node_with_struct/mystruct/node.py similarity index 100% rename from csp/examples/e_24_cpp_struct_example/mystruct/node.py rename to examples/5_cpp/2_cpp_node_with_struct/mystruct/node.py diff --git a/csp/examples/e_24_cpp_struct_example/mystruct/struct.py b/examples/5_cpp/2_cpp_node_with_struct/mystruct/struct.py similarity index 100% rename from csp/examples/e_24_cpp_struct_example/mystruct/struct.py rename to examples/5_cpp/2_cpp_node_with_struct/mystruct/struct.py diff --git a/csp/examples/e_24_cpp_struct_example/pyproject.toml b/examples/5_cpp/2_cpp_node_with_struct/pyproject.toml similarity index 100% rename from csp/examples/e_24_cpp_struct_example/pyproject.toml rename to examples/5_cpp/2_cpp_node_with_struct/pyproject.toml diff --git a/csp/examples/e_24_cpp_struct_example/setup.py b/examples/5_cpp/2_cpp_node_with_struct/setup.py similarity index 100% rename from csp/examples/e_24_cpp_struct_example/setup.py rename to examples/5_cpp/2_cpp_node_with_struct/setup.py diff --git a/csp/examples/e_24_cpp_struct_example/struct.cpp b/examples/5_cpp/2_cpp_node_with_struct/struct.cpp similarity index 100% rename from csp/examples/e_24_cpp_struct_example/struct.cpp rename to examples/5_cpp/2_cpp_node_with_struct/struct.cpp diff --git a/csp/examples/e_12_caching_example.py b/examples/6_advanced/e_12_caching_example.py similarity index 100% rename from csp/examples/e_12_caching_example.py rename to examples/6_advanced/e_12_caching_example.py diff --git a/csp/examples/e_99_csp_nand_computer.py b/examples/98_just_for_fun/e_99_csp_nand_computer.py similarity index 100% rename from csp/examples/e_99_csp_nand_computer.py rename to examples/98_just_for_fun/e_99_csp_nand_computer.py diff --git a/csp/examples/e_22_profiling.py b/examples/99_developer_tools/e_22_profiling.py similarity index 100% rename from csp/examples/e_22_profiling.py rename to examples/99_developer_tools/e_22_profiling.py diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 000000000..3142aada4 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,3 @@ +# `csp` examples + +