作成方法ガイド/UUID Python
-
22-08-2019 - |
質問
の作成方法を教えてください、こPythonることはプラットフォームですか。かりを用いる方法ActivePython WindowsでWindowsのみで使用します。があるのを用いる方法平野Python?
解決
Uuidはモジュールは、Python2.5よび、RFC対応UUID ます。のモジュールdocsおよび、RFCます。[源]
Docs:
- Python2: http://docs.python.org/2/library/uuid.html
- Python3:https://docs.python.org/3/library/uuid.html
例(2および3):
>>> import uuid
>>> uuid.uuid4()
UUID('bd65600d-8669-4903-8a14-af88203add38')
>>> str(uuid.uuid4())
'f50ec0b7-f960-400d-91f0-c42a6d44e3d0'
>>> uuid.uuid4().hex
'9fe2c4e93f654fdbb24c02b15259716c'
他のヒント
あなたは、Python 2.5以降を使用している場合は、<のhref = "https://docs.python.org/2/library/uuid.html#module-uuid" のrel = "noreferrer" タイトル= "UUIDモジュール ">の uuidのモジュールのにはすでにPythonの標準ディストリビューションに含まれています。
例:
>>> import uuid
>>> uuid.uuid4()
UUID('5361a11b-615c-42bf-9bdb-e2c3790ada14')
からコピー: https://docs.python.org/2/library/uuid.html(掲載のリンクがアクティブではありませんでしたし、彼らは更新を続けるので)
>>> 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')
Iは、データベース型操作のためのランダムキーとしてGUIDを使用します。
ダッシュや余分な文字を16進数形式は、私には不必要に長いようです。しかし、私はまた、16進数を表す文字列は、彼らがそのような「+」、「=」、など。
のように、いくつかの状況では問題を引き起こす可能性があります文字が含まれていないという点で、非常に安全であることを好みますの代わりに16進数、Iは、URLセーフのBase64文字列を使用します。以下は、(ランダム性の必要量を有する以外)かかわらず、任意のUUID / GUID仕様に準拠していない。
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('=', '')
この関数は、完全に設定可能であり、指定されたフォーマットに基づいて一意のUIDを生成
例: - [8,4、4、4、12]、これは上述の形式であり、それは次のUUIDを生成する
の 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
あなたは、コードの下に、あなたのモデルまたは一意のフィールドの主キーのためにUUIDを渡す必要がある場合はUUIDオブジェクトを返します -
import uuid
uuid.uuid4()
あなたはURLのパラメータとしてUUIDを渡す必要がある場合は、コードの以下のように行うことができます -
import uuid
str(uuid.uuid4())
- あなたはUUIDのために進値をしたい場合は、あなたは、以下のいずれかを実行することができます
import uuid
uuid.uuid4().hex