Skip to content

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
def random_string_generator(
    str_size: int,
    allowed_chars: str = string.ascii_letters,
    rng: np.random.Generator = None,
) -> str:
    """Generate random strings from allowed charactes with specified size.

    Args:
        str_size: Size of output string
        allowed_chars: chars from which to pick
        rng: rng to control randomness

    Returns:
        random string generate from specified chars

    Example:

    ```pycon
    >>> from strawman.utils import random_string_generator
    >>> random_string_generator(10)
    'rhVShtnDZw'
    ```
    """
    if rng is None:
        rng = _init_rng()
    return "".join(sequence_choice(allowed_chars, rng) for x in range(str_size))

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
def sequence_choice(seq: Sequence, rng: np.random.Generator = None) -> Any:
    """Choose an element randomly from the sequence.

    Args:
        seq: sequence to choose from
        rng: rng to control randomness

    Returns:
        chosen element

    Example:
    ```pycon
    >>> from strawman.utils import sequence_choice
    >>> sequence_choice([1,2,3,4])
    2
    >>> sequence_choice("abcdef")
    'f'
    >>>
    ```
    """
    if rng is None:
        rng = _init_rng()
    return seq[rng.integers(0, len(seq))]

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
def shuffle(
    mylist: List,
    rng: np.random.Generator = None,
) -> List:
    """Return a new shuffled list.

    Args:
        mylist: List to shuffle
        rng: rng to control randomness

    Returns:
        shuffled list

    Example:
    ```pycon
    >>> from strawman.utils import shuffle
    >>> shuffle([1,2,3,4])
    [2, 1, 3, 4]
    ```
    """
    if rng is None:
        rng = _init_rng()
    return [mylist[i] for i in rng.permutation(len(mylist))]

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
def shuffled_overlong(
    mylist: List, length: int, rng: np.random.Generator = None
) -> List:
    """Return a shuffled list which can be longer or shorter (containing the same elements).

    Args:
        mylist: The list from which to choose elements
        length: length of output
        rng: rng to control randomness

    Returns:
        shuffled list with specified length containing input elements

    Example:
    ```pycon
    >>> from strawman.utils import shuffled_overlong
    >>> shuffled_overlong([1,2,3,4],length=10)
    [4, 3, 1, 2, 1, 2, 4, 3, 3, 4]
    ```
    """
    if rng is None:
        rng = _init_rng()
    res: List = []
    while len(res) < length:
        for i in rng.permutation(len(mylist)):
            res.append(mylist[i])
            if len(res) == length:
                break
    return res

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
def split_seq(seq: Sequence, parts: int) -> List:
    """Split a sequence into :obj:`parts` (which are not necessarily the same size).

    Args:
        seq: Sequence to split
        parts: Number of parts

    Returns:
        split sequence as list

    Example:

    ```pycon
    >>> from strawman.utils import split_seq
    >>> split_seq("abcdefgh",parts=3)
    ['ab', 'cd', 'efgh']
    ```
    """
    split_size = len(seq) // parts
    start = 0
    res = []
    for offset_mult in range(1, parts):
        res.append(seq[start : split_size * offset_mult])
        start += split_size
    res.append(seq[split_size * offset_mult :])
    return res