refactoring python script

master
Totem4 Totem4 1 year ago
parent e0e8979afd
commit e1437dd2f1

@ -2,22 +2,17 @@
import vim import vim
import re import re
import sys
def dec2banana(num, dictstart = None, shiftend = None, minlength = None, dictionary = None):
#defaults
if dictstart is None: dictstart = 0
if shiftend is None: shiftend = 0
if minlength is None: minlength = 0
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
def dec2banana(num):
dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
numdict = len(dictionary) numdict = len(dictionary)
v = num v = num
st = "" st = ""
l = 0 l = 0
i = (numdict - 1 + dictstart + shiftend) % numdict i = (numdict - 1) % numdict
while not (v == 0 and i == (numdict - 1 + dictstart) % numdict and l >= minlength): while not (v == 0 and i == (numdict - 1) % numdict and l >= 0):
r = v % len(dictionary[i]) r = v % len(dictionary[i])
v = int(v / len(dictionary[i])) v = int(v / len(dictionary[i]))
st = dictionary[i][r] + st st = dictionary[i][r] + st
@ -27,18 +22,15 @@ def dec2banana(num, dictstart = None, shiftend = None, minlength = None, diction
return(st) return(st)
def banana2dec(banana, dictstart = None, shiftend = None, dictionary = None): def banana2dec(banana):
#defaults dictionary = [list("bcdfglmnprstvz"), list("aeiou")]
if dictstart is None: dictstart = 0
if shiftend is None: shiftend = 0
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
numdict = len(dictionary) numdict = len(dictionary)
if (len(banana) - shiftend) % numdict != 0: if len(banana) % numdict != 0:
return("Banana non valida") return("Banana non valida")
v = 0 v = 0
for i in range(len(banana)): for i in range(len(banana)):
r = (numdict + i + dictstart) % numdict r = (numdict + i) % numdict
try: try:
v = v * len(dictionary[r]) + dictionary[r].index(banana[i]) v = v * len(dictionary[r]) + dictionary[r].index(banana[i])
except: except:
@ -46,17 +38,14 @@ def banana2dec(banana, dictstart = None, shiftend = None, dictionary = None):
return(v) return(v)
def isbanana(banana, dictstart = None, shiftend = None, dictionary = None): def isbanana(banana):
#defaults dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
if dictstart is None: dictstart = 0
if shiftend is None: shiftend = 0
if dictionary is None: dictionary = [list("bcdfglmnprstvz"), list("aeiou")] #, list("123456")
numdict = len(dictionary) numdict = len(dictionary)
if (len(banana) - shiftend) % numdict != 0:
if len(banana) % numdict != 0:
return(False) return(False)
for i in range(len(banana)): for i in range(len(banana)):
r = (numdict + i + dictstart) % numdict r = (numdict + i) % numdict
if banana[i] not in dictionary[r]: if banana[i] not in dictionary[r]:
return(False) return(False)
@ -66,8 +55,7 @@ def callback(x):
return "%s" % dec2banana(int(x[0], base=10)).upper() return "%s" % dec2banana(int(x[0], base=10)).upper()
def text2banana(text): def text2banana(text):
text = re.sub(r"[0-9][0-9]*",callback,text) return re.sub(r"[0-9][0-9]*",callback,text)
return text
def callback_dec(x): def callback_dec(x):
b = x[0].strip().lower() b = x[0].strip().lower()
@ -77,8 +65,7 @@ def callback_dec(x):
return x[0] return x[0]
def text2dec(text): def text2dec(text):
text = re.sub(r"[A-Z][A-Z]*",callback_dec,text) return re.sub(r"[A-Z][A-Z]*",callback_dec,text)
return text
def get_selected_text(): def get_selected_text():
buf = vim.current.buffer buf = vim.current.buffer

@ -1,3 +1,7 @@
" banana.vim - Convert Base Banana (https://git.lattuga.net/itec/banana) and back
" T4 - totem4@tropici.net
" Version: 0.1
if exists("g:loaded_Banana") || &cp if exists("g:loaded_Banana") || &cp
finish finish
endif endif

Loading…
Cancel
Save