Example #1
0
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)
Example #2
0
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)
Example #3
0
def text_from_regex(draw, regexp):
    random = draw(st.randoms())
    return Rstr(random).xeger(regexp)
Example #4
0
 def __init__(self, regex, seed=None):
     self.gen = Rstr(Random(seed))
     self.regex = regex
Example #5
0
 def setUp(self) -> None:
     self.rs = Rstr()
Example #6
0
 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
Example #7
0
 def setUp(self) -> None:
     self.rs = Rstr(random.SystemRandom())
Example #8
0
 def test_add_alphabet(self) -> None:
     rs = Rstr()
     rs.add_alphabet('evens', '02468')
     assert_matches('^[02468]{1,10}$', rs.evens())
Example #9
0
 def test_alphabet_at_instantiation(self) -> None:
     rs = Rstr(vowels='AEIOU')
     assert_matches('^[AEIOU]{1,10}$', rs.vowels())