Skip to content

Python cleanup v2#17

Merged
wouterl merged 8 commits intoDP-3T:masterfrom
jherland:cleanup_v2
Apr 28, 2020
Merged

Python cleanup v2#17
wouterl merged 8 commits intoDP-3T:masterfrom
jherland:cleanup_v2

Conversation

@jherland
Copy link
Copy Markdown
Contributor

Resubmit some initial cleanup patches on top of the rewritten codebase.

There is no 'test_requires' argument to setuptools.setup(). Instead,
pass extras_require with appropriate keys for the other kinds of
dependencies.

Add the development dependencies outlined in README.md.

All dependencies can now be installed with:

  pip install -e .[dev,test]
The example code under examples/ also function as valuable tests.
This adds a tests/test_examples.py file that runs each example script
under examples/ as a separate test case.

For the examples, we simply consider a zero exit code to signal that the
examples completed successfully. A non-zero exit causes a test failure.
…nd line

With pre-commit, flake8 is automatically run only on the diff to be
committed. However, when invoking `flake8` from the command line, the
current config covers all python files found in the entire repository,
including third-party packages in venv.

This patch teaches flake8 to exclude the venv directory as well as any
dotfiles when run from the command line.
We need to provide cryptographically secure random shuffling.

Looking at Python's implementation of random.shuffle()[1], we see that
(a) it is a straightforward implementation of Durstenfeld's shuffle
algorithm[2] (which is as unbiased as its random number source), and
(b) it allows for a custom `random` function to be passed in.

Hence, AFAICS, we should get a cryptographically secure shuffle as long
as we pass in a `random` function that itself is cryptographically
secure.

Quoting the documentation of the secrets module:

  The secrets module provides access to the most secure source of
  randomness that your operating system provides.

  class secrets.SystemRandom

    A class for generating random numbers using the highest-quality
    sources provided by the operating system.

Therefore, the best we can do is AFAICS to simply use the `random`
function available from secrets.SystemRandom class.

[1]: https://github.com/python/cpython/blob/v3.6.10rc1/Lib/random.py#L263
[2]: https://en.wikipedia.org/wiki/Fisher–Yates_shuffle#The_modern_algorithm
[3]: https://docs.python.org/3.6/library/secrets.html#random-numbers
Comment thread tests/test_examples.py
Requested-by: Wouter Lueks <wouter.lueks@epfl.ch>
@wouterl wouterl merged commit 20e1564 into DP-3T:master Apr 28, 2020
@wouterl
Copy link
Copy Markdown
Collaborator

wouterl commented Apr 28, 2020

Fantastic!! Many thanks for the nice code and cleanups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants