Newer
Older
# -----------------------------------------------------------------------------
# Distributed Systems (TDDD25)
# -----------------------------------------------------------------------------
# Author: Sergiu Rafiliu (sergiu.rafiliu@liu.se)
# Modified: 24 July 2013
#
# Copyright 2012 Linkoping University
# -----------------------------------------------------------------------------
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""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."""
with open(self.db_file, "a") as myfile:
myfile.write(fortune + "\n%\n")
#
# Your code here.
#
pass