datacamp / pythonwhat

Verify Python code submissions and auto-generate meaningful feedback messages.
http://pythonwhat.readthedocs.io/
GNU Affero General Public License v3.0
69 stars 31 forks source link

Add templated expr_code #385

Closed hermansje closed 5 years ago

hermansje commented 5 years ago

It would be useful to be able to use the zoomed code in exprcode. We decided to use string replace on "__focus_\" instead of using format. Using format would not allow the use of {} anywhere in exprcode. "__focus_\ "is also replaced in the feedback message.

Usage: expr_code="list(__focus__)"

Todo: Add usage to pyhtonwhat docs

codecov[bot] commented 5 years ago

Codecov Report

Merging #385 into master will increase coverage by <.01%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #385      +/-   ##
==========================================
+ Coverage   94.08%   94.08%   +<.01%     
==========================================
  Files          26       26              
  Lines        2113     2115       +2     
==========================================
+ Hits         1988     1990       +2     
  Misses        125      125
Impacted Files Coverage Δ
pythonwhat/checks/has_funcs.py 96.21% <100%> (+0.04%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 5599e7e...9c7ddad. Read the comment docs.

TimSangster commented 5 years ago

I'm not 100% convinced this will work. If there is any expr_code out there that tries to use an empty dict, calling format on it will give a IndexError?

>>> expr_code = "mydict = {}"
>>> expr_code.format(focus="some code")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
hermansje commented 5 years ago

As discussed, doing string replacement with __focus__ seems like a good idea.