Skip to content

Reference

Roger Chen edited this page Jan 14, 2016 · 10 revisions

Useful Python functions

Generate a list of class logins

import string
import itertools

def generate_login_list(num, minimum_length=2):
    """
    Generates a list of class logins, starting with 2-letter logins. Does not include logins that
    start with 't' or 'r'.

    >>> generate_login_list(3)
    ['aa', 'ab', 'ac']

    """
    accounts = []
    alph = string.lowercase
    non_reserved = lambda account: account[0] not in ('t', 'r')
    for length in itertools.count(minimum_length):
        accounts.extend(map(''.join, filter(non_reserved, itertools.product(alph, repeat=length))))
        if len(accounts) >= num:
            return accounts[:num]

Clone this wiki locally