""" A movie database solution using Durus, a Python object database, for persistence. Available for both Python 2.x and Python 3.x """ from durus.persistent import Persistent from durus.persistent_dict import PersistentDict from durus.persistent_set import PersistentSet # Our "Models" -- it's just Python. class Actor(Persistent): def __init__(self, name): self.name = name def __str__(self): return '' % self.name class Movie(Persistent): def __init__(self, title): self.title = title self.actors = PersistentSet() def add_actor(self, actor): self.actors.add(actor) def __str__(self): return '' % self.title class Database(Persistent): def __init__(self): self.movies = PersistentDict() self.actors = PersistentDict() def add_movie(self, movie): assert movie.title not in self.movies, 'Already exists!' self.movies[movie.title] = movie return movie def get_movie(self, title): return self.movies.get(title, None) def add_actor(self, actor): assert actor.name not in self.actors, 'Already exists!' self.actors[actor.name] = actor def get_actor(self, name): return self.actors.get(name, None) def appears_in(self, actor): """(actor : Actor) -> [Movie,] Returns a sequence of Movies, if any, the Actor played a role in. """ return [m for m in self.movies.values() if actor in m.actors] def get_db(connection): root = connection.get_root() if 'films' not in root: root['films'] = Database() connection.commit() return root.get('films') def load_it(connection): # load some sample data db = get_db(connection) for name in ['Mel Gibson', 'Anthony Hopkins', 'Danny Glover']: if not db.get_actor(name): db.add_actor(Actor(name)) mel = db.get_actor('Mel Gibson') danny = db.get_actor('Danny Glover') for movie in ['Mutiny on the Bounty', 'Braveheart', 'Lethal Weapon', 'Lethal Weapon II', 'Lethal Weapon III']: if not db.get_movie(movie): flick = db.add_movie(Movie(movie)) # mel is prolific it seems flick.add_actor(mel) try: mutiny = db.get_movie('Mutiny on the Bounty') mutiny.add_actor(db.get_actor('Anthony Hopkins')) # lets add co-stars for Lethal series for movie in db.movies.values(): if 'Lethal Weapon' in movie.title: movie.add_actor(danny) except AssertionError: pass connection.commit() if __name__ == '__main__': from examples.durus.flicks.movies import load_it, get_db from durus.file_storage import FileStorage from durus.connection import Connection # Durus also offers a remote client-server storage suitable for # multiple processes to access simultaneously. Keeping it simple here with # a FileStorage" connection = Connection(FileStorage("movies.durus")) # comment this following line out after running the script once, just # to prove persistence oh doubting Thomas load_it(connection) db = get_db(connection) hopkins = db.get_actor('Anthony Hopkins') mel = db.get_actor('Mel Gibson') for actor in (hopkins, mel): print() print('%s is in:' % actor.name) print([str(m) for m in db.appears_in(actor)]) # what movies? print("\nComplete Movie Listing") for m in db.movies.values(): print(m.title, ':', ','.join([str(a) for a in m.actors]))