RandomUser class¶
-
class
randomuser.
RandomUser
(get_params=None, user_data=None, api_info=None)[source]¶ -
__init__
(get_params=None, user_data=None, api_info=None)[source]¶ Initialize RandomUser object
- Parameters
get_params – (Optional) Dictionary mapping query parameter names to their values. See https://randomuser.me/documentation for details on parameters.
user_data – (Optional) If specified, this _data will be used instead of querying the API for user _data. Use in instances where the user _data has already been generated (e.g. restoring user _data, creating multiple users with single call to API using the ‘results’ parameter)
api_info – (Optional) If the user is being generated with the user_data parameter, the _info variable will be set to this. Otherwise, it will be ignored when generating a random user.
-
class
PictureSize
[source]¶ Constants for size parameter in
RandomUser.get_picture()
-
LARGE
= 'large'¶
-
MEDIUM
= 'medium'¶
-
THUMBNAIL
= 'thumbnail'¶
-
-
class
Info
[source]¶ Constants for
RandomUser._info
dictionary keys-
SEED
= 'seed'¶
-
RESULTS
= 'results'¶
-
PAGE
= 'page'¶
-
VERSION
= 'version'¶
-
-
exception
APIError
(message)[source]¶ Exception to raise when the API query returns an error
Documentation on API errors: https://randomuser.me/documentation#errors
-
_data
= {}¶ Dictionary where the random user data will be stored
-
_info
= {}¶ Dictionary where info section of results will be stored
-
get_first_name
(capitalize=True)[source]¶ Returns first name
- Parameters
capitalize – (Default = True) Capitalize first letter if True
-
get_last_name
(capitalize=True)[source]¶ Returns last name
- Parameters
capitalize – (Default = True) Capitalize first letter if True
-
get_full_name
(capitalize=True)[source]¶ Returns first and last name separated by a space
- Parameters
capitalize – (Default = True) Capitalize first letter of each name if True
-
get_dob
(parse_time=False)[source]¶ Returns date of birth as a string in the format ‘%Y-%m-%dT%H:%M:%SZ’ (ISO 8601 standard)
- Parameters
parse_time – (Default = False) If True, parse date of birth string using time.strptime() and return the results instead of a string
-
get_street
(capitalize=True, split_number_name=False)[source]¶ Returns street address
- Parameters
capitalize – (Default = True) Capitalize first letter of words if True
split_number_name – (Default = False) If False, return the full street address as a string. If True, return a dictionary with keys ‘number’ and ‘name’ containing the street number and name, respectively
-
get_city
(capitalize=True)[source]¶ Returns city
- Parameters
capitalize – (Default = True) Capitalize first letter of words if True
-
get_state
(capitalize=True)[source]¶ Returns state
- Parameters
capitalize – (Default = True) Capitalize first letter of words if True
-
get_country
(capitalize=True)[source]¶ Returns country
- Parameters
capitalize – (Default = True) Capitalize first letter of words if True
-
get_coordinates
()[source]¶ Returns a dictionary with keys ‘longitude’ and ‘latitude’ mapped to their respective values
-
_format_phone_number
(phone_string, strip_parentheses=True, strip_hyphens=True)[source]¶ Takes a string representation of a phone number and strips characters based on parameter values
- Parameters
phone_string – The phone number as a string
strip_parentheses – (Default = True) Remove ‘(‘ and ‘)’ characters if True
strip_hyphens – (Default = True) Remove ‘-‘ characters if True
-
get_phone
(strip_parentheses=False, strip_hyphens=False)[source]¶ Returns phone number as a string in the format ‘(###)-###-####’
- Parameters
strip_parentheses – (Default = False) Omit parentheses if True
strip_hyphens – (Default = False) Omit hyphens if True
-
get_cell
(strip_parentheses=False, strip_hyphens=False)[source]¶ Returns cell phone number as a string in the format ‘(###)-###-####’
- Parameters
strip_parentheses – (Default = False) Omit parentheses if True
strip_hyphens – (Default = False) Omit hyphens if True
-
get_registered
(parse_time=False)[source]¶ Returns registration date as a string in the format ‘%Y-%m-%dT%H:%M:%SZ’ (ISO 8601 standard)
- Parameters
parse_time – (Default = False) If True, parse date string using time.strptime() and return the results instead of a string
-
get_picture
(size='large')[source]¶ Returns url to a .jpg of the generated user
- Parameters
size – (Default =
PictureSize.LARGE
) The size of picture to return the url for. Size values are stored as constants inPictureSize
nested class.
-
get_info
()[source]¶ Returns a dictionary with information about the API query
Keys for the info dictionary are stored as constants in
Info
nested class.
-
_parse_time
(date_string)[source]¶ Parses the date string format returned by the API and returns the time tuple result of time.strptime()
- Parameters
date_string – The date string in the format ‘%Y-%m-%dT%H:%M:%SZ’
-
static
generate_users
(amount, get_params=None)[source]¶ Returns a list containing the specified amount of randomly generated users.
The Random User Generator API can generate multiple users in a single query instead of connecting once for each user and increasing load on both ends.
- Parameters
amount – The number of users to generate.
get_params – (Optional) Dictionary mapping query parameter names to their values. See https://randomuser.me/documentation for details on parameters.
-