Advertisement
tsvtln

devices

Feb 8th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | Source Code | 0 0
  1. import json
  2. import time
  3.  
  4. devices_db = "devices.json"
  5.  
  6.  
  7. def read_devices(fp: str) -> list:
  8.     # we check if the 'db' exists already, otherwise we return empty list
  9.     try:
  10.         with open(fp, "r") as f:
  11.             return json.load(f)
  12.     except (FileNotFoundError, json.JSONDecodeError):
  13.         return []
  14.  
  15.  
  16. def write_devices(fp: str, devices: list) -> None:
  17.     # for writing the devices to the 'db'
  18.     with open(fp, "w") as f:
  19.         json.dump(devices, f, indent=4)
  20.  
  21.  
  22. def parse_devices_input(input_str: str) -> list:
  23.     # we remove the "devices = " part (case-insensitive)
  24.     prefix = "devices = "
  25.     if input_str.lower().startswith(prefix):
  26.         input_str = input_str[len(prefix):].strip()
  27.  
  28.     # and we split the string by commas and clean up each device name from the quotes
  29.     devices = [device.strip().strip('"').strip("“").strip("”") for device in input_str.split(",")]
  30.     return devices
  31.  
  32.  
  33. def main():
  34.     added_devices = []
  35.     # get the current inventory of devices
  36.     inventory = read_devices(devices_db)
  37.     # get the input from the user
  38.     user_input = input("Enter devices:\n")
  39.     # call the helper function to parse the input
  40.     new_devices = parse_devices_input(user_input)
  41.  
  42.  
  43.     # start timer for the check
  44.     check_start = time.time()
  45.  
  46.     # we check every device from the input to see if it's already in the 'db'
  47.     # we'll return an output if the device exists to the user, if not we'll append it to a list, so we can
  48.     # return the added devices afterwards
  49.     for device in new_devices:
  50.         if device in inventory:
  51.             print(f"Device: {device} already exists.")
  52.         else:
  53.             inventory.append(device)
  54.             added_devices.append(device)
  55.  
  56.     # end the timer for the check
  57.     check_end = time.time()
  58.     check_time = check_end - check_start
  59.  
  60.     # save the new devices to the 'db'
  61.     write_devices(devices_db, inventory)
  62.  
  63.     # we return to the user the devices that were added succesfully or if nothing was added we return according output
  64.     if added_devices:
  65.         print("Devices added:")
  66.         for device in added_devices:
  67.             print(f"• {device}")
  68.     else:
  69.         print("No new devices were added.")
  70.  
  71.     # at the end we return the timer for the check
  72.     print(f"Time for performing the check: {check_time:.4f} seconds")
  73.  
  74.  
  75. if __name__ == "__main__":
  76.     main()
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement