3

I want to create a list of random user, wes a given number At the moment i'm able to get a list of random email. I'm trying to create an object users that will contains, n number given of users with random informations.

import random
import string
domains = ["gmail.com"]
letters = string.ascii_lowercase[:12]


def get_random_domain(domains):
    return random.choice(domains)


def get_random_name(letters, length):
    return ''.join(random.choice(letters) for i in range(length))


def generate_random_emails(nb, length):
    users = {}
    for j in range(nb):
        users = {
            'email':[get_random_name(letters, length) + '@' + get_random_domain(domains) for i in range(nb)],
            'username': get_random_name(letters, length),
            'Display_Name': get_random_name(letters, length)
        }

    return users


def main():
    print(generate_random_emails(4, 7))


if __name__ == "__main__":
    main()

And it's returning

{'email': ['[email protected]', '[email protected]', '[email protected]'], 'username': 'icliilf', 'DisplayName': 'efbdida'}

What I want to achieve is getting something like that

    {{'email':'[email protected]', 'username':'kgfadjb','DisplayName':'kgfadjb'},
    {'email':'[email protected]', 'username':'lifjekb','DisplayName':'lifjekb'},
    {'email':'[email protected]', 'username':'cckcbdh','DisplayName':'cckcbdh'}}

I know i'm doing something wrong with my loop inside

generate_random_emails

3 Answers 3

3

Try:

def generate_random_emails(nb, length):
    users = []
    for j in range(nb):
        users.append({
            'email': get_random_name(letters, length) + '@' + get_random_domain(domains),
            'username': get_random_name(letters, length),
            'Display_Name': get_random_name(letters, length)
        })

    return users

Explanations:

  1. You need to save the users created in the for loop. Currently, you only return the last created user. One solution is to define users as a list and append new users inside the loop.

  2. In the email definition, you do not need a for loop as you only want one email for a given user. You can directly set:

'email': get_random_name(letters, length) + '@' + get_random_domain(domains)

Full code

def generate_random_emails(nb, length):
    # Users output
    users = []
    # For desired number of users
    for j in range(nb):
        # Add one user with :
        #   - 1 email
        #   - 1 username
        #   - 1 display_name
        users.append({
            'email': get_random_name(letters, length) + '@' + get_random_domain(domains),
            'username': get_random_name(letters, length),
            'Display_Name': get_random_name(letters, length)
        })
    return users

print(generate_random_emails(4, 7))
# [{'email': '[email protected]', 'username': 'fjfgcfe', 'Display_Name': 'dliddfa'}, 
#  {'email': '[email protected]', 'username': 'bkjgdfe', 'Display_Name':'ceddfce'}, 
#  {'email': '[email protected]', 'username': 'jdekjcd', 'Display_Name': 'ijeffgi'},
#  {'email': '[email protected]', 'username': 'adgieki', 'Display_Name': 'idacgci'}]
Sign up to request clarification or add additional context in comments.

Comments

1

You are assigning new value to the same variable users in your for loop, instead to appending it to a list and then returning the list.

Consider this:

def generate_random_emails(nb, length):
    users = []
    for j in range(nb):
        user = {
            'email': get_random_name(letters, length) + '@' + get_random_domain(domains),
            'username': get_random_name(letters, length),
            'Display_Name': get_random_name(letters, length)
        }
        users.append(user)

    return users

You are also generating three random names for a single user. I don't know if that's your intent, but maybe you want to generate one random name and use it in email, username and Display_Name alike.

Comments

0

You can't get exactly this structure:

{{'email':'[email protected]', 'username':'kgfadjb','DisplayName':'kgfadjb'},
    {'email':'[email protected]', 'username':'lifjekb','DisplayName':'lifjekb'},
    {'email':'[email protected]', 'username':'cckcbdh','DisplayName':'cckcbdh'}}

because it is a set of dicts. Or would be, if dicts were hashable. Only hashable objects can be elements of a set. You can, however, have a list of dicts:

import random
import string
domains = ["gmail.com"]
letters = string.ascii_lowercase[:12] # why only a-k? but that's your choice

def get_random_domain(domains):
    return random.choice(domains)   

def get_random_name(letters, length):
    return ''.join(random.sample(letters, length))

def get_random_email(letters, length):
    return get_random_name(letters, length) + '@' + get_random_domain(domains)

def generate_random_users(nb, length):
    users = [
             {'email': get_random_email(letters, length)
              'username': get_random_name(letters, length),
              'Display_Name': get_random_name(letters, length)
             } for j in range(nb)
            ]
    return users

def main():
    print(generate_random_users(4, 7))

if __name__ == "__main__":
    main()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.