renamed functions

This commit is contained in:
itec 2020-10-29 16:22:30 +01:00
parent aec04ae2c1
commit 989f3820a7
4 changed files with 116 additions and 116 deletions

View file

@ -1,5 +1,5 @@
"""Top-level package for banana."""
from .bananalib import *
from .libbanana import *
__author__ = """itec"""
__email__ = "itec@ventuordici.org"

View file

@ -1,104 +0,0 @@
"""Main module."""
import logging
import random
log = logging.getLogger("bananalib")
class Codec:
def __init__(self, dictstart=0, shiftend=0, minlength=0, dictionary=None):
self.dictstart = dictstart
self.shiftend = shiftend
if dictionary is None:
self.dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
else:
self.dictionary = dictionary
def encode(self, num, minlength=0):
dictionary = self.dictionary
numdict = len(dictionary)
v = num
st = ""
length = 0
idx = (numdict - 1 + self.dictstart + self.shiftend) % numdict
while not (
v == 0
and idx == (numdict - 1 + self.dictstart) % numdict
and length >= minlength
):
r = v % len(dictionary[idx])
v = int(v / len(dictionary[idx]))
st = dictionary[idx][r] + st
idx = (idx - 1) % numdict
length += 1
return st
def decode(self, word):
dictionary = self.dictionary
numdict = len(dictionary)
if (len(word) - self.shiftend) % numdict != 0:
raise ValueError("Banana non valida")
v = 0
for i in range(len(word)):
r = (numdict + i + self.dictstart) % numdict
try:
v = v * len(dictionary[r]) + dictionary[r].index(word[i])
except (ValueError, KeyError):
raise ValueError("Carattere non valido in posizione %d" % i + 1)
return v
def is_valid(self, word):
dictionary = self.dictionary
numdict = len(dictionary)
if (len(word) - self.shiftend) % numdict != 0:
return False
for i in range(len(word)):
r = (numdict + i + self.dictstart) % numdict
if word[i] not in dictionary[r]:
return False
return True
def random(self, minlength=6, prng=random.Random()):
numdict = len(self.dictionary)
word = ""
if minlength < 1:
return ""
curr_dict = (numdict - 1 + self.dictstart + self.shiftend) % numdict
final_dict = (numdict - 1 + self.dictstart) % numdict
while curr_dict != final_dict or len(word) < minlength:
word = prng.choice(self.dictionary[curr_dict]) + word
curr_dict = (curr_dict - 1) % numdict
return word
class BananaCodec(Codec):
def __init__(self):
super().__init__()
class RibesCodec(Codec):
def __init__(self):
super().__init__(0, 1)
class AnanasCodec(Codec):
def __init__(self):
super().__init__(1, 0)
class AvocadoCodec(Codec):
def __init__(self):
super().__init__(1, 1)
if __name__ == "__main__":
print("Ciao sono la libreria banana")

View file

@ -17,12 +17,12 @@ def get_codec(args):
if args.avocado:
return banana.AvocadoCodec()
kwargs = {}
if args.dictionary:
kwargs["dictionary"] = args.dictionary
if args.dictstart:
kwargs["dictstart"] = args.dictstart
if args.shiftend:
kwargs["shiftend"] = args.shiftend
if args.alphabets:
kwargs["alphabets"] = args.alphabets
if args.shiftalpha:
kwargs["shiftalpha"] = args.shiftalpha
if args.alphaend:
kwargs["alphaend"] = args.alphaend
return banana.Codec(**kwargs)
@ -58,7 +58,7 @@ def colon_separated_list(s):
def main():
parser = argparse.ArgumentParser(description="Convert dec number to banana")
parser = argparse.ArgumentParser(description="Convert number to banana")
parser.add_argument(
"--log-level", choices=["DEBUG", "INFO", "WARN", "ERROR"], default="WARN"
)
@ -67,15 +67,15 @@ def main():
parser.add_argument("--banana", action="store_true")
parser.add_argument("--ribes", action="store_true")
parser.add_argument(
"--dictionary",
help="Set dictionary in colon-separated list",
"--alphabets",
help="Set alphabets in colon-separated list",
type=colon_separated_list,
)
parser.add_argument(
"--dictstart", help="Set starting dictionary", type=int, default=0
"--shiftalpha", help="Set shift for alphabets", type=int, default=0
)
parser.add_argument(
"--shiftend", help="Set shift for ending dictionary", type=int, default=0
"--alphaend", help="Set shift for ending alphabets", type=int, default=0
)
sub = parser.add_subparsers()
encode = sub.add_parser("encode", help="Convert numbers to words")

104
banana/libbanana.py Normal file
View file

@ -0,0 +1,104 @@
"""Main module."""
import logging
import random
log = logging.getLogger("libbanana")
class Codec:
def __init__(self, shiftalpha=0, alphaend=0, minlength=0, alphabets=None):
self.shiftalpha = shiftalpha
self.alphaend = alphaend
if alphabets is None:
self.alphabets = [list("bcdfglmnprstvz"), list("aeiou")]
else:
self.alphabets = alphabets
def encode(self, num, minlength=1):
alphabets = self.alphabets
numalpha = len(alphabets)
v = num
st = ""
length = 0
idx = (numalpha - 1 + self.shiftalpha + self.alphaend) % numalpha
while not (
v == 0
and idx == (numalpha - 1 + self.shiftalpha) % numalpha
and length >= minlength
):
r = v % len(alphabets[idx])
v = int(v / len(alphabets[idx]))
st = alphabets[idx][r] + st
idx = (idx - 1) % numalpha
length += 1
return st
def decode(self, word):
alphabets = self.alphabets
numalpha = len(alphabets)
if (len(word) - self.alphaend) % numalpha != 0:
raise ValueError("Banana non valida")
v = 0
for i in range(len(word)):
r = (numalpha + i + self.shiftalpha) % numalpha
try:
v = v * len(alphabets[r]) + alphabets[r].index(word[i])
except (ValueError, KeyError):
raise ValueError("Carattere non valido in posizione %d" % i + 1)
return v
def is_valid(self, word):
alphabets = self.alphabets
numalpha = len(alphabets)
if (len(word) - self.alphaend) % numalpha != 0:
return False
for i in range(len(word)):
r = (numalpha + i + self.shiftalpha) % numalpha
if word[i] not in alphabets[r]:
return False
return True
def random(self, minlength=6, prng=random.Random()):
numalpha = len(self.alphabets)
word = ""
if minlength < 1:
return ""
curr_alpha = (numalpha - 1 + self.shiftalpha + self.alphaend) % numalpha
final_alpha = (numalpha - 1 + self.shiftalpha) % numalpha
while curr_alpha != final_alpha or len(word) < minlength:
word = prng.choice(self.alphabets[curr_alpha]) + word
curr_alpha = (curr_alpha - 1) % numalpha
return word
class BananaCodec(Codec):
def __init__(self):
super().__init__()
class RibesCodec(Codec):
def __init__(self):
super().__init__(0, 1)
class AnanasCodec(Codec):
def __init__(self):
super().__init__(1, 0)
class AvocadoCodec(Codec):
def __init__(self):
super().__init__(1, 1)
if __name__ == "__main__":
print("Hi I'm the basebanana library")