def firefox(cls): ver = ( 'Gecko/{0} Firefox/{1}.0'.format( DatetimeProvider.date_time_between( datetime(2011, 1, 1)), random.randint(4, 15)), 'Gecko/{0} Firefox/3.6.{1}'.format( DatetimeProvider.date_time_between( datetime(2010, 1, 1)), random.randint(1, 20)), 'Gecko/{0} Firefox/3.8'.format( DatetimeProvider.date_time_between( datetime(2010, 1, 1)), ), ) tmplt_win = '({0}; {1}; rv:1.9.{2}.20) {3}' tmplt_lin = '({0}; rv:1.9.{1}.20) {2}' tmplt_mac = '({0}; rv:1.9.{1}.20) {2}' platforms = ( tmplt_win.format(cls.windows_platform_token(), cls.random_element(cls.langs), random.randint(0, 2), random.choice(ver)), tmplt_lin.format(cls.linux_platform_token(), random.randint(5, 7), random.choice(ver)), tmplt_mac.format(cls.mac_platform_token(), random.randint(2, 6), random.choice(ver)), ) return 'Mozilla/5.0 ' + cls.random_element(platforms)
def ssn(cls): """ Returns 11 character Estonian personal identity code (isikukood, IK). Age of person is between 16 and 90 years, based on local computer date. This function assigns random sex to person. An Estonian Personal identification code consists of 11 digits, generally given without any whitespace or other delimiters. The form is GYYMMDDSSSC, where G shows sex and century of birth (odd number male, even number female, 1-2 19th century, 3-4 20th century, 5-6 21st century), SSS is a serial number separating persons born on the same date and C a checksum. https://en.wikipedia.org/wiki/National_identification_number#Estonia """ age = datetime.timedelta(days=random.randrange(Provider.min_age, Provider.max_age)) birthday = datetime.date.today() - age if birthday.year < 2000: ik = random.choice(('3', '4')) elif birthday.year < 2100: ik = random.choice(('5', '6')) else: ik = random.choice(('7', '8')) ik += "%02d%02d%02d" % ((birthday.year % 100), birthday.month, birthday.day) ik += str(random.randrange(0, 999)).zfill(3) return ik + str(checksum([int(ch) for ch in ik]))
def zipcode(cls, state=None, n=1): if not state: if n == 1: return "%05d" % random.randint(501, 99950) ["%05d" % random.randint(501, 99950) for _ in range(n)] else: if n == 1: # the zip code table only contains the first 3 digits of the code, # so we'll make up the remaining two try: return random.choice(cls.state_abbr_to_zipcodes[state]) + cls.random_digits(2) except: raise Exception('Invalid state "{0}"'.format(state)) # if n>1, state will contain an array of states return [random.choice(cls.state_abbr_to_zipcodes[s]) + cls.random_digits(2) for s in state]
def password(cls, length=10, special_chars=True, digits=True, upper_case=True, lower_case=True): """ Generates a random password. @param length: Integer. Length of a password @param special_chars: Boolean. Whether to use special characters !@#$%^&*()_+ @param digits: Boolean. Whether to use digits @param upper_case: Boolean. Whether to use upper letters @param lower_case: Boolean. Whether to use lower letters @return: String. Random password """ choices = "" required_tokens = [] if special_chars: required_tokens.append(random.choice("!@#$%^&*()_+")) choices += "!@#$%^&*()_+" if digits: required_tokens.append(random.choice(string.digits)) choices += string.digits if upper_case: required_tokens.append(random.choice(string.ascii_uppercase)) choices += string.ascii_uppercase if lower_case: required_tokens.append(random.choice(string.ascii_lowercase)) choices += string.ascii_lowercase assert len(required_tokens) <= length, "Required length is shorter than required characters" # Generate a first version of the password chars = [random.choice(choices) for x in range(length)] # Pick some unique locations random_indexes = set() while len(random_indexes) < len(required_tokens): random_indexes.add(random.randint(0, len(chars) - 1)) # Replace them with the required characters for i, index in enumerate(random_indexes): chars[index] = required_tokens[i] return ''.join(chars)
def password(cls, length=10, special_chars=True, digits=True, upper_case=True, lower_case=True): """ Generates a random password. @param length: Integer. Length of a password @param special_chars: Boolean. Whether to use special characters !@#$%^&*()_+ @param digits: Boolean. Whether to use digits @param upper_case: Boolean. Whether to use upper letters @param lower_case: Boolean. Whether to use lower letters @return: String. Random password """ chars = "" if special_chars: chars += "!@#$%^&*()_+" if digits: chars += string.digits if upper_case: chars += string.ascii_uppercase if lower_case: chars += string.ascii_lowercase return ''.join(random.choice(chars) for x in range(length))
def random_element(cls, elements=('a', 'b', 'b')): """ Returns a random element from a passed object. If `elements` is a dictionary, the value will be used as a weighting element. For example:: random_element({"{{variable_1}}": 0.5, "{{variable_2}}": 0.2, "{{variable_3}}": 0.2, "{{variable_4}}": 0.1}) will have the following distribution: * `variable_1`: 50% probability * `variable_2`: 20% probability * `variable_3`: 20% probability * `variable_4`: 10% probability """ if isinstance(elements, dict): choices = elements.keys() probabilities = elements.values() return choice_distribution(list(choices), list(probabilities)) else: return random.choice(list(elements))
def timezone(cls): return random.choice(cls.random_element(cls.countries)['timezones'])
def random_letter(cls): """Returns a random letter (between a-z and A-Z).""" return random.choice(getattr(string, 'letters', string.ascii_letters))
def gen_id() -> str: return ''.join([random.choice(string.hexdigits) for x in range(12)])
def node(self): return random.choice(self._nodes)