Lookaside List - Lookaside List
#!/usr/bin/python3
# ===================================================================
# lookaside list
# ===================================================================

import random

class LookAsideList:

    def __init__(self,depth = 1):
        self.lalst = {}
        if depth < 1:
            self.depth = 1
        else:
            self.depth = depth

    def reset(self,depth=1):
        if depth < 1:
            return False
        self.lalst = {}
        self.depth = depth
        return True

    def get(self,id):
        if id in self.lalst.keys():
            return self.lalst[id]
        return None

    def set(self,id,rec):
        if id in self.lalst.keys():
            ##print('id alread in la list')
            self.lalst[id] = rec
            return True
        klst = list(self.lalst.keys())
        if len(klst) < self.depth:
            ##print('la list is not full ' +
            ##     f'depth={self.depth} count={len(klst)}')
            self.lalst[id] = rec
            return True
        ##print('la list is full, replacing an entry')
        k = random.choice(klst)
        self.lalst.pop(k)
        self.lalst[id] = rec
        return True

    def dump(self):
        print(f'depth = {self.depth}')
        for k in self.lalst.keys():
            print(f'[{k:3}]  {self.lalst[k]}')

# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

if __name__ == '__main__':

    import user_interface as ui

    def menu():
        print('---------  ------------------------------------')
        print(' options  description')
        print('    1     set/add record to lookaside list')
        print('    2     get record')
        print('    3     reset lookaside list')
        print('    4     dump/display lookaside list')
        print('   99     exit program')

    lal = LookAsideList()

    while True:

        ui.clear_screen()
        menu()

        print()
        s = ui.get_user_input('    Enter a selection: ')
        if not s:
            break

        if s == '1':
            print()
            s1 = ui.get_user_input('    Enter rec ID: ')
            if not s1:
                print()
                print('no record ID entered')
                ui.pause
                continue
            s2 = ui.get_user_input('    Enter rec: ')
            if not s2:
                print()
                print('no record entered')
                ui.pause()
                continue
            print()
            if not lal.set(s1,s2):
               print('set record failed')
            else:
               print('set record succeeded')
            ui.pause()
            continue

        if s == '2':
            print()
            s = ui.get_user_input('    enter record ID: ')
            if not s:
                continue
            rec = lal.get(s)
            print()
            if rec is None:
                print('no records found')
            else:
                print(rec)
            ui.pause()
            continue

        if s == '3':
            print()
            lal.reset(2)
            print('lookaside list reset - depth = 2')
            ui.pause()
            continue

        if s == '4':
            print()
            lal.dump()
            ui.pause()
            continue

        if s == '99':
            break

        print()
        print(f'unknown selection ({s})')
        ui.pause()

    print()