Aufgrund einer Störung des s3 Storage, könnten in nächster Zeit folgende GitLab Funktionen nicht zur Verfügung stehen: Container Registry, Job Artifacs,. Wir bitten um Verständnis. Es wird mit Hochdruck an der Behebung des Problems gearbeitet. Weitere Informationen zur Störung des Object Storage finden Sie hier:

Commit 8be5fa03 authored by LammLukas's avatar LammLukas
Browse files

Implemented tool to generate random RWTHOnline lists and deleted old example_data

parent 6298f061
This diff is collapsed.
Script to generate random user data in RWTHOnline .csv format
Author: L. Lamm (
from src.participant import Participant
from src.utils import write_csv
"""Generate list of participants"""
participant_list = Participant.generate_random_participant_list(50)
"""Arrange Data for .csv export"""
file_data = [['STUDY_PROGRAMME',
for participant in participant_list:
file_data.append(['Masterstudium - Musterstudiengang',
'9999 99 999',
'[3000] Musteringenieurwesen',
'Max Mustermann Dr.-Ing. Univ.-Prof.',
'BS I (2131|101), BS II (2131|102)',
str(participant.Lastname) + '(at)',
"""Export data to .csv file"""
write_csv('./data/example_inputdata.csv', file_data)
......@@ -7,6 +7,7 @@ from src.exportable import Exportable
from randomuser import RandomUser
from random import randint
class Participant(Exportable):
"""Class for object of type Participant"""
def __init__(self, firstname=None, lastname=None, matriculation=None, number_of_trials=None):
......@@ -28,7 +29,7 @@ class Participant(Exportable):
def generate_random_participant():
"""Generates a random participant using randomuser package"""
user = RandomUser()
user = RandomUser({'nat': 'us'})
participant = Participant(firstname=user.get_first_name(),
......@@ -4,12 +4,11 @@ Package providing various utilities for the Miraculix software
Author: L. Lamm (
import csv
import sqlite3
def write_csv(file, data):
"""Write data to .csv file"""
with open(file, "w+", newline='') as outfile:
with open(file, "w+", newline='', encoding='utf-8') as outfile:
file_writer = csv.writer(outfile, delimiter=';')
for row in data:
Metadata-Version: 1.1
Name: randomuser
Version: 1.5.1
Summary: Python class for generating random user data using API
Author: Connor de la Cruz
License: MIT
Description: Python Random User Generator
.. |pypi| image::
:alt: PyPI
.. |license| image::
:alt: PyPI - License
.. |github| image::
:alt: GitHub
Python class for generating random user data using
` <>`__ API.
Basic Usage
.. code:: python
from randomuser import RandomUser
# Generate a single user
user = RandomUser()
# Generate a list of 10 random users
user_list = RandomUser.generate_users(10)
Both ``RandomUser()`` and ``RandomUser.generate_users()`` can optionally
accept a dictionary of parameter names to desired values using the
``get_params`` optional parameter:
.. code:: python
# Generate a random female user
user = RandomUser({'gender': 'female'})
# Generate a list of 10 random users from Canada
user_list = RandomUser.generate_users(10, {'nat': 'ca'})
For more information on what parameters can be specified, see the
` documentation <>`__.
.. readme-only
Method Overview
For details on the RandomUser class and optional parameters for these
methods, see the
`documentation <>`__.
Getter Methods
- ``get_cell()``
- ``get_city()``
- ``get_dob()``
- ``get_email()``
- ``get_first_name()``
- ``get_full_name()``
- ``get_gender()``
- ``get_id()``
- ``get_id_number()``
- ``get_id_type()``
- ``get_info()``
- ``get_last_name()``
- ``get_login_md5()``
- ``get_login_salt()``
- ``get_login_sha1()``
- ``get_login_sha256()``
- ``get_nat()``
- ``get_password()``
- ``get_phone()``
- ``get_picture()``
- ``get_postcode()``
- ``get_registered()``
- ``get_state()``
- ``get_street()``
- ``get_username()``
- ``get_zipcode()``
Platform: UNKNOWN
\ No newline at end of file
# Class for generating random user information
# Imports
# ----------------------------------------------------------------
from urllib import request, error
from urllib.parse import urlencode
import json
import time
import re
# Global Variables
# ----------------------------------------------------------------
# Python module version
__version__ = '1.5.1'
# Version of the random user API
URL = '{}/'.format(API_VERSION)
# Classes
# ----------------------------------------------------------------
class RandomUser:
#: Dictionary where the random user data will be stored
_data = {}
#: Dictionary where info section of results will be stored
_info = {}
# Constants
class PictureSize:
"""Constants for size parameter in :meth:`RandomUser.get_picture()`"""
LARGE = 'large'
MEDIUM = 'medium'
THUMBNAIL = 'thumbnail'
class Info:
"""Constants for :attr:`RandomUser._info` dictionary keys"""
SEED = 'seed'
RESULTS = 'results'
PAGE = 'page'
VERSION = 'version'
# Exceptions
class APIError(Exception):
"""Exception to raise when the API query returns an error
Documentation on API errors:
def __init__(self, message):
' API returned an error: {}'.format(message)
# Functions
def __init__(self, get_params=None, user_data=None, api_info=None):
"""Initialize RandomUser object
:param get_params: (Optional) Dictionary mapping query parameter names
to their values. See for
details on parameters.
:param 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)
:param 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.
global URL
if user_data is not None:
self._data = user_data
self._info = api_info
self.request_url = URL
if get_params:
self.request_url += '?' + urlencode(get_params)
def _generate_user(self):
"""Query the API and store results in _data and _info"""
results = json.loads(request.urlopen(self.request_url).read())
if 'error' in results:
raise RandomUser.APIError(results['error'])
self._data = results['results'][0]
self._info = results['info']
# Personal Info
# --------------------------------
def get_first_name(self, capitalize=True):
"""Returns first name
:param capitalize: (Default = True) Capitalize first letter if True
first_name = self._data['name']['first']
return first_name.title() if capitalize else first_name
def get_last_name(self, capitalize=True):
"""Returns last name
:param capitalize: (Default = True) Capitalize first letter if True
last_name = self._data['name']['last']
return last_name.title() if capitalize else last_name
def get_full_name(self, capitalize=True):
"""Returns first and last name separated by a space
:param capitalize: (Default = True) Capitalize first letter of each
name if True
first_name = self.get_first_name(capitalize)
last_name = self.get_last_name(capitalize)
full_name = '{} {}'.format(first_name, last_name)
return full_name
def get_gender(self):
"""Returns gender"""
return self._data['gender']
def get_dob(self, parse_time=False):
"""Returns date of birth as a string in the format '%Y-%m-%dT%H:%M:%SZ'
(ISO 8601 standard)
:param parse_time: (Default = False) If True, parse date of birth
string using time.strptime() and return the results instead of a
dob = self._data['dob']['date']
if parse_time:
dob = self._parse_time(dob)
return dob
def get_age(self):
"""Returns the age of the user"""
return self._data['dob']['age']
def get_nat(self):
"""Returns nationality"""
return self._data['nat']
# Location
# --------------------------------
def get_street(self, capitalize=True):
"""Returns street address
:param capitalize: (Default = True) Capitalize first letter of words if
street = self._data['location']['street']
return street.title() if capitalize else street
def get_city(self, capitalize=True):
"""Returns city
:param capitalize: (Default = True) Capitalize first letter of words if
city = self._data['location']['city']
return city.title() if capitalize else city
def get_state(self, capitalize=True):
"""Returns state
:param capitalize: (Default = True) Capitalize first letter of words if
state = self._data['location']['state']
return state.title() if capitalize else state
def get_postcode(self):
"""Returns post code"""
return self._data['location']['postcode']
def get_zipcode(self):
""" Returns zip code (wrapper for get_postcode())"""
return self.get_postcode()
def get_coordinates(self):
"""Returns a dictionary with keys 'longitude' and 'latitude' mapped to
their respective values
return self._data['location']['coordinates']
# TODO: def get_timezone(self)
# Contact
# --------------------------------
def _format_phone_number(self, phone_string,
strip_parentheses=True, strip_hyphens=True):
"""Takes a string representation of a phone number and strips
characters based on parameter values
:param phone_string: The phone number as a string
:param strip_parentheses: (Default = True) Remove '(' and ')'
characters if True
:param strip_hyphens: (Default = True) Remove '-' characters if True
if strip_parentheses:
phone_string = re.sub('[()]', '', phone_string)
if strip_hyphens:
phone_string = re.sub('-', '', phone_string)
return phone_string
def get_phone(self, strip_parentheses=False, strip_hyphens=False):
"""Returns phone number as a string in the format '(###)-###-####'
:param strip_parentheses: (Default = False) Omit parentheses if True
:param strip_hyphens: (Default = False) Omit hyphens if True
return self._format_phone_number(self._data['phone'],
def get_cell(self, strip_parentheses=False, strip_hyphens=False):
"""Returns cell phone number as a string in the format '(###)-###-####'
:param strip_parentheses: (Default = False) Omit parentheses if True
:param strip_hyphens: (Default = False) Omit hyphens if True
return self._format_phone_number(self._data['cell'],
def get_email(self):
"""Returns email address"""
return self._data['email']
# Login
# --------------------------------
def get_username(self):
"""Returns username"""
return self._data['login']['username']
def get_password(self):
"""Returns password"""
return self._data['login']['password']
def get_registered(self, parse_time=False):
"""Returns registration date as a string in the format
'%Y-%m-%dT%H:%M:%SZ' (ISO 8601 standard)
:param parse_time: (Default = False) If True, parse date string using
time.strptime() and return the results instead of a string
registered = self._data['registered']['date']
if parse_time:
registered = self._parse_time(registered)
return registered
def get_registered_age(self):
"""Returns the age (in years) since registration date"""
return self._data['registered']['age']
def get_login_salt(self):
"""Returns user login salt"""
return self._data['login']['salt']
def get_login_md5(self):
"""Returns user login md5"""
return self._data['login']['md5']
def get_login_sha1(self):
"""Returns user login sha1"""
return self._data['login']['sha1']
def get_login_sha256(self):
"""Returns user login sha256"""
return self._data['login']['sha256']
def get_login_uuid(self):
"""Returns user login uuid"""
return self._data['login']['sha256']
# ID
# --------------------------------
def get_id_type(self):
"""Returns the ID type"""
return self._data['id']['name']
def get_id_number(self):
"""Returns the ID number"""
return self._data['id']['value']
def get_id(self):
"""Returns a dictionary mapping 'type' to ID type and 'number' to ID
return {'type': self.get_id_type(), 'number': self.get_id_number()}
# Misc
# --------------------------------
def get_picture(self, size=PictureSize.LARGE):
"""Returns url to a .jpg of the generated user
:param size: (Default = :attr:`PictureSize.LARGE`) The size of picture
to return the url for. Size values are stored as constants in
:class:`PictureSize` nested class.
return self._data['picture'][size]
def get_info(self):
"""Returns a dictionary with information about the API query
Keys for the info dictionary are stored as constants in :class:`Info`
nested class.
return self._info
# Helper Functions
# --------------------------------
def _parse_time(self, date_string):
"""Parses the date string format returned by the API and returns the
time tuple result of time.strptime()
:param date_string: The date string in the format '%Y-%m-%dT%H:%M:%SZ'
date_format = '%Y-%m-%dT%H:%M:%SZ'
return time.strptime(date_string, date_format)
# Static Methods
# --------------------------------
def generate_users(amount, get_params=None):
"""Returns a list containing the specified amount of randomly generated
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.
:param amount: The number of users to generate.
:param get_params: (Optional) Dictionary mapping query parameter names
to their values. See for
details on parameters.
global URL
if get_params is None:
get_params = {}
# Max amount allowed is 5,000
# (
get_params['results'] = amount if amount <= 5000 else 5000
request_url = URL + '?' + urlencode(get_params)
results = json.loads(request.urlopen(request_url).read())
if 'error' in results:
raise RandomUser.APIError(results['error'])
info = results['info']
users = []
for user_data in results['results']:
user = RandomUser(user_data=user_data, api_info=info)
return users
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment