Argparse outside functions

This commit is contained in:
itec78 2020-08-30 14:59:07 +02:00
parent a3e975edbd
commit b91da15172
5 changed files with 55 additions and 57 deletions

10
ananas2dec.py Executable file
View file

@ -0,0 +1,10 @@
#!/usr/bin/env python3
import argparse
import banana
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert ananas string to dec")
parser.add_argument("ananas", help="String to be converted")
args = parser.parse_args()
print(banana.banana2dec(args.ananas, 1, 0))

View file

@ -1,76 +1,36 @@
#!/usr/bin/env python3
import argparse
def dec2banana():
parser = argparse.ArgumentParser(description="Convert dec number to banana")
parser.add_argument("num", help="number to be converted", type=int)
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs='+', default=[list("bcdfglmnprstvz"), list("aeiou")]) # , list("123456")
parser.add_argument("--exactlength", help="Set exact length", type=int, default=0)
parser.add_argument("--minlength", help="Set minimum length", type=int, default=0)
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)
args = parser.parse_args()
dictionary = args.dictionary
def dec2banana(num, dictstart = 0, shiftend = 0, minlength = 0, dictionary = [list("bcdfglmnprstvz"), list("aeiou")]):
numdict = len(dictionary)
minlength = args.minlength
v = args.num
v = num
st = ""
l = 0
if args.exactlength == 0:
i = (numdict - 1 + args.dictstart + args.shiftend) % numdict
while not (v == 0 and i == (numdict - 1 + args.dictstart) % numdict and l >= args.minlength):
r = v % len(dictionary[i])
v = int(v / len(dictionary[i]))
st = dictionary[i][r] + st
i = (i - 1) % numdict
l += 1
else:
i = (numdict - 1 + args.dictstart + args.shiftend + args.exactlength) % numdict
while not (l >= args.exactlength):
r = v % len(dictionary[i])
v = int(v / len(dictionary[i]))
st = dictionary[i][r] + st
i = (i - 1) % numdict
l += 1
if v > 0:
print("Error, result longer than exactlength")
return
print(st)
i = (numdict - 1 + dictstart + shiftend) % numdict
while not (v == 0 and i == (numdict - 1 + dictstart) % numdict and l >= minlength):
r = v % len(dictionary[i])
v = int(v / len(dictionary[i]))
st = dictionary[i][r] + st
i = (i - 1) % numdict
l += 1
return(st)
def banana2dec():
parser = argparse.ArgumentParser(description="Convert banana string to dec")
parser.add_argument("banana", help="strin to be converted")
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs='+', default=[list("bcdfglmnprstvz"), list("aeiou")]) # , list("123456")
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)
args = parser.parse_args()
dictionary = args.dictionary
def banana2dec(banana, dictstart = 0, shiftend = 0, dictionary = [list("bcdfglmnprstvz"), list("aeiou")] ):
numdict = len(dictionary)
banana = args.banana
v = 0
for i in range(len(banana)):
r = (numdict + i + args.dictstart) % numdict
r = (numdict + i + dictstart) % numdict
try:
v = v * len(dictionary[r]) + dictionary[r].index(banana[i])
except:
print("Carattere non valido in posizione", i+1)
return()
print(v)
return(v)
if __name__ == "__main__":
#dec2banana()
#banana2dec()
print("Ciao sono la libreria banana")

View file

@ -1,5 +1,13 @@
#!/usr/bin/env python3
import argparse
import banana
if __name__ == "__main__":
banana.banana2dec()
parser = argparse.ArgumentParser(description="Convert banana string to dec")
parser.add_argument("banana", help="String to be converted")
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs='+', default=[list("bcdfglmnprstvz"), list("aeiou")]) # , list("123456")
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)
args = parser.parse_args()
print(banana.banana2dec(args.banana, args.dictstart, args.shiftend, args.dictionary))

11
dec2ananas.py Executable file
View file

@ -0,0 +1,11 @@
#!/usr/bin/env python3
import argparse
import banana
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert dec number to banana")
parser.add_argument("num", help="number to be converted", type=int)
parser.add_argument("--minlength", help="Set minimum length", type=int, default=0)
args = parser.parse_args()
print(banana.dec2banana(args.num, 1, 0, args.minlength))

View file

@ -1,5 +1,14 @@
#!/usr/bin/env python3
import argparse
import banana
if __name__ == "__main__":
banana.dec2banana()
parser = argparse.ArgumentParser(description="Convert dec number to banana")
parser.add_argument("num", help="number to be converted", type=int)
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=0)
parser.add_argument("--dictionary", help="Set dictionary", type=list, nargs='+', default=[list("bcdfglmnprstvz"), list("aeiou")]) # , list("123456")
args = parser.parse_args()
print(banana.dec2banana(args.num, args.dictstart, args.shiftend, args.minlength, args.dictionary))