r/learnpython • • 16d ago

Rate my customer/employee id generation logic

def generate_id(connection_tuple, id_user = "customer"):
    if id_user.lower() == "customer":
        table = "customer"
        id_string = "cust"
    elif id_user.lower() == "employee":
        table = "employees"
        id_string = "emp"
    cursor = connection_tuple[1]
    connection = connection_tuple[0]
    try_cursor_execute(f"select count(*) form {table}")
    last_person = cursor.fetchone()[0]
    current_person = last_person+1
    no_of_zeros = 6 - len(str(current_person))
    id = id_string + "0"*no_of_zeros + str(current_perosn)
    return id

I have to build a hotel database system thing for my 12th grade Computer Science project.. This is the customer id / employee id generation logic I came up with. Employee IDs are 9 characters, and customer IDs are 10 characters. But this thing seems sort of janky. Is there a better way to do this?

try_cursor_execute() is a function I defined, it is cursor.execute but in a try block. connection tuple is (connection, cursor)

6 Upvotes

21 comments sorted by

View all comments

Show parent comment

1

u/pachura3 16d ago

So why do you bother asking:

Is there a better way to do this?

0

u/MooseNew4887 16d ago

I want to use that way when I build a real system 🙂

1

u/desrtfx but other languages pro 16d ago

I want to use that way when I build a real system

...and yet, you are contesting every single recommendation