Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI #434

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft

AI #434

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def wrapped_callback(event_type, data):
result["message_sections"] = [
dict(
type=typ,
messages=[highlighted_markdown(message) for message in step_result.get(typ, [])],
unparsed_messages=(unparsed_messages := step_result.get(typ, [])),
messages=[highlighted_markdown(message) for message in unparsed_messages],
)
for typ in ["messages", "passed_tests", "lint"]
]
Expand Down
72 changes: 40 additions & 32 deletions core/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
from copy import deepcopy
from functools import cached_property, cache
from importlib import import_module
from io import StringIO
from pathlib import Path
from random import shuffle
from textwrap import indent
from tokenize import Untokenizer, generate_tokens
from types import MethodType
from typing import Union, List, get_type_hints

Expand Down Expand Up @@ -108,11 +106,6 @@ def basic_signature(func):
joined = ", ".join(inspect.signature(func).parameters)
return f'({joined})'


def basic_function_header(func):
return highlighted_markdown(f" def {func.__name__}{basic_signature(func)}:")


def clean_solution_function(func, source):
return re.sub(
rf"def {func.__name__}\(.+?\):",
Expand Down Expand Up @@ -333,11 +326,12 @@ def get_step(cls, step_name):
return step

def step_texts(cls, *, raw: bool):
result = [step.raw_text if raw else step.text for step in cls.steps[:-1]] + [cls.final_text]
if not raw:
result = [highlighted_markdown(text) for text in result]
if "__copyable__" in str(result) or "__no_auto_translate__" in str(result):
qa_error(f"{cls} has __copyable__ or __no_auto_translate__ in step texts")
return [step.raw_text if raw else step.text for step in cls.steps[:-1]] + [cls.final_text]

def step_texts_parsed(cls):
result = [highlighted_markdown(text) for text in cls.step_texts(raw=False)]
if "__copyable__" in str(result) or "__no_auto_translate__" in str(result):
qa_error(f"{cls} has __copyable__ or __no_auto_translate__ in step texts")
return result

@property
Expand All @@ -346,11 +340,13 @@ def slug(cls):

@property
def title(cls):
return unwrapped_markdown(
t.get(
t.page_title(cls.slug),
cls.raw_title,
)
return unwrapped_markdown(cls.unparsed_title)

@property
def unparsed_title(cls):
return t.get(
t.page_title(cls.slug),
cls.raw_title,
)

@property
Expand Down Expand Up @@ -384,14 +380,17 @@ def step_dicts(self):
dict(
index=index,
text=text,
unparsed_text=unparsed_text,
name=name,
hints=[highlighted_markdown(hint) for hint in getattr(step, "hints", [])],
unparsed_hints=(unparsed_hints := getattr(step, "hints", [])),
hints=[highlighted_markdown(hint) for hint in unparsed_hints],
solution=getattr(step, "get_solution", None),
program=getattr(step, "program", None),
prediction=get_predictions(step),
requirements=step.get_all_requirements() if getattr(step, "is_step", False) else None,
)
for index, (name, text, step) in
enumerate(zip(self.step_names, self.step_texts(raw=False), self.steps))
for index, (name, text, unparsed_text, step) in
enumerate(zip(self.step_names, self.step_texts_parsed(), self.step_texts(raw=False), self.steps))
]


Expand Down Expand Up @@ -495,11 +494,9 @@ def check_with_messages(self):

if self.code_source != "shell":
try:
tree = self.tree
except SyntaxError:
result["lint"] = list(lint(self.tree))
except Exception:
pass
else:
result["lint"] = lint(tree)

return result

Expand All @@ -520,12 +517,20 @@ def get_all_requirements(cls):
assert cls.hints
elif cls.requirements:
translated = t.get(t.requirements(cls), cls.requirements.strip())
result.append(dict(type="custom", message=highlighted_markdown(translated)))
result.append(dict(
type="custom",
message=highlighted_markdown(translated),
unparsed=dict(message=translated),
))

assert result, cls

if cls.expected_code_source:
result.append(dict(type="custom", message=highlighted_markdown(cls.expected_code_source_term())))
result.append(dict(
type="custom",
message=highlighted_markdown(cls.expected_code_source_term()),
unparsed=dict(message=cls.expected_code_source_term()),
))
return result

@classmethod
Expand Down Expand Up @@ -593,13 +598,16 @@ def check(self):
@classmethod
def get_requirements(cls):
result = [dict(type="exercise")]
inputs = cls.example_inputs()
stdin_input = inputs.pop("stdin_input", None)
raw_inputs = cls.example_inputs()
stdin_input = raw_inputs.pop("stdin_input", None)
if not cls.is_function_exercise:
inputs = highlighted_markdown(indented_inputs_string(inputs))
result.append(dict(type="non_function_exercise", inputs=inputs))
raw_inputs = indented_inputs_string(raw_inputs)
inputs = highlighted_markdown(raw_inputs)
result.append(dict(type="non_function_exercise", inputs=inputs, unparsed=dict(inputs=raw_inputs)))
else:
result.append(dict(type="function_exercise", header=basic_function_header(cls.solution)))
raw_header = f"def {cls.solution.__name__}{basic_signature(cls.solution)}:"
header = highlighted_markdown(" " + raw_header)
result.append(dict(type="function_exercise", header=header, unparsed=dict(header=raw_header)))
if goal := cls.function_exercise_goal():
result.append(dict(type="function_exercise_goal", print_or_return=goal))
if stdin_input:
Expand Down Expand Up @@ -854,7 +862,7 @@ def get_pages():
return dict(
pages={
slug: dict(
**select_attrs(page, "slug title index step_names"),
**select_attrs(page, "slug title unparsed_title index step_names"),
steps=page.step_dicts,
)
for slug, page in pages.items()
Expand Down
Loading
Loading