Skip to content

Commit

Permalink
import random as pyrandom in random.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bmos committed Mar 11, 2024
1 parent 41a62b9 commit 435344d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
10 changes: 5 additions & 5 deletions petl/test/util/test_random.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import random
import random as pyrandom

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

Expand Down Expand Up @@ -46,10 +46,10 @@ def test_dummytable_custom_fields():
and that it accepts and uses custom column names provided.
"""
columns = (
('count', partial(random.randint, 0, 100)),
('pet', partial(random.choice, ('dog', 'cat', 'cow'))),
('color', partial(random.choice, ('yellow', 'orange', 'brown'))),
('value', random.random)
('count', partial(pyrandom.randint, 0, 100)),

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Module 'random' has no 'randint' member Warning test

Module 'random' has no 'randint' member

Check warning

Code scanning / Pylint (reported by Codacy)

Module 'random' has no 'randint' member Warning test

Module 'random' has no 'randint' member
('pet', partial(pyrandom.choice, ['dog', 'cat', 'cow', ])),

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Module 'random' has no 'choice' member Warning test

Module 'random' has no 'choice' member

Check warning

Code scanning / Pylint (reported by Codacy)

Module 'random' has no 'choice' member Warning test

Module 'random' has no 'choice' member
('color', partial(pyrandom.choice, ['yellow', 'orange', 'brown'])),

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Module 'random' has no 'choice' member Warning test

Module 'random' has no 'choice' member

Check warning

Code scanning / Pylint (reported by Codacy)

Module 'random' has no 'choice' member Warning test

Module 'random' has no 'choice' member
('value', pyrandom.random),
)
rows = 35

Expand Down
39 changes: 19 additions & 20 deletions petl/util/random.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import absolute_import, print_function, division

import hashlib
import random
import random as pyrandom
import time
from collections import OrderedDict
from functools import partial
Expand Down Expand Up @@ -57,7 +57,6 @@ def randomtable(numflds=5, numrows=100, wait=0, seed=None):


class RandomTable(Table):

def __init__(self, numflds=5, numrows=100, wait=0, seed=None):
self.numflds = numflds
self.numrows = numrows
Expand All @@ -68,35 +67,38 @@ def __init__(self, numflds=5, numrows=100, wait=0, seed=None):
self.seed = seed

def __iter__(self):

nf = self.numflds
nr = self.numrows
seed = self.seed

# N.B., we want this to be stable, i.e., same data each time
random.seed(seed)
pyrandom.seed(seed)

# construct fields
flds = ['f%s' % n for n in range(nf)]
flds = ["f%s" % n for n in range(nf)]

Check warning

Code scanning / Prospector (reported by Codacy)

Formatting a regular string which could be a f-string (consider-using-f-string) Warning

Formatting a regular string which could be a f-string (consider-using-f-string)

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Formatting a regular string which could be an f-string Warning

Formatting a regular string which could be an f-string
yield tuple(flds)

# construct data rows
for _ in xrange(nr):
# artificial delay
if self.wait:
time.sleep(self.wait)
yield tuple(random.random() for n in range(nf))
yield tuple(pyrandom.random() for n in range(nf))

Check notice

Code scanning / Semgrep (reported by Codacy)

Depending on the context, generating weak random numbers may expose cryptographic functions, which rely on these numbers, to be exploitable. Note

Depending on the context, generating weak random numbers may expose cryptographic functions, which rely on these numbers, to be exploitable.

Check warning

Code scanning / Bandit (reported by Codacy)

Standard pseudo-random generators are not suitable for security/cryptographic purposes. Warning

Standard pseudo-random generators are not suitable for security/cryptographic purposes.

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


def dummytable(numrows=100,
fields=(('foo', partial(random.randint, 0, 100)),
('bar', partial(random.choice, ('apples', 'pears',
'bananas', 'oranges'))),
('baz', random.random)),
wait=0, seed=None):
def dummytable(
numrows=100,
fields=(
('foo', partial(pyrandom.randint, 0, 100)),

Check warning

Code scanning / Pylint (reported by Codacy)

Wrong hanging indentation (remove 4 spaces). Warning

Wrong hanging indentation (remove 4 spaces).
('bar', partial(pyrandom.choice, ('apples', 'pears', 'bananas', 'oranges'))),

Check warning

Code scanning / Pylint (reported by Codacy)

Wrong hanging indentation (remove 4 spaces). Warning

Wrong hanging indentation (remove 4 spaces).
('baz', pyrandom.random),

Check warning

Code scanning / Pylint (reported by Codacy)

Wrong hanging indentation (remove 4 spaces). Warning

Wrong hanging indentation (remove 4 spaces).
),
wait=0,
seed=None,
):
"""
Construct a table with dummy data. Use `numrows` to specify the number of
rows. Set `wait` to a float greater than zero to simulate a delay on each
Expand All @@ -121,13 +123,11 @@ def dummytable(numrows=100,
...
<BLANKLINE>
>>> # customise fields
... import random
>>> import random as pyrandom
>>> from functools import partial
>>> fields = [('foo', random.random),
... ('bar', partial(random.randint, 0, 500)),
... ('baz', partial(random.choice,
... ['chocolate', 'strawberry', 'vanilla']))]
>>> fields = [('foo', pyrandom.random),
... ('bar', partial(pyrandom.randint, 0, 500)),
... ('baz', partial(pyrandom.choice, ['chocolate', 'strawberry', 'vanilla']))]
>>> table2 = etl.dummytable(100, fields=fields, seed=42)
>>> table2
+---------------------+-----+-------------+
Expand Down Expand Up @@ -164,7 +164,6 @@ def dummytable(numrows=100,


class DummyTable(Table):

def __init__(self, numrows=100, fields=None, wait=0, seed=None):
self.numrows = numrows
self.wait = wait
Expand All @@ -186,7 +185,7 @@ def __iter__(self):
fields = self.fields.copy()

# N.B., we want this to be stable, i.e., same data each time
random.seed(seed)
pyrandom.seed(seed)

# construct header row
hdr = tuple(text_type(f) for f in fields.keys())
Expand Down

0 comments on commit 435344d

Please sign in to comment.