Skip to content

Commit

Permalink
new method of getting a random seed
Browse files Browse the repository at this point in the history
  • Loading branch information
bmos committed Mar 11, 2024
1 parent b7f6193 commit 9d05018
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions petl/util/random.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
from __future__ import absolute_import, print_function, division


import datetime
import hashlib
import random
import time
from collections import OrderedDict
from functools import partial

from petl.compat import xrange, text_type
from petl.util.base import Table


from petl.util.base import Table
def randomseed():
"""
Obtain the hex digest of a sha256 hash of the
current epoch time in nanoseconds. E.g.::
>>> s = randomseed()
>>> assert isinstance(s, str)
>>> assert s != ""

Check warning

Code scanning / Pylint (reported by Codacy)

Trailing whitespace Warning

Trailing whitespace
Note that the data are generated on the fly and are not stored in memory,
so this function can be used to simulate very large tables.
The only supported seed types are: None, int, float, str, bytes, and bytearray.
"""

time_ns = str(time.time()).encode()
hash_time = hashlib.sha256(time_ns).hexdigest()
return hash_time


def randomtable(numflds=5, numrows=100, wait=0, seed=None):
Expand All @@ -36,9 +54,19 @@ def randomtable(numflds=5, numrows=100, wait=0, seed=None):
| 0.026535969683863625 | 0.1988376506866485 | 0.6498844377795232 |
+----------------------+----------------------+---------------------+
...
<BLANKLINE>
>>> table = etl.randomtable(3, 10)
>>> table[0]
('f0', 'f1', 'f2')
>>> table = etl.randomtable(3, 100)
>>> len(table)
101
Note that the data are generated on the fly and are not stored in memory,
so this function can be used to simulate very large tables.
The only supported seed types are: None, int, float, str, bytes, and bytearray.
"""

Expand All @@ -52,7 +80,7 @@ def __init__(self, numflds=5, numrows=100, wait=0, seed=None):
self.numrows = numrows
self.wait = wait
if seed is None:
self.seed = datetime.datetime.now()
self.seed = randomseed()
else:
self.seed = seed

Expand All @@ -77,7 +105,7 @@ def __iter__(self):
yield tuple(random.random() for n in range(nf))

def reseed(self):
self.seed = datetime.datetime.now()
self.seed = randomseed()


def dummytable(numrows=100,
Expand Down Expand Up @@ -108,6 +136,7 @@ def dummytable(numrows=100,
| 4 | 'apples' | 0.09369523986159245 |
+-----+----------+----------------------+
...
<BLANKLINE>
>>> # customise fields
... import random
Expand All @@ -132,12 +161,22 @@ def dummytable(numrows=100,
| 0.4219218196852704 | 15 | 'chocolate' |
+---------------------+-----+-------------+
...
<BLANKLINE>
>>> table3_1 = etl.dummytable(50)
>>> len(table3_1)
51
>>> table3_2 = etl.dummytable(100)
>>> table3_1[5] != table3_2[5]
True
Data generation functions can be specified via the `fields` keyword
argument.
Note that the data are generated on the fly and are not stored in memory,
so this function can be used to simulate very large tables.
The only supported seed types are: None, int, float, str, bytes, and bytearray.
"""

Expand All @@ -154,7 +193,7 @@ def __init__(self, numrows=100, fields=None, wait=0, seed=None):
else:
self.fields = OrderedDict(fields)
if seed is None:
self.seed = datetime.datetime.now()
self.seed = randomseed()
else:
self.seed = seed

Expand All @@ -181,4 +220,4 @@ def __iter__(self):
yield tuple(fields[f]() for f in fields)

def reseed(self):
self.seed = datetime.datetime.now()
self.seed = randomseed()

0 comments on commit 9d05018

Please sign in to comment.