I'm struggling a bit to generate ID of type integer
for given string
in Python.
I thought the built-it hash
function is perfect but it appears that the IDs are too long sometimes. It's a problem since I'm limited to 64bits as maximum length.
My code so far: hash(s) % 10000000000
.
The input string(s) which I can expect will be in range of 12-512 chars long.
Requirements are:
I would be glad if someone can provide any tips / solutions.
The id() function returns a unique id for the specified object. All objects in Python has its own unique id. The id is assigned to the object when it is created.
uuid1() is defined in UUID library and helps to generate the random id using MAC address and time component. bytes : Returns id in form of 16 byte string. int : Returns id in form of 128-bit integer. hex : Returns random id as 32 character hexadecimal string.
You should create an instance of the Student class, then call it's method. just like user = Student() , then call user. enter_name() .
I would do something like this:
>>> import hashlib
>>> m = hashlib.md5()
>>> m.update("some string")
>>> str(int(m.hexdigest(), 16))[0:12]
'120665287271'
The idea:
If characters a-f
are also okay, I would do m.hexdigest()[0:12]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With