1
1
from __future__ import absolute_import , print_function , division
2
2
3
-
4
- import datetime
3
+ import hashlib
5
4
import random
6
5
import time
7
6
from collections import OrderedDict
8
7
from functools import partial
8
+
9
9
from petl .compat import xrange , text_type
10
+ from petl .util .base import Table
10
11
11
12
12
- from petl .util .base import Table
13
+ def randomseed ():
14
+ """
15
+ Obtain the hex digest of a sha256 hash of the
16
+ current epoch time in nanoseconds.
17
+ """
18
+
19
+ time_ns = str (time .time ()).encode ()
20
+ hash_time = hashlib .sha256 (time_ns ).hexdigest ()
21
+ return hash_time
13
22
14
23
15
24
def randomtable (numflds = 5 , numrows = 100 , wait = 0 , seed = None ):
@@ -36,9 +45,11 @@ def randomtable(numflds=5, numrows=100, wait=0, seed=None):
36
45
| 0.026535969683863625 | 0.1988376506866485 | 0.6498844377795232 |
37
46
+----------------------+----------------------+---------------------+
38
47
...
48
+ <BLANKLINE>
39
49
40
50
Note that the data are generated on the fly and are not stored in memory,
41
51
so this function can be used to simulate very large tables.
52
+ The only supported seed types are: None, int, float, str, bytes, and bytearray.
42
53
43
54
"""
44
55
@@ -52,7 +63,7 @@ def __init__(self, numflds=5, numrows=100, wait=0, seed=None):
52
63
self .numrows = numrows
53
64
self .wait = wait
54
65
if seed is None :
55
- self .seed = datetime . datetime . now ()
66
+ self .seed = randomseed ()
56
67
else :
57
68
self .seed = seed
58
69
@@ -77,7 +88,7 @@ def __iter__(self):
77
88
yield tuple (random .random () for n in range (nf ))
78
89
79
90
def reseed (self ):
80
- self .seed = datetime . datetime . now ()
91
+ self .seed = randomseed ()
81
92
82
93
83
94
def dummytable (numrows = 100 ,
@@ -108,6 +119,7 @@ def dummytable(numrows=100,
108
119
| 4 | 'apples' | 0.09369523986159245 |
109
120
+-----+----------+----------------------+
110
121
...
122
+ <BLANKLINE>
111
123
112
124
>>> # customise fields
113
125
... import random
@@ -132,12 +144,19 @@ def dummytable(numrows=100,
132
144
| 0.4219218196852704 | 15 | 'chocolate' |
133
145
+---------------------+-----+-------------+
134
146
...
147
+ <BLANKLINE>
148
+
149
+ >>> table3_1 = etl.dummytable(50)
150
+ >>> table3_2 = etl.dummytable(100)
151
+ >>> table3_1[5] == table3_2[5]
152
+ False
135
153
136
154
Data generation functions can be specified via the `fields` keyword
137
155
argument.
138
156
139
157
Note that the data are generated on the fly and are not stored in memory,
140
158
so this function can be used to simulate very large tables.
159
+ The only supported seed types are: None, int, float, str, bytes, and bytearray.
141
160
142
161
"""
143
162
@@ -154,7 +173,7 @@ def __init__(self, numrows=100, fields=None, wait=0, seed=None):
154
173
else :
155
174
self .fields = OrderedDict (fields )
156
175
if seed is None :
157
- self .seed = datetime . datetime . now ()
176
+ self .seed = randomseed ()
158
177
else :
159
178
self .seed = seed
160
179
@@ -181,4 +200,4 @@ def __iter__(self):
181
200
yield tuple (fields [f ]() for f in fields )
182
201
183
202
def reseed (self ):
184
- self .seed = datetime . datetime . now ()
203
+ self .seed = randomseed ()
0 commit comments