Commit d4f77b12 authored by Jost Rossel's avatar Jost Rossel
Browse files

Add random generator utils

parent 54b1ea55
import random
import string
class RandomCharacterGenerator:
def __init__(self, string_length_between=(3, 10)) -> None:
self.minmax = string_length_between
self.alnum = string.ascii_letters + string.digits
self.ascii_values = string.ascii_letters + string.digits + string.punctuation
self.utf8_values = [
chr(i) for i in range(32, 0x110000) if chr(i).isprintable() and chr(i) not in "<&>\"'/"
]
def random_string(self):
func = random.choice(
[
self.random_utf8_string,
self.random_alnum_string,
self.random_alpha_string,
]
)
return func()
def random_utf8_char(self):
return random.choice(self.utf8_values)
def random_utf8_string(self):
return "".join(random.choices(self.utf8_values, k=random.randint(*self.minmax)))
def random_ascii_char(self):
return random.choice(self.ascii_values)
def random_ascii_string(self):
return "".join(random.choices(self.ascii_values, k=random.randint(*self.minmax)))
def random_digit_char(self):
return random.choice(string.digits)
def random_digit_string(self):
return "".join(random.choices(string.digits, k=random.randint(*self.minmax)))
def random_alpha_char(self):
return random.choice(string.ascii_letters)
def random_alpha_string(self):
return "".join(random.choices(string.ascii_letters, k=random.randint(*self.minmax)))
def random_alnum_char(self):
return random.choice(self.alnum)
def random_alnum_string(self):
return "".join(random.choices(self.alnum, k=random.randint(*self.minmax)))
def switch_random_character(rcg: RandomCharacterGenerator, in_str: str) -> str:
"""Switches a random character(s) in a given string.
If the value is a number it returns a number (but also as string).
"""
try:
number = float(in_str)
number += random.randint(-100000, 100000)
return str(number)
except ValueError:
pass
index = random.randint(0, len(in_str) - 1)
return in_str[:index] + rcg.random_alnum_char() + in_str[index + 1 :]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment