CSC 221 Weekly Assignments: Week 10

CSC 221


Overview

This week Chris will guide you through the completion of our Base64 encoder / decoder program.

Thursday, November 5th

Classwork / Homework

Today we put it all together! We already have the middle of our program: bits_to_base64 and base64_to_bits. What's left is connecting them to real files:

encode:  file --> bit string --> Base64 string --> text file
decode:  text file --> Base64 string --> bit string --> file

Step 0: Download the starter code

Download these three files into your project directory:

Or from the terminal:

$ wget https://ict.gctaa.net/f26/courses/csc221/goals/resources/base64_starter/b64tools.py
$ wget https://ict.gctaa.net/f26/courses/csc221/goals/resources/base64_starter/base64encode
$ wget https://ict.gctaa.net/f26/courses/csc221/goals/resources/GHClogoSmall.png
$ chmod +x base64encode

Open b64tools.py and look around. Two functions, file_to_bits and bits_to_file, are already done. Chris will walk through them at the start of class. file_to_bits uses Python's bitwise operators & (and) and << (left shift) to check each of the 8 bits in a byte, one at a time. bits_to_file works just like your bits_to_base64, except it uses groups of 8 bits (one byte) instead of 6.

Step 1: Copy in your code

Copy the bodies of your bits_to_base64 and base64_to_bits functions into the spots marked TODO. Then run:

$ python3 b64tools.py

If it prints nothing, every test passed. Don't go on until it does.

Step 2: Write encode_file

Follow the numbered comments in encode_file. Each step is one line that calls a function already in the file. The only new thing is step 4, writing a string to a text file:

    with open(outname, 'w') as outfile:
        outfile.write(b64str)

Then try it. base64encode is already finished, and it calls your encode_file:

$ ./base64encode GHClogoSmall.png
Wrote b64.txt
$ cat b64.txt

The output should start with iVBORw0KGgo.

Step 3: Write decode_file and base64decode

Follow the numbered comments in decode_file. For step 1, reading a text file looks like this. strip() removes any newline at the end, which would confuse base64_to_bits.

    with open(filename) as infile:
        b64str = infile.read().strip()

Next, copy base64encode to make base64decode:

$ cp base64encode base64decode

Open base64decode and change it to call decode_file instead of encode_file. Then test it:

$ ./base64decode b64.txt
Wrote file.bin
$ cmp file.bin GHClogoSmall.png

cmp compares two files byte by byte. If it prints nothing, the files are identical. Your Base64 encoder and decoder work! Commit and push b64tools.py, base64encode, and base64decode now.

Step 4: Real file names

Right now encoded_filename and decoded_filename always return b64.txt and file.bin. Our customer wants GHClogoSmall.png to encode to GHClogoSmall_b64_png.txt, and that file to decode back to GHClogoSmall.png. Add these doctests to the two functions, then replace the placeholder return lines with code that makes them pass:

def encoded_filename(filename):
    """
      >>> encoded_filename('GHClogoSmall.png')
      'GHClogoSmall_b64_png.txt'
      >>> encoded_filename('notes.pdf')
      'notes_b64_pdf.txt'
    """


def decoded_filename(filename):
    """
      >>> decoded_filename('GHClogoSmall_b64_png.txt')
      'GHClogoSmall.png'
      >>> decoded_filename('notes_b64_pdf.txt')
      'notes.pdf'
    """

You already know everything you need from chapter 3: split, slicing, and +. To test with real names, move the original out of the way first, so you know the decoder really made a new copy:

$ ./base64encode GHClogoSmall.png
Wrote GHClogoSmall_b64_png.txt
$ mv GHClogoSmall.png original.png
$ ./base64decode GHClogoSmall_b64_png.txt
Wrote GHClogoSmall.png
$ cmp GHClogoSmall.png original.png

Commit and push again.

This task will be evaluated using the following rubric:

  • A: You finish Step 4. Real file names work and cmp prints nothing.
  • B: You finish Step 3. base64encode and base64decode are committed and copy the logo exactly to file.bin.
  • C: You work diligently all period long and are actively engaged in trying to understand the problem.
  • D: You're there in body, but not in spirit.
  • E: No show.

Monday, November 2nd

Classwork / Homework

We'll start with some quick review just to make sure that we're all along for the ride with this assignment. I know Jeff has spent time talking through this with you, so hopefully this will be nice review!

Here is a solution to the problem posed during last class. Chris will walk through it with you at the start of class. Pay close attention, since today's task is to run it in reverse.

Our bits_to_base64 function gets us from bits to Base64, which is what the encoder needs. The decoder needs to go the other way. Add the following function header and doctests to our program, then add a function body which makes the doctests pass.

def base64_to_bits(b64str):
    """
      >>> base64_to_bits('R0hn')
      '010001110100100001100111'
      >>> base64_to_bits('VGVz')
      '010101000110010101110011'
      >>> base64_to_bits('TQ==')
      '01001101'
      >>> base64_to_bits('TWE=')
      '0100110101100001'
      >>> base64_to_bits('nWlTXneO1zD')
      '100111010110100101010011010111100111011110001110110101110011000011'
      >>> s2 = '10110101100010110011010101010110111100011101101011100101'
      >>> base64_to_bits('tYs1VvHa5Q==') == s2
      True
    """

This task will be evaluated using the following rubric:

  • A: You finish a function body that makes all tests pass.
  • B: You finish a function body that makes the first two tests pass.
  • C: You work diligently all period long and are actively engaged in trying to understand the problem.
  • D: You're there in body, but not in spirit.
  • E: No show.