PChar(case=2, alpha=2)
: generate random characters, case{0: lowercase, 1: uppercase, 2: both}, alpha{0: alpha, 1: nonalpha, 2: both}
class PChar(RandomGenerator):
''' Generate characters randomly, PChar(case, alpha)
case = 0 , only lowercase
case = 1 , only uppercase
case = 2 , lower case and uppercase
alpha = 0, only alpha
alpha = 1, only nonalpha
alpha = 2, alpha + nonalpha'''
def __init__(self, case=2, alpha=2, **kwargs):
RandomGenerator.__init__(self, **kwargs)
self.case = case
self.alpha = alpha
self.init_random(**kwargs)
def func(self, index):
if self.alpha == 0:
charList = alpha
if self.alpha == 1:
charList = ''.join([x for x in nonalpha.keys()])
else: class PChar(RandomGenerator):
''' Generate characters randomly, PChar(case, alpha)
case = 0 , only lowercase
case = 1 , only uppercase
case = 2 , lower case and uppercase
alpha = 0, only alpha
alpha = 1, only nonalpha
alpha = 2, alpha + nonalpha'''
def __init__(self, case=2, alpha=2, **kwargs):
RandomGenerator.__init__(self, **kwargs)
self.case = case
self.alpha = alpha
self.init_random(**kwargs)
def func(self, index):
if self.alpha == 0:
charList = alpha
if self.alpha == 1:
charList = ''.join([x for x in nonalpha.keys()])
else:
charList = ''.join([x for x in nonalpha.keys()]) + alpha
if self.case == 0:
char = self.choice(charList)
elif self.case == 1:
char = self.choice(charList).upper()
else:
char = self.choice([self.choice(charList), self.choice(charList).upper()])
return char
charList = ''.join([x for x in nonalpha.keys()]) + alpha
if self.case == 0:
char = self.choice(charList)
elif self.case == 1:
char = self.choice(charList).upper()
else:
char = self.choice([self.choice(charList), self.choice(charList).upper()])
return char