Many stdlib libraries have a simple CLI:
Some of my favourites:
$ python3 -m http.server
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
$ python3 -m webbrowser https://www.python.org
$ python3 -m uuid
5f73cb76-01d7-4390-8cda-17fe9672a29f
$ python3 -m calendar
2024
January February March
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7 1 2 3 4 1 2 3
[snip]
I think it would be useful to add a CLI to random
to be randomly select a choice (using random.choice
):
$ python3 -m random curry "fish n chips" tacos
tacos
This wouldn’t be a Python-specific tool, like pdb
or pickletools
, but a generally useful tool like the ones above. I also can’t think of an existing CLI tool to do this (cross-platform or not).
We could add CLI arguments: if the input is an integer, return a random integer between 1 and the input (via random.randint
), if it’s a float, return a raondom float between 0 and the input (via random.uniform
).
For example:
$ python3 -m random a b c
b
$ python3 -m random 4
1
$ python3 -m random 2.5
1.5817380334578792
And we could also have explicit arguments:
$ python3 -m random --choice a b c
a
$ python3 -m random --integer 4
3
$ python3 -m random --float 2.5
2.2419142269846186
$ python3 -m random --integer 2
1
$ python3 -m random --float 2
1.4463835577108637
For me, just the random choice would be the most important, but I can see at least picking a random integer would also be useful, like a dice throw.
I’ve implemented this, and it’s about 60 lines of code, and 40 lines of tests.
Currently, python -m random
generates some sample test output. We could remove this, or if we still want it, move it to python -m random --test
.
What do you think?
(See also the re
CLI proposal.)