utils
random_string_generator(str_size, allowed_chars=string.ascii_letters, rng=None)
Generate random strings from allowed charactes with specified size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
str_size |
int
|
Size of output string |
required |
allowed_chars |
str
|
chars from which to pick |
ascii_letters
|
rng |
Generator
|
rng to control randomness |
None
|
Returns:
Type | Description |
---|---|
str
|
random string generate from specified chars |
Example:
>>> from strawman.utils import random_string_generator
>>> random_string_generator(10)
'rhVShtnDZw'
Source code in /home/docs/checkouts/readthedocs.org/user_builds/strawman/envs/latest/lib/python3.10/site-packages/strawman/utils.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
sequence_choice(seq, rng=None)
Choose an element randomly from the sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Sequence
|
sequence to choose from |
required |
rng |
Generator
|
rng to control randomness |
None
|
Returns:
Type | Description |
---|---|
Any
|
chosen element |
Example:
>>> from strawman.utils import sequence_choice
>>> sequence_choice([1,2,3,4])
2
>>> sequence_choice("abcdef")
'f'
>>>
Source code in /home/docs/checkouts/readthedocs.org/user_builds/strawman/envs/latest/lib/python3.10/site-packages/strawman/utils.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
shuffle(mylist, rng=None)
Return a new shuffled list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mylist |
List
|
List to shuffle |
required |
rng |
Generator
|
rng to control randomness |
None
|
Returns:
Type | Description |
---|---|
List
|
shuffled list |
Example:
>>> from strawman.utils import shuffle
>>> shuffle([1,2,3,4])
[2, 1, 3, 4]
Source code in /home/docs/checkouts/readthedocs.org/user_builds/strawman/envs/latest/lib/python3.10/site-packages/strawman/utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
shuffled_overlong(mylist, length, rng=None)
Return a shuffled list which can be longer or shorter (containing the same elements).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mylist |
List
|
The list from which to choose elements |
required |
length |
int
|
length of output |
required |
rng |
Generator
|
rng to control randomness |
None
|
Returns:
Type | Description |
---|---|
List
|
shuffled list with specified length containing input elements |
Example:
>>> from strawman.utils import shuffled_overlong
>>> shuffled_overlong([1,2,3,4],length=10)
[4, 3, 1, 2, 1, 2, 4, 3, 3, 4]
Source code in /home/docs/checkouts/readthedocs.org/user_builds/strawman/envs/latest/lib/python3.10/site-packages/strawman/utils.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
split_seq(seq, parts)
Split a sequence into :obj:parts
(which are not necessarily the same size).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq |
Sequence
|
Sequence to split |
required |
parts |
int
|
Number of parts |
required |
Returns:
Type | Description |
---|---|
List
|
split sequence as list |
Example:
>>> from strawman.utils import split_seq
>>> split_seq("abcdefgh",parts=3)
['ab', 'cd', 'efgh']
Source code in /home/docs/checkouts/readthedocs.org/user_builds/strawman/envs/latest/lib/python3.10/site-packages/strawman/utils.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|