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, json_path, dest_path, max_loops):
- self.json_path = json_path
- self.dest_path = dest_path
- self.max_loops = max_loops
- self.path = dest_path
- self.build_user()
- # returns a list of names from json file
- def get_names(self):
- with open(self.json_path, 'r', encoding="utf-8-sig") as f:
- data = json.load(f)
- return data
- # returns a list of usernames
- def generate_username(self, name):
- stripped_name = name.replace(" ", "")
- rand_nums = [str(random.randint(0, 9)) for _ in range(4)]
- username = stripped_name + "".join(rand_nums).lower().replace("'", "")
- return username
- # returns a single password
- def generate_password(self):
- 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))
- return password
- # returns a list of random pay amounts
- def random_pay_amounts(self):
- random_pays = []
- for _ in range(self.max_loops):
- pay = round(random.uniform(12, 20), 2)
- random_pays.append(pay)
- return random_pays
- # Generate 500 dates excluding Saturdays and Sundays
- def generate_time_clock_punches(self):
- today = datetime.date.today()
- dates = []
- while len(dates) < 500:
- if today.weekday() not in [5, 6]:
- dates.append(today)
- today -= datetime.timedelta(days=1)
- # Generate time clock punches
- punches = []
- for date in dates:
- if date.weekday() == 4: # Friday
- 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))
- punches.append((date, time_in, time_out))
- # Determine which dates are eligible for an attendance bonus
- eligible_dates = []
- for date, time_in, time_out in punches:
- if (date.weekday() == 4 and time_in.hour >= 6 and time_in.hour <= 12) or \
- (date.weekday() < 4 and time_in.hour <= 6 and time_out.hour >= 16 and time_out.minute >= 30):
- eligible_dates.append(date)
- # Randomly select more than half of the eligible dates
- bonus_dates = random.sample(eligible_dates, k=len(eligible_dates) // 2 + 1)
- return punches, bonus_dates
- # get a list of employee ids
- def generate_employee_ids(self):
- employee_ids = set()
- while len(employee_ids) < 100:
- employee_id = random.randint(1000000, 9999999)
- employee_ids.add(employee_id)
- return list(employee_ids)
- # build user profile
- def build_user(self):
- print("Building Users")
- print("Getting All Names")
- all_names = self.get_names()
- print("Getting All Employee Id's")
- employee_ids = self.generate_employee_ids()
- all_users = []
- for index, name in enumerate(all_names):
- print(f"Building User #{index+1}")
- first, last = name.split(" ")
- username = self.generate_username(name)
- password = self.generate_password()
- time_clock_punches, bonus_dates = self.generate_time_clock_punches()
- user_model = {
- username: {
- "id": employee_ids[index],
- "first_name": first,
- "last_name": last,
- "password": password,
- "bonus_date_count": len(bonus_dates),
- "time_cards": time_clock_punches
- }
- }
- all_users.append(user_model)
- print("Storing Users In ", self.path)
- self.write_to_json(all_users, self.path)
- print("Users Stored Successfully")
- # write to new json file
- def write_to_json(self, info, path):
- try:
- with open(path, 'r', encoding="utf-8-sig") as f:
- data = json.load(f, object_hook=self.object_hook_func)
- data = info
- with open(path, 'w', encoding="utf-8-sig") as new:
- data = json.dump(data, indent=4, default=self.default_func)
- except:
- with open(path, 'w+', encoding="utf-8-sig") as n:
- data = json.dump(info, indent=4, default=self.default_func)
- # obtain from new json file
- def get_employee_data(self):
- with open("./employees.json", 'r', encoding="utf-8-sig") as f:
- data = json.load(f)
- return data
- # write to database
- def write_to_database(self):
- pass
- def default_func(self, obj):
- if isinstance(obj, date):
- return {
- '__type__': 'date',
- 'date': obj.isoformat(),
- }
- return obj
- def object_hook_func(self, obj):
- if '__type__' in obj and obj['__type__'] == 'date':
- return date.fromisoformat(obj['date'])
- return obj
- if __name__ == '__main__':
- timer = Timer()
- timer.start()
- seedDb = SeedDb("./names.json", "./user_profiles.json", 100)
- timer.stop()
Advertisement
Add Comment
Please, Sign In to add comment