mekasu0124

Untitled

Mar 6th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.82 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, json_path, dest_path, max_loops):
  11.         self.json_path = json_path
  12.         self.dest_path = dest_path
  13.         self.max_loops = max_loops
  14.         self.path = dest_path
  15.  
  16.         self.build_user()
  17.  
  18.     # returns a list of names from json file
  19.     def get_names(self):
  20.         with open(self.json_path, 'r', encoding="utf-8-sig") as f:
  21.             data = json.load(f)
  22.  
  23.         return data
  24.  
  25.     # returns a list of usernames
  26.     def generate_username(self, name):
  27.         stripped_name = name.replace(" ", "")
  28.         rand_nums = [str(random.randint(0, 9)) for _ in range(4)]
  29.         username = stripped_name + "".join(rand_nums).lower().replace("'", "")
  30.  
  31.         return username
  32.  
  33.     # returns a single password    
  34.     def generate_password(self):
  35.         password = ''.join(random.choices(string.ascii_letters + string.digits, k=12))
  36.        
  37.         while not (any(c.islower() for c in password)
  38.                 and any(c.isupper() for c in password)
  39.                 and any(c.isdigit() for c in password)):
  40.             password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
  41.        
  42.         return password
  43.  
  44.     # returns a list of random pay amounts
  45.     def random_pay_amounts(self):
  46.         random_pays = []
  47.  
  48.         for _ in range(self.max_loops):
  49.             pay = round(random.uniform(12, 20), 2)
  50.             random_pays.append(pay)
  51.        
  52.         return random_pays
  53.    
  54.     # Generate 500 dates excluding Saturdays and Sundays
  55.     def generate_time_clock_punches(self):
  56.         today = datetime.date.today()
  57.         dates = []
  58.        
  59.         while len(dates) < 500:
  60.             if today.weekday() not in [5, 6]:
  61.                 dates.append(today)
  62.             today -= datetime.timedelta(days=1)
  63.  
  64.         # Generate time clock punches
  65.         punches = []
  66.        
  67.         for date in dates:
  68.             if date.weekday() == 4:  # Friday
  69.                 time_in = datetime.time(random.randint(6, 12), random.randint(0, 59))
  70.                 time_out = datetime.time(random.randint(12, 16), random.randint(30, 59))
  71.             else:
  72.                 time_in = datetime.time(random.randint(6, 12), random.randint(0, 59))
  73.                 time_out = datetime.time(random.randint(12, 16), random.randint(30, 59))
  74.  
  75.             punches.append((date, time_in, time_out))
  76.  
  77.         # Determine which dates are eligible for an attendance bonus
  78.         eligible_dates = []
  79.        
  80.         for date, time_in, time_out in punches:
  81.             if (date.weekday() == 4 and time_in.hour >= 6 and time_in.hour <= 12) or \
  82.             (date.weekday() < 4 and time_in.hour <= 6 and time_out.hour >= 16 and time_out.minute >= 30):
  83.                 eligible_dates.append(date)
  84.  
  85.         # Randomly select more than half of the eligible dates
  86.         bonus_dates = random.sample(eligible_dates, k=len(eligible_dates) // 2 + 1)
  87.  
  88.         return punches, bonus_dates
  89.  
  90.     # get a list of employee ids
  91.     def generate_employee_ids(self):
  92.         employee_ids = set()
  93.  
  94.         while len(employee_ids) < 100:
  95.             employee_id = random.randint(1000000, 9999999)
  96.             employee_ids.add(employee_id)
  97.        
  98.         return list(employee_ids)
  99.  
  100.     # build user profile
  101.     def build_user(self):
  102.         print("Building Users")
  103.  
  104.         print("Getting All Names")
  105.         all_names = self.get_names()
  106.  
  107.         print("Getting All Employee Id's")
  108.         employee_ids = self.generate_employee_ids()
  109.  
  110.         all_users = []
  111.  
  112.         for index, name in enumerate(all_names):
  113.             print(f"Building User #{index+1}")
  114.  
  115.             first, last = name.split(" ")
  116.             username = self.generate_username(name)
  117.             password = self.generate_password()
  118.             time_clock_punches, bonus_dates = self.generate_time_clock_punches()
  119.  
  120.             user_model = {
  121.                 username: {
  122.                     "id": employee_ids[index],
  123.                     "first_name": first,
  124.                     "last_name": last,
  125.                     "password": password,
  126.                     "bonus_date_count": len(bonus_dates),
  127.                     "time_cards": time_clock_punches
  128.                 }
  129.             }
  130.  
  131.             all_users.append(user_model)
  132.  
  133.         print("Storing Users In ", self.path)
  134.         self.write_to_json(all_users, self.path)
  135.         print("Users Stored Successfully")
  136.  
  137.     # write to new json file
  138.     def write_to_json(self, info, path):
  139.         try:
  140.             with open(path, 'r', encoding="utf-8-sig") as f:
  141.                 data = json.load(f, object_hook=self.object_hook_func)
  142.  
  143.                 data = info
  144.  
  145.                 with open(path, 'w', encoding="utf-8-sig") as new:
  146.                     data = json.dump(data, indent=4, default=self.default_func)
  147.  
  148.         except:
  149.             with open(path, 'w+', encoding="utf-8-sig") as n:
  150.                 data = json.dump(info, indent=4, default=self.default_func)
  151.  
  152.     # obtain from new json file
  153.     def get_employee_data(self):
  154.         with open("./employees.json", 'r', encoding="utf-8-sig") as f:
  155.             data = json.load(f)
  156.  
  157.         return data
  158.    
  159.     # write to database
  160.     def write_to_database(self):
  161.         pass
  162.  
  163.     def default_func(self, obj):
  164.         if isinstance(obj, date):
  165.             return {
  166.                 '__type__': 'date',
  167.                 'date': obj.isoformat(),
  168.             }
  169.         return obj
  170.  
  171.     def object_hook_func(self, obj):
  172.         if '__type__' in obj and obj['__type__'] == 'date':
  173.             return date.fromisoformat(obj['date'])
  174.         return obj
  175.  
  176. if __name__ == '__main__':
  177.     timer = Timer()
  178.     timer.start()
  179.     seedDb = SeedDb("./names.json", "./user_profiles.json", 100)
  180.     timer.stop()
  181.  
  182.  
  183.  
Advertisement
Add Comment
Please, Sign In to add comment