import random import csv from collections import defaultdict random.seed(420) def handle_csv(): with open('generator/responses.csv', newline='\n') as csvfile: spamreader = csv.reader(csvfile, delimiter=',', quotechar='"') quotes = [] for row in spamreader: quote = row[1].replace('\n', ' ') quotes.append(quote) return quotes class Test: def __init__(self, users) -> None: self.connections = set() self.names_used = set() self.users = users self.post_counter = 1 self.quotes = handle_csv() self.reposts = defaultdict(list) def get_name(self): return random.choice(self.users) def get_existing_name(self): lst = list(self.names_used) lst.sort() return random.choice(lst) def get_random(self, bottom, top): return random.randint(bottom, top) def generate_add(self, name1 = None, name2 = None, generate = True, useExisting = False): getter = self.get_existing_name if useExisting else self.get_name name1 = name1 if name1 is not None else getter() name2 = name2 if name2 is not None else getter() name_tuple = (name1, name2) if generate: while name1 == name2: name2 = getter() while name_tuple in self.connections: name1 = getter() name2 = getter() while name1 == name2: name2 = getter() name_tuple = (name1, name2) if name_tuple in self.connections: return "" self.connections.add(name_tuple) self.connections.add((name2, name1)) self.names_used.add(name1) self.names_used.add(name2) return f"add {name1} {name2}", name1, name2 def generate_remove(self, name1 = None, name2 = None): named_tuple = (name1, name2) if name1 is not None and name2 is not None \ else random.choice(self.connections) self.connections.remove(named_tuple) return f"remove {name1} {name2}", name1, name2 def generate_distance(self, name1 = None, name2 = None, is_unique = True): name1 = name1 if name1 is not None else self.get_existing_name() name2 = name2 if name2 is not None else self.get_existing_name() while is_unique and name1 == name2: name2 = self.get_name() return f"distance {name1} {name2}", name1, name2 def generate_suggestions(self, name1 = None): name1 = name1 if name1 is not None else self.get_existing_name() return f"suggestions {name1}", name1 def generate_friends(self, name1 = None): name1 = name1 if name1 is not None else self.get_existing_name() return f"friends {name1}", name1 def generate_common(self, name1 = None, name2 = None, is_unique = True): name1 = name1 if name1 is not None else self.get_existing_name() name2 = name2 if name2 is not None else self.get_existing_name() while is_unique and name1 == name2: name2 = self.get_name() return f"common {name1} {name2}", name1, name2 def generate_popular(self, name1 = None): name1 = name1 if name1 is not None else self.get_existing_name() return f"popular {name1}", name1 def generate_clique(self, name1 = None, generate = True, size = 1): name1 = name1 if name1 is not None else self.get_existing_name() result = "" if generate: names = list(set([self.get_name() for _ in range(size - 1)])) names.append(name1) names.sort() for nameFst in names: for nameSnd in names: if nameFst == nameSnd: continue msg, _, _ = self.generate_add(nameFst, nameSnd) result += f"{msg}\n" return f"{result}common-group {name1}", name1 def generate_create(self, name = None, useExisting = False): getter = self.get_existing_name if useExisting else self.get_name name = name if name is not None else getter() line = random.choice(self.quotes) self.reposts[self.post_counter] = [] self.post_counter += 1 return f"create {name} \"{line}\"" def generate_repost(self, name = None, id = None, repost_id = None, isRepost = False, useExisting = False): getter = self.get_existing_name if useExisting else self.get_name name = name if name is not None else getter() id = id if id is not None else random.choice(list(self.reposts.keys())) try: if isRepost: repost_id = repost_id if repost_id is not None else random.choice(self.reposts[id]) self.reposts[id].append(self.post_counter) self.post_counter += 1 return f"repost {name} {id} {repost_id}" except: pass self.reposts[id].append(self.post_counter) self.post_counter += 1 return f"repost {name} {id}" def generate_lca(self, id = None, repost_id1 = None, repost_id2 = None): id = id if id is not None else random.choice(list(self.reposts.keys())) try: while len(self.reposts[id]) < 2: id = random.choice(list(self.reposts.keys())) except: pass repost_id1 = random.choice(self.reposts[id]) repost_id2 = random.choice(self.reposts[id]) while repost_id1 == repost_id2: repost_id2 = random.choice(self.reposts[id]) return f"common-repost {id} {repost_id1} {repost_id2}" def generate_show(self, num = 1, with_reposts = False): rep = [] if with_reposts: lst = set() for _ in range(random.randint(1, num)): try: post = random.choice(list(self.reposts.keys())) lst.add((post, random.choice(self.reposts[post]))) except: pass rep = [f"get-reposts {num} {repost}" for num, repost in lst] num -= len(rep) lst = set(random.choices(list(self.reposts.keys()), k=num)) return rep + [f"get-reposts {num}" for num in lst] def generate_like(self, name = None, id = None, repost_id = None, isRepost = False): name = name if name is not None else self.get_name() id = id if id is not None else random.choice(list(self.reposts.keys())) try: if isRepost: repost_id = repost_id if repost_id is not None else random.choice(self.reposts[id]) return f"like {name} {id} {repost_id}" except: pass return f"like {name} {id}" def generate_get_like(self, num = 1, with_reposts = False): rep = [] if with_reposts: lst = set() for _ in range(random.randint(1, num)): try: post = random.choice(list(self.reposts.keys())) lst.add((post, random.choice(self.reposts[post]))) except: pass rep = [f"get-likes {num} {repost}" for num, repost in lst] num -= len(rep) lst = set(random.choices(list(self.reposts.keys()), k=num)) return rep + [f"get-likes {num}" for num in lst] def generate_feed(self, name = None, size = 1): name = name if name is not None else self.get_existing_name() return f"feed {name} {size}" def generate_profile(self, name = None): name = name if name is not None else self.get_existing_name() return f"view-profile {name}" def generate_friend_repost(self, name = None, id = None): name = name if name is not None else self.get_existing_name() id = id if id is not None else random.choice(list(self.reposts.keys())) return f"friends-repost {name} {id}"