Skip to content

Lesson 4: Putting it together

In three sessions you have installed a Linux, written a program and put it on your PATH, and learned to change a file without touching the mouse.

Today, two commands that answer the same question in different ways: what is in this file, and what changed in it. Then practice with everything else.

We take pw apart and see how it works.

Follow what actually happens when you run it:

  • What makes the file allowed to run at all?
  • What decides that Python should run it, and not bash?
  • What turns pw 24 into a number your code can use?

diff compares two files and shows you only what is different.

Terminal window
cp notes.txt notes-old.txt
vim notes.txt # change one line
diff notes-old.txt notes.txt

The output is short on purpose. Two files with a thousand identical lines and one difference produce one line of output.

This is the version you will see everywhere:

Terminal window
diff -u notes-old.txt notes.txt

A line starting with - was removed, a line starting with + was added, and the lines around them are there so you can see where you are.

Challenge, two minutes. Copy a file, change two lines in vim, and read the diff. Then change a line back and watch that difference disappear.

You met less in lesson 1 as a way to read a long file. It is worth knowing properly, because a lot of tools hand their output to it without telling you.

You need something long to try it on. seq counts, and > puts the counting into a file:

Terminal window
seq 1 5000 > long.txt
less long.txt

Every line is its own number, so you always know where you landed. Press G and you should be looking at 5000.

Look at the keys:

Key Does
j k down and up one line
Ctrl + d Ctrl + u down and up half a page
Space, b forward and back a whole page
/word then n search, then next match
g G top and bottom
q quit

Some of those are vim’s keys, doing exactly what they do in vim. You already know how to use this.

Small things, all from the last three weeks.

Terminal window
history | wc -l # how many commands have you run
history | grep cd | wc -l # how many of them were cd
ls /usr/bin | less # scroll through everything your machine can do
find ~ -name '*.py' # every Python file you own

Can you run ls without leaving vim?

Put this in a file, run it, and fix it in vim until it works.

#!/usr/bin/env python3
class Numbers:
def __init__(self, values):
self.values = values
def total(self):
result = 0
for value in self.values:
result = result + value
return result
def average(self):
return self.total() / len(self.value)
def biggest(self):
best = self.values[0]
for value in self.values:
if value > best
best = value
return best
def main():
numbers = Numbers([4, 8, 15, 16, 23, 42])
print("numbers: ", numbers.values)
print("total: ", numbers.total())
print("biggest: ", numbers.bigest())
print("average: ", numbers.average())
if __name__ == "__main__":
main()

When it is right, it prints this:

numbers: [4, 8, 15, 16, 23, 42]
total: 108
biggest: 42
average: 18.0

Three things are wrong. Each one stops the program and points roughly at where to look. Fix one, run it again, and the next one appears.

Read those error messages properly rather than guessing. The line number is where Python noticed the problem, which is not always the line that is wrong.

Random characters are strong but hard to remember. Words are easy to remember and easier to guess. This mixes the two.

Your machine can have a dictionary, but it does not come with one:

Terminal window
sudo apt install wbritish
less /usr/share/dict/words

Then give pw a new trick: take a few words at random and dress them up.

pw --words 3 -> SaDd$le3lanTern7quIet

Reading the file and picking the words is the easy half. The rest is yours to decide: which letters become capitals, where the digits go, which characters get swapped for symbols.

Everything you need is in the standard library, and --words is another argparse argument next to the one you already have.

One question to keep hold of, because we come back to it: does swapping an s for a $ really make a password harder to guess, or only harder to type?