CS193Q - Day 3

> Nick's Python Guide - maybe open this in a new tab so you can get to its chapters as we go

String strip()

# strip example:
>>> line = '    hello there   \n'
>>> line.strip()
'hello there'

File Read Write

Python Guide: File Read/Write

We'll switch over to the guide page - look at the diagrams in there.

with open('file.txt) as f:
    for line in f:
        # line str from file

alt: file read loop

File loop, most common, uses least memory, one line at a time

for line in f:
    # process each line
    line = line.strip()  # often do this

In loop, line has '\n' at end. Use line.strip() - return string without whitespace at ends.

Expert extra: in reality there are three line endings found in the wild: '\n', '\r\n', '\r'. Python tries to paper over this difference, so lines all appear to have '\n' as they come in.

crazycat.py - Exercise / Demo

crazycat.py - look at print_file_plain()

Try it out with files, see what .strip() does, time it

See PyCharm feature to run Doctests - look at crazy_str() function and test it. See how works in print__file_crazy.

Note: no global variables - use parameters (and Python is not copying big structures).

Demo the > program output capture feature from command line.


Dict Type

d = {}  # empty dict
d['a'] = 'alpha'   # set
d['g'] = 'gamma'
d['b'] = 'beta'
d['a']   -> 'alpha'  # retrieve
'a' in d -> True     # "in" key check

alt: python dict keys a b g

>>> d = {}
>>> d['a'] = 'apple'
>>> d['g'] = 'grape'
>>> d['d'] = 'donut'
>>> 
>>> d  # literal syntax - r/w
{'a': 'apple', 'g': 'grape', 'd': 'donut'}
>>>
>>> d['a']
'apple'
>>>
>>> 'a' in d  # "in" efficient
True
>>> 'x' in d
False
>>> not 'x' in d  # NO: not this way
True
>>> 'x' not in d  # YES: "not in" form preferred
True
>>>
>>> d.keys()
dict_keys(['a', 'g', 'd'])
>>> 
>>> # .keys() not a list, but works in loop
>>> for key in d.keys():
...   print(key, d[key])
... 
a apple
g grape
d donut
>>> 
>>> # better: go through keys in sorted order
>>> for key in sorted(d.keys()):
...   print(key, d[key])
... 
a apple
d donut
g grape
>>> 
>>>
>>> d.keys()[0]   # Not a list
TypeError: 'dict_keys' object is not subscriptable
>>> 
>>> lst = list(d.keys())  # Make list of it
>>> lst[0]
'a'
>>>

Dict Count Algorithm - ip-count.py

counts = {}
for word in xxxxxxx:
    if word not in counts:
        counts[word] = 0
    counts[word] += 1

String Split

# Split example - extract parts of a line
# a common situation
>>> line = '24,37,10/2024\n'
>>> line = line.strip()
>>> parts = line.split(',')
['24', '37', '10/2024']
>>> parts[0]
'24'
>>> sum = 0
>>> sum += int(parts[0])
>>> sum
24
>>>
>>> # join() is reverse of split()
>>> ','.join(['aa', 'bb', 'cc'])
'aa,bb,cc'
>>>

Example: ip-count.py - add ip count code

Open up in PyCharm and code it up.

1. Two functions TBD read_counts() print_counts

2. See Doctest - can use local file

3. How does data get from read_counts() to print_counts() - see main() This is the right way to do it. Note: doing it without global variables is the best.


Other File Operations

Open interpreter >>> in crazycat folder. Type file commands there to start.

s = f.read()  # read whole file as a string
lines = f.readlines()  # read whole file as list of line strs