Skip to content

Commit

Permalink
Fix the behavior for append
Browse files Browse the repository at this point in the history
  • Loading branch information
acbart committed Sep 5, 2018
1 parent 191da10 commit f39b25e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 31 deletions.
28 changes: 11 additions & 17 deletions examples/Example JN Problem.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": false
},
"metadata": {},
"source": [
"Create, initialize, and print an integer variable that represents your age."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"deletable": false
},
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from pedal.plugins.grade_magic import load_ipython_extension\n",
Expand All @@ -24,9 +20,8 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 11,
"metadata": {
"deletable": false,
"scrolled": true
},
"outputs": [
Expand All @@ -36,7 +31,7 @@
"'sleep'"
]
},
"execution_count": 23,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -60,10 +55,8 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 12,
"metadata": {
"deletable": false,
"editable": false,
"scrolled": false
},
"outputs": [
Expand Down Expand Up @@ -109,8 +102,8 @@
" $(last.element).animate({\"background-color\": \"#F6FAFF\"}, 1000);\n",
"}\n",
"\n",
"on_run_code.push(\"from pedal.plugins.grade_magic import blockpy_grade\");\n",
"on_run_code.push(\"print(blockpy_grade(1, student_code))\");\n",
"on_run_code.push(\"from pedal.plugins.grade_magic import execute_on_run_code\");\n",
"on_run_code.push('print(execute_on_run_code(\"gently(\\\\\\\"TEST\\\\\\\")\", student_code))');\n",
"\n",
"on_run_code = on_run_code.join(\"\\n\");\n",
"console.log(on_run_code);\n",
Expand Down Expand Up @@ -150,7 +143,8 @@
}
],
"source": [
"%grade_blockpy 1"
"%%grade\n",
"gently(\\\"TEST\\\")"
]
}
],
Expand All @@ -172,7 +166,7 @@
"pygments_lexer": "ipython3",
"version": "3.5.4"
},
"student_mode": true
"student_mode": false
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down
2 changes: 1 addition & 1 deletion pedal/plugins/grade_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def execute_on_run_code(on_run, student_code):
# If the %grade magic is used, we run the code directly.
LOCAL_GRADE = r'''
on_run_code.push("from pedal.plugins.grade_magic import execute_on_run_code");
on_run_code.push("print(execute_on_run_code({on_run_code}, student_code))");
on_run_code.push('print(execute_on_run_code({on_run_code}, student_code))');
'''

# If the %grade_blockpy magic is used, we need to get the on_run from blockpy.
Expand Down
16 changes: 8 additions & 8 deletions pedal/tifa/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ def _format_message(issue, data):
"was used. One of the times that you gave <code>{name}</code> "
"a value was incorrect."
).format(line=data['position']['line'], name=data['name'])
elif issue == 'Iterating over empty list':
elif issue == 'Iterating over non-list':
if 'name' not in data or data['name'] is None:
expression = "expression"
else:
expression = "variable <code>{}</code>".format(data['name'])
return ("The {expression} was set as an empty list, "
"and then you attempted to use it in an iteration on line "
"{line}. You should only iterate over non-empty lists."
return ("The {expression} is not a list, but you used "
"it in the iteration on line {line}. You should only iterate "
"over sequences like lists."
).format(line=data['position']['line'], expression=expression)
elif issue == 'Iterating over non-list':
elif issue == 'Iterating over empty list':
if 'name' not in data or data['name'] is None:
expression = "expression"
else:
expression = "variable <code>{}</code>".format(data['name'])
return ("The {expression} is not a list, but you used "
"it in the iteration on line {line}. You should only iterate "
"over sequences like lists."
return ("The {expression} was set as an empty list, "
"and then you attempted to use it in an iteration on line "
"{line}. You should only iterate over non-empty lists."
).format(line=data['position']['line'], expression=expression)
elif issue == 'Incompatible types':
op = OPERATION_DESCRIPTION.get(data['operation'].__class__,
Expand Down
2 changes: 0 additions & 2 deletions pedal/tifa/tifa.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,6 @@ def visit_If(self, node):
self.report_issue("Malformed Conditional")

# Visit the bodies
#this_path_id = self.path_id
this_path_id = self.path_chain[0]
if_path = Tifa.NewPath(self, this_path_id, "i")
with if_path:
Expand Down Expand Up @@ -1158,7 +1157,6 @@ def __enter__(self):
self.tifa.name_map[self.id] = {}
self.tifa.path_parents[self.id] = self.origin_path
def __exit__(self, type, value, traceback):
val = self.tifa.path_chain[0]
self.tifa.path_names.pop()
self.tifa.path_chain.pop(0)

Expand Down
5 changes: 3 additions & 2 deletions pedal/tifa/type_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ def load_attr(self, attr, tifa, callee=None, callee_position=None):
if attr == 'append':
def _append(tifa, function_type, callee, args, position):
if args:
cloned_type = ListType(subtype=args[0].clone(),
empty=False)
if callee:
tifa.append_variable(callee, ListType(args[0].clone()),
position)
tifa.append_variable(callee, cloned_type, position)
self.empty = False
self.subtype = args[0]
return FunctionType(_append, 'append')
Expand Down
2 changes: 1 addition & 1 deletion tests/example_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from pedal.report import *
report.set_source(STUDENT_SOURCE)
# get_output, set_success, gently, explain
from pedal.tifa import tifa_analysis()
from pedal.tifa import tifa_analysis
# tifa_analysis
from pedal.mistakes import mistakes
# misconceptions
Expand Down
4 changes: 4 additions & 0 deletions tests/test_tifa.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@
'int_membership_in_comprehension':
['4 in (a for a in [1,2,3])', ['Unused Variable', "Incompatible types"], []],

'prevent_empty_iteration_in_appended_list':
['eles = [1,2,3]\nx = []\nfor ele in eles:\n x.append(ele)\nfor e2 in x:\n e2+1',
['Iterating over empty list'], []],

# Built-in modules
'import_string_letters':
['import string\nstring.letters+""', ['Incompatible types'], []],
Expand Down

0 comments on commit f39b25e

Please sign in to comment.