Today: program style: readability and decomposition, bits and bytes, computer hardware CPU, RAM, storage

Ethic: Privacy

Big Picture - The Truth About Software

We'll start a the highest level, seeing the truisms that guide software building. There's software in everything, so you should know the lay of the land.

The Natural Sate of Code = Broken

"Broken" is the natural state of code. It's easy to type in some code, and have it not work. We need a plan to work in this environment. Code can work so nicely, we should keep in mind that even more easily it can fail to work.

Goal #1 - Code That Computes the Correct Answer

The main thing we want from code. Other goals are secondary.

Q: Can You Judge Code By Looking at It?

Can you judge code correctness by looking at it? The surprising answer is - no. To really judge, you need to simulate what the loops and if-statements will with various inputs. In effect, you need to run the code to see what it does.

How To Judge - Run Tests

We need to run the code against a few inputs, checking the output for each case. If the code works against a few cases, suggests it is probably correct. It is not a 100% proof, which is surprisingly difficult or impossible to obtain, but tests are very good in practice.

Corollary: Code Not Run is Probably Buggy

There was a big AT&T Phone outage in 1990. The cause was code in an error-handling section - code to run in the event of a problem. The code had a bug in it, but of course it had run very little. Imagine that error handling code is sitting dormant for years, waiting for a particular situation. But when it does happen .. what are the odds that code is bug free?

Tests help us here too. We can make sure that every line has been run. There are modern "code coverage" tools that make sure that every line has been run in some test or other.

Goal #2 - Clean Code

Clean code with good style. This helps reduce bugs in the first place, and it's easier to fix and add features to code that is already clean. Stanford has always put an emphasis on writing clean code with good style.

Goal #3 - Run Fast

If the code is works correctly and looks good, we might also want to tune it to run fast or use less memory. For some bits of code, speed is crucial. However, the best strategy is generally getting the code working first before messing with it for maximum performance.


Program Design Strategy

Why is code written the way it is? Today we tell the outside, strategic story, driving what forms of code work best.

We'll look over to the Python guide chapters on style readability and decomposition.

Python Guide: PEP8 Tactics

Python Guide: Readable Code

Python Guide: Decomposition


Bits and Bytes

At the smallest scale in the computer, information is stored as bits and bytes. In this section, we'll look at how that works.

Bit

Byte

How Many Patterns With N Bits?

How many different patterns can be made with 1, 2, or 3 bits?

Number of bits Different Patterns
1 0 1
2 00 01 10 11
3 000 001 010 011
100 101 110 111
Number of bits Different Patterns
1 0 1
2 00 01 10 11
3 000 001 010 011
100 101 110 111
Number of bits Number of Patterns
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256

One Byte - 256 Patterns

"HDR" Image

Future Image Format: AVIF


We'll do this if we have time.

What is a Computer?

You have one on your person all day. You're debugging code for one. You see the output of them constantly. What is it and how does it work?

Step 1 - Why is it called Silicon Valley?

Moore's Law

Aside: Chip Factories are Expensive

Quick Tour of How Computers Work

Computer - CPU, RAM, Storage

alt: computer is made of CPU, RAM, storage

Extra: GPU

Want to talk about running a computer program...

Running A Program = Gets own area in RAM
"Process"

alt:each running program gets its own area in RAM

Operating System (OS)

RAM holds code + variables

alt:process area in RAM area stores both code and values, CPU core runs the code

CPU / Cores

crypto.py on a CPU

Process Manager

Browser Tab = Process


Python Shields us from Hardware Details - Great!

Python shields us from much detail about CPU and RAM, which is great. We're just peeking at the details here to get a little insight about what it means for a program to run, use CPU and RAM.

Hardware Demo Program

Nick's Hardware Squandering Program!

> hardware-demo.zip

Demo: computer is mostly idle to start. Idle CPU is cool. CPU starts running hard, generates heat .. fan spins! This program is an infinite loop, see the code below. It uses 100% of one core. Why is the fan running on my laptop? Use Activity Monitor (Mac), Task Manager (Windows) to see programs that are currently running, see CPU% and MEM%. Run program twice, once in each of 2 terminals - 200%

Core function of -cpu feature:

def use_cpu(n):
    """
    Infinite loop counting a variable 0, 1, 2...
    print a line every n (0 = no printing)
    """
    i = 0
    while True:
        if n != 0 and i % n == 0:
            print(i)
        i = i + 1

Try 1000 first ... yikes! Try 1 million instead. Type ctrl-c in the terminal to kill the process.

$ python3 hardware-demo.py -cpu 1000000
0
1000000
2000000
3000000
4000000
5000000
6000000
7000000
^CTraceback (most recent call last):
  File "hardware-demo.py", line 66, in 
    main()
  File "hardware-demo.py", line 56, in main
    use_cpu(n)
  File "hardware-demo.py", line 24, in use_cpu
    i = i + 1
KeyboardInterrupt

(ctrl-c to exit)

Run It Twice

Demo: Nick opens a second terminal. This needs to be done outside of PyCharm - see the Command Line chapter. Run a second copy of hardware-demo.py. Look in the process manager .. now see two programs running at once.


Let's Talk About RAM

When code reads and writes values, those values are stored in RAM. RAM is a big array of bytes, read and written by the CPU.

Say we have this code

n = 10
s = 'Hello'
lst = [1, 2, 3]
lst2 = lst

Every value in use by the program takes up space in RAM.

alt:python values each taking space in RAM

RAM

How Many Bytes does a Python Value Use?

Demo using -mem, Look in activity monitor (task manager), "mem" area, 100 = 100 MB per second. Watch our program use more and more memory of the machine. Program exits .. not in the list any more! Fancy: try killing off the process from inside the process manager window.

$ python3 hardware-demo.py -mem 100
Memory MB: 100
Memory MB: 200
Memory MB: 300
Memory MB: 400
Memory MB: 500
Memory MB: 600
Memory MB: 700
^CTraceback (most recent call last):
...
KeyboardInterrupt
(ctrl-c to exit)