-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_generator.py
112 lines (103 loc) · 6.02 KB
/
user_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 6 15:43:52 2017
@author: hamperfait
"""
"""
options = [(trainingDateStart, testingDateStart, testingDateEnd, windowDateEnd, trainingDataSetSize,
- samplingRate, config, 'SVM', [a, b, c], features)
- for a, b, c in
- itertools.product(['poly', 'rbf', 'sigmoid'], [True, False], [0.01, 0.02, 0.03, 0.04, 0.05])]
- options = options.__iter__()
"""
import argparse
import os.path
import string
import sys
import subprocess
# Name part
parser = argparse.ArgumentParser(description='Generate usernames with A LOT of different of options.')
parser.add_argument("-n", "--Names", help="The path to the file containing the dictionary with names. If no path is specified, initials will be used.", default="")
parser.add_argument("-mN", "--min-chars-N", help="The minimum number of chars from the Name. Default is 1.", type=int, default=1)
parser.add_argument("-xN", "--max-chars-N", help="The maximum number of chars from the Name. Default is 1.", type=int, default=7)
# Surname part
parser.add_argument("Surnames", help="The path to the file containing the dictionary with names.")
parser.add_argument("-mS", "--min-chars-S", help="The minimum number of chars from the Surname.", type=int, default=1)
parser.add_argument("-xS", "--max-chars-S", help="The maximum number of chars from the Surname.", type=int, default=7)
#Year part
parser.add_argument("-y", "--year", help="Add a year (or more) at the end", type=int, nargs='+')
parser.add_argument("-yr", "--year-range", help="Add a range of years that are to be added at the end", type=int, nargs=2)
parser.add_argument("-d", "--digits", help="The number of digits that will be addded (1990 vs 90). Can also be used to add numbers from 0 to 9.", type=int, nargs='+', choices=[0,1,2,3,4], default=[0])
# Misc
parser.add_argument("-u1", "--union1", help="Select if you want a binding character [_ . - ] etc. between name and surname. Default is None. ", nargs='+', default=[""])
parser.add_argument("-u2", "--union2", help="Select if you want a binding character [_ . - ] etc. between surname and year(if any). If no year is provided, we recommend to turn this off, since it will do NUFFIN. Default is None. ", nargs='+', default=[""])
parser.add_argument("-o", "--output", help="If you want to specify the name of the output file. Default is... usernames!Yay!", default="usernames!Yay!.txt")
parser.add_argument("-m", "--mode", help="If you want the results to be appended to the file or to overwrite. Default is overWrite.", choices=["a", "w"], default="w")
parser.add_argument("-sO", "--switch-order", help="Choose if name and surname have to be switched", action='store_true', default=False)
parser.add_argument("-dX", "--delete-duplicates", help="Delete the duplicates in the file", action="store_true", default=True)
results = parser.parse_args()
print(results)
def check_files(names, surnames):
if names and not os.path.isfile(names):
print("We're sorry. We didn't find the file in the specified directory. And we're NOT going to generate it for you ;)")
sys.exit()
if not surnames is not None and os.path.isfile(surnames):
print("We're sorry. Didn't we say the Surnames file actually must exist? We might have forgotten, sorry...")
sys.exit()
def delete_duplicates():
lines_seen = set() # holds lines already seen
with open("uniq_names", "w") as outfile:
with open(results.output, "r") as reader:
for line in reader:
if line not in lines_seen: # not a duplicate
outfile.write(line)
lines_seen.add(line)
def main(namesPath, surnamesPath):
with open(results.output, results.mode.lower()) as usernames:
try:
names = open(namesPath, 'r')
except IOError:
print("We will only work with initials, no file.")
names=list(string.ascii_lowercase)
pass
for name in names:
try:
surnames = open(surnamesPath, 'r')
except IOError:
surnames = list(string.ascii_lowercase)
for surname in surnames:
for namechars in range(results.min_chars_N, results.max_chars_N+1):
for surnamechars in range(results.min_chars_S, results.max_chars_S+1):
for union1 in results.union1:
for digits in results.digits:
for union2 in results.union2:
if results.year_range is not None:
for year in range(results.year_range[0], results.year_range[1]):
usernames.write("%s%s%s%s%d\n" % (name[:min(len(name), namechars)].lower(), union1, surname[:min(len(surname), surnamechars)].lower(), union2, year))
elif results.year is not None:
for year in results.year:
usernames.write("%s%s%s%s%d\n" % (name[:min(len(name), namechars)].lower(), union1, surname[:min(len(surname), surnamechars)].lower(), union2, year))
else:
usernames.write("%s%s%s\n" % (name[:min(len(name), namechars)].lower(), union1, surname[:min(len(surname), surnamechars)].lower()))
try:
names.close()
surnames.close()
except:
pass
if __name__ == "__main__":
if results.switch_order:
print(True)
names = results.Surnames
surnames = results.Names
else:
names=results.Names
surnames=results.Surnames
check_files(names, surnames)
main(names, surnames)
if results.delete_duplicates:
if sys.platform == "linux" or sys.platform == "linux2":
subprocess.call("sort %s | uniq > %s" % (results.output, "temp.txt"), shell = True)
os.rename("temp.txt", results.output)
else:
delete_duplicates()