-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomisedString.py
More file actions
103 lines (92 loc) · 4.21 KB
/
randomisedString.py
File metadata and controls
103 lines (92 loc) · 4.21 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
__version__ = "1.4.1"
__packagename__ = "randomisedstring"
def updatePackage():
from time import sleep
from json import loads
import http.client
print(f"Checking updates for Package {__packagename__}")
try:
host = "pypi.org"
conn = http.client.HTTPSConnection(host, 443)
conn.request("GET", f"/pypi/{__packagename__}/json")
data = loads(conn.getresponse().read())
latest = data['info']['version']
if latest != __version__:
try:
import subprocess
from pip._internal.utils.entrypoints import (
get_best_invocation_for_this_pip,
get_best_invocation_for_this_python,
)
from pip._internal.utils.compat import WINDOWS
if WINDOWS:
pip_cmd = f"{get_best_invocation_for_this_python()} -m pip"
else:
pip_cmd = get_best_invocation_for_this_pip()
subprocess.run(f"{pip_cmd} install {__packagename__} --upgrade")
print(f"\nUpdated package {__packagename__} v{__version__} to v{latest}\nPlease restart the program for changes to take effect")
sleep(3)
except:
print(f"\nFailed to update package {__packagename__} v{__version__} (Latest: v{latest})\nPlease consider using pip install {__packagename__} --upgrade")
sleep(3)
else:
print(f"Package {__packagename__} already the latest version")
except:
print(f"Ignoring version check for {__packagename__} (Failed)")
class Imports:
from secrets import choice, randbelow
class RandomisedString:
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(RandomisedString, cls).__new__(cls)
return cls._instance
def __init__(self):
"""
Initialize the Generator and use the public functions to generate a randomized string.
"""
self.LOWER_CASE_ASCIIS = list(range(97, 122 + 1))
self.UPPER_CASE_ASCIIS = list(range(65, 90 + 1))
self.NUMBER_ASCIIS = list(range(48, 57 + 1))
self.ALPHANUMERIC_ASCIIS = self.LOWER_CASE_ASCIIS + self.UPPER_CASE_ASCIIS + self.NUMBER_ASCIIS
def AlphaNumeric(self, _min=10, _max=20) -> str:
"""
Generates a string with numbers and alphabets(a-z, A-Z, 0-9)
:param _min: Minimum possible length of generated string
:param _max: Maximum possible length of generated string
:return: A random string of the specified size
"""
_minLength = min(_min, _max)
_maxLength = max(_min, _max)
if _maxLength == _minLength:
_maxLength += 1
length = Imports.randbelow(_maxLength - _minLength) + _minLength
string = ''.join(chr(Imports.choice(self.ALPHANUMERIC_ASCIIS)) for _ in range(length))
return string
def OnlyNumeric(self, _min=10, _max=20) -> str:
"""
Generates a string with only numbers(0-9). Convert the string to int if needed
:param _min: Minimum possible length of generated string
:param _max: Maximum possible length of generated string
:return: A random string of the specified size
"""
_minLength = min(_min, _max)
_maxLength = max(_min, _max)
if _maxLength == _minLength:
_maxLength += 1
length = Imports.randbelow(_maxLength - _minLength) + _minLength
string = ''.join(chr(Imports.choice(self.NUMBER_ASCIIS)) for _ in range(length))
return string
def OnlyAlpha(self, _min=10, _max=20) -> str:
"""
Generates a string with only Alphabets(a-z, A-Z)
:param _min: Minimum possible length of generated string
:param _max: Maximum possible length of generated string
:return: A random string of the specified size
"""
_minLength = min(_min, _max)
_maxLength = max(_min, _max)
if _maxLength == _minLength: _maxLength += 1
length = Imports.randbelow(_maxLength - _minLength) + _minLength
string = ''.join(chr(Imports.choice(self.LOWER_CASE_ASCIIS + self.UPPER_CASE_ASCIIS)) for _ in range(length))
return string