-
Notifications
You must be signed in to change notification settings - Fork 0
/
atmos.py
169 lines (125 loc) · 5.37 KB
/
atmos.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import os
import requests
import re
import base64
def clear():
os.system("clear||cls")
def randint(minimum, maximum, num=1):
url = f"https://www.random.org/integers/?num={num}&min={minimum}&max={maximum}&col=5&base=10&format=plain&rnd=new"
#random numbers generated by atmospheric noise. (as close to random as one can get while not having to use math.)
response = requests.get(url)
if response.status_code == 200:
if num > 1:
numbers_list = re.split(r'[\n\t]+', response.text.strip())
return list(map(int, numbers_list))
else:
return int(response.text)
else:
raise ValueError(f"\nRequest to random.org failed with status code: {response.status_code}. Unable to generate random number.")
#NOTE
#https://stackoverflow.com/a/65499571/15114290 | True == 1 when working with sets.
#Sets are also ordered from boolean, ints, strings
def choice(list_of_data):
maximum = len(list_of_data)-1
#for example lets say "list_of_data" is 11 items long, we need to account for index 0, 0 is the minimum so now there are 10 index spots to choose from, not 11.
#we will get an "index out of range" error without "-1".
index = randint(0, maximum, 1)
#List or tuple
if isinstance(list_of_data, (list, tuple, str)):
option = list_of_data[index]
return option
#Dictionary
elif isinstance(list_of_data, dict):
keys = list(list_of_data.keys())
selected_key = keys[index]
selected_value = list_of_data[selected_key]
selected_dict = {selected_key: selected_value}
return selected_dict #type dictionary
#Set
elif isinstance(list_of_data, set):
set_list = list(list_of_data)
set_option = set_list[index]
return set_option
else:
raise ValueError('\nUnable to make a choice. The data provided is not a list, tuple, set, or dictionary.')
def shuffle(list_of_data):
if isinstance(list_of_data, list):
random_numbers = [num for num in randint(0, len(list_of_data)-1, len(list_of_data))]
for i, j in enumerate(random_numbers):
list_of_data[i], list_of_data[j] = list_of_data[j], list_of_data[i]
return list_of_data
elif isinstance(list_of_data, tuple):
list_of_data = list(list_of_data)
random_numbers = [num for num in randint(0, len(list_of_data)-1, len(list_of_data))]
for i, j in enumerate(random_numbers):
list_of_data[i], list_of_data[j] = list_of_data[j], list_of_data[i]
return tuple(list_of_data)
elif isinstance(list_of_data, dict):
list_of_data = list(list_of_data.items())
random_numbers = [num for num in randint(0, len(list_of_data)-1, len(list_of_data))]
for i, j in enumerate(random_numbers):
list_of_data[i], list_of_data[j] = list_of_data[j], list_of_data[i]
return dict(list_of_data)
elif isinstance(list_of_data, str):
random_numbers = [num for num in randint(0, len(list_of_data)-1, len(list_of_data))]
chars = list(list_of_data)
for i, j in enumerate(random_numbers):
chars[i], chars[j] = chars[j], chars[i]
list_of_data = ''.join(chars)
return list_of_data
else:
raise ValueError('\nUnable to shuffle data. The data provided is not a list, tuple, or dictionary.')
def gen_bytes(length):
if not isinstance(length, int):
raise ValueError(f'\n\nInvalid type, "gen_bytes()" takes type "int", value given is "{type(length)}".')
else:
byte_array = bytearray()
if length == 1:
for _ in range(length):
number = randint(0, 255, 1)
byte_array.append(number)
return bytes(byte_array)
elif length > 1:
list_of_numbers = randint(0, 255, length)
for number in list_of_numbers:
byte_array.append(int(number))
return bytes(byte_array)
else:
list_of_numbers = randint(0, 255, 32)
for number in list_of_numbers:
byte_array.append(int(number))
return bytes(byte_array)
def gen_bits(length):
if not isinstance(length, int):
raise ValueError(f'\n\nInvalid type, "gen_bits()" takes type "int", value given is "{type(length)}".')
else:
if length == 1:
bit = randint(0, 1, 1)
result = 0
result = (result << 1) | int(bit)
return result
elif length > 1:
bits = randint(0, 1, length)
result = 0
for bit in bits:
result = (result << 1) | int(bit)
return result
else:
bits = randint(0, 1, 32)
result = 0
for bit in bits:
result = (result << 1) | int(bit)
return result
#20 is the default if no argument is passed and the function is just called.
def bytes_urlsafe(length=20):
if not isinstance(length, (int, type(None))):
raise ValueError(f'\n\nInvalid type, "bytes_urlsafe()" takes type "int" or "None", value given is "{type(length)}".')
else:
if not length:
length=20 #default if None is provided.
bytes_to_make_safe = gen_bytes(length)
urlsafe_token = base64.urlsafe_b64encode(bytes_to_make_safe)
return urlsafe_token.decode()
if __name__ == '__main__':
clear()
print("uwu")