| 1 |
|
| 2 |
|
| 3 |
import random |
| 4 |
from collections import OrderedDict, namedtuple |
| 5 |
|
| 6 |
from random_order.cache import FAILED_FIRST_LAST_FAILED_BUCKET_KEY |
| 7 |
|
| 8 |
""" |
| 9 |
`bucket` is a string representing the bucket in which the item falls based on user's chosen |
| 10 |
bucket type. |
| 11 |
|
| 12 |
`disabled` is either a falsey value to mark that the item is ready for shuffling (shuffling is not disabled), |
| 13 |
or a truthy value in which case the item won't be shuffled among other items with the same key. |
| 14 |
|
| 15 |
In some cases it is important for the `disabled` to be more than just True in order |
| 16 |
to preserve a distinct disabled sub-bucket within a larger bucket and not mix it up with another |
| 17 |
disabled sub-bucket of the same larger bucket. |
| 18 |
""" |
| 19 |
ItemKey = namedtuple("ItemKey", field_names=("bucket", "disabled", "x")) |
| 20 |
ItemKey.__new__.__defaults__ = (None, None) |
| 21 |
|
| 22 |
|
| 23 |
def _shuffle_items(items, bucket_key=None, disable=None, seed=None, session=None): |
| 24 |
""" |
| 25 |
Shuffles a list of `items` in place. |
| 26 |
|
| 27 |
If `bucket_key` is None, items are shuffled across the entire list. |
| 28 |
|
| 29 |
`bucket_key` is an optional function called for each item in `items` to |
| 30 |
calculate the key of bucket in which the item falls. |
| 31 |
|
| 32 |
Bucket defines the boundaries across which items will not |
| 33 |
be shuffled. |
| 34 |
|
| 35 |
`disable` is a function that takes an item and returns a falsey value |
| 36 |
if this item is ok to be shuffled. It returns a truthy value otherwise and |
| 37 |
the truthy value is used as part of the item's key when determining the bucket |
| 38 |
it belongs to. |
| 39 |
""" |
| 40 |
|
| 41 |
if seed is not None: |
| 42 |
random.seed(seed) |
| 43 |
|
| 44 |
|
| 45 |
if not bucket_key and not disable: |
| 46 |
random.shuffle(items) |
| 47 |
return |
| 48 |
|
| 49 |
def get_full_bucket_key(item): |
| 50 |
assert bucket_key or disable |
| 51 |
if bucket_key and disable: |
| 52 |
return ItemKey(bucket=bucket_key(item, session), disabled=disable(item, session)) |
| 53 |
elif disable: |
| 54 |
return ItemKey(disabled=disable(item, session)) |
| 55 |
else: |
| 56 |
return ItemKey(bucket=bucket_key(item, session)) |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
buckets = OrderedDict() |
| 62 |
for item in items: |
| 63 |
full_bucket_key = get_full_bucket_key(item) |
| 64 |
if full_bucket_key not in buckets: |
| 65 |
buckets[full_bucket_key] = [] |
| 66 |
buckets[full_bucket_key].append(item) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
bucket_keys = list(buckets.keys()) |
| 71 |
|
| 72 |
for full_bucket_key in buckets.keys(): |
| 73 |
if full_bucket_key.bucket == FAILED_FIRST_LAST_FAILED_BUCKET_KEY: |
| 74 |
|
| 75 |
continue |
| 76 |
|
| 77 |
if not full_bucket_key.disabled: |
| 78 |
random.shuffle(buckets[full_bucket_key]) |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
if bucket_keys and bucket_keys[0].bucket == FAILED_FIRST_LAST_FAILED_BUCKET_KEY: |
| 84 |
new_bucket_keys = list(buckets.keys())[1:] |
| 85 |
random.shuffle(new_bucket_keys) |
| 86 |
new_bucket_keys.insert(0, bucket_keys[0]) |
| 87 |
else: |
| 88 |
new_bucket_keys = list(buckets.keys()) |
| 89 |
random.shuffle(new_bucket_keys) |
| 90 |
|
| 91 |
items[:] = [item for bk in new_bucket_keys for item in buckets[bk]] |
| 92 |
return |
| 93 |
|
| 94 |
|
| 95 |
def _get_set_of_item_ids(items): |
| 96 |
return set(item.nodeid for item in items) |
| 97 |
|
| 98 |
|
| 99 |
def _disable(item, session): |
| 100 |
if hasattr(item, "get_closest_marker"): |
| 101 |
marker = item.get_closest_marker("random_order") |
| 102 |
else: |
| 103 |
marker = item.get_marker("random_order") |
| 104 |
if marker: |
| 105 |
is_disabled = marker.kwargs.get("disabled", False) |
| 106 |
if is_disabled: |
| 107 |
|
| 108 |
|
| 109 |
return item.parent.name |
| 110 |
return False |