-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.py
89 lines (69 loc) · 3.26 KB
/
init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import division
def initRepeat(container, func, n):
"""Call the function *container* with a generator function corresponding
to the calling *n* times the function *func*.
:param container: The type to put in the data from func.
:param func: The function that will be called n times to fill the
container.
:param n: The number of times to repeat func.
:returns: An instance of the container filled with data from func.
This helper function can be used in conjunction with a Toolbox
to register a generator of filled containers, as individuals or
population.
>>> import random
>>> random.seed(42)
>>> initRepeat(list, random.random, 2) # doctest: +ELLIPSIS,
... # doctest: +NORMALIZE_WHITESPACE
[0.6394..., 0.0250...]
See the :ref:`list-of-floats` and :ref:`population` tutorials for more examples.
"""
return container(func() for _ in xrange(n))
def initIterate(container, generator):
"""Call the function *container* with an iterable as
its only argument. The iterable must be returned by
the method or the object *generator*.
:param container: The type to put in the data from func.
:param generator: A function returning an iterable (list, tuple, ...),
the content of this iterable will fill the container.
:returns: An instance of the container filled with data from the
generator.
This helper function can be used in conjunction with a Toolbox
to register a generator of filled containers, as individuals or
population.
>>> import random
>>> from functools import partial
>>> random.seed(42)
>>> gen_idx = partial(random.sample, range(10), 10)
>>> initIterate(list, gen_idx) # doctest: +SKIP
[1, 0, 4, 9, 6, 5, 8, 2, 3, 7]
See the :ref:`permutation` and :ref:`arithmetic-expr` tutorials for
more examples.
"""
return container(generator())
def initCycle(container, seq_func, n=1):
"""Call the function *container* with a generator function corresponding
to the calling *n* times the functions present in *seq_func*.
:param container: The type to put in the data from func.
:param seq_func: A list of function objects to be called in order to
fill the container.
:param n: Number of times to iterate through the list of functions.
:returns: An instance of the container filled with data from the
returned by the functions.
This helper function can be used in conjunction with a Toolbox
to register a generator of filled containers, as individuals or
population.
>>> func_seq = [lambda:1 , lambda:'a', lambda:3]
>>> initCycle(list, func_seq, n=2)
[1, 'a', 3, 1, 'a', 3]
See the :ref:`funky` tutorial for an example.
"""
return container(func() for _ in xrange(n) for func in seq_func)
__all__ = ['initRepeat', 'initIterate', 'initCycle']
if __name__ == "__main__":
import doctest
import random
random.seed(64)
doctest.run_docstring_examples(initRepeat, globals())
random.seed(64)
doctest.run_docstring_examples(initIterate, globals())
doctest.run_docstring_examples(initCycle, globals())