# library.py # Steve Marschner (srm2), Lillian Lee (ljl2) """Example classes for CS1110 final review.""" class Document(object): """Instances represent scholarly documents. Instance variables: title [str]: title of the document authors [list of str]: names of authors, in the form 'Last, F.' works_cited [list of Document]: documents cited by this document. """ def __init__(self, title, authors, works_cited): """A new document with given title, author list, and bibliography.""" self.title = title self.authors = authors[:] self.works_cited = works_cited[:] def biblio_entry(self): """A generic bibliography entry for this document, with just the authors and title. """ return ", ".join(self.authors) + ' "' + self.title + '."' def bibliography(self): """The bibliography of this document, which is a concatenation of the entries generated by all the cited works. """ entries = [] for doc in self.works_cited: entries.append(doc.biblio_entry()) entries.sort() return '\n'.join(entries) def deep_biblio_set(self, depth): """The deep bibliography: a set of documents cited by this document, documents cited by those documents, and so on, going back up to levels. E.g. depth=1 is a normal bibliography.""" all_cited = set(self.works_cited) if depth > 1: for doc in self.works_cited: all_cited = all_cited.union(doc.deep_biblio_set(depth-1)) return all_cited def deep_bibliography(self, depth): """Formatted version of the deep bibliography.""" all_cited = self.deep_biblio_set(depth) entries = [] for doc in all_cited: entries.append(doc.biblio_entry()) entries.sort() return '\n'.join(entries) class Book(Document): """Instances represent books. Instance variables: publisher [str]: name of publisher pub_year [int]: year of publication """ def __init__(self, title, authors, works_cited, publisher, pub_year): """A new book with the given properties.""" Document.__init__(self, title, authors, works_cited) self.publisher = publisher self.pub_year = pub_year def biblio_entry(self): """A more specific bibliography entry for this book.""" return (", ".join(self.authors) + ' ' + self.title + '. ' + self.publisher + ", " + str(self.pub_year) + ".") class Article(Document): """Instances represent articles that appear in journals. Instance variables: journal [str]: title of the journal month [int]: month (1 = Jan, etc.) of issue in which article appears year [int]: year in which article appeared """ MONTH_NAMES = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun' 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] def __init__(self, title, authors, works_cited, journal, month, year): """A new journal article with the given properties.""" Document.__init__(self, title, authors, works_cited) self.journal = journal self.month = month self.year = year def biblio_entry(self): """A more specific bibliography entry for this article.""" return (", ".join(self.authors) + ' "' + self.title + '." ' + self.journal + ", " + self.MONTH_NAMES[self.month] + ' ' + str(self.year) + ".") class Library(object): """Instances represent library collections.""" def __init__(self, documents): """A library containing the given documents.""" self.documents = documents[:] def citing_documents(self, document): """A list of the documents that cite the given document.""" result = [] for doc in self.documents: if document in doc.works_cited: result.append(doc) return result def works_by(self, author): """A list of the documents authored by a particular person.""" result = [] for doc in self.documents: if author in doc.authors: result.append(doc) return result def doc_citation_count(self, document): """The number of documents that cite a particular document.""" return len(self.citing_documents(document)) def author_citation_count(self, author): """The total number of citations to works by a particular author.""" result = 0 for doc in self.documents: for cited_doc in doc.works_cited: if author in cited_doc.authors: result += 1 return result def author_set(self): """A set containing all the authors of works in the library.""" result = set() for doc in self.documents: for author in doc.authors: result.add(author) return result if __name__ == '__main__': a = Article("On the counting of books", ["Schmo, J.", "Doe, J."], [], "Acta Bibliotecnica", 5, 2003) b = Book("Systems of accounting for citations", ["Schmo, J.", "Friendly, O."], [a], "Addison-Wesley", 2004) c = Article("Review of shelving systems", ["Peters, K."], [a,b], "Acta Bibliotecnica", 7, 2004) d = Article("A method for scanning books", ["Able, K.", "Baker, C."], [a], "International Journal of Computer Vision", 6, 2008) e = Article("Deciphering marginal notes", ["Able, K.", "Chipper, F.", "Baker, C."], [d], "International Journal of Computer Vision", 3, 2009) f = Article("Do we need shelves?", ["Peters, K."], [c,d,e], "Acta Bibliotecnica", 7, 2009) g = Book("The rise of citation indices", ["Colleague, M."], [a,b,e], "Taylor & Francis", 2009) h = Article("Data stewardship", ["Gries, D.", "Peters, K.", "Lee, A."], [a,b,d,e,f,g], "Acta Bibliotecnica", 8, 2011) print h.bibliography() lib = Library([a,b,c,d,e,f,g,h]) print lib.doc_citation_count(a), lib.doc_citation_count(d) print 'citing e:', lib.citing_documents(e) print 'by A:', lib.works_by('Able, K.') print lib.author_set() for author in lib.author_set(): print author + ":", lib.author_citation_count(author) print f.deep_bibliography(3)