17

I am making a web app for practice, it is a job portal. I want to create 8 digit unique IDs for job which will be visible to the end user. Ids can have numbers and alphabets. Pattern needs to be 8 digits without hyphens or dashes like XXXXXXXX.

I know there is UUID thing in python. But they seem to generate the id in their specific format. Is there a way to get the id in my required format? If not can someone please guide me on how to go about it?

I know there is a similar thread How can I generate a unique ID in Python?; but it does not answer my question specifically. It did not answer what I was trying to achieve.

Thanks in advance.

All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
RyuCoder
  • 1,714
  • 2
  • 13
  • 21
  • shameless plug: use my coupon code generator at https://github.com/scardine/coupon-encode – Paulo Scardine Mar 09 '17 at 18:44
  • Possible duplicate of [How can I generate a unique ID in Python?](http://stackoverflow.com/questions/1210458/how-can-i-generate-a-unique-id-in-python) – Morishiri Mar 09 '17 at 18:45

3 Answers3

38

Well you could use uuid.hex

import uuid
uuid.uuid4().hex[:8]  # Might reduce uniqueness because of slicing

Or Django also has helper function get_random_string which accepts two parameters length (default=12) and allowed_chars:

from django.utils.crypto import get_random_string
get_random_string(8)
Aamir Rind
  • 38,793
  • 23
  • 126
  • 164
  • Might be worth mentioning that the uuid4 would only have alphabet characters a-f in it. Django's helper function would be a better choice. – wim Mar 09 '17 at 18:51
  • 1
    Using `.lower()` creates a bias, as now lowercase letters are twice more likely to occur than digits. It would be better to use the `allowed_chars` parameter if you only want lowercase+digits. – knbk Mar 09 '17 at 19:17
  • Django helper function did the trick I was looking for. Thanks. – RyuCoder Mar 10 '17 at 06:41
  • @knbk I have removed it now from the answer. Thanks! – Aamir Rind Jun 01 '20 at 15:13
7

Use os.urandom for the data, and base64 encode it;

In [1]: import os

In [2]: import base64

In [3]: base64.b64encode(os.urandom(6)).decode('ascii')
Out[3]: '6Amtry80'
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • This solved my use case! Though the Base64 alphabet table contains characters like "+" and "/" which are not safe for use in URLs and file system names, thus I used url-safe versions of the encode/decode functions instead: `base64.urlsafe_b64encode()` and `base64.urlsafe_b64decode()`. Ref: https://docs.python.org/3/library/base64.html – tnwei Sep 22 '21 at 02:30
2

TLDR: use hashids to convert one-to-one between sequential integers and random-looking strings

If you are creating a web app and need to map string ids like 'x4ua9fam' to some entry in a database, you want a robust one-to-one method of converting back and forth between an integer and the string id.

Instead of generating a random id each time and checking if it has been taken, you can assign the next available sequential integer (0, 1, 2, ...) to each new item and encode it into a unique string using hashids. When the user requests the string, you can decode it back into the integer and get your data.

rovyko
  • 4,068
  • 5
  • 32
  • 44