Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Online Courses / University Track / Python from Zero

Module 6 · Wrap-up

Discussion prompt, recap and references

Reflect on one question, review the module and find the named sources.

  • 25 minutes
  • Recap
  • References

Real-world case: The directory that was deleted on the wrong server

31 January 2017 · A code-hosting service and its production database

On 31 January 2017 a hosted code-sharing service was fighting a database that had become unstable. A tired engineer, working late, tried to clear the data directory of a secondary database server so it could copy a fresh set of data. The command was run on the primary server instead.

About 310 GB of data were reduced to about 4.5 GB before the command was stopped. The team then found that their backup routes did not give them what they needed. Regular backups ran only every 24 hours, and the last usable copy was about six hours old.

The service came back online after about 18 hours, but changes made between 17:20 and midnight UTC, to projects, comments, user accounts, issues and snippets, were lost. The company published an open postmortem and apologised to the users affected.

How the case shows what you learned in this module

Module ideaIn the case
One command, the wrong placeA delete run on the wrong machine removed almost everything. A prompt that shows the server name, and a check of the path, are cheap protection.
Backups must be testedHaving a backup process is not the same as having a copy you have restored from.
Count what you haveKnowing how big the data should be would have shown at once that something was badly wrong.
Open reporting helps othersThe published postmortem let other teams learn from the failure.

Workplace takeaway. Treat the original data as untouchable: write results to a new place, check what a command will affect before running it, and prove your backups by restoring from them.

Sources: GitLab, Postmortem of database outage of January 31 (10 February 2017); GitLab.com database incident (1 February 2017); The Register (1 February 2017); InfoQ (February 2017)

Discussion prompt

20 minutes. Think of a data file you use at work or study. In 150 to 200 words, say what each line holds, which checks a program should make before trusting it, what the program should write and where, and how you would know that no rows went missing.

Private: nothing you type here is sent or stored.

This course has no discussion board yet, so the prompt is a private reflection.

Recap

  • Files keep data; with open(...) opens and closes safely, and mode w erases.
  • Lines are text: strip, split and convert before calculating.
  • CSV with DictReader gives a list of dictionaries; check the header and the first and last rows.
  • Check files and rows, compare counts, write to a new file and test your backups.

Flashcards: 12 workplace review cards

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 12 · Why do programs use files?
Keepafter the endSharebetween programsReadand write
Files

A file keeps data after the program ends and lets programs share it. A sales list or a class register is read from a file, not typed again each time.

Card 2 of 12 · How do you open a file safely?
OpenwithUsef in the blockEndauto close
with open

Use with open(name) as f:. The file closes automatically at the end of the block, even if something goes wrong inside.

Card 3 of 12 · What do the modes r, w and a mean?
rreadwerasesaadds
Modes

r reads, w writes after erasing, a adds to the end. Choosing w for an existing file erases it as soon as it is opened.

Card 4 of 12 · Why must you never open your only copy with w?
Open wemptyCrashnothing leftFixnew file
Overwrite

The file is emptied at once, before anything is written. If the program then fails, the data is gone. Write to a new file instead.

Card 5 of 12 · What does strip do to a line?
Linewith newlinestripcleanNextsplit
strip

It removes the newline and spaces at the ends, so a line read as "Ann,70\n" becomes "Ann,70".

Card 6 of 12 · What does split do?
Lineone textsplitpartsListof text
split

It cuts a line at a separator into a list: "Ann,70".split(",") gives ["Ann", "70"]. A comma is the usual separator in data files.

Card 7 of 12 · Why convert with int or float?
Text"70"int70Thenadd
Convert

Everything read from a file is text, so "70" + "85" gives "7085". Convert to numbers before adding or comparing.

Card 8 of 12 · What is a CSV file?
Lineone recordCommaseparatesHeadernames
CSV

A table written as text: one record per line, fields separated by commas, and a first line naming the columns. Most spreadsheets can save to it.

Card 9 of 12 · What does DictReader do?
HeaderkeysRowdictionaryUseby name
DictReader

It reads a CSV and gives each row as a dictionary, using the header as keys. You then write row["score"] instead of counting columns.

Card 10 of 12 · How do you deal with a bad row?
Testeach rowSkipand countReportthem
Bad rows

Test it before use: the right number of parts and a field that really is a number. Skip it, count it and report it; do not hide it.

Card 11 of 12 · Why compare counts in and out?
Read10Used8Skipped2
Counts

Rows read should equal rows used plus rows skipped. If they differ, some rows have gone missing and the result cannot be trusted.

Card 12 of 12 · What makes a backup trustworthy?
Makea copyRestoreas a testThentrust it
Backup

A restore that you have actually carried out. A backup that has never been restored may be empty, old or unreadable.

Module notes

Key terms. File: data kept after a program ends. Mode: read, write or append. Parse: turn text into values. CSV: a table in text form with comma-separated fields. Header: the first line naming the columns. Record: one row as a dictionary. Backup: a separate copy of data, useful only if a restore has been tested.

Rules and formulas. rows read = rows used + rows skipped; never open your only copy with mode w; average = total / rows used.

Limits of this module. Examples use small text and CSV files in the same folder as the program. Larger files, spreadsheets and databases need further tools. Run each example with your own small file to confirm the output shown.

Next

Continue to Module 7: Errors, Testing and Debugging. You can also return to the course overview or the University Track.

References and further reading