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

generate exercises from solutions #14

Closed
DavidLeoni opened this issue Aug 25, 2018 · 0 comments
Closed

generate exercises from solutions #14

DavidLeoni opened this issue Aug 25, 2018 · 0 comments

Comments

@DavidLeoni
Copy link
Owner

DavidLeoni commented Aug 25, 2018

It would be cool to automatically generate exercises from solutions when building .To do so, we need to distinguish two types of exercises.

Type of exercises

There can be two kinds of exercises, exercises in python files and exercises in jupyter files.

Since Jupman 0.8, it is possible to automatically generate an exercise from a solution file by stripping text marked with special tags. It is possible to inspect generated files in _build/jupman/ directory

Note: in the zips for the students containing solutions, the tag comments are automatically removed from the solution files as well.

Exercises in python files

See example: exercises/python-intro/python-intro.ipynb

In this type of exercises, typically you have a jupyter file (like python-intro.ipynb) that describes the exercise and then the actual exercises are in python files.

If there is a solution file eding in _solution.py but no file ending in _exercise.py like this:

  • python_intro1_solution.py
  • python_intro1_test.py

then Jupman will try to generate one from a _solution.py file. To do so, it will look for tags to strip inside the solution file.

If there is already an exercise file like this:

  • python_intro2_exercise.py
  • python_intro2_solution.py
  • python_intro2_test.py

Jupman will just copy the existing file.

Exercises in jupyter files

See example: exercises/jupyter-intro/jupyter-intro-solution.ipynb

This type of exercises stay in a jupyter notebook itself. In this case, the following applies:

If there is a notebook ending in -solution.ipynb, the following applies (WARNING: for ipynb files we use dash -, not the underscore _):

  • the notebook must contain a title as markdown like # bla bla solution

    Note text must contain solution text, which can be customized in conf.py (you might need to translate it)

  • the notebook must contain tags to strip

Tags to strip

Start tags begin with a # while end tags begin with a #\

jupman-raise

Replaces code inside with an Exception (text is customizable in conf.py). Be careful to position the comment exactly with the indentation yuoi want the raise to appear. For example:

def add(x,y):   
    #jupman-raise
    return x + y
    #/jupman-raise

becomes

def add(x,y):   
    raise Exception('TODO IMPLEMENT ME !')

jupman-strip

Just strips code inside

def f(x):
    print(x)

#jupman-strip
def help_func(x,y):
    return x - y
#/jupman-strip

def g(y):
    return y

becomes

def f(x):
    print(x)

def g(y):
    return y
    

write solution here

This special tag for python code erases whatever is found afterwards the # write solution here comment

  • you can put how many spaces you want in the comment
  • phrase can be customized in conf.py
w = 5

#  write  solution here

x = 5 + w
y = 2 + x

becomes

w = 5

#  write  solution here
DavidLeoni added a commit that referenced this issue Aug 27, 2018
- fixed bugs
- now using proper nbformat library to parse ipynb files
- added write solution here special tag
DavidLeoni added a commit that referenced this issue Aug 27, 2018
- fixed bugs
- now using proper nbformat library to parse ipynb files
- added write solution here special tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant