pyroclastTech's profile picture

Published by

published
updated

Category: Web, HTML, Tech

{code} what code do i use to make blog ID's?

i use a very simple piece of code to make the identifiers you see at the bottom of the page. eg. kkkkkk-4444. its used so that when the article goes ive it can be found by the identifier just in case theres multiple with the same name. here it is as raw python three.

=====================================

import random

import string


def generate_unique_code():

    # Generate a random 6-figure number

    numbers = ''.join(str(random.randint(0, 9)) for _ in range(6))

    

    # Generate a random 5-letter string

    letters = ''.join(random.choices(string.ascii_uppercase, k=5))


    # Combine numbers and letters with a dash

    code = f"{numbers}-{letters}"


    # Check if the code has been generated before

    with open("generated_codes.txt", "r") as file:

        existing_codes = file.read().splitlines()


    while code in existing_codes:

        # Regenerate numbers and letters if code exists

        numbers = ''.join(str(random.randint(0, 9)) for _ in range(6))

        letters = ''.join(random.choices(string.ascii_uppercase, k=5))

        code = f"{numbers}-{letters}"


    # Save the code to the file

    with open("generated_codes.txt", "a") as file:

        file.write(code + "\n")


    return code


def main():

    # Generate and print a unique code

    unique_code = generate_unique_code()

    print("Unique Code:", unique_code)


if __name__ == "__main__":

    main()

=====================================

this generates a random code with 5 capital letters and 6 numbers separated with a dash, then it checks it against generate_codes.txt to see if its been used before, if not it gets saved. this allows every blog or article to be found via a unique code along with the title.


blog identifier: 143945-KYXFU


0 Kudos

Comments

Displaying 0 of 0 comments ( View all | Add Comment )