Now Code

Introduction to Now Code

Overview

  • Teaching: 10 min
  • Exercises: 0 min

Questions

  • How can I put everything I have learnt so far into practice?

Objectives

  • Undertake a set of exercises to put into practice what you have learnt in previous weeks.
  • Undertstand how writing pseudocode or flowcharts can help to plan your programming.
  • Practice pair programming where developers work in tandem, one pilot, one navigator.

The aim of this week's lesson (and the later Now Code 2) is to give you an opportunity to practice your programming and good working practice. You will attempt a number of exercises which will involve working in pairs to design, implement and test your solutions.

Python virtual environments

If you choose not to use notebooks.azure.com you may need to setup your computer to override the default behaviour of Python. Fortunately Python provides the means to manage this issue through the creation of environments using the [mini]conda package manager. This also allows us to install the libraries we need for our different programs and keep them separate in case the cause conflits or use different versions of libraries.

We can create a new environment with the following:

% conda create -n python3 python=3.7

Answer y at the prompt to create the environment.

The prompt advises us to activate our new environment with:

% conda activate python3

On 'linux.bath' when returning to the environment you may have to use the commandsource activate python3. Recall that the problem last week was that we did not have the correct version of pytest available, so we will install this to our new environment, we also want to use ipython3 by default. Note that in order to this you have to activate the environment as above. Your prompt will now be prefixed with the name of the environment:

(python3) % conda install pytest
...
(python3) % conda install ipython
...

Again you will need to answer y to install when prompted.

Where necessary the exercises comprising now-code should advise you if you need to install specific additional libraries or packages . When they do, or if packages are not present you should be able to install them to your environment using conda install <library>. If not you can try pip install <library> You can create and remove conda environments so feel free to investigate further after the workshop.

Pseudocode

So far the example we have undertaken have been relatively short due to time constraints. This means that we have generally not spent too long planning our code, however for anything but the simplest programs this can be a mistake. Writing pseduocode or flowcharts can help us to understand what our program needs to do. When we have more significant programming challenges it can also help us to break down the code into functions and architect the different components that we will need to include.

As an example let's consider the mean function that we have considered in previous weeks. This functionality can be summarised as follows:

Function: mean(list)

Takes list passed from calling function

If list is empty, return warning message: it is not possible to take the mean of an empty list
If list contains non-numerical values, return warning message: it is only possible to average numerical values
Otherwise: 
    sum list
    count items in list
    return mean: sum/count

Now because of the natural language of Python we can readily turn this into executable code:

def mean(sample):
    '''
    Takes a list of numbers, sample

    and returns the mean.
    '''

    assert len(sample) != 0, "Unable to take the mean of an empty list"
    for value in sample:
        assert isinstance(value,int) or isinstance(value,float), "Value in list is not a number."
    sample_mean = sum(sample) / len(sample)
    return sample_mean

Before starting any of the exercises try to contruct pseudocode for your solution.

Pair programming

In professional development practices such as code review and pair programming are used to ensure quality, develop understanding and provide mentoring. During these exercises you are encouraged to work with a neighbour. Develop your pseudocode together to plan how you are going to tackle the problem. When you start coding one of your will drive or pilot, explaining what you are doing when appropriate, and the other will navigate or guide, as necessary. Both should understand what and why you are doing what you are doing at all times.

Swap frequently, and at least for each part/subexercise, but when you are pilot only ever work on your own copy of the code. In order to do this, one of you will have to create your own remote copy of the repository, e.g. on github and make your partner a collaborator.

When you clone the repository for each exercise the original remote will be set as origin. To check this and change the remote follow these commands:

% git remote -v
origin  https://github.com/arc-bath/<exercise>.git (fetch)
origin  https://github.com/arc-bath/<exercise>.git (push)
% git remote remove origin

create a new empty remote repository on you github account called rainfall. Then add it to your local repository and push it to the remote, by following the instructions on github or with the following:

% git add remote origin https://github.com/<your_username>/<exercise>.git
% git push -u origin master

Key Points

  • Undertake a series of programming challenges to combine what you have learnt in previous weeks.
  • Writing pseudocode or flowcharts can help you plan the logic you will need to implement in your programs.
  • Pair-programming can help to mentor and develop our understanding as programmers.