| Introduction |
This programming assignment is designed to show how Python functions can define
other Python code (in this case, a class): we call exec on a string
that represents the description of a Python class, which causes Python to
define the class just as if it were written in a file and imported (in which
case Python reads the file as a big string and does the same thing).
Your code will rely heavily on string formatting operations: I suggest using
the str.format method to do the replacements, and now is a good time
to learn it if you don't know it; but you are free to use whatever string
processing tools you want.
I suggest that you look at the code below and write/test as much of the Point class as you can, directly in Eclipse (especially the methods __getitem__, __eq__, _replace__, Then you need to write the pnamedtuple function, which given the appropriate arguments, would construct a string containing the same information as your Point class, using the .format method to replace generic parts with the actual string needed in the pnamedtuple being defined. You should download the program3 project folder and use it to create an Eclipse project. Your function (put it in the pcollections.py module) can be tested in a driver I supply. Also examine the miniexample.py module, which performs a similar but simpler task of writing a function; but all the elements needed to write the class appear in a simplfied form here. You should work on this assignment in pairs, with someone in your lab section. Try to find someone who lives near you, with similar programming skills, and work habits/schedule: e.g., talk about whether you prefer to work mornings, nights, or weekends; what kind of commitment you will make to submit program early. If you believe that it is impossible for you to work with someone, because of some special reason(s), you should send me email stating them and asking for special permission to work alone (which I do grant, but not frequently). Only one student should submit all parts of the the assignment, but both student's names should appear in the comments at the top of each submitted .py file. It should look something like # Romeo Montague, Lab 1 # Juliet Capulet, Lab 1 # We certify that we worked cooperatively on this programming # assignment, according to the rules for pair programming Print this document and carefully read it, marking any parts that contain important detailed information that you find (for review before you turn in the files). The code you write should be as compact and elegant as possible, using appropriate Python idioms. |
| Problem #1: pnamedtuple |
Problem Summary:Write a function named pnamedtuple that is passed information about a named tuple: it returns a reference to class object from which we can construct instances of the specified named tuple. We might use this class as follows:from pcollections import pnamedtuple
Point = pnamedtuple('Point', 'x y')
p = Point(0,0)
...perform operations on p using methods defined in Point
Please note that although many of the examples in this description use the
Point class, your pnamedtuple class must work for all legal
calls to this function.
I created six templates (one big, two medium, three small), which are strings that had parts to fill in using the format method; all but the small strings are triple-quoted, multi-line strings, that look like large chunks of Python code (see miniexample.py in the download to help decode this paragraph).
Note calling the following format method on the string
Details
Of course, our pnamedtuple function should work for Point and and other legal call. The actual namedtuple class in Python is implemented differently, but this programming assignment requires you to use the implemenation above. TestingI provided a short driver script whose infinite loop at the end allows you to enter one-line Python statements commands and then see their result (via exc. You and also enter a command to run the batch_test and batch_self_check, providing each with a file that is appropriate for each.Files for batch_test (see bt.txt for an example) just contain commands that will be executed; many are calls to the print function, which show the result of the print. Files for batch_self_check (see bsc.txt for an example) contain three parts.
You may find it useful to build real test files for the batch_test or batch_self_check to perform all sorts of checks on your code automatically; I would even be happy if students who did an excellent job constructing these files can create a software marketplace and sell their test files to other students in the class. But, you cannot sell your code! You can sell only these test files. Maybe one of you can even sell your test code to me and the TAs for grading this assignment. Remember that your pnamedtuple function can print on the console, for debugging purposes, the string it is about to exec so you can look for errors there (just eyeball whether the code correct). The show_listing function (defined in the pnamedtuple function) display a string on the console, numbering its lines (useful when exec finds an error: it reports a line number that show_listing shows). I have included two programs that David Kay published in ICS-31 that use Python's namedtuple (with those names changed to pnamedtuple). |