Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Provide feedback

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

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Command-line interface for therandom module #118131

Closed
Labels
3.13bugs and security fixesstdlibPython modules in the Lib dirtype-featureA feature request or enhancement
@hugovk

Description

@hugovk

Feature or enhancement

Proposal:

Many stdlib libraries have a simple CLI:

Some of my favourites:

$python3 -m http.serverServing HTTP on :: port 8000 (http://[::]:8000/) ...$python3 -m webbrowser https://www.python.org$python3 -m uuid5f73cb76-01d7-4390-8cda-17fe9672a29f$python3 -m calendar                                  2024      January                   February                   MarchMo 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]

It would be useful to add a CLI torandom to be randomly select a choice (usingrandom.choice):

$python3 -m random curry"fish n chips" tacosfish n chips

We can also print a random number:

  • if the input is an integer, print a random integer between 1 and the input (viarandom.randint)
  • if it's a float, print a random float between 0 and the input (viarandom.uniform).

For example:

$python3 -m random a b cc$python3 -m random 64$python3 -m random 2.51.9597922929238814

Also with explicit arguments:

$python3 -m random --choice a b cb$python3 -m random --integer 66$python3 -m random --float 2.51.1778540129562416$python3 -m random --float 61.4311142568750403

This isn't a Python-specific tool, likepdb orpickletools, but a generally useful tool likehttp.server anduuid. I can't find an existing cross-platform tool for this. Linux hasshuf to return a random choice, but it's not natively available in macOS (it's part of a Homebrew package) or Windows (uutils can be installed using Scoop). There's also anum-utils package for Unix to print a random integer.

For me, just the random choice is the most important, but I can see at least picking a random integer is also useful, like a dice throw.

Currently,python -m random generates some sample test output; if we still need it I propose moving it topython -m random --test.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixesstdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp