Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reproduce uuid from java code in python

Tags:

java

python

uuid

In a Java application files are created where the filename is a UUID generated from a protein sequence (e.g. TTCCPSIVARSNFNVCRLPGTPEAICATYTGCIIIPGATCPGDYAN) created using the function UUID.nameUUIDFromBytes. This results in the UUID c6a0deb5-0c4f-3961-9d19-3f0fde0517c2.

UUID.namedUUIDFromBytes doesn't take a namespace as a parameter, whereas in python uuid.uuid3 does. According to What namespace does the JDK use to generate a UUID with nameUUIDFromBytes?, the namespace should have been passed as part of the name, but it's no longer possible to change the java code.

Is there a way to create a custom namespace in the python code such that it will produce the same UUID's as the Java code?

like image 875
Jon Avatar asked Jan 14 '15 09:01

Jon


People also ask

Can you generate UUID from string?

The fromString() method of UUID class in Java is used for the creation of UUID from the standard string representation of the same. Parameters: The method takes one parameter UUID_name which is the string representation of the UUID. Return Value: The method returns the actual UUID created from the specified string.

How do you generate multiple UUIDs in Python?

This module provides immutable UUID objects (the UUID class) and the functions uuid1() , uuid3() , uuid4() , uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified in RFC 4122. If all you want is a unique ID, you should probably call uuid1() or uuid4() .

Is UUID built in Python?

UUID, Universal Unique Identifier, is a python library which helps in generating random objects of 128 bits as ids. It provides the uniqueness as it generates ids on the basis of time, Computer hardware (MAC etc.).


1 Answers

nameUUIDFromBytes only takes one parameter, which is supposed to be the concatenation of the namespace and name just like you say. The namespace parameter is supposed to be a UUID, and as far as I know, they don't have a null value defined.

A "null uuid" can be passed to Python's uuid3 like this. This should work as long as the namespace has a bytes attribute (tested with Python 2 and 3):

class NULL_NAMESPACE:
    bytes = b''
uuid.uuid3(NULL_NAMESPACE, 'TTCCPSIVARSNFNVCRLPGTPEAICATYTGCIIIPGATCPGDYAN')
# returns: UUID('c6a0deb5-0c4f-3961-9d19-3f0fde0517c2')
like image 145
André Laszlo Avatar answered Sep 18 '22 13:09

André Laszlo