|
| 1 | +# The problem requires implementing an in-memory key-value store |
| 2 | +# where you can set a key-value pair and |
| 3 | +# retrieve the value of a key. |
| 4 | + |
| 5 | +# Example: |
| 6 | +# db.set(101, ["Sanjeev", "ProdSec"]) |
| 7 | +# db.set(102, ["Deep", "DevSecOps"]) |
| 8 | +# db.get(102) |
| 9 | +# Think of command line for n inputs, set for n inputs, then based on command implement get, delete |
| 10 | +# Example: |
| 11 | +# Choose below options for operations: |
| 12 | +# 1. set/create entries |
| 13 | +# How many entries: 2 |
| 14 | +# emp_id: input |
| 15 | +# emp_details: input([]) |
| 16 | +# 2. get emp_id details |
| 17 | +# 3. delete emp_id details |
| 18 | +# 4. update emp_id details |
| 19 | +# use loop for interactive session |
| 20 | +# usr argparse for cmd options |
| 21 | +# use json to store the data for permanent use |
| 22 | +# handle as much error as you can, also some edge cases |
| 23 | +import argparse |
| 24 | +import json |
| 25 | +import os |
| 26 | + |
| 27 | +class DB: |
| 28 | + def __init__(self, filename='db.json'): |
| 29 | + self.filename = filename |
| 30 | + self.dic = self.load() |
| 31 | + |
| 32 | + def load(self): |
| 33 | + if os.path.exists(self.filename): |
| 34 | + with open(self.filename, 'r') as f: |
| 35 | + return json.load(f) |
| 36 | + return {} |
| 37 | + |
| 38 | + def save(self): |
| 39 | + with open(self.filename, 'w') as f: |
| 40 | + json.dump(self.dic, f, indent=4) |
| 41 | + |
| 42 | + def set(self, key: int, value: list) -> None: |
| 43 | + if not isinstance(key, int): |
| 44 | + raise TypeError("Key must be an integer") |
| 45 | + if not isinstance(value, list): |
| 46 | + raise TypeError("Value must be a list") |
| 47 | + if key in self.dic: |
| 48 | + raise ValueError(f"Key {key} already exists") |
| 49 | + self.dic[key] = value |
| 50 | + self.save() |
| 51 | + |
| 52 | + def get(self, key: int) -> list: |
| 53 | + if not isinstance(key, int): |
| 54 | + raise TypeError("Key must be an integer") |
| 55 | + if key in self.dic: |
| 56 | + return self.dic[key] |
| 57 | + else: |
| 58 | + print(f"No record found for emp id: {key}") |
| 59 | + return None |
| 60 | + |
| 61 | + def delete(self, key: int) -> None: |
| 62 | + if not isinstance(key, int): |
| 63 | + raise TypeError("Key must be an integer") |
| 64 | + if key in self.dic: |
| 65 | + print(f"Deleting emp id {key} details: {self.dic[key]}") |
| 66 | + del self.dic[key] |
| 67 | + print(f"Emp id {key} deleted successfully.") |
| 68 | + self.save() |
| 69 | + else: |
| 70 | + print(f"No record found for emp id: {key}") |
| 71 | + print(f"Current employee list: {self.dic}") |
| 72 | + |
| 73 | +def main(): |
| 74 | + db = DB() |
| 75 | + |
| 76 | + parser = argparse.ArgumentParser(description="Database operations") |
| 77 | + subparsers = parser.add_subparsers(dest="command") |
| 78 | + |
| 79 | + # Set command |
| 80 | + set_parser = subparsers.add_parser("set", help="Set a new entry") |
| 81 | + set_parser.add_argument("key", type=int, help="Key (employee ID)") |
| 82 | + set_parser.add_argument("value", nargs='+', help="Value (employee details)") |
| 83 | + |
| 84 | + # Get command |
| 85 | + get_parser = subparsers.add_parser("get", help="Get an entry") |
| 86 | + get_parser.add_argument("key", type=int, help="Key (employee ID)") |
| 87 | + |
| 88 | + # Delete command |
| 89 | + delete_parser = subparsers.add_parser("delete", help="Delete an entry") |
| 90 | + delete_parser.add_argument("key", type=int, help="Key (employee ID)") |
| 91 | + |
| 92 | + args = parser.parse_args() |
| 93 | + |
| 94 | + if args.command == "set": |
| 95 | + try: |
| 96 | + db.set(args.key, args.value) |
| 97 | + print(f"Set key {args.key} with value {args.value}") |
| 98 | + except (TypeError, ValueError) as e: |
| 99 | + print(e) |
| 100 | + |
| 101 | + elif args.command == "get": |
| 102 | + try: |
| 103 | + details = db.get(args.key) |
| 104 | + if details: |
| 105 | + print(f"Emp id {args.key} details: {details}") |
| 106 | + except TypeError as e: |
| 107 | + print(e) |
| 108 | + |
| 109 | + elif args.command == "delete": |
| 110 | + try: |
| 111 | + db.delete(args.key) |
| 112 | + except TypeError as e: |
| 113 | + print(e) |
| 114 | + |
| 115 | + else: |
| 116 | + while True: |
| 117 | + print("\nChoose an operation:") |
| 118 | + print("1. Set a new entry") |
| 119 | + print("2. Get an entry") |
| 120 | + print("3. Delete an entry") |
| 121 | + print("4. Exit") |
| 122 | + |
| 123 | + choice = input("Enter your choice (1/2/3/4): ") |
| 124 | + |
| 125 | + if choice == '1': |
| 126 | + try: |
| 127 | + key = int(input("Enter key (employee ID): ")) |
| 128 | + value = input("Enter value (employee details separated by spaces): ").split() |
| 129 | + db.set(key, value) |
| 130 | + print(f"Set key {key} with value {value}") |
| 131 | + except (TypeError, ValueError) as e: |
| 132 | + print(e) |
| 133 | + |
| 134 | + elif choice == '2': |
| 135 | + try: |
| 136 | + key = int(input("Enter key (employee ID): ")) |
| 137 | + details = db.get(key) |
| 138 | + if details: |
| 139 | + print(f"Emp id {key} details: {details}") |
| 140 | + except TypeError as e: |
| 141 | + print(e) |
| 142 | + |
| 143 | + elif choice == '3': |
| 144 | + try: |
| 145 | + key = int(input("Enter key (employee ID): ")) |
| 146 | + db.delete(key) |
| 147 | + except TypeError as e: |
| 148 | + print(e) |
| 149 | + |
| 150 | + elif choice == '4': |
| 151 | + print("Exiting the program.") |
| 152 | + break |
| 153 | + |
| 154 | + else: |
| 155 | + print("Invalid choice. Please choose a valid option.") |
| 156 | + |
| 157 | +if __name__ == "__main__": |
| 158 | + main() |
0 commit comments