mekasu0124

Untitled

Mar 6th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.64 KB | None | 0 0
  1. import random
  2. import json
  3. import datetime
  4. import string
  5.  
  6. from timer import Timer
  7. from datetime import date
  8.  
  9. class SeedDb:
  10.     def __init__(self, names_path, dest_path, max_loops):
  11.         self.names_path = names_path
  12.         self.dest_path = dest_path
  13.         self.max_loops = max_loops
  14.         self.path = dest_path
  15.         self.build_users()
  16.  
  17.     def build_users(self):
  18.         print("Building Users")
  19.         all_names = self.get_all_names()
  20.         all_user_ids = self.generate_employee_ids()
  21.         all_pay_amounts = self.random_pay_amounts()
  22.  
  23.         all_users = {}
  24.  
  25.         for index, name in enumerate(all_names):
  26.             print(f"Building User #{index+1}")
  27.  
  28.             iden = all_user_ids[index]
  29.             first, last = name.split(" ")
  30.             username = self.generate_username(name)
  31.             password = self.generate_password()
  32.             pay_rate = all_pay_amounts[index]
  33.             all_dates = self.generate_dates()
  34.             all_times = self.generate_time_punches(all_dates)
  35.  
  36.             user = {
  37.                 username: {
  38.                     "id": iden,
  39.                     "first_name": first,
  40.                     "last_name": last,
  41.                     "password": password,
  42.                     "pay_rate": pay_rate,
  43.                     "time_punches": all_times
  44.                 }
  45.             }
  46.  
  47.             all_users.update(user)
  48.             print(f"User #{index+1} Created")
  49.  
  50.         print("Saving All Users")
  51.         self.write_to_json(all_users)
  52.         print("All Users Saved")
  53.  
  54.     def generate_dates(self):
  55.         print("Building Dates")
  56.  
  57.         today = datetime.date.today()
  58.         dates = []
  59.  
  60.         while len(dates) < 500:
  61.             if today.weekday() not in [5, 6]:
  62.                 dates.append(today)
  63.  
  64.             today -= datetime.timedelta(days=1)
  65.  
  66.         print("Returning Dates")
  67.         return dates
  68.  
  69.     def generate_time_punches(self, dates):
  70.         print("Generating Time Punches")
  71.  
  72.         punches = {}
  73.  
  74.         for date in dates:
  75.             if date.weekday() == 4:
  76.                 time_in = datetime.time(random.randint(6, 12), random.randint(0, 59))
  77.                 time_out = datetime.time(random.randint(12, 16), random.randint(30, 59))
  78.             else:
  79.                 time_in = datetime.time(random.randint(6, 12), random.randint(0, 59))
  80.                 time_out = datetime.time(random.randint(12, 16), random.randint(30, 59))
  81.  
  82.             new_punch = {
  83.                 date.__format__("%m/%d/%Y"): {
  84.                     "time_in": time_in.__format__("%H:%M"),
  85.                     "time_out": time_out.__format__("%H:%M")
  86.                 }
  87.             }
  88.  
  89.             punches.update(new_punch)
  90.  
  91.         print("Returning All Dates")
  92.         return punches
  93.  
  94.     def write_to_json(self, info):
  95.         print("Attempting To Open ", self.dest_path)
  96.  
  97.         try :
  98.             with open(self.dest_path, 'r', encoding="utf-8-sig") as f:
  99.                 print(self.dest_path, " Opened Successfully")
  100.                 data = json.load(f, object_hook = self.object_hook_func)
  101.  
  102.                 data = info
  103.  
  104.                 print("Attempting To Save All Users")
  105.                 with open(self.dest_path, 'w', encoding="utf-8-sig") as new:
  106.                     data = json.dump(data, new, indent = 4, default = self.default_func)
  107.                 print("All Users Saved Successfully")
  108.         except:
  109.             print("Error Opening ", self.dest_path)
  110.             print("Attempting To Create New File: ", self.dest_path)
  111.             with open(self.dest_path, 'w+', encoding="utf-8-sig") as n:
  112.                 print(self.dest_path, " Created Successfully")
  113.                 print("Attempting To Save All Users")
  114.                 data = json.dump(info, n, indent=4, default=self.default_func)
  115.             print("All Users Saved Successfully")
  116.  
  117.     def random_pay_amounts(self):
  118.         print("Generating Random Pay Rates")
  119.         random_pays = []
  120.  
  121.         for _ in range(self.max_loops):
  122.             pay = round(random.uniform(12, 20), 2)
  123.             random_pays.append(pay)
  124.        
  125.         print("Returning Pay Rates")
  126.         return random_pays
  127.  
  128.     def generate_employee_ids(self):
  129.         print("Generating Employee ID's")
  130.         employee_ids = set()
  131.  
  132.         while len(employee_ids) < 100:
  133.             employee_id = random.randint(1000000, 9999999)
  134.             employee_ids.add(employee_id)
  135.        
  136.         print("Returning List Of Employee ID's")
  137.         return list(employee_ids)
  138.  
  139.     def generate_username(self, name):
  140.         print("Generating Username")
  141.  
  142.         stripped_name = name.replace(" ", "")
  143.         rand_nums = [str(random.randint(0, 9)) for _ in range(4)]
  144.         username = stripped_name + "".join(rand_nums).lower().replace("'", "")
  145.  
  146.         print("Returning Username")
  147.         return username
  148.    
  149.     def generate_password(self):
  150.         print("Generating Password")
  151.         password = ''.join(random.choices(string.ascii_letters + string.digits, k=12))
  152.        
  153.         while not (any(c.islower() for c in password)
  154.                 and any(c.isupper() for c in password)
  155.                 and any(c.isdigit() for c in password)):
  156.             password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
  157.        
  158.         print("Returning Password")
  159.         return password
  160.  
  161.     def get_all_names(self):
  162.         print("Opening names.json")
  163.         with open(self.names_path, 'r', encoding="utf-8-sig") as f:
  164.             print("Returning List Of Names")
  165.             return json.load(f)
  166. if __name__ == '__main__':
  167.     timer = Timer()
  168.     timer.start()
  169.     SeedDb("./names.json", "all_users.json", 100)
  170.     timer.stop()
Advertisement
Add Comment
Please, Sign In to add comment