Skip to content

Commit bf15ced

Browse files
committed
try passing config instead
1 parent 1582b88 commit bf15ced

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

public/main.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
# main.py
22
from pyscript import PyWorker, document, window
33
import js
4+
import json
45
from challenge import multiplication_challenge as mc
56

6-
def challenge(attr):
7-
return mc[attr]
8-
97
def make_worker():
108
worker = PyWorker("worker.py", type="pyodide")
11-
worker.sync.challenge = challenge
129
return worker
1310

1411
# ---- Initialize Ace Editor from Python ----
@@ -42,10 +39,11 @@ async def run(event):
4239
output.innerText = "⏳ Running..."
4340

4441
# ---- TIMEOUT HANDLING ----
45-
timeout_ms = 1500 # 1.5 second limit
42+
timeout_ms = mc["timeout_ms"]
4643

4744
# Create a promise that wraps worker.sync.evaluate
48-
eval_promise = worker.sync.evaluate(code)
45+
mc_json = json.dumps(mc)
46+
eval_promise = worker.sync.evaluate(code, mc_json)
4947

5048
# Create the timeout promise
5149
timeout_promise = window.Promise.new(

public/pyscript.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"files": {
3-
"https://jacklowrie.github.io/pyscript-coding-challenge/challenge.py": ""
3+
"./challenge.py": ""
44
}
55
}
66

public/worker.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# worker.py
22
from pyscript import sync
3+
import json
34
import ast
4-
challenge = sync.challenge
5+
56
op_symbol_map = {
67
ast.Add: '+',
78
ast.Sub: '-',
@@ -30,45 +31,64 @@ def symbol_lookup(key, by_symbol=True):
3031
else:
3132
return op_symbol_map[key]
3233

33-
def check_rules(code):
34+
def check_rules(code, config):
3435
try:
3536
tree = ast.parse(code)
3637
except SyntaxError:
3738
return False # syntax errors will be caught later
3839

39-
forbidden_ops = [symbol_lookup(s) for s in challenge("forbidden_operators")]
40+
forbidden_ops = [symbol_lookup(s) for s in config["forbidden_operators"]]
4041
for node in ast.walk(tree):
4142
if isinstance(node, ast.BinOp) or isinstance(node, ast.AugAssign):
4243
for op in forbidden_ops:
4344
if isinstance(node.op, op):
4445
return True, symbol_lookup(type(node.op), False)
4546
return False, None
4647

47-
def evaluate(code):
48+
def evaluate(code, config_json):
49+
print("worker.py: Entering evaluate function.")
50+
51+
# --- PARSE THE JSON STRING ---
52+
config = None
53+
try:
54+
# Parse the JSON string back into a Python dictionary
55+
config = json.loads(config_json)
56+
print("worker.py: Successfully parsed challenge data JSON.")
57+
except json.JSONDecodeError as e:
58+
print(f"worker.py: Failed to decode challenge data JSON: {e}")
59+
return f"💥 Error: Could not parse challenge data - {e}"
60+
except Exception as e:
61+
print(f"worker.py: Unexpected error parsing challenge data: {e}")
62+
return f"💥 Unexpected error with challenge data: {e}"
63+
64+
65+
66+
67+
4868
code = code.replace("\t", " ")
4969
namespace = {}
5070
try:
5171
exec(code, namespace)
5272
except Exception as e:
5373
return f"💥 Code failed to compile:\n{e}"
5474

55-
if challenge("function_name") not in namespace:
56-
return f"💥 {challenge("function_name")} function not defined"
75+
if config["function_name"] not in namespace:
76+
return f"💥 {config["function_name"]} function not defined"
5777

58-
rule_violation, symbol = check_rules(code)
78+
rule_violation, symbol = check_rules(code, config)
5979
if rule_violation:
6080
return f"💥 Your solution cannot use the {symbol} operator!"
6181

62-
tests = challenge("test_cases")
82+
tests = config["test_cases"]
6383

6484
results = []
6585
for test in tests:
6686
try:
6787
exec(code, namespace)
68-
if challenge("function_name") not in namespace:
69-
results.append(f"error: '{challenge("function_name")}' function not defined")
88+
if config["function_name"] not in namespace:
89+
results.append(f"error: '{config["function_name"]}' function not defined")
7090
continue
71-
results.append(namespace[challenge("function_name")](test[0], test[1]))
91+
results.append(namespace[config["function_name"]](test[0], test[1]))
7292
except Exception as e:
7393
results.append( f"error: {e}")
7494

0 commit comments

Comments
 (0)