Skip to content

Commit 1985529

Browse files
authored
Add runtime test of all the examples (#135)
* Add runtime test of all the examples * Remove uninteresting example * Create run-examples action * Do not execute test_examples_run on build
1 parent e735d78 commit 1985529

File tree

84 files changed

+1065
-18
lines changed

Some content is hidden

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

84 files changed

+1065
-18
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
- name: pre-commit checks
4040
run: pre-commit run -a
4141
- name: run tests
42-
run: py.test -v --capture=tee-sys tests
42+
run: py.test -v --capture=tee-sys --ignore=tests/test_examples_run.py tests
4343

4444
viewer:
4545
name: Build PDL live viewer

.github/workflows/run-examples.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
name: Run examples
3+
4+
on:
5+
workflow_dispatch:
6+
7+
jobs:
8+
tests:
9+
name: Exeution tests
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
python-version: ['3.11', '3.12']
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Cache pip
23+
uses: actions/cache@v4
24+
with:
25+
# This path is specific to Ubuntu
26+
path: ${{ env.pythonLocation }}
27+
# Look to see if there is a cache hit for the setup file
28+
key: ${{ runner.os }}-pip-new3-${{ env.pythonLocation }}-${{ hashFiles('setup.py') }}
29+
restore-keys: |
30+
${{ runner.os }}-pip-new3
31+
${{ runner.os }}-new3
32+
- name: Install dependencies
33+
run: pip install --upgrade --upgrade-strategy eager .[all]
34+
- name: pip list packages
35+
run: pip list
36+
- name: show pip dependencies
37+
run: |
38+
pip install pipdeptree
39+
pipdeptree -fl
40+
- name: run tests
41+
run: py.test -v --capture=tee-sys tests/test_examples_run.py

examples/hello/test.pdl

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/react/react_fun.pdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ defs:
111111
Act: {"name": "Finish", "arguments": {"topic": "2.869047619047619"}}
112112
- call: react_inner
113113
args:
114-
context: []
114+
pdl_context: []
115115
examples: ${ examples }
116116
question: ${ question }
117117
model: ${ model }

examples/talk/react_fun.pdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ defs:
111111
Act: {"name": "Finish", "arguments": {"topic": "2.869047619047619"}}
112112
- call: react_inner
113113
args:
114-
context: []
114+
pdl_context: []
115115
examples: ${ examples }
116116
question: ${ question }
117117
model: ${ model }

src/pdl/pdl.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class InterpreterConfig(TypedDict, total=False):
4444
role: RoleType
4545
"""Default role.
4646
"""
47+
cwd: Path
48+
"""Path considered as the current working directory for file reading."""
4749

4850

4951
def exec_program(
@@ -127,7 +129,7 @@ def exec_str(
127129

128130

129131
def exec_file(
130-
prog: str,
132+
prog: str | Path,
131133
config: Optional[InterpreterConfig] = None,
132134
scope: Optional[ScopeType] = None,
133135
output: Literal["result", "all"] = "result",
@@ -144,6 +146,10 @@ def exec_file(
144146
Return the final result.
145147
"""
146148
program, loc = parse_file(prog)
149+
if config is None:
150+
config = InterpreterConfig()
151+
if config.get("cwd") is None:
152+
config["cwd"] = Path(prog).parent
147153
result = exec_program(program, config, scope, loc, output)
148154
return result
149155

@@ -284,18 +290,21 @@ def main():
284290
else:
285291
batch = 1
286292

287-
config = InterpreterConfig(
288-
yield_result=stream_result, yield_background=stream_background, batch=batch
289-
)
290293
pdl_file = Path(args.pdl)
291294
if args.trace == "*_trace.json":
292295
trace_file = str(pdl_file.with_suffix("")) + "_trace.json"
293296
else:
294297
trace_file = args.trace
298+
config = InterpreterConfig(
299+
yield_result=stream_result,
300+
yield_background=stream_background,
301+
batch=batch,
302+
cwd=pdl_file.parent,
303+
)
295304
pdl_interpreter.generate(
296305
pdl_file,
297306
args.log,
298-
InterpreterState(**config, cwd=pdl_file.parent),
307+
InterpreterState(**config),
299308
initial_scope,
300309
trace_file,
301310
)

src/pdl/pdl_interpreter.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,9 +1337,21 @@ def process_input(
13371337
read, block = process_expr_of(block, "read", scope, loc)
13381338
if read is not None:
13391339
file = state.cwd / read
1340-
with open(file, encoding="utf-8") as f:
1341-
s = f.read()
1342-
append_log(state, "Input from File: " + str(file), s)
1340+
try:
1341+
with open(file, encoding="utf-8") as f:
1342+
s = f.read()
1343+
append_log(state, "Input from File: " + str(file), s)
1344+
except Exception as exc:
1345+
if isinstance(exc, FileNotFoundError):
1346+
msg = f"file {str(file)} not found"
1347+
else:
1348+
msg = f"Fail to open file {str(file)}"
1349+
raise PDLRuntimeError(
1350+
message=msg,
1351+
loc=loc,
1352+
trace=ErrorBlock(msg=msg, location=loc, program=block),
1353+
fallback="",
1354+
) from exc
13431355
else:
13441356
message = ""
13451357
if block.message is not None:
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Question: Noah charges $60 for a large painting and $30 for a small painting.
2+
Last month he sold eight large paintings and four small paintings.
3+
If he sold twice as much this month, how much is his sales for this month?
4+
5+
Answer: Let's think step by step.
6+
He sold 8 large paintings and 4 small paintings last month.
7+
He sold twice as many this month.
8+
8 large paintings x $60 = << 8*60= 480 >> 480
9+
4 small paintings x $30 = << 4*30= 120 >> 120
10+
So he sold << 480+120= 600 >> 600 paintings last month.
11+
Therefore he sold << 600*2= 1200 >> this month.
12+
The answer is $1200.
13+
14+
Question: Noah charges $10 for a small vase and $30 for a large vases
15+
Last month he sold five large vases and three small vases.
16+
If he sold three times as much this month, how much is his sales for this month?
17+
18+
Answer: Let's think step by step.
19+
He sold 5 large vases and 3 small vases last month.
20+
He sold three times as many this month.
21+
5 large vases x $30 = << 5*30= 150 >> 150
22+
3 small vases x $10 = << 3*10= 30 >> 30
23+
So he sold << 150+30= 180 >> 180 vases last month.
24+
Therefore he sold << 180*3= 540 >> this month.
25+
The answer is $540.
26+
27+
Question: Noah charges $10 for a large box and $5 for a small box.
28+
Last month he sold four large boxes and two small boxes.
29+
If he sold half as much this month, how much is his sales for this month?
30+
31+
Answer: Let's think step by step.
32+
He sold 4 large boxes and 2 small boxes last month.
33+
He sold half as many this month.
34+
4 large boxes x $10 = << 4*10= 40 >> 40
35+
2 small boxes x $5 = << 2*5= 10 >> 10
36+
So he sold << 40+10= 50 >> 50 boxes last month.
37+
Therefore he sold << 50/2= 25.0 >> this month.
38+
The answer is 25
39+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Question: Noah charges $60 for a large painting and $30 for a small painting.
2+
Last month he sold eight large paintings and four small paintings.
3+
If he sold twice as much this month, how much is his sales for this month?
4+
5+
Answer: Let's think step by step.
6+
He sold 8 large paintings and 4 small paintings last month.
7+
He sold twice as many this month.
8+
8 large paintings x $60 = << 8*60= 480 >> 480
9+
4 small paintings x $30 = << 4*30= 120 >> 120
10+
So he sold << 480+120= 600 >> 600 paintings last month.
11+
Therefore he sold << 600*2= 1200 >> this month.
12+
The answer is $1200.
13+
14+
Question: Noah charges $10 for a small vase and $30 for a large vases
15+
Last month he sold five large vases and three small vases.
16+
If he sold three times as much this month, how much is his sales for this month?
17+
18+
Answer: Let's think step by step.
19+
He sold 5 large vases and 3 small vases last month.
20+
He sold three times as many this month.
21+
5 large vases x $30 = << 5*30= 150 >> 150
22+
3 small vases x $10 = << 3*10= 30 >> 30
23+
So he sold << 150+30= 180 >> 180 vases last month.
24+
Therefore he sold << 180*3= 540 >> this month.
25+
The answer is $540.
26+
27+
Question:
28+
Noah charges $10 for a small vase and $30 for a large vase.
29+
Last month he sold five large vases and three small vases.
30+
If he sold three times as much this month, how much is his sales for this month?
31+
Answer: Let's think step by step.
32+
He sold 5 large vases and 3 small vases last month.
33+
He sold three times as much this month.
34+
5 large vases x $30 = << 5*30= 150 >> 150
35+
3 small vases x $10 = << 3*10 = 30 >> 30
36+
So he sold << 150+30 = 180 >> 180 vases last month.
37+
Therefore he sold << 180*3 = 540 >> 540 vases this month.
38+
The answer is 540
39+
40+
41+
Question:
42+
Noah charges $10 for a small vase and $30 for a large vase.
43+
Last month he sold five large vases and three small vases.
44+
If he sold three times as much this month, how much is his sales for this month?
45+
Answer: Let's think step by step.
46+
He sold 5 large vases and 3 small vases last month.
47+
He sold three times as much this month.
48+
5 large vases x $30 = << 5*30 = 150 >> 150
49+
3 small vases x $10 = << 3*10 = 30 >> 30
50+
So he sold << 150+30 = 180 >> 180 vases last month.
51+
Therefore he sold << 180*3 = 540 >> 540 vases this month.
52+
The answer is 540
53+
54+
55+
Question:
56+
Noah charges $10 for a small vase and $30 for a large vase.
57+
Last month he sold five large vases and three small vases.
58+
If he sold three times as much this month, how much is his sales for this month?
59+
Answer: Let's think step by step.
60+
He sold 5 large vases and 3 small vases last month.
61+
He sold three times as much this month.
62+
5 large vases x $30 = << 5*30 = 150 >> 150
63+
3 small vases x $10 = << 3*10 = 30 >> 30
64+
So he sold << 150+30 = 180 >> 180 vases last month.
65+
Therefore he sold << 180*3 = 540 >> 540 vases this month.
66+
The answer is 540
67+
68+
69+
Question
70+
71+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
What is APR?
2+
Annual Percentage Rate (APR) is the total cost of borrowing money, expressed as a percentage. It includes the interest rate and any additional fees or charges associated with the loan. APR is used to compare different loan offers and to understand the true cost of borrowing money over time.
3+

0 commit comments

Comments
 (0)