Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Script to program

Fedor Baart

When you start to program you often start to program from top to bottom. You solve problems as they come along and put all code in one file. This is called a script. As you progress and learn more about programming and you add more features you will notice that you’ll also start to program different. In this lecture we’ll show you how a script typicall evolves into a program. To make the transitions we will use basic programming guidelines that help us to choose what to change.

Script

This is an example of how you would program your first “a ship moving along a route”. The goal is to compute the total duration of the trip. Notice that we don’t use any external libraries, only internal python functions. We also don’t use any loops or functions or classes. The function does work just fine but has some rome for improvements. In this notebook we’ll use several general program guidelines to rewrite our program several times. The goal is to learn to know the guidelines and to apply them.

Route

Input

  • Ship velocity: 2m/s

  • Route: Rotterdam, Dordrecht, Den Bosch, Rotterdam

  • Distances: (measure)

Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 60000.0

DRY

One of the first principles is “Don’t Repeat Yourself”. We want to avoid duplication. The idea behind it is that if you program the same lines multiple times you also have to change multiple lines when things change.

By making sure we don’t have any repetitions we reduce the maintenance and make our software easier adaptable. Here we reduce the repetitions by using a for loop.

Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 60000.0
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 60000.0

Encapsulate: Function

The amount of work done in a for loop can become quite large. Up to now it’s just 6 lines, but as you reach 10-20 or certainly the end of the page you want to split up the work into logical steps. That makes your code easier to read for others. It also allows you to later test seperate parts of your program.

Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 60000.0
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 60000.0

Generators

Now for a slightly more complicated step. We can have functions that “return” one time. But we can also make functions that yield “multiple times”. Let’s start with a simple example. These functions don’t return values but return generators because they generate results.

one(): 1
generator one(): <generator object one at 0x1058250b0>
x: 1
next one(): 1
ERROR:root:Generator raised the error:
Traceback (most recent call last):
  File "/var/folders/fh/tcbrjg6n28b0lzjzh07b5t6m0000gn/T/ipykernel_2054/2021167945.py", line 6, in <module>
    next(generator_one)
StopIteration
x: 1
x: 2
x: 3
list: [1, 2, 3]
sum: 6

Lazy evaluation

Lets’ use this to generate our durations in the move functions. Then we can just sum them up, without needing the “t” variable. The principle here is to be lazy, do not evaluate things that you do not need or before you need it. Another lazy concepts is “YAGNI” You Aight Gonna Need It. Only make things that you need now, not things that you might need at some point in the future.

Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
[10000.0, 20000.0, 30000.0]
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 60000.0

Delegating to subgenerators

['do', 're', 'mi', 'do', 'do', 're', 'mi', 'do', 'doooo']
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
[10000.0, 20000.0, 30000.0]
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 60000.0

Let’s complicate

Let’s add some extra wait times in Rotterdam Dordrecht. Let’s include a lock that we also need to pass the van Brienenoord Bridge. That takes 1800 seconds. Brug

Waiting for bridge on edge ('Rotterdam', 'Dordrecht')
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
[1800, 10000.0, 20000.0, 30000.0]
Waiting for bridge on edge ('Rotterdam', 'Dordrecht')
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 61800.0

Do not reinvent the wheel

Up to know we did our own administration for waiting times. There is a library that we can use for that. The simpy library is a discrete event model that allows to keep track of times spent on activities. Use external libraries when possible. Then you don’t have to maintain so much code.

Waiting for bridge on edge ('Rotterdam', 'Dordrecht')
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 61800.0

Single responsibility

Some functions did multiple things at the same time. It is a good idea to make functions with only 1 responsibility. That allows you to replace, rewrite parts easier. Let’s put the edge generation in a seperate generator and recompute the distances.

Waiting for bridge on edge ('Rotterdam', 'Dordrecht')
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 61800.0

Namespaces

We already structured our code in functions. We can add multiple levels of names to organize our code. Those are called namespaces. If you want to structure your code in deeper levels you often use modules and classes. Sorting code in classes is called object oriented programming.

Waiting for bridge on edge ('Rotterdam', 'Dordrecht')
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 61800.0

Global variables are harmfull

When you don’t know where a variable comes from, you’re more likely to make mistakes.

Global
Overwriting config.json
Waiting for bridge on edge ('Rotterdam', 'Dordrecht')
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 61800.0

Don’t call us, we’ll call you

We can use the so called “hollywood principle” or “inversion of control” to add custom functions to the pass edge.

Waiting for bridge on edge ('Rotterdam', 'Dordrecht')
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 61800.0
Waiting for bridge on edge ('Rotterdam', 'Dordrecht') for 1800s
Going from Rotterdam to Dordrecht, distance: 20000
Going from Dordrecht to Den Bosch, distance: 40000
Going from Den Bosch to Rotterdam, distance: 60000
t: 61800.0