#create a basic dictionary for a publication def createPublication(type): """This function creates a dictionary that represents a generic publication. It has only one key called "type" """ publication = {'type':type} return publication #check if the given parameter is a valid book def isBook(pub): return (type(pub) is dict) and ('type' in pub) and (pub['type'] == "book") and ('title' in pub) and ('author' in pub) and ('publisher' in pub) and ('location' in pub) and ('year' in pub) #create a dictionary that represents a book def createBook(title,author,publisher,location,year): """This function takes in the various attributes for a book and returns a dictionary with keys title, author, publisher, location, year with their respective values equal to whatever was passed to this function. It also has a key called "type" that has the value "book" """ record = createPublication("book") record['title'] = title record['author'] = author record['publisher'] = publisher record['location'] = location record['year'] = year return record #create a dictionary that represents a journal article def createArticle(title,author,journalName,volume,issue,year): """This function takes in the various attributes for a journal article and returns a dictionary with keys title, author, journalName, volume, issue, year with their respective values equal to whatever was passed to this function. It also has a key called "type" that has the value "article" """ record = createPublication("article") record['title'] = title record['author'] = author record['journalName'] = journalName record['volume'] = volume record['issue'] = issue record['year'] = year return record #check if the given parameter is a valid journal article def isArticle(pub): return (type(pub) is dict) and ('type' in pub) and (pub['type'] == "article") and ('title' in pub) and ('author' in pub) and ('journalName' in pub) and ('volume' in pub) and ('issue' in pub) and ('year' in pub) #create a dictionary that represents a webpage def createWebpage(title,url,date): """This function takes in the various attributes for a web page and returns a dictionary with keys title, url, date with their respective values equal to whatever was passed to this function. It also has a key called "type" that has the value "webpage" """ record = createPublication("webpage") record['title'] = title record['url'] = url record['date'] = date return record #check if the given parameter is a valid web page def isWebpage(pub): return (type(pub) is dict) and ('type' in pub) and (pub['type'] == "webpage") and ('title' in pub) and ('url' in pub) and ('date' in pub) #create an instance that represents the famous book "Midnight's Children" rushdie = createBook("Midnight's Children","Salman Rushdie","Jonathan Cape","London",1980) #create an instance that represents a journal article by Turing turing = createArticle("Computing machinery and intelligence","A. M. Turing", "Mind", 59, 236, 1950) #create an instance that represents a web page ccis = createWebpage("CCIS at Northeastern University","https://www.ccis.northeastern.edu/","10th August 2018") #function that creates a formatted string for a publication that conforms to the APA style. This function determines what type of publication is passed, and acts accordingly def citeApa(pub): if (isBook(pub)): return "{} ({}). {}. {}: {}.".format(pub['author'],pub['year'],pub['title'],pub['location'],pub['publisher']) elif (isArticle(pub)): return "{} ({}). {}. {}, {}({}).".format(pub['author'], pub['year'], pub['title'], pub['journalName'], pub['volume'], pub['issue']) elif (isWebpage(pub)): return "{}. Retrieved {}, from {}.".format(pub['title'], pub['date'], pub['url']) raise ValueError("Not a valid publication") #function that creates a formatted string for a publication that conforms to the MLA style. This function determines what type of publication is passed, and acts accordingly def citeMla(pub): if (isBook(pub)): return "{}. {}. {}: {}, {}.".format(pub['author'], pub['title'], pub['location'], pub['publisher'], pub['year']) elif (isArticle(pub)): return "{}. \"{}.\" {} {}.{} ({}).".format(pub['author'], pub['title'], pub['journalName'],pub['volume'], pub['issue'], pub['year']) elif (isWebpage(pub)): return "\"{}.\" Web. {} <{}>.".format(pub['title'], pub['date'], pub['url']) raise ValueError("Not a valid publication") ############### tests ################### #tests for publications defined in a non-OO manner import unittest class PublicationTests(unittest.TestCase): def setUp(self): #create an instance that represents the famous book "Midnight's Children" rushdie = createBook("Midnight's Children","Salman Rushdie","Jonathan Cape","London",1980) #create an instance that represents a journal article by Turing turing = createArticle("Computing machinery and intelligence","A. M. Turing", "Mind", 59, 236, 1950) #create an instance that represents a web page ccis = createWebpage("CCIS at Northeastern University","https://www.ccis.northeastern.edu/","10th August 2018") def test_citeApa(self): self.assertEquals("Salman Rushdie (1980). Midnight's Children. London: Jonathan Cape.",citeApa(rushdie)) self.assertEquals("A. M. Turing (1950). Computing machinery and intelligence. Mind, 59(236).",citeApa(turing)) self.assertEquals("CCIS at Northeastern University. Retrieved 10th August 2018, from https://www.ccis.northeastern.edu/.",citeApa(ccis)) def test_citeMla(self): self.assertEquals("Salman Rushdie. Midnight's Children. London: Jonathan Cape, 1980.",citeMla(rushdie)) self.assertEquals("A. M. Turing. \"Computing machinery and intelligence.\" Mind 59.236 (1950).",citeMla(turing)) self.assertEquals("\"CCIS at Northeastern University.\" Web. 10th August 2018 .",citeMla(ccis)) suite = unittest.TestLoader().loadTestsFromTestCase(PublicationTests) unittest.TextTestRunner(verbosity=2).run(suite)