Exemplo n.º 1
0
    def test_use_cache_of_0(self):
        """
            Test using cache
        """

        dlist = DiskList(cache_size=0)

        l1 = []
        l2 = []
        l3 = []
        l4 = []

        for i in range(100):
            dlist.append(i)
            l1.append(i)

        for i in dlist:
            l2.append(i)

        for i in dlist:
            l3.append(i)

        l4 = dlist[0:50]
        for i in dlist[50:]:
            l4.append(i)

        self.assertTrue(
            all([
                i == j and j == k and k == l
                for i, j, k, l in zip(l1, l2, l3, l4)
            ]))
Exemplo n.º 2
0
    def test_extend(self):
        """
            Test the extend method
        """

        dlist = DiskList()
        dlist.extend(['0', '1', '2', '3'])

        self.assertTrue(len(dlist) == 4)
Exemplo n.º 3
0
    def test_clear_empty(self):
        """
            Test clear method when list is empty
        """

        dlist = DiskList()

        dlist.clear()

        self.assertTrue(len(dlist) == 0)
Exemplo n.º 4
0
class StoryTeller:
    """Forms short stories with given files, and returns a string with it."""
    def __init__(self):
        self.people = DiskList("people.txt")
        self.actions = DiskList("actions.txt")
        self.places = DiskList("places.txt")

    def random_fable(self):
        """Forms a random short story with the fable formula, returning a String with it."""
        #whata... x.x
        pair = self.people.random_pair()
        return "You are going to " +self.actions.random_item()+ " in order to kill " + pair[0] + " and then finally save your beloved " +pair[1]+ " and live forever happy together..."

    def random_love_tragedy(self):
        """Forms a random short story with the Romeo and Juliet formula, returning a String with it."""
        pair = self.people.random_pair()
        act = self.actions.random_item()
        plc = self.places.random_item()
        return pair[0]+ " and " +pair[1]+ " loved each other, but their famylies hated each other, beacuse of that " +pair[0]+ ", "+act+" "+plc+", discovering this, "+pair[1]+" kills self, but actually "+pair[0]+",do NOT done that thing i said, and finds "+pair[1]+" dead, seeing that "+pair[0]+" kills self too."

    def random_future(self):
        """Forms a very short story telling the future."""
        who = self.people.random_item()
        act = self.actions.random_item()
        plc = self.places.random_item()
        return who +" will "+ act +" "+plc
Exemplo n.º 5
0
    def test_remove(self):
        """
            Test the remove method
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('2')
        dlist.append('3')
        dlist.append('0')

        dlist.remove('0')

        self.assertTrue(dlist[0] == '1' and dlist[1] == '2' and dlist[2] == '3'
                        and dlist[3] == '0')
Exemplo n.º 6
0
    def test_len(self):
        """
            Test the len() function
        """

        dlist = DiskList()

        self.assertTrue(len(dlist) == 0)
Exemplo n.º 7
0
    def test_count(self):
        """
            Test count method
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('3')
        dlist.append('3')

        self.assertTrue(
            dlist.count('1') == 1 and dlist.count('3') == 2
            and dlist.count('1337') == 0)
Exemplo n.º 8
0
    def test_clear_not_empty(self):
        """
            Test clear method
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('2')
        dlist.append('3')

        dlist.clear()

        self.assertTrue(len(dlist) == 0)
Exemplo n.º 9
0
    def test_insert(self):
        """
            Test inserting new items
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('2')
        dlist.append('3')

        dlist.insert(1, '10')

        self.assertTrue(len(dlist) == 5 and dlist[1] == '10')
Exemplo n.º 10
0
    def test_add(self):
        """
            Test the + operator
        """

        dlist1 = DiskList()
        dlist2 = DiskList()
        dlist1.append('1')
        dlist1.append('2')
        dlist2.append('3')
        dlist2.append('4')

        dlist = dlist1 + dlist2

        self.assertTrue(len(dlist) == 4)
Exemplo n.º 11
0
    def test_index(self):
        """
            Test the index method
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('2')
        dlist.append('3')

        self.assertTrue(dlist.index('2') == 2)
Exemplo n.º 12
0
    def test_index_with_bounds(self):
        """
            Test the index method with bounds
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('2')
        dlist.append('3')

        self.assertRaises(ValueError, lambda: dlist.index('2', 0, 1))
Exemplo n.º 13
0
    def test_pop_without_index(self):
        """
            Test the pop method without index
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('2')
        dlist.append('3')

        result = dlist.pop()

        self.assertTrue(dlist[0] == '0' and dlist[1] == '1' and dlist[2] == '2'
                        and result == '3' and len(dlist) == 3)
Exemplo n.º 14
0
    def test_append(self):
        """
            Test appending new items
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('2')
        dlist.append('3')

        self.assertTrue(len(dlist) == 4)
Exemplo n.º 15
0
    def test_clear(self):
        """
            Test the clear method
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')

        dlist.clear()

        self.assertTrue(len(dlist) == 0)
Exemplo n.º 16
0
    def test_getitem(self):
        """
            Test the [] operator for getting items
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('2')
        dlist.append('3')

        self.assertTrue(dlist[0] == '0' and dlist[1] == '1' and dlist[2] == '2'
                        and dlist[-1] == '3')
Exemplo n.º 17
0
    def test_iteration(self):
        """
            Test iterating through the DiskList
        """

        dlist = DiskList()
        dlist.append('0')
        dlist.append('1')
        dlist.append('2')
        dlist.append('3')

        result_list = []

        for item in dlist:
            result_list.append(item)

        self.assertTrue(result_list[0] == '0' and result_list[1] == '1'
                        and result_list[2] == '2' and result_list[3] == '3')
Exemplo n.º 18
0
 def __init__(self):
     self.people = DiskList("people.txt")
     self.actions = DiskList("actions.txt")
     self.places = DiskList("places.txt")
Exemplo n.º 19
0
def main():
    """
        Compare disklists and lists
    """

    # Instantiation
    print('|---------- Instanciation ----------|')
    # List
    start = timer()
    for _ in range(0, 1000):
        l = []
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for _ in range(0, 1000):
        dl = DiskList()
    end = timer()
    print('DiskList: {:.10f} sec'.format((end - start) / 1000))

    # Instantiation with big object
    print('|---------- Instanciation with big object ----------|')
    # List
    start = timer()
    for _ in range(0, 1000):
        lbo = []
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for _ in range(0, 1000):
        dlbo = DiskList()
    end = timer()
    print('DiskList: {:.10f} sec'.format((end - start) / 1000))

    # Append
    print('|---------- Appending ----------|')
    # List
    start = timer()
    for i in range(0, 1000):
        l.append(i)
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for i in range(0, 1000):
        dl.append(i)
    end = timer()
    print('DiskList: {:.10f} sec'.format((end - start) / 1000))

    # Append big object
    print('|---------- Appending big object ----------|')
    # List
    big_obj = ["test" for _ in range(0, 1000)]
    start = timer()
    for i in range(0, 1000):
        lbo.append(big_obj)
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for i in range(0, 1000):
        dlbo.append(big_obj)
    end = timer()
    print('DiskList: {:.10f} sec'.format((end - start) / 1000))

    # Inserting
    print('|---------- Inserting ----------|')
    # List
    start = timer()
    for i in range(0, 1000):
        l.insert(i, i)
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for i in range(0, 1000):
        dl.insert(i, i)
    end = timer()
    print('DiskList: {:.10f} sec'.format((end - start) / 1000))

    # Inserting big object
    print('|---------- Inserting big object ----------|')
    # List
    big_obj = ["test" for _ in range(0, 1000)]
    start = timer()
    for i in range(0, 1000):
        lbo.insert(i, big_obj)
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for i in range(0, 1000):
        dlbo.insert(i, big_obj)
    end = timer()
    print('DiskList: {:.10f} sec'.format((end - start) / 1000))

    # Getting with index
    print('|------ Getting with index ------|')
    # List
    start = timer()
    for i in range(0, 1000):
        l[i]
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for _ in range(0, 1000):
        dl[i]
    end = timer()
    print('DiskList: {:.10f} sec'.format((end - start) / 1000))

    # Setting with index
    print('|------ Setting with index ------|')
    # List
    start = timer()
    for i in range(0, 1000):
        l[i] = i
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for _ in range(0, 1000):
        dl[i] = i
    end = timer()
    print('DiskList: {:.10f} sec'.format((end - start) / 1000))

    # Iterating
    print('|---------- Iterating ----------|')
    # List
    start = timer()
    for i in range(0, 1000):
        for item in l:
            continue
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for _ in range(0, 1000):
        for item in dl:
            continue
    end = timer()
    print('DiskList without cache: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    dl = DiskList(cache_size=256)  # 256 items
    for i in range(0, 1000):
        dl.append(i)
    start = timer()
    for _ in range(0, 1000):
        for item in dl:
            continue
    end = timer()
    print('DiskList with cache: {:.10f} sec'.format((end - start) / 1000))

    # Iterating with big object
    print('|---------- Iterating with big object ----------|')
    # List
    start = timer()
    for i in range(0, 1000):
        for item in lbo:
            continue
    end = timer()
    print('List: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    start = timer()
    for _ in range(0, 1000):
        for item in dlbo:
            continue
    end = timer()
    print('DiskList without cache: {:.10f} sec'.format((end - start) / 1000))
    # DiskList
    dlbo = DiskList(cache_size=256)  # 256 items
    big_obj = ["test" for _ in range(0, 1000)]
    for i in range(0, 1000):
        dlbo.append(big_obj)
    start = timer()
    for _ in range(0, 1000):
        for item in dlbo:
            continue
    end = timer()
    print('DiskList with cache: {:.10f} sec'.format((end - start) / 1000))