Skip to content

Commit

Permalink
Fix: DeprecationWarning: Seeding based on hashing is deprecated since…
Browse files Browse the repository at this point in the history
… Python 3.9 and will be removed in a subsequent version.
  • Loading branch information
bmos committed Mar 11, 2024
1 parent 20ff077 commit 41a62b9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Version 1.7.15
* Add unit tests for randomtable, dummytable, and their supporting functions and classes.
By :user:`bmos`, :issue:`657`.

* Fix: DeprecationWarning: Seeding based on hashing is deprecated since Python 3.9 and will be removed in a subsequent version.
By :user:`bmos`, :issue:`657`.

Version 1.7.14
--------------

Expand Down
16 changes: 15 additions & 1 deletion petl/test/util/test_random.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import random

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing module docstring Warning test

Missing module docstring

Check warning

Code scanning / Pylint (reported by Codacy)

Missing module docstring Warning test

Missing module docstring
import time
from functools import partial

from petl.util.random import randomtable, RandomTable, dummytable, DummyTable
from petl.util.random import randomseed, randomtable, RandomTable, dummytable, DummyTable


def test_randomseed():
"""
Ensure that randomseed provides a non-empty string that changes.
"""
seed_1 = randomseed()
time.sleep(1)
seed_2 = randomseed()

assert isinstance(seed_1, str)

Check warning

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Warning test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

Check notice

Code scanning / Semgrep (reported by Codacy)

The application was found using assert in non-test code. Note test

The application was found using assert in non-test code.
assert seed_1 != ""

Check warning

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Warning test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

Check notice

Code scanning / Semgrep (reported by Codacy)

The application was found using assert in non-test code. Note test

The application was found using assert in non-test code.
assert seed_1 != seed_2

Check warning

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Warning test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

Check notice

Code scanning / Semgrep (reported by Codacy)

The application was found using assert in non-test code. Note test

The application was found using assert in non-test code.


def test_randomtable():
Expand Down
33 changes: 26 additions & 7 deletions petl/util/random.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
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.
"""

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 +45,11 @@ def randomtable(numflds=5, numrows=100, wait=0, seed=None):
| 0.026535969683863625 | 0.1988376506866485 | 0.6498844377795232 |
+----------------------+----------------------+---------------------+
...
<BLANKLINE>
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 +63,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 +88,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 +119,7 @@ def dummytable(numrows=100,
| 4 | 'apples' | 0.09369523986159245 |
+-----+----------+----------------------+
...
<BLANKLINE>
>>> # customise fields
... import random
Expand All @@ -132,12 +144,19 @@ def dummytable(numrows=100,
| 0.4219218196852704 | 15 | 'chocolate' |
+---------------------+-----+-------------+
...
<BLANKLINE>
>>> table3_1 = etl.dummytable(50)
>>> table3_2 = etl.dummytable(100)
>>> table3_1[5] == table3_2[5]
False
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 +173,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 +200,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 41a62b9

Please sign in to comment.