Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
New York University School of Continuing and Professional Studies Division of Programs in Information Technology Introduction to Python Homework, Session 9 EXERCISES Ex. 9.1 Write a function callme that prints "function called…" every time you call it. Call the function with the following code: callme() callme() callme() Expected output: function called... function called... function called... Ex. 9.2 Write a function printupper that takes one argument, a string, a prints that string uppercased. Call it with the following code: printupper('hello') printupper('my you are loud') printupper('I am not loud. You are.') Expected output: HELLO MY YOU ARE LOUD I AM NOT LOUD. YOU ARE. Ex. 9.3 Write a function addme that takes two arguments, adds them together and returns the two arguments added / concatenated. Call it thusly: x = addme(4, 5) print x y = addme('hey', 'you') print y Expected output: 9 heyyou Ex. 9.4 Write a function printlist that takes a list and loops through and prints each element of the list. Call it thusly: printlist([1, 2, 'a', 'b']) Expected output: 1 2 a b Ex. 9.5 Create a module file named yourname.py where yourname is your first name. Do not put a shebang (!#) line at the top. Create a def hello: function (that prints hello, world!) inside the yourname.py module. Now in the same directory where you saved the module, create a python script. In the script have an import yourname statement, and then call the function through the module: yourname.hello() Save the file and then run it. Expected calls and output: import yourname yourname.hello() # hello, world! Ex. 9.6 Modify the above function to include an optional argument. If name=[something], print hello, [something]! instead of hello, world! But if the name= parameter is not passed, revert to saying hello, world! So your def hello function code will be modified to accept the name=text argument (i.e., def hello(name=False)), and then test to see if text has a value -- if it is True (i.e., if name: is True). If it is True, print it after 'hello, '. If it doesn't, print 'hello, world!' Expected calls and output: import yourname yourname.hello() yourname.hello(name='Python') # hello, world! # hello, Python! Ex. 9.7 Create a function getlines(filename) that takes a filename, opens the file for that filename, copies the lines of the file (i.e., from readlines()) to a list variable, and then returns the list. In the calling code, call the function with a known filename, and assign the return value of the call to a variable. Loop through the variable (of course it is a list) and print out each line in the file. Expected calls and output: lines = getlines('testfile.txt') for line in lines: print line # prints each line in file HOMEWORK 9.1 Create a module called filelib.py that has three functions: def getlines(filename, newlines=False): takes a filename, opens it and returns the lines from the file, each stripped of any newline at the end. If newlines=True, the newlines will not be removed. Remember to close the file before returning. def gettext(filename): takes a filename, opens it and returns a string that contains the text of the file. def getfields(filename, delimiter=None): takes a filename, opens it and returns a list of lists in which each list is a line from the file, split on the delimiter (for example, the module will split on a comma if the comma is passed as delimiter). If delimiter is not passed, the function splits on whitespace (i.e., the default behavior of split()). Make sure that the final element of each line (or the line itself) is strip()ped. If a delimeter is passed that cannot be found in any one of the lines, raise a ValueError exception with a message indicating the problem. Save your functions in a file called filelib.py and save it in a folder, for example your python_scripts directory. Now create a test Python script with the following code: #!/usr/bin/env python import filelib data_file = '../python_data/student_db.txt' lines = filelib.getlines(data_file, newlines=True) print len(lines) text = filelib.gettext(data_file) print len(text) # when the below line is uncommented, your module should raise a # ValueError exception and, in the raised error, explain that the # delimeter does not appear to be in the file. See the slide on # raise (not covered in our class discussion) for details on raising an error with message. #list_of_lists = filelib.getfields(data_file, delimiter='baddelimeter') list_of_lists = filelib.getfields(data_file, delimiter=':') print list_of_lists Expected output: 8 332 [['id','address','city','state','zip'], ['jk43','23 Marfield Lane','Plainview','NY','10023'], ['ZXE99','315 W. 115th Street, Apt. 11B','New York','NY','10027'], ['jab44','23 Rivington Street', Apt. 3R','New York','NY','10002'], ['ak9','234 Main Street','Philadelphia','PA','08990'], ['ap172','19 Boxer Rd.','New York','NY','10005'], ['JB23','115 Karas Dr.','Jersey City','NJ','07127'], ['jb29','119 Xylon Dr.','Jersey City','NJ','07127']]