Skip to content
Snippets Groups Projects
database.py 1.36 KiB
Newer Older
  • Learn to ignore specific revisions
  • Muhammad Ismail's avatar
    Muhammad Ismail committed
    # -----------------------------------------------------------------------------
    # Distributed Systems (TDDD25)
    # -----------------------------------------------------------------------------
    # Author: Sergiu Rafiliu (sergiu.rafiliu@liu.se)
    # Modified: 24 July 2013
    #
    # Copyright 2012 Linkoping University
    # -----------------------------------------------------------------------------
    
    Muhammad Ismail's avatar
    Muhammad Ismail committed
    #test
    
    Muhammad Ismail's avatar
    Muhammad Ismail committed
    """Implementation of a simple database class."""
    
    import random
    
    
    class Database(object):
    
        """Class containing a database implementation."""
    
        def __init__(self, db_file):
            self.db_file = db_file
            self.rand = random.Random()
            self.rand.seed()
    
            file = open(self.db_file)
            self.array = file.read().split('\n%\n')
            del self.array[len(self.array) - 1]
            #
            # Your code here.
            #
            pass
    
        def read(self):
            """Read a random location in the database."""
            if not len(self.array):
                return
            else:
                return self.array[self.rand.randint(0, (len(self.array) - 1))]
    
            #
            # Your code here.
            #
            pass
    
        def write(self, fortune):
            """Write a new fortune to the database."""
    
    Muhammad Ismail's avatar
    Muhammad Ismail committed
            self.array.append(fortune)
    
    Muhammad Ismail's avatar
    Muhammad Ismail committed
            with open(self.db_file, "a") as myfile:
                myfile.write(fortune + "\n%\n")
    
            #
            # Your code here.
            #
            pass