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.
Warm up
Section titled “Warm up”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 24into a number your code can use?
Commands of the day
Section titled “Commands of the day”diff compares two files and shows you only what is different.
cp notes.txt notes-old.txtvim notes.txt # change one linediff notes-old.txt notes.txtThe 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:
diff -u notes-old.txt notes.txtA 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:
seq 1 5000 > long.txtless long.txtEvery 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.
Practice
Section titled “Practice”Small things, all from the last three weeks.
history | wc -l # how many commands have you runhistory | grep cd | wc -l # how many of them were cdls /usr/bin | less # scroll through everything your machine can dofind ~ -name '*.py' # every Python file you ownCan you run ls without leaving vim?
Task: fix what is broken
Section titled “Task: fix what is broken”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: 108biggest: 42average: 18.0Three 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.
Bonus: teach pw some words
Section titled “Bonus: teach pw some words”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:
sudo apt install wbritishless /usr/share/dict/wordsThen give pw a new trick: take a few words at random and dress them up.
pw --words 3 -> SaDd$le3lanTern7quIetReading 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?