Skip to content

Commit e70e4d7

Browse files
timkpaineCarreau
authored andcommitted
Merge pull request #174 from Point72/tkp/slacktut
Start organizing examples, increase test coverage of examples
2 parents ef1a239 + 5c1ef8a commit e70e4d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+358
-362
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ lint-cpp:
4444
echo "C++ linting disabled for now"
4545

4646
lint-docs:
47-
python -m mdformat --check docs/wiki/ README.md examples/README.md
48-
python -m codespell_lib docs/wiki/ README.md examples/README.md
47+
python -m mdformat --check docs/wiki/ README.md examples/
48+
python -m codespell_lib docs/wiki/ README.md examples/ --skip "*.cpp,*.h"
4949

5050
# lint: lint-py lint-cpp ## run lints
5151
lint: lint-py lint-docs ## run lints
@@ -62,8 +62,8 @@ fix-cpp:
6262
echo "C++ autoformatting disabled for now"
6363

6464
fix-docs:
65-
python -m mdformat docs/wiki/ README.md examples/README.md
66-
python -m codespell_lib --write docs/wiki/ README.md examples/README.md
65+
python -m mdformat docs/wiki/ README.md examples/
66+
python -m codespell_lib --write docs/wiki/ README.md examples/ --skip "*.cpp,*.h"
6767

6868
fix: fix-py fix-cpp fix-docs ## run autofixers
6969

conda/dev-environment-unix.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ channels:
55
dependencies:
66
- bison
77
- brotli
8+
- build
89
- bump2version>=1
910
- cmake
1011
- codespell
1112
- compilers
1213
- cyrus-sasl
1314
- exprtk
1415
- flex
16+
- graphviz
1517
- python-graphviz
1618
- gtest
17-
- httpx
18-
- isort
19+
- httpx>=0.20,<1
20+
- isort>=5,<6
1921
- libarrow=15
2022
- librdkafka
2123
- libboost-headers
@@ -24,16 +26,18 @@ dependencies:
2426
- mdformat
2527
- ninja
2628
- numpy
29+
- pillow
2730
- psutil
2831
- pyarrow=15
2932
- pandas
3033
- pillow
3134
- polars
35+
- psutil
3236
- pytz
3337
- pytest
38+
- pytest-asyncio
3439
- pytest-cov
3540
- pytest-sugar
36-
- pytest-asyncio
3741
- python<3.12
3842
- python-rapidjson
3943
- rapidjson
@@ -44,6 +48,7 @@ dependencies:
4448
- slack-sdk
4549
- sqlalchemy
4650
- tar
51+
- threadpoolctl
4752
- tornado
4853
- twine
4954
- unzip

csp/tests/test_examples.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ def _get_module(folder, filename):
1717
return None
1818

1919

20-
def _get_modules_to_test(folder):
20+
def _get_modules_to_test(*folders):
21+
folder = ".".join(folders) if len(folders) > 0 else folders[0]
2122
return [
2223
(file, _get_module(folder, file))
23-
for file in os.listdir(os.path.join(EXAMPLES_ROOT, folder))
24+
for file in os.listdir(os.path.join(EXAMPLES_ROOT, *folders))
2425
if file.endswith(".py")
2526
]
2627

@@ -31,12 +32,37 @@ def _no_examples_folder_or_running_sdist_tests():
3132

3233
@pytest.mark.skipif(_no_examples_folder_or_running_sdist_tests(), reason="no examples present or manually skipping")
3334
class TestExamples:
34-
@pytest.mark.parametrize("filename,module", _get_modules_to_test("1_basics"))
35+
@pytest.mark.parametrize("filename,module", _get_modules_to_test("01_basics"))
3536
def test_1_basics(self, filename, module):
3637
assert module.main
3738
module.main()
3839

39-
@pytest.mark.parametrize("filename,module", _get_modules_to_test("2_intermediate"))
40+
@pytest.mark.parametrize("filename,module", _get_modules_to_test("02_intermediate"))
4041
def test_2_intermediate(self, filename, module):
4142
assert module.main
4243
module.main()
44+
45+
@pytest.mark.parametrize("filename,module", _get_modules_to_test("03_using_adapters", "parquet"))
46+
def test_3_adapters_parquet(self, filename, module):
47+
assert module.main
48+
module.main()
49+
50+
@pytest.mark.parametrize("filename,module", _get_modules_to_test("04_writing_adapters"))
51+
def test_4_writing_adapters(self, filename, module):
52+
assert module.main
53+
module.main()
54+
55+
@pytest.mark.parametrize("filename,module", _get_modules_to_test("06_advanced"))
56+
def test_6_advanced(self, filename, module):
57+
assert module.main
58+
module.main()
59+
60+
@pytest.mark.parametrize("filename,module", _get_modules_to_test("98_just_for_fun"))
61+
def test_98_just_for_fun(self, filename, module):
62+
assert module.main
63+
module.main()
64+
65+
@pytest.mark.parametrize("filename,module", _get_modules_to_test("99_developer_tools"))
66+
def test_99_developer_tools(self, filename, module):
67+
assert module.main
68+
module.main()

examples/01_basics/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Basics
2+
3+
- [Simplest Possible Graph](./e1_basic.py)
4+
- [Ticking Graphs](./e2_ticking.py)
5+
- [Complete Example (Trading)](./e3_trade_pnl.py)
6+
- [Visualizing a Graph](./e4_show_graph.py)

examples/01_basics/e1_basic.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from datetime import datetime
2+
3+
import csp
4+
from csp import ts
5+
6+
7+
@csp.node
8+
def add(x: ts[int], y: ts[int]) -> ts[int]:
9+
return x + y
10+
11+
12+
@csp.graph
13+
def my_graph():
14+
x = csp.const(1)
15+
y = csp.const(2)
16+
17+
sum = add(x, y)
18+
19+
csp.print("x", x)
20+
csp.print("y", y)
21+
csp.print("sum", sum)
22+
23+
24+
def main():
25+
csp.run(my_graph, starttime=datetime.now())
26+
27+
28+
if __name__ == "__main__":
29+
main()

examples/01_basics/e2_ticking.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from datetime import datetime, timedelta
2+
3+
import csp
4+
from csp import ts
5+
6+
7+
@csp.node
8+
def add(x: ts[int], y: ts[int]) -> ts[int]:
9+
return x + y
10+
11+
12+
@csp.node
13+
def accum(val: ts[int]) -> ts[int]:
14+
with csp.state():
15+
s_sum = 0
16+
if csp.ticked(val):
17+
s_sum += val
18+
return val
19+
20+
21+
@csp.graph
22+
def my_graph():
23+
st = datetime(2020, 1, 1)
24+
25+
# Dummy x values
26+
x = csp.curve(int, [(st + timedelta(1), 1), (st + timedelta(2), 2), (st + timedelta(3), 3)])
27+
28+
# Dummy y values
29+
y = csp.curve(int, [(st + timedelta(1), -1), (st + timedelta(3), -1), (st + timedelta(4), -1)])
30+
31+
# Add the time series
32+
sum = add(x, y)
33+
34+
# Accumulate the result
35+
acc = accum(sum)
36+
37+
csp.print("x", x)
38+
csp.print("y", y)
39+
csp.print("sum", sum)
40+
csp.print("accum", acc)
41+
42+
43+
def main():
44+
start = datetime(2020, 1, 1)
45+
csp.run(my_graph, starttime=start)
46+
47+
48+
if __name__ == "__main__":
49+
main()
File renamed without changes.
File renamed without changes.

examples/02_intermediate/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Intermediate
2+
3+
- [Graph Loops (`csp.feedback`)](./e1_feedback.py)
4+
- [Statistics Nodes](./e2_stats.py)
5+
- [Statistics Nodes with Numpy](./e3_numpy_stats.py)
6+
- [Expression Nodes with `exprtk`](./e4_exprtk.py)

examples/2_intermediate/e_13_feedback.py renamed to examples/02_intermediate/e1_feedback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def main():
8080
if show_graph:
8181
csp.showgraph.show_graph(my_graph)
8282
else:
83-
csp.run(my_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=60), realtime=True)
83+
csp.run(my_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=5), realtime=False)
8484

8585

8686
if __name__ == "__main__":

0 commit comments

Comments
 (0)