def generate(amt: int) -> None : rs = Rstr(SystemRandom()) codes = dict() if os.path.exists(codes_path) : with open(codes_path, 'r') as fp : codes = json.load(fp)['codes'] count = 0 new_codes = [] while count < amt : cd = rs.xeger(pattern) if cd not in codes : new_codes.append(cd) codes[cd] = True count += 1 # separate JSON of most recently generated codes with open(os.path.join(path, 'obj/new-codes.json'), 'w') as fp : json.dump({'codes' : {cd : True for cd in new_codes}}, fp, indent=INDENT) else : codes = {rs.xeger(pattern) : True for _ in range(amt)} with open(codes_path, 'w') as fp : json.dump({'codes' : codes}, fp, indent=INDENT)
from contextlib import contextmanager import errno import os import re from random import SystemRandom import tempfile from rstr import Rstr from ._compat import which rstr = Rstr(SystemRandom()) import_module = __import__ def genpass(pattern=r'[\w]{32}'): """generates a password with random chararcters """ try: return rstr.xeger(pattern) except re.error as e: raise ValueError(str(e)) @contextmanager def mkdir_open(path, mode="r"): try: dir_path = os.path.dirname(path) os.makedirs(dir_path)
def text_from_regex(draw, regexp): random = draw(st.randoms()) return Rstr(random).xeger(regexp)
def __init__(self, regex, seed=None): self.gen = Rstr(Random(seed)) self.regex = regex
def setUp(self) -> None: self.rs = Rstr()
def gen_access_key(self): rs = Rstr(SystemRandom()) self.access_key = rs.xeger(r'[A-Z]\d[A-Z]-\d[A-Z]\d') return self.access_key
def setUp(self) -> None: self.rs = Rstr(random.SystemRandom())
def test_add_alphabet(self) -> None: rs = Rstr() rs.add_alphabet('evens', '02468') assert_matches('^[02468]{1,10}$', rs.evens())
def test_alphabet_at_instantiation(self) -> None: rs = Rstr(vowels='AEIOU') assert_matches('^[AEIOU]{1,10}$', rs.vowels())