ExtraFunctions 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- extrafunctions-0.1.0/ExtraFunctions/__init__.py +339 -0
- extrafunctions-0.1.0/ExtraFunctions.egg-info/PKG-INFO +7 -0
- extrafunctions-0.1.0/ExtraFunctions.egg-info/SOURCES.txt +8 -0
- extrafunctions-0.1.0/ExtraFunctions.egg-info/dependency_links.txt +1 -0
- extrafunctions-0.1.0/ExtraFunctions.egg-info/requires.txt +1 -0
- extrafunctions-0.1.0/ExtraFunctions.egg-info/top_level.txt +2 -0
- extrafunctions-0.1.0/PKG-INFO +7 -0
- extrafunctions-0.1.0/README.md +0 -0
- extrafunctions-0.1.0/pyproject.toml +16 -0
- extrafunctions-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import random as r
|
|
4
|
+
from PIL import Image
|
|
5
|
+
import time as t
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class UknownFormatError(Exception):
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def negative(used):
|
|
15
|
+
try:
|
|
16
|
+
int(used)
|
|
17
|
+
type1=int
|
|
18
|
+
except:
|
|
19
|
+
if type(used) == str:
|
|
20
|
+
type1=str
|
|
21
|
+
else:
|
|
22
|
+
raise UknownFormatError(f"{used} is an unkown format")
|
|
23
|
+
|
|
24
|
+
if type1 == int:
|
|
25
|
+
returned = -type1(used)
|
|
26
|
+
return returned
|
|
27
|
+
else:
|
|
28
|
+
returned1 = list(used)
|
|
29
|
+
returned = ""
|
|
30
|
+
for i in range(len(returned1)):
|
|
31
|
+
index = -i + (len(returned1)-1)
|
|
32
|
+
returned += type1(returned1[index])
|
|
33
|
+
return returned
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class SaveError(Exception):
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def Save(variable, name):
|
|
45
|
+
with open("save.json", "r") as file:
|
|
46
|
+
saved = json.load(file)
|
|
47
|
+
|
|
48
|
+
for item in saved:
|
|
49
|
+
if item[0] == name:
|
|
50
|
+
raise SaveError(f"{name} already exists.")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
saved.append([name, variable])
|
|
54
|
+
with open("save.json", "w") as file:
|
|
55
|
+
json.dump(saved, file)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def SaveReset():
|
|
61
|
+
if os.path.exists("save.json"):
|
|
62
|
+
os.remove("save.json")
|
|
63
|
+
else:
|
|
64
|
+
raise SaveError
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def SaveReturn(name):
|
|
70
|
+
if not os.path.exists("save.json"):
|
|
71
|
+
raise SaveError("save.json does not exist.")
|
|
72
|
+
with open("save.json", "r") as file:
|
|
73
|
+
saved = json.load(file)
|
|
74
|
+
for item in saved:
|
|
75
|
+
if item[0] == name:
|
|
76
|
+
return item[1]
|
|
77
|
+
raise SaveError(f"{name} does not exist.")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def DeleteSave(name):
|
|
82
|
+
if not os.path.exists("save.json"):
|
|
83
|
+
raise SaveError("save file does not exist.")
|
|
84
|
+
with open("save.json", "r") as file:
|
|
85
|
+
saved = json.load(file)
|
|
86
|
+
for item in saved:
|
|
87
|
+
if item[0] == name:
|
|
88
|
+
saved.remove(item)
|
|
89
|
+
|
|
90
|
+
with open("save.json", "w") as file:
|
|
91
|
+
json.dump(saved, file)
|
|
92
|
+
return
|
|
93
|
+
raise SaveError(f"{name} does not exist.")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def SaveChange(variable, name):
|
|
98
|
+
if not os.path.exists("save.json"):
|
|
99
|
+
raise SaveError("save.json does not exist.")
|
|
100
|
+
|
|
101
|
+
with open("save.json", "r") as file:
|
|
102
|
+
saved = json.load(file)
|
|
103
|
+
|
|
104
|
+
for item in saved:
|
|
105
|
+
if item[0] == name:
|
|
106
|
+
item[1] = variable
|
|
107
|
+
|
|
108
|
+
with open("save.json", "w") as file:
|
|
109
|
+
json.dump(saved, file)
|
|
110
|
+
|
|
111
|
+
return
|
|
112
|
+
|
|
113
|
+
raise SaveError(f"{name} does not exist.")
|
|
114
|
+
def Exists(name):
|
|
115
|
+
if not os.path.exists("save.json"):
|
|
116
|
+
return False
|
|
117
|
+
|
|
118
|
+
with open("save.json", "r") as file:
|
|
119
|
+
saved = json.load(file)
|
|
120
|
+
|
|
121
|
+
for item in saved:
|
|
122
|
+
if item[0] == name:
|
|
123
|
+
return True
|
|
124
|
+
|
|
125
|
+
return False
|
|
126
|
+
def ReturnSaved():
|
|
127
|
+
if not os.path.exists("save.json"):
|
|
128
|
+
raise SaveError("Nothing is saved, ReturnSaved() has nothing to return.")
|
|
129
|
+
with open("save.json") as file:
|
|
130
|
+
return json.load(file)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def RandomString(length):
|
|
134
|
+
characters = [
|
|
135
|
+
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
|
136
|
+
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
|
137
|
+
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
|
|
138
|
+
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
|
|
139
|
+
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
|
|
140
|
+
" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",",
|
|
141
|
+
"-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]",
|
|
142
|
+
"^", "_", "`", "{", "|", "}", "~"
|
|
143
|
+
]
|
|
144
|
+
string = ""
|
|
145
|
+
for i in range(length):
|
|
146
|
+
string += r.choice(characters)
|
|
147
|
+
return string
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def FileExists(file):
|
|
153
|
+
try:
|
|
154
|
+
with open(file, "rb"):
|
|
155
|
+
pass
|
|
156
|
+
return True
|
|
157
|
+
except FileNotFoundError:
|
|
158
|
+
return False
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def load(file):
|
|
163
|
+
extension = os.path.splitext(file)[1].lower()
|
|
164
|
+
if extension in [".png", ".jpg", ".jpeg", ".gif", ".bmp"]:
|
|
165
|
+
return Image.open(file)
|
|
166
|
+
else:
|
|
167
|
+
with open(file, "rb") as f:
|
|
168
|
+
return f.read()
|
|
169
|
+
def TimeLenFunc(func, *input):
|
|
170
|
+
start = t.perf_counter()
|
|
171
|
+
func(*input)
|
|
172
|
+
end = t.perf_counter()
|
|
173
|
+
return end - start
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def attempt(func, *inputs, times):
|
|
178
|
+
work = False
|
|
179
|
+
for i in range(times):
|
|
180
|
+
try:
|
|
181
|
+
func(*inputs)
|
|
182
|
+
work = True
|
|
183
|
+
break
|
|
184
|
+
except Exception:
|
|
185
|
+
pass
|
|
186
|
+
if not work:
|
|
187
|
+
raise Exception(f"{func}() failed after {times} attempts. ")
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def average(numbers):
|
|
192
|
+
n = 0
|
|
193
|
+
for i in range(len(numbers)):
|
|
194
|
+
n += numbers[i]
|
|
195
|
+
return n / len(numbers)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class InfiniteError(Exception):
|
|
200
|
+
pass
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def WaitUntil(boolian, max_frames):
|
|
206
|
+
n = 0
|
|
207
|
+
while not boolian():
|
|
208
|
+
if n > max_frames:
|
|
209
|
+
raise InfiniteError(f"{boolian} never became true.")
|
|
210
|
+
else:
|
|
211
|
+
n+=1
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def WeightedRandom(options, values):
|
|
216
|
+
Weight = []
|
|
217
|
+
for i in range(len(options)):
|
|
218
|
+
for k in range(values[i]):
|
|
219
|
+
Weight += [options[i]]
|
|
220
|
+
return Weight[r.randint(0, len(Weight) -1)]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
class NonexistentPlanetError(Exception):
|
|
225
|
+
pass
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def PlanetInfo(planet, info=False):
|
|
230
|
+
|
|
231
|
+
planets = {
|
|
232
|
+
"Mercury": {
|
|
233
|
+
"Distance from sun": "36 million miles",
|
|
234
|
+
"Moons": 0,
|
|
235
|
+
"Mass": "330,104,000,000,000,000,000,000 kg",
|
|
236
|
+
"Day": "176 days",
|
|
237
|
+
"Year": "88 days - half a Mercury day!"
|
|
238
|
+
},
|
|
239
|
+
|
|
240
|
+
"Venus": {
|
|
241
|
+
"Distance from sun": "67 million miles",
|
|
242
|
+
"Moons": 0,
|
|
243
|
+
"Mass": "4,867,000,000,000,000,000,000 kg",
|
|
244
|
+
"Day": "117 days",
|
|
245
|
+
"Year": "225 days"
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
"Earth": {
|
|
249
|
+
"Distance from sun": "93 million miles",
|
|
250
|
+
"Moons": 1,
|
|
251
|
+
"Mass": "5,972,000,000,000,000,000,000 kg",
|
|
252
|
+
"Day": "24 hours",
|
|
253
|
+
"Year": "365 days"
|
|
254
|
+
},
|
|
255
|
+
|
|
256
|
+
"Mars": {
|
|
257
|
+
"Distance from sun": "142 million miles",
|
|
258
|
+
"Moons": 2,
|
|
259
|
+
"Mass": "641,710,000,000,000,000,000,000 kg",
|
|
260
|
+
"Day": "24 hours 39 minutes",
|
|
261
|
+
"Year": "687 days"
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
"Jupiter": {
|
|
265
|
+
"Distance from sun": "484 million miles",
|
|
266
|
+
"Moons": 95,
|
|
267
|
+
"Mass": "1,898,000,000,000,000,000,000,000,000 kg",
|
|
268
|
+
"Day": "9 hours 56 minutes",
|
|
269
|
+
"Year": "11.86 years"
|
|
270
|
+
},
|
|
271
|
+
|
|
272
|
+
"Saturn": {
|
|
273
|
+
"Distance from sun": "886 million miles",
|
|
274
|
+
"Moons": 274,
|
|
275
|
+
"Mass": "568,000,000,000,000,000,000,000,000 kg",
|
|
276
|
+
"Day": "10 hours 33 minutes",
|
|
277
|
+
"Year": "29.45 years"
|
|
278
|
+
},
|
|
279
|
+
|
|
280
|
+
"Uranus": {
|
|
281
|
+
"Distance from sun": "1.8 billion miles",
|
|
282
|
+
"Moons": 28,
|
|
283
|
+
"Mass": "86,810,000,000,000,000,000,000,000 kg",
|
|
284
|
+
"Day": "17 hours 14 minutes",
|
|
285
|
+
"Year": "84 years"
|
|
286
|
+
},
|
|
287
|
+
|
|
288
|
+
"Neptune": {
|
|
289
|
+
"Distance from sun": "2.8 billion miles",
|
|
290
|
+
"Moons": 16,
|
|
291
|
+
"Mass": "102,400,000,000,000,000,000,000,000 kg",
|
|
292
|
+
"Day": "16 hours 6 minutes",
|
|
293
|
+
"Year": "164.8 years"
|
|
294
|
+
},
|
|
295
|
+
"Planet X" : {"Distance from sun": "???", "Moons": "???", "Mass": "???", "Day": "???", "Year": "???"}
|
|
296
|
+
}
|
|
297
|
+
if planet not in planets:
|
|
298
|
+
raise NonexistentPlanetError(f"{planet} is not a planet!")
|
|
299
|
+
cplanet = planets[planet]
|
|
300
|
+
if not info:
|
|
301
|
+
return(cplanet)
|
|
302
|
+
else:
|
|
303
|
+
return cplanet[info]
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def clamp(variable, top, bottom):
|
|
308
|
+
if variable > top:
|
|
309
|
+
return top
|
|
310
|
+
if variable < bottom:
|
|
311
|
+
return bottom
|
|
312
|
+
return variable
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def DistanceTranslate(amount, from_unit, to_unit):
|
|
317
|
+
units = {
|
|
318
|
+
"mm": 0.001,
|
|
319
|
+
"cm": 0.01,
|
|
320
|
+
"m": 1,
|
|
321
|
+
"km": 1000,
|
|
322
|
+
"mile": 1609.344,
|
|
323
|
+
"au": 149597870700,
|
|
324
|
+
"ly": 9460730472580800,
|
|
325
|
+
"o": 10000000000,
|
|
326
|
+
"oha": 590000000000
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
from_unit = from_unit.lower()
|
|
330
|
+
to_unit = to_unit.lower()
|
|
331
|
+
|
|
332
|
+
if from_unit not in units:
|
|
333
|
+
raise ValueError(f"Unknown distance unit: {from_unit}")
|
|
334
|
+
|
|
335
|
+
if to_unit not in units:
|
|
336
|
+
raise ValueError(f"Unknown distance unit: {to_unit}")
|
|
337
|
+
|
|
338
|
+
metres = amount * units[from_unit]
|
|
339
|
+
return metres / units[to_unit]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Pillow
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ExtraFunctions"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A collection of useful Python utility functions."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"Pillow"
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[tool.setuptools.packages.find]
|
|
16
|
+
where = ["."]
|