Page MenuHomePhabricator

Use random.sample() instead of random.shuffle() in site_tests.py
Closed, ResolvedPublic

Description

Introduction

random.sample() return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.
https://docs.python.org/3/library/random.html?highlight=choices#random.sample

What to do

site_tests uses shuffle over a given list and copies a sublist:

great_wikis = self.site.family.languages_by_size[:-size]
random.shuffle(great_wikis)
great_wikis = great_wikis[:size]

This can be simplified with random.sample().
These issues can be found at https://codesearch.wmcloud.org/pywikibot/?q=shuffle&i=nope&files=&excludeFiles=&repos=

Usefull links

Event Timeline

random.choices() creates a list with replacements, so you might have duplicates.
random.sample() maybe?

Mpaa renamed this task from Use random.choises() instead of random.shuffle() in site_tests.py to Use random.choices() instead of random.shuffle() in site_tests.py.May 13 2022, 6:24 AM
Mpaa updated the task description. (Show Details)

random.choices() creates a list with replacements, so you might have duplicates.
random.sample() maybe?

Ah, yes!

Xqt renamed this task from Use random.choices() instead of random.shuffle() in site_tests.py to Use random.sample() instead of random.shuffle() in site_tests.py.May 13 2022, 9:31 AM
Xqt triaged this task as Low priority.
Xqt added a project: good first task.
Xqt updated the task description. (Show Details)
Xqt updated the task description. (Show Details)

Change 791491 had a related patch set uploaded (by Klein Muçi; author: Klein Muçi):

[pywikibot/core@master] random.sample() instead of random.shuffle()

https://gerrit.wikimedia.org/r/791491

Xqt added a subscriber: Klein.

@Klein: Thank you for this patch. Please add yourself to the AUTHORS.rst if not already done.

Xqt removed a project: Patch-For-Review.

Change 791491 merged by jenkins-bot:

[pywikibot/core@master] random.sample() instead of random.shuffle()

https://gerrit.wikimedia.org/r/791491