From db9165e556d7ad7ee9d4c0d4342f2ab73b2887fd Mon Sep 17 00:00:00 2001 From: ulif <uli@gnufix.de> Date: Wed, 20 Apr 2016 09:39:45 +0200 Subject: [PATCH] pep8. Use multiples of 4 as indent, shorten overlong lines and fix other minor things to keep pep8-conformity. --- diceware/random_sources.py | 28 +++++++++++++++------------- tests/test_random_sources.py | 13 +++++++------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/diceware/random_sources.py b/diceware/random_sources.py index 95ac5b6..4b95ab8 100644 --- a/diceware/random_sources.py +++ b/diceware/random_sources.py @@ -132,7 +132,7 @@ class RealDiceRandomSource(object): acceptable range and issue an hint about the procedure. """ if num_rolls == 0: - raise(ValueError) + raise(ValueError) if (self.dice_sides ** num_rolls) < len(sequence): print( "Warning: entropy is reduced! Using only first %s of %s " @@ -151,18 +151,20 @@ class RealDiceRandomSource(object): num_rolls = int(math.log(len(sequence), self.dice_sides)) use_modulo = False if num_rolls < 1: - # If this happens, there are less values in the sequence to choose from than there are dice sides. - # First check whehter the length is 1. Then we don't have to do anything else - if len(sequence) == 1: - # Check whether len(sequence) is a factor of dice.sides - return sequence[0] - if self.dice_sides % len(sequence) == 0: - use_modulo = True - num_rolls = 1 - else: - # otherwise We will perform one extra roll and apply modulo - use_modulo=True - num_rolls = 2 + # If this happens, there are less values in the sequence to + # choose from than there are dice sides. + # First check whehter the length is 1. Then we don't have + # to do anything else + if len(sequence) == 1: + # Check whether len(sequence) is a factor of dice.sides + return sequence[0] + if self.dice_sides % len(sequence) == 0: + use_modulo = True + num_rolls = 1 + else: + # otherwise We will perform one extra roll and apply modulo + use_modulo = True + num_rolls = 2 self.pre_check(num_rolls, sequence) result = 0 for i in range(num_rolls, 0, -1): diff --git a/tests/test_random_sources.py b/tests/test_random_sources.py index c29375a..c101bce 100644 --- a/tests/test_random_sources.py +++ b/tests/test_random_sources.py @@ -258,7 +258,8 @@ class TestRealDiceRandomSource(object): assert "Please roll 5 dice (or a single dice 5 times)." in out def test_sequence_less_than_dice_sides(self, capsys, monkeypatch): - # Test to see whether we can use a n-sided die to choose from a sequence with less than n items + # Test to see whether we can use a n-sided die to choose from + # a sequence with less than n items src = RealDiceRandomSource(None) src.dice_sides = 6 # A length of 1 requires no rolls @@ -268,16 +269,16 @@ class TestRealDiceRandomSource(object): assert "roll" not in out assert picked == 1 # A length of 2,3 only requires 1 roll - for choice_length in (2,3): + for choice_length in (2, 3): self.fake_input_values(["1"], monkeypatch) - picked = src.choice(range(1,choice_length + 1)) + picked = src.choice(range(1, choice_length + 1)) out, err = capsys.readouterr() assert "roll 1 dice" in out assert picked == 1 # A length of 4,5 requires 2 rolls - for choice_length in (4,5): - self.fake_input_values(["1","1"], monkeypatch) - picked = src.choice(range(1,choice_length + 1)) + for choice_length in (4, 5): + self.fake_input_values(["1", "1"], monkeypatch) + picked = src.choice(range(1, choice_length + 1)) out, err = capsys.readouterr() assert "roll 2 dice" in out assert picked == 1 -- GitLab