gh-131236: allow to generate multiple UUIDs at once via CLI#131218
gh-131236: allow to generate multiple UUIDs at once via CLI#131218hugovk merged 20 commits intopython:mainfrom
Conversation
Sometimes you need more than one UUID. Inspired by https://www.man7.org/linux/man-pages/man1/uuidgen.1.html
|
Thank you for idea, but we usually open issue before adding any feature. Contribution process is documented here: https://devguide.python.org/ |
Lib/uuid.py
Outdated
| help="The name used as part of generating the uuid. " | ||
| "Only required for uuid3/uuid5 functions.") | ||
| parser.add_argument("--count", type=int, default=1, | ||
| help="Generate more uuids in loop. ") |
There was a problem hiding this comment.
| help="Generate more uuids in loop. ") | |
| help="Generate COUNT UUIDs. Default: 1.") |
Or add formatter_class=argparse.ArgumentDefaultsHelpFormatter to argparse.ArgumentParser() and:
| help="Generate more uuids in loop. ") | |
| help="Generate this many UUIDs.") |
And also delete "By default uuid4 function is used."
There was a problem hiding this comment.
I would like to have a short option -C as well so to mimick uuidgen, and use NUM as a metavar instead of COUNT. In addition, we can just say Generate NUM fresh UUIDs
There was a problem hiding this comment.
And also delete "By default uuid4 function is used."
Isn't it the default though?
There was a problem hiding this comment.
Yes, but argparse.ArgumentDefaultsHelpFormatter prints the default for us:
❯ ./python.exe -m uuid -h
usage: python.exe -m uuid [-h]
[-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}]
[-n NAMESPACE] [-N NAME] [--count COUNT]
Generates a uuid using the selected uuid function.
options:
-h, --help show this help message and exit
-u, --uuid {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}
The function to use to generate the uuid. By default
uuid4 function is used. (default: uuid4)
-n, --namespace NAMESPACE
The namespace is a UUID, or '@ns' where 'ns' is a
well-known predefined UUID addressed by namespace
name. Such as @dns, @url, @oid, and @x500. Only
required for uuid3/uuid5 functions. (default: None)
-N, --name NAME The name used as part of generating the uuid. Only
required for uuid3/uuid5 functions. (default: None)
--count COUNT Generate more uuids in loop. (default: 1)(I have an idea to add colour to those in future.)
There was a problem hiding this comment.
Oh. I thought that the suggestion was separate, my bad
There was a problem hiding this comment.
With ArgumentDefaultsHelpFormatter it would look the following:
It seems that the typesetting of the help text mixed – Do we start the help text with a capital letter? Do we use a period at the end (this looks strange with the (default: 1) generated by ArgumentDefaultsHelpFormatter)?
> python -m uuid -h
usage: python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n NAMESPACE] [-N NAME] [-C NUM]
Generates a uuid using the selected uuid function.
options:
-h, --help show this help message and exit
-u, --uuid {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}
The function to use to generate the uuid. (default: uuid4)
-n, --namespace NAMESPACE
The namespace is a UUID, or '@ns' where 'ns' is a well-known predefined UUID addressed by namespace name. Such as @dns, @url, @oid, and @x500. Only required
for uuid3/uuid5 functions. (default: None)
-N, --name NAME The name used as part of generating the uuid. Only required for uuid3/uuid5 functions. (default: None)
-C, --count NUM Generate NUM fresh UUIDs. (default: 1)
There was a problem hiding this comment.
Adding ArgumentDefaultsHelpFormatter, going for lowercase first letter, removing period at the end, adding metavar="NS" for namespace yields a more compact and consistent help output:
> python -m uuid -h
usage: python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n NS] [-N NAME] [-C NUM]
Generate a UUID using the selected UUID function.
options:
-h, --help show this help message and exit
-u, --uuid {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}
function to generate the UUID (default: uuid4)
-n, --namespace NS a UUID, or '@ns' where 'ns' is a well-known predefined UUID addressed by namespace name, such as @dns, @url, @oid, and @x500 (only required for uuid3/uuid5)
(default: None)
-N, --name NAME name used as part of generating the UUID (only required for uuid3/uuid5) (default: None)
-C, --count NUM generate NUM fresh UUIDs (default: 1)
I'm still not happy with the verbose --namespace help, though:
-n, --namespace NS a UUID, or '@ns' where 'ns' is a well-known predefined UUID addressed by namespace name, such as @dns, @url, @oid, and @x500 (only required for uuid3/uuid5)
(default: None)
Is the following any better?
-n, --namespace NS a UUID, or @dns, @url, @oid, @x500, or any '@ns' where 'ns' is a well-known predefined UUID addressed by namespace name (only required for uuid3/uuid5)
(default: None)
Or specify the few choices explicitly?
-n, --namespace {any UUID,@dns,@url,@oid,@x500}
a UUID, or a well-known predefined UUID addressed by namespace name (only required for uuid3/uuid5) (default: None)
There was a problem hiding this comment.
Adding
ArgumentDefaultsHelpFormatter, going for lowercase first letter, removing period at the end, addingmetavar="NS"for namespace yields a more compact and consistent help output:
👍
I'm still not happy with the very verbose
"a UUID, or '@ns' where 'ns' is a well-known predefined UUID addressed by namespace name, such as @dns, @url, @oid, and @x500 (only required for uuid3/uuid5)", though. Is"a UUID, or @dns, @url, @oid, @x500, or any '@ns' where 'ns' is a well-known predefined UUID addressed by namespace name (only required for uuid3/uuid5)"any better?
It's a bit better, and I don't have a much better suggestion :)
I think we could also prefix and shorten the "(only required for uuid3/uuid5)" bits, so if you're not using uuid3/5 you can immediately skip it and save some mental effort:
-n, --namespace NS uuid3/uuid5 only: [etc]
(default: None)
-N, --name NAME uuid3/uuid5 only: name used as part of generating the UUID (default: None)
There was a problem hiding this comment.
For --namespace, maybe specify the few choices explicitly?
-n, --namespace {any UUID,@dns,@url,@oid,@x500}
uuid3/uuid5 only: a UUID, or a well-known predefined UUID addressed by namespace name (default: None)
There was a problem hiding this comment.
Thanks for your ideas. Here's the concise result:
> python -m uuid -h
usage: python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n {any UUID,@dns,@url,@oid,@x500}] [-N NAME] [-C NUM]
Generate a UUID using the selected UUID function.
options:
-h, --help show this help message and exit
-u, --uuid {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}
function to generate the UUID (default: uuid4)
-n, --namespace {any UUID,@dns,@url,@oid,@x500}
uuid3/uuid5 only: a UUID, or a well-known predefined UUID addressed by namespace name) (default: None)
-N, --name NAME uuid3/uuid5 only: name used as part of generating the UUID (default: None)
-C, --count NUM generate NUM fresh UUIDs (default: 1)
|
As mentioned, please open an issue, and add a NEWS file. cc @picnixz |
Lib/uuid.py
Outdated
| help="The name used as part of generating the uuid. " | ||
| "Only required for uuid3/uuid5 functions.") | ||
| parser.add_argument("--count", type=int, default=1, | ||
| help="Generate more uuids in loop. ") |
There was a problem hiding this comment.
I would like to have a short option -C as well so to mimick uuidgen, and use NUM as a metavar instead of COUNT. In addition, we can just say Generate NUM fresh UUIDs
Lib/uuid.py
Outdated
| help="The name used as part of generating the uuid. " | ||
| "Only required for uuid3/uuid5 functions.") | ||
| parser.add_argument("--count", type=int, default=1, | ||
| help="Generate more uuids in loop. ") |
There was a problem hiding this comment.
And also delete "By default uuid4 function is used."
Isn't it the default though?
|
We also need some |
|
Hi all, thank you for the feedback. I've created an issue, created a news entry, updated the help text to "Generate NUM fresh UUIDs.", added |
picnixz
left a comment
There was a problem hiding this comment.
Do we have tests for the CLI? if yes, please update them, otherwise let's add them in a follow-up PR.
Misc/NEWS.d/next/Library/2025-03-14-12-22-02.gh-issue-131236.HjqFq0.rst
Outdated
Show resolved
Hide resolved
sobolevn
left a comment
There was a problem hiding this comment.
Sorry, it is not clear for me, what's your use-case. You can already generate any amount of uuids as simple as:
» for i in {1..4} ; do ./python.exe -m uuid ; done
11afb3ee-2e62-4e7f-93d2-e073cd44b093
4ebad66f-a4b2-4f5e-b34a-2bfbdc6db7ac
d8068024-d316-4ccd-bfae-ded9c3f5c504
400a0201-5fe1-4868-895d-9db6d3288e5c
Basically, all shells support loops.
…jqFq0.rst Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@sobolevn, invoking 100 Python interpreters on Windows via Git Bash (MINGW64) takes 13s when it could complete in 0.102s MINGW64$ time sh -c 'seq 100 | xargs -i python.exe -m uuid'
real 0m12.974s
user 0m0.090s
sys 0m0.363s
MINGW64$ time python.exe -c 'import uuid
for _ in range(100):
print(uuid.uuid4())
'
real 0m0.102s
user 0m0.000s
sys 0m0.015s |
@picnixz, thank you for your review. I've added a CLI test |
|
@simon04 thanks, these numbers make sense to me! |
|
@simon04 Tip: there's usually no need to click the "Update branch" button: https://devguide.python.org/getting-started/pull-request-lifecycle/#update-branch-button |
hugovk
left a comment
There was a problem hiding this comment.
Thank you!
Something else I noticed, but this can be a followup PR. The docs start with:
- library reference
- "Command-Line Usage"
- library "Example"
- "Command-Line Example"
Let's restructure (and use sentence case):
- library reference
- library "Example"
- "Command-line usage"
- "Command-line example"
…thon#131218) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Fixes #131236.
Add
--countto the main() of the uuid module.Sometimes you need more than one UUID. In order to print 42 UUIDs, run
python -m uuid --count 42Inspired by https://www.man7.org/linux/man-pages/man1/uuidgen.1.html
📚 Documentation preview 📚: https://cpython-previews--131218.org.readthedocs.build/