Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import json
- import datetime
- import string
- from timer import Timer
- from datetime import date
- class SeedDb:
- def __init__(self, names_path, dest_path, max_loops):
- self.names_path = names_path
- self.dest_path = dest_path
- self.max_loops = max_loops
- self.path = dest_path
- self.build_users()
- def build_users(self):
- print("Building Users")
- all_names = self.get_all_names()
- all_user_ids = self.generate_employee_ids()
- all_pay_amounts = self.random_pay_amounts()
- all_users = {}
- for index, name in enumerate(all_names):
- print(f"Building User #{index+1}")
- iden = all_user_ids[index]
- first, last = name.split(" ")
- username = self.generate_username(name)
- password = self.generate_password()
- pay_rate = all_pay_amounts[index]
- all_dates = self.generate_dates()
- all_times = self.generate_time_punches(all_dates)
- user = {
- username: {
- "id": iden,
- "first_name": first,
- "last_name": last,
- "password": password,
- "pay_rate": pay_rate,
- "time_punches": all_times
- }
- }
- all_users.update(user)
- print(f"User #{index+1} Created")
- print("Saving All Users")
- self.write_to_json(all_users)
- print("All Users Saved")
- def generate_dates(self):
- print("Building Dates")
- today = datetime.date.today()
- dates = []
- while len(dates) < 500:
- if today.weekday() not in [5, 6]:
- dates.append(today)
- today -= datetime.timedelta(days=1)
- print("Returning Dates")
- return dates
- def generate_time_punches(self, dates):
- print("Generating Time Punches")
- punches = {}
- for date in dates:
- if date.weekday() == 4:
- time_in = datetime.time(random.randint(6, 12), random.randint(0, 59))
- time_out = datetime.time(random.randint(12, 16), random.randint(30, 59))
- else:
- time_in = datetime.time(random.randint(6, 12), random.randint(0, 59))
- time_out = datetime.time(random.randint(12, 16), random.randint(30, 59))
- new_punch = {
- date.__format__("%m/%d/%Y"): {
- "time_in": time_in.__format__("%H:%M"),
- "time_out": time_out.__format__("%H:%M")
- }
- }
- punches.update(new_punch)
- print("Returning All Dates")
- return punches
- def write_to_json(self, info):
- print("Attempting To Open ", self.dest_path)
- try :
- with open(self.dest_path, 'r', encoding="utf-8-sig") as f:
- print(self.dest_path, " Opened Successfully")
- data = json.load(f, object_hook = self.object_hook_func)
- data = info
- print("Attempting To Save All Users")
- with open(self.dest_path, 'w', encoding="utf-8-sig") as new:
- data = json.dump(data, new, indent = 4, default = self.default_func)
- print("All Users Saved Successfully")
- except:
- print("Error Opening ", self.dest_path)
- print("Attempting To Create New File: ", self.dest_path)
- with open(self.dest_path, 'w+', encoding="utf-8-sig") as n:
- print(self.dest_path, " Created Successfully")
- print("Attempting To Save All Users")
- data = json.dump(info, n, indent=4, default=self.default_func)
- print("All Users Saved Successfully")
- def random_pay_amounts(self):
- print("Generating Random Pay Rates")
- random_pays = []
- for _ in range(self.max_loops):
- pay = round(random.uniform(12, 20), 2)
- random_pays.append(pay)
- print("Returning Pay Rates")
- return random_pays
- def generate_employee_ids(self):
- print("Generating Employee ID's")
- employee_ids = set()
- while len(employee_ids) < 100:
- employee_id = random.randint(1000000, 9999999)
- employee_ids.add(employee_id)
- print("Returning List Of Employee ID's")
- return list(employee_ids)
- def generate_username(self, name):
- print("Generating Username")
- stripped_name = name.replace(" ", "")
- rand_nums = [str(random.randint(0, 9)) for _ in range(4)]
- username = stripped_name + "".join(rand_nums).lower().replace("'", "")
- print("Returning Username")
- return username
- def generate_password(self):
- print("Generating Password")
- password = ''.join(random.choices(string.ascii_letters + string.digits, k=12))
- while not (any(c.islower() for c in password)
- and any(c.isupper() for c in password)
- and any(c.isdigit() for c in password)):
- password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
- print("Returning Password")
- return password
- def get_all_names(self):
- print("Opening names.json")
- with open(self.names_path, 'r', encoding="utf-8-sig") as f:
- print("Returning List Of Names")
- return json.load(f)
- if __name__ == '__main__':
- timer = Timer()
- timer.start()
- SeedDb("./names.json", "all_users.json", 100)
- timer.stop()
Advertisement
Add Comment
Please, Sign In to add comment