test: refactor: use `random.sample` for choosing random keys in wallet_taproot.py #24496

pull theStack wants to merge 1 commits into bitcoin:master from theStack:202203-test-refactor-replace_key_manual_key_sampling_with_randomsample changing 1 files +1 −14
  1. theStack commented at 5:50 PM on March 7, 2022: member

    The Python3 standard library method random.sample has the exact same functionality as the helper method rand_keys(...) (that is, random sampling without replacement) on a generic set or sequence, i.e. we can simply replace it. See https://docs.python.org/3/library/random.html#random.sample Note that this is also safer: in case that the sample size k is larger than the population count, random.sample throws an error:

    $ python3
    Python 3.8.12 (default, Sep 26 2021, 13:12:50)
    [Clang 11.1.0 ] on openbsd7
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import random
    >>> random.sample([23, 42], 3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.8/random.py", line 363, in sample
        raise ValueError("Sample larger than population or is negative")
    ValueError: Sample larger than population or is negative
    

    while the custom method would get stuck in an endless loop.

  2. test: refactor: use `random.sample` for choosing random keys in wallet_taproot.py 31846b006d
  3. fanquake added the label Tests on Mar 7, 2022
  4. shaavan approved
  5. shaavan commented at 8:25 AM on March 8, 2022: contributor

    Code Review ACK 31846b006da46852cfce91e53427dc8f871b3fda

    I prefer the use of a built-in function over a custom one. The reasons are:

    • built-in functions are usually less prone to bugs.
    • There’s a standardized way of using the built-in function; hence a code reviewer could easily understand their use case.

    And in this case:

    • Using built-in function prevents at least one bug, that is:

    Note that this is also safer: in case that the sample size k is larger than the population count, random.sample throws an error while the custom method would get stuck in an endless loop.

    • Also, it helps remove redundant code from the functional test file and make the file cleaner.
  6. brunoerg commented at 12:04 PM on March 8, 2022: member

    Strong concept ACK

  7. MarcoFalke merged this on Mar 8, 2022
  8. MarcoFalke closed this on Mar 8, 2022

  9. theStack deleted the branch on Mar 8, 2022
  10. danielabrozzoni approved
  11. danielabrozzoni commented at 2:24 PM on March 8, 2022: contributor

    tACK 31846b006da46852cfce91e53427dc8f871b3fda - I prefer to use the random.sample function as, as you said, calling rand_keys(n) with n >= len(KEYS) results in a infinite loop. This change also makes the code cleaner.

  12. sidhujag referenced this in commit d0abdc1595 on Mar 8, 2022
  13. DrahtBot locked this on Mar 8, 2023

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-14 21:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me