#!/usr/local/bin/python3 import shelve class Library: def __init__(self, fn): self.fn = fn self.shelf = shelve.open(fn) def add(self, book): self.shelf[book.isbn] = book def get_by_isbn(self, isbn): return self.shelf[isbn] def get_by_title(self, title): for book in self.shelf.values(): if book.title == title: return book return None def get_by_author(self, author): for book in self.shelf.values(): for a in book.authors: if a == author: return book return None def close(self): self.shelf.close() class Book: def __init__(self, isbn, title, authors): self.isbn, self.title, self.authors = isbn, title, authors def __eq__(self, other): if type(other) is type(self): return self.__dict__ == other.__dict__ return False def __ne__(self, other): return not self.__eq__(other) class Author: def __init__(self, first_name, middle_name, last_name): self.first_name, self.middle_name, self.last_name = first_name, middle_name, last_name def __eq__(self, other): if type(other) is type(self): return self.__dict__ == other.__dict__ return False def __ne__(self, other): return not self.__eq__(other)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question