← 返回首页
bpo-44431: Added command line functionality to UUID module. by ephenix · Pull Request #26751 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
54 changes: 54 additions & 0 deletions Lib/uuid.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@
# make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

Command-Line usage:

python -m uuid [-h] | COMMAND [NAMESPACE] [NAME]

# generate a random uuid
>>> python -m uuid

# generate a uuid1
>>> python -m uuid uuid1

# generate a uuid3 or uuid5
>>> python -m uuid uuid5 NAMESPACE_DNS python.org

"""

import os
Expand Down Expand Up @@ -727,3 +741,43 @@ def uuid5(namespace, name):
NAMESPACE_URL = UUID('6ba7b811-9dad-11d1-80b4-00c04fd430c8')
NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')

if __name__ == '__main__':
"""
Enables uuid to be called from the command line

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

I wonder if we can move this logic into a helper method so that we just call the method here? Most libraries seem to follow this e.g Lib/timeit.py

"""

allowed_cmds = ['-h','--help','uuid1', 'uuid3', 'uuid4', 'uuid5']
if len(sys.argv) == 1:
# By default, print a random uuid.
print(uuid4())
else:
cmd = sys.argv[1]
if cmd not in allowed_cmds:
raise Exception(f"Command not found: [{cmd}]. Run `python -m uuid -h` for more information.")
if cmd in ['-h', '--help']:
print(__doc__)
if cmd == 'uuid1':
print(uuid1())
if cmd in ['uuid3', 'uuid5']:
if len(sys.argv) != 4:
raise Exception(f"Incorrect number of arguments. Example usage: \n python -m uuid {cmd} NAMESPACE NAME")
else:
namespace = sys.argv[2]
name = sys.argv[3]
if namespace == "NAMESPACE_DNS":
namespace = NAMESPACE_DNS
elif namespace == "NAMESPACE_URL":
namespace = NAMESPACE_URL
elif namespace == "NAMESPACE_OID":
namespace = NAMESPACE_OID
elif namespace == "NAMESPACE_X500":
namespace = NAMESPACE_X500
else:
namespace = UUID(namespace)
if cmd == "uuid3":
print(uuid3(namespace, name))
else:
print(uuid5(namespace, name))
if cmd == 'uuid4':
print(uuid4())
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UUID module now supports being called from command line. ex > python -m uuid

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

I don't think we need an example in a news entry. I would do way with ex > python -m uuid .

Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.