uwhpsc-2016 / homework1

Homework #1
1 stars 1 forks source link

How to get Jupiter Notebook file to be part of homework? #16

Open werrah opened 8 years ago

werrah commented 8 years ago

I have done all my work for exercise 1 in a Jupiter notebook file. How do I now incorporate that into my homework repository? I'm assuming a user is supposed to be able to run the functions collatz_step() and collatz() from the bash command line using the exercise1.py file, not my Jupiter notebook, correct?

I tried to cut and paste my code into the exercise1.py file, but I don't know how to then execute the functions for values of n.

I'm assuming I'm just missing a fairly easy step, so any direction would be appreciated.

quantheory commented 8 years ago

The tested functions (collatz_step, collatz, and so on) should be in the exercise1.py file. Then in your Jupyter notebook, you can import these functions and use them to generate plots for the report. I would put the Jupyter notebook at the top level of your repo for this purpose.

werrah commented 8 years ago

OK, so I have the function collatz_step in my exercise1.py file. I think I'm just missing what the bash command is to execute the function and I feel like I'm just missing something basic, but I can't find it in the references. I know to run a python script in bash, I can enter the command like we did in class:

$ python ./hello.py

And that prints "Hello world." (for example)

However, if I wanted to print the results for a function within a python file in the terminal, how do I do that? Basically, I know my function works in a jupiter notebook, but in the terminal, what command would I enter to make sure that, say collatz_step(20) returns the correct value.

I feel like it's a pretty basic question, but my programming experience is not that great.

cswiercz commented 8 years ago

There are a number of ways to run your code from the command line in addition to importing your code from a Jupyter notebook.

1. Write some "script-level" code in the module exercise1.py.

Here's a nice feature of Python. Suppose you have some code that looks like this:

# exercise1.py

def collatz_step(n):
    # do some stuff
    result =  # stuff
    return result

if __name__ == '__main__':
    print 'Running script...'
    x = foo(2)
    print x

What happens when you run $ python exercise1.py is that is will execute the code following the if __name__ == '__main__' block. This is a decent way to run some "quick tests" on the functions defined in the module.

2. Write tests in the supplied test script test_homework1.py

The test script is already written to import the contents of the submodule homework1.exercise1. In particular, it will import the functions collatz_step() and collatz() that you defined in homework1/exercise1.py. In fact, at the top of test_homework.py you can see the following statements:

# ...
from homework1.exercise1 import collatz_step, collatz
# ...

Once these are imported into the test script you can use them within the script to write tests. Follow the syntax of the tests already provided for you to write your own. Then, to execute the test script just run the following from the terminal

$ python test_homework1.py

and the test suite will run, reporting errors in present.

3. Import the homework1 module into a Jupyter notebook.

As @quantheory suggested, if you create a Jupyter notebook in the same directory as the homework1 module. (The module containing the __init__.py file.) Then you can import the contents of the homework1 module into the currently running notebook. That is, you could have a cell with the command

import homework1
import homework1.exercise1
homework1.exercise1.collatz_step(6)

# ...

That way, you can play around with your code in the notebook. However, note that if you make any changes to the module homework1 you will have to "restart the notebook kernel" before re-importing the module.

This method is okay for playing around with your code. However, option 2 above is, in my opinion a cleaner, more permanent way, and more efficient way to test the validity of your code. Plus, the test suite approach is very common in the working world so it's good to learn how it's done. Also, the test suite approach is more "fixed" in the sense that notebooks tend to be a more temporary experiment bed to try out new ideas before solidifying them in code.

These are all reasons why we're using the test suite approach in this class.

quantheory commented 8 years ago

You can do a couple of things.

  1. If you run the test_homework1.py in the top level of your repo, that already has tests written using the standard library unittest module (and you can add more). This would be the preferred way to write test scripts. Note that it imports functions from homework1.exercise1.
  2. If you want to just try some values by hand to see what you get, you can start an interactive python session (ipython) in the top level of your repo, and you can type in the commands to import a function from homework1.exercise1 by hand, then type in collatz_step(20) and see what you get.
quantheory commented 8 years ago

Oops! Commented at the same time. @cswiercz's answer looks more detailed. :smile:

werrah commented 8 years ago

Both those answers help a lot. I will mess around with it later today. Thank you so much!

cswiercz commented 8 years ago

I think it's worth spending some time during Tuesday's lecture to talk about good workflow.

cswiercz commented 8 years ago

Also, just to be clear, we won't be using any code written in notebook files for testing the validity of your code. Using notebooks is only for you to help debug your code. In the end all we want is the functions defined in the homework1 Python package.

gadamico commented 8 years ago

This was all very helpful for me, too!