Variable Length Records

Step 1: Create a file containing variable length records

  1. Implement a function to create a file containing variable length records. This function should take the following argument: (1) a file name and (2) a string. Iterate over the string, creating one record for each letter in the string. Each record should contain:

    • a 4 byte length
    • the letter repeated length times.

    For length use the following:

    length = ord(letter)
    

    For example:

    length = ord("A")     --> 65
    

    Do not put newline characters between records.

  2. Run your script. Check the content of the file you have created.

What you will learn:

Step 2: Read and process the variable length records

  1. Implement a function that processes (prints) each the records in your variable length record file. This function should take the following parameter: a file name.
  2. Run your script.
  3. Modify your function so that it takes an additional parameter: a function that processes each record in the file.
  4. Run your script again, passing a function that coverts a record to upper case and prints it.

What you will learn:

Step 3: Implement iterators and filters

  1. Implement a generator function that iterates over each of the records in your variable length record file and produces (with yield) each record. The generator function should take the following argument: a file object open for reading.
  2. Implement a generator function that acts as a filter. This function will take two arguments: (1) an iterable and (2) a filter function/predicate. The generator function applies the filter function to each item in the iterable and produces only those items for which the filter function/predicate returns true.
  3. Implement a generator function that acts as a transformer. This function will take two arguments: (1) an iterable and a (2) transformer function. The generator function applies the transformer function to each item in the iterable before producing it.

What you will learn: