Skip to content

Instantly share code, notes, and snippets.

@saurabh2590
Last active March 31, 2023 07:10
Show Gist options
  • Save saurabh2590/f9cce4cc8c7cf101f2eea7d6393565bb to your computer and use it in GitHub Desktop.
Save saurabh2590/f9cce4cc8c7cf101f2eea7d6393565bb to your computer and use it in GitHub Desktop.
Names Generating logic in GLU

Generate name logic

Inputs

  • We have two defined arrays for names, first part and last part. And theu are localised for country and languag. Please find example from DE.
"usernames": {
                "part1": "Adler\nAffen\nAmeisen\nAmsel\nAustern\nBienen\nBison\nDachs\nDelfin\nDino\nDrossel\nEinhorn\nElch\nElefanten\nElster\nEnten\nEulen\nFalken\nFisch\nFlamingo\nFliegen\nFloh\nForellen\nFrosch\nFuchs\nGiraffen\nGorilla\nHahnen\nHamster\nHasen\nHecht\nHirsch\nHummer\nIgel\nJaguar\nKatzen\nKoala\nKobra\nKorallen\nKrokodil\nLachs\nLama\nLemming\nLeoparden\nLuchs\nMaulwurfs\nMeisen\nNashorn\nOtter\nPanda\nPapageien\nPelikan\nPferde\nPinguin\nPuma\nQuallen\nRaben\nReh\nRobben\nSchafs\nSchnecken\nSchwalben\nSpatzen\nSpecht\nSpinnen\nStorchen\nTauben\nTiger\nUhu\nVogel\nWal\nWespen\nWolfs\nZebra", 
                "part2": "Acker\nAllee\nBach\nBerg\nBrunnen\nBurg\nChaussee\nDamm\nDorf\nFeld\nFelsen\nFlur\nForst\nGarten\nGasse\nGrund\nHaus\nHausen\nHeim\nHof\nInsel\nKoppel\nLand\nNest\nPark\nPlatz\nRing\nSee\nStadt\nSteg\nSteig\nStra\u00dfe\nSumpf\nTal\nTeich\nTor\nUfer\nWald\nWall\nWeg\nWiese"
            }

Logic

  • Based on these part 1 and part 2 names as input we have the following function to generate unique names.
  • The function is called N times from the request orignator to return N unique names.
  • We try 1000 times to find a unique combination before aborting the request.
  • Every generation hits DB and do not have good performance for requests for large number of unique names.
def generate_unique_username(
    usernames_pt1: List[str], usernames_pt2: List[str], system: System
) -> str:
    """
    generating a username consisting of 4 parts:
    :param usernames_pt1: 1st part of the username (list of names coming from the resourceCMS)
    :param usernames_pt2: 2nd part of the username (list of names coming from the resourceCMS)
    :param system:

    3rd part of the username: random number (as specified below)
    4th part of the username: random letter (as specified below)
    :return: username
    """

    LETTERS_POOL = list("abcdefghjkmnpqrstuvwxyz")

    PART1_LENGTH = len(usernames_pt1)
    PART2_LENGTH = len(usernames_pt2)
    PART3_LENGTH = 199
    PART4_LENGTH = len(LETTERS_POOL)

    tries = 0

    while tries < 1000:
        part1 = usernames_pt1[randint(0, PART1_LENGTH - 1)]
        part2 = usernames_pt2[randint(0, PART2_LENGTH - 1)]
        part3 = str(randint(0, PART3_LENGTH - 1))
        part4 = LETTERS_POOL[randint(0, PART4_LENGTH - 1)]
        username = f"{part1}{part2}{part3}{part4}"

        try:
            tries += 1
            # We try to get user user with the username and when it doesn't exist return the value.
            User.objects.for_system(system).get(username=username)
        except User.DoesNotExist:
            return username

    # could not find a not existing username within max_tries of 1000
    raise RequestAborted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment