-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOgenerate.py
More file actions
executable file
·81 lines (58 loc) · 2.05 KB
/
IOgenerate.py
File metadata and controls
executable file
·81 lines (58 loc) · 2.05 KB
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
from random import randint, SystemRandom, choice, normalvariate, expovariate
from datetime import date
import string
import csv
def get_contents(file_path="input.dat"):
rows = []
f = open(file_path, "r", encoding='utf8')
for line in csv.reader(f, delimiter=','):
row = []
for element in line:
element = element.replace("\'", "''")
element = element.replace("%", "")
if element == "\\N" or element == " ":
element = ""
row.append(element)
rows.append(row)
f.close()
return rows
def write_contents(lists=[], file_path="output.dat"):
f = open(file_path, "w+", encoding='utf8')
for line in lists:
f.write(str(line) + "\n")
f.close()
def random_(length=4, sample=string.ascii_uppercase + string.digits):
"""
Returns a random string of given length and sample
:param length: how log the string should be
:param sample: See https://docs.python.org/2/library/string.html for
string vars. Some useful examples: ascii_letters,
ascii_lowercase, ascii_uppercase, digits, hexdigits
:return: [str]
"""
return ''.join(SystemRandom().choice(sample) for _ in range(length))
def random_week_days(days=2):
result = ""
sample = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]
while len(result) < days * 2:
r = choice(sample)
sample.remove(r)
result += r
return result
def random_date():
t = date.today()
y = t.year - randint(-2, 20)
m = randint(1, 12)
d = abs(t.day - randint(0, 28)) + 1
return str(t.replace(y, m, d))
def random_time():
first = random_(1, "012")
if first != "2":
second = random_(1, string.digits)
else:
second = random_(1, "0123")
third = random_(1, "012345")
fourth = random_(1, string.digits)
return first + second + ":" + third + fourth
def random_datetime():
return random_date() + " " + random_time()