| 1 |
from __future__ import absolute_import, print_function, division |
| 2 |
|
| 3 |
import hashlib |
| 4 |
import random as pyrandom |
| 5 |
import time |
| 6 |
from collections import OrderedDict |
| 7 |
from functools import partial |
| 8 |
|
| 9 |
from petl.compat import xrange, text_type |
| 10 |
from petl.util.base import Table |
| 11 |
|
| 12 |
|
| 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 |
| 22 |
|
| 23 |
|
| 24 |
def randomtable(numflds=5, numrows=100, wait=0, seed=None): |
| 25 |
""" |
| 26 |
Construct a table with random numerical data. Use `numflds` and `numrows` to |
| 27 |
specify the number of fields and rows respectively. Set `wait` to a float |
| 28 |
greater than zero to simulate a delay on each row generation (number of |
| 29 |
seconds per row). E.g.:: |
| 30 |
|
| 31 |
>>> import petl as etl |
| 32 |
>>> table = etl.randomtable(3, 100, seed=42) |
| 33 |
>>> table |
| 34 |
+----------------------+----------------------+---------------------+ |
| 35 |
| f0 | f1 | f2 | |
| 36 |
+======================+======================+=====================+ |
| 37 |
| 0.6394267984578837 | 0.025010755222666936 | 0.27502931836911926 | |
| 38 |
+----------------------+----------------------+---------------------+ |
| 39 |
| 0.22321073814882275 | 0.7364712141640124 | 0.6766994874229113 | |
| 40 |
+----------------------+----------------------+---------------------+ |
| 41 |
| 0.8921795677048454 | 0.08693883262941615 | 0.4219218196852704 | |
| 42 |
+----------------------+----------------------+---------------------+ |
| 43 |
| 0.029797219438070344 | 0.21863797480360336 | 0.5053552881033624 | |
| 44 |
+----------------------+----------------------+---------------------+ |
| 45 |
| 0.026535969683863625 | 0.1988376506866485 | 0.6498844377795232 | |
| 46 |
+----------------------+----------------------+---------------------+ |
| 47 |
... |
| 48 |
<BLANKLINE> |
| 49 |
|
| 50 |
Note that the data are generated on the fly and are not stored in memory, |
| 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. |
| 53 |
|
| 54 |
""" |
| 55 |
|
| 56 |
return RandomTable(numflds, numrows, wait=wait, seed=seed) |
| 57 |
|
| 58 |
|
| 59 |
class RandomTable(Table): |
| 60 |
def __init__(self, numflds=5, numrows=100, wait=0, seed=None): |
| 61 |
self.numflds = numflds |
| 62 |
self.numrows = numrows |
| 63 |
self.wait = wait |
| 64 |
if seed is None: |
| 65 |
self.seed = randomseed() |
| 66 |
else: |
| 67 |
self.seed = seed |
| 68 |
|
| 69 |
def __iter__(self): |
| 70 |
nf = self.numflds |
| 71 |
nr = self.numrows |
| 72 |
seed = self.seed |
| 73 |
|
| 74 |
# N.B., we want this to be stable, i.e., same data each time |
| 75 |
pyrandom.seed(seed) |
| 76 |
|
| 77 |
# construct fields |
| 78 |
flds = ["f%s" % n for n in range(nf)] |
| 79 |
yield tuple(flds) |
| 80 |
|
| 81 |
# construct data rows |
| 82 |
for _ in xrange(nr): |
| 83 |
# artificial delay |
| 84 |
if self.wait: |
| 85 |
time.sleep(self.wait) |
| 86 |
yield tuple(pyrandom.random() for n in range(nf)) |
| 87 |
|
| 88 |
def reseed(self): |
| 89 |
self.seed = randomseed() |
| 90 |
|
| 91 |
|
| 92 |
def dummytable( |
| 93 |
numrows=100, |
| 94 |
fields=( |
| 95 |
('foo', partial(pyrandom.randint, 0, 100)), |
| 96 |
('bar', partial(pyrandom.choice, ('apples', 'pears', 'bananas', 'oranges'))), |
| 97 |
('baz', pyrandom.random), |
| 98 |
), |
| 99 |
wait=0, |
| 100 |
seed=None, |
| 101 |
): |
| 102 |
""" |
| 103 |
Construct a table with dummy data. Use `numrows` to specify the number of |
| 104 |
rows. Set `wait` to a float greater than zero to simulate a delay on each |
| 105 |
row generation (number of seconds per row). E.g.:: |
| 106 |
|
| 107 |
>>> import petl as etl |
| 108 |
>>> table1 = etl.dummytable(100, seed=42) |
| 109 |
>>> table1 |
| 110 |
+-----+----------+----------------------+ |
| 111 |
| foo | bar | baz | |
| 112 |
+=====+==========+======================+ |
| 113 |
| 81 | 'apples' | 0.025010755222666936 | |
| 114 |
+-----+----------+----------------------+ |
| 115 |
| 35 | 'pears' | 0.22321073814882275 | |
| 116 |
+-----+----------+----------------------+ |
| 117 |
| 94 | 'apples' | 0.6766994874229113 | |
| 118 |
+-----+----------+----------------------+ |
| 119 |
| 69 | 'apples' | 0.5904925124490397 | |
| 120 |
+-----+----------+----------------------+ |
| 121 |
| 4 | 'apples' | 0.09369523986159245 | |
| 122 |
+-----+----------+----------------------+ |
| 123 |
... |
| 124 |
<BLANKLINE> |
| 125 |
|
| 126 |
>>> import random as pyrandom |
| 127 |
>>> from functools import partial |
| 128 |
>>> fields = [('foo', pyrandom.random), |
| 129 |
... ('bar', partial(pyrandom.randint, 0, 500)), |
| 130 |
... ('baz', partial(pyrandom.choice, ['chocolate', 'strawberry', 'vanilla']))] |
| 131 |
>>> table2 = etl.dummytable(100, fields=fields, seed=42) |
| 132 |
>>> table2 |
| 133 |
+---------------------+-----+-------------+ |
| 134 |
| foo | bar | baz | |
| 135 |
+=====================+=====+=============+ |
| 136 |
| 0.6394267984578837 | 12 | 'vanilla' | |
| 137 |
+---------------------+-----+-------------+ |
| 138 |
| 0.27502931836911926 | 114 | 'chocolate' | |
| 139 |
+---------------------+-----+-------------+ |
| 140 |
| 0.7364712141640124 | 346 | 'vanilla' | |
| 141 |
+---------------------+-----+-------------+ |
| 142 |
| 0.8921795677048454 | 44 | 'vanilla' | |
| 143 |
+---------------------+-----+-------------+ |
| 144 |
| 0.4219218196852704 | 15 | 'chocolate' | |
| 145 |
+---------------------+-----+-------------+ |
| 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 |
| 153 |
|
| 154 |
Data generation functions can be specified via the `fields` keyword |
| 155 |
argument. |
| 156 |
|
| 157 |
Note that the data are generated on the fly and are not stored in memory, |
| 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. |
| 160 |
|
| 161 |
""" |
| 162 |
|
| 163 |
return DummyTable(numrows=numrows, fields=fields, wait=wait, seed=seed) |
| 164 |
|
| 165 |
|
| 166 |
class DummyTable(Table): |
| 167 |
def __init__(self, numrows=100, fields=None, wait=0, seed=None): |
| 168 |
self.numrows = numrows |
| 169 |
self.wait = wait |
| 170 |
if fields is None: |
| 171 |
self.fields = OrderedDict() |
| 172 |
else: |
| 173 |
self.fields = OrderedDict(fields) |
| 174 |
if seed is None: |
| 175 |
self.seed = randomseed() |
| 176 |
else: |
| 177 |
self.seed = seed |
| 178 |
|
| 179 |
def __setitem__(self, item, value): |
| 180 |
self.fields[text_type(item)] = value |
| 181 |
|
| 182 |
def __iter__(self): |
| 183 |
nr = self.numrows |
| 184 |
seed = self.seed |
| 185 |
fields = self.fields.copy() |
| 186 |
|
| 187 |
# N.B., we want this to be stable, i.e., same data each time |
| 188 |
pyrandom.seed(seed) |
| 189 |
|
| 190 |
# construct header row |
| 191 |
hdr = tuple(text_type(f) for f in fields.keys()) |
| 192 |
yield hdr |
| 193 |
|
| 194 |
# construct data rows |
| 195 |
for _ in xrange(nr): |
| 196 |
# artificial delay |
| 197 |
if self.wait: |
| 198 |
time.sleep(self.wait) |
| 199 |
yield tuple(fields[f]() for f in fields) |
| 200 |
|
| 201 |
def reseed(self): |
| 202 |
self.seed = randomseed() |