Frage

Wie erstelle ich eine GUID in Python, die plattformunabhängig ist? Ich höre, gibt es ein Verfahren unter Verwendung von Activepython unter Windows, aber es ist nur für Windows, weil es COM verwendet. Gibt es eine Methode Ebene mit Python?

War es hilfreich?

Lösung

  

Das UUID-Modul, in Python 2.5 und höher, bietet RFC-konform UUID   Generation. Siehe die Modul-Dokumentation und die RFC für weitere Einzelheiten. [ Quelle ]

Docs:

Beispiel (Arbeits auf 2 und 3):

>>> import uuid
>>> uuid.uuid4()
UUID('bd65600d-8669-4903-8a14-af88203add38')
>>> str(uuid.uuid4())
'f50ec0b7-f960-400d-91f0-c42a6d44e3d0'
>>> uuid.uuid4().hex
'9fe2c4e93f654fdbb24c02b15259716c'

Andere Tipps

Wenn Sie mit Python 2.5 oder höher, die uuid Modul bereits mit der Python Standard-Distribution enthalten ist.

Beispiel:

>>> import uuid
>>> uuid.uuid4()
UUID('5361a11b-615c-42bf-9bdb-e2c3790ada14')

Kopiert von: https://docs.python.org/2/library/uuid.html (Da die Links nicht aktiv geschrieben waren, und sie halten Aktualisierung)

>>> import uuid

>>> # make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

>>> # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

>>> # make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

>>> # make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')

>>> # convert a UUID to a string of hex digits in standard form
>>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f'

>>> # get the raw 16 bytes of the UUID
>>> x.bytes
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'

>>> # make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

Ich benutze GUIDs als Zufallsschlüssel für Datenbanktyp Operationen.

Die hexadezimale Form, mit den Strichen und zusätzlichen Zeichen scheint unnötig lange zu mir. Aber Ich mag auch, dass Strings repräsentieren hexadezimale Zahlen sind sehr sicher, dass sie nicht enthalten Zeichen, die Probleme in einigen Situationen wie ‚+‘ kann dazu führen, ‚=‘, etc ..

Statt hexadezimal, verwende ich einen URL-safe base64 String. Die folgende entspricht nicht jede UUID / GUID-Spezifikation aber (anders als die erforderliche Menge an Zufälligkeit aufweisen).

import base64
import uuid

# get a UUID - URL safe, Base64
def get_a_uuid():
    r_uuid = base64.urlsafe_b64encode(uuid.uuid4().bytes)
    return r_uuid.replace('=', '')

Diese Funktion ist vollständig konfigurierbar und erzeugt einzigartigen uid auf das Format basierte angegeben

zB: - [8, 4, 4, 4, 12], das ist das Format erwähnt, und es wird die folgende UUID generiert

  

LxoYNyXe-7hbQ-Cajt-DSdU-PDAht56cMEWi

 import random as r

 def generate_uuid():
        random_string = ''
        random_str_seq = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        uuid_format = [8, 4, 4, 4, 12]
        for n in uuid_format:
            for i in range(0,n):
                random_string += str(random_str_seq[r.randint(0, len(random_str_seq) - 1)])
            if n != 12:
                random_string += '-'
        return random_string

Wenn Sie UUID für einen Primärschlüssel für Ihr Modell oder eindeutiges Feld unten Code dann passieren gibt die UUID Objekt -

 import uuid
 uuid.uuid4()

Wenn Sie UUID als Parameter für die URL übergeben können Sie wie unten Code tun -

import uuid
str(uuid.uuid4())

Wenn Sie die Hex-Wert für eine UUID möchten, können Sie das unter einen tun -

import uuid    
uuid.uuid4().hex
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top