From 9881d09fc5b739725f0527e0c4c1fdf87ac55eb2 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 3 Oct 2020 16:18:07 +0200 Subject: [PATCH] refactor random --- banana/bananalib.py | 37 ++++++++++++++++--------------------- banana/cli.py | 27 ++++++++------------------- 2 files changed, 24 insertions(+), 40 deletions(-) diff --git a/banana/bananalib.py b/banana/bananalib.py index 55639d3..4b0466b 100644 --- a/banana/bananalib.py +++ b/banana/bananalib.py @@ -1,4 +1,5 @@ """Main module.""" +import random class Codec: @@ -60,6 +61,21 @@ class Codec: return True + def random(self, minlength=6): + + numdict = len(self.dictionary) + word = "" + + curr_dict = (numdict - 1 + self.dictstart + self.shiftend) % numdict + final_dict = (numdict - 1 + self.dictstart) % numdict + print("final", final_dict) + while curr_dict != final_dict or len(word) < minlength: + word = random.choice(self.dictionary[curr_dict]) + word + curr_dict = (curr_dict - 1) % numdict + print("curr", curr_dict) + + return word + class BananaCodec(Codec): def __init__(self): @@ -81,26 +97,5 @@ class AvocadoCodec(Codec): super().__init__(1, 1) -def bananarandom(dictstart=0, shiftend=0, minlength=6, dictionary=None): - import random - - # defaults - if dictionary is None: - dictionary = [list("bcdfglmnprstvz"), list("aeiou")] - - numdict = len(dictionary) - st = "" - length = 0 - - i = (numdict - 1 + dictstart + shiftend) % numdict - while not (i == (numdict - 1 + dictstart) % numdict and length >= minlength): - r = random.randint(0, len(dictionary[i]) - 1) - st = dictionary[i][r] + st - i = (i - 1) % numdict - length += 1 - - return st - - if __name__ == "__main__": print("Ciao sono la libreria banana") diff --git a/banana/cli.py b/banana/cli.py index ebfa113..2185933 100644 --- a/banana/cli.py +++ b/banana/cli.py @@ -39,6 +39,10 @@ def main_check(args): sys.exit(1) +def main_random(args): + print(get_codec(args).random(minlength=args.minlength)) + + def main(): parser = argparse.ArgumentParser(description="Convert dec number to banana") parser.add_argument("--ananas", action="store_true") @@ -66,6 +70,10 @@ def main(): check.add_argument("--quiet", "-q", action="store_true") check.set_defaults(func=main_check) + rand = sub.add_parser("random", help="Generate random banana") + rand.add_argument("--minlength", help="Set minimum length", type=int, default=6) + rand.set_defaults(func=main_random) + args = parser.parse_args() if not hasattr(args, "func"): print("You need to select one subcommand. \nUse --help", file=sys.stderr) @@ -74,25 +82,6 @@ def main(): args.func(args) -def bananarandom(): - parser = argparse.ArgumentParser(description="Generate random banana") - parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs="+") - parser.add_argument( - "--dictstart", help="Set starting dictionary", type=int, default=0 - ) - parser.add_argument( - "--shiftend", help="Set shift for ending dictionary", type=int, default=0 - ) - parser.add_argument("--minlength", help="Set minimum length", type=int, default=6) - args = parser.parse_args() - - print( - banana.bananarandom( - args.dictstart, args.shiftend, args.minlength, args.dictionary - ) - ) - - if __name__ == "__main__": # pragma: no cover main()