I've got the below def, ITEM comes from a menu. I'm having problems with encoding hex though, as the format of the command is different i.e. var then command. How can I have bottomtext.insert(INSERT, var + process) execute please? At the moment all that happens is that testingtext.encode("hex") is inserted into bottomtext. I did look at exec but there seems to be a general view that exec shouldn't be necessary? Base64 + url encoding work fine. Any assistance please?
Thank you
def gui(item):
def default_encode(s):
s.encode('oops')
print s.encode
encodehexstring = '.encode("hex")'
mapping = {"encode_b64": base64.encodestring,"encode_url": urllib.quote_plus,"encode_hex": encodehexstring}
process = mapping.get(item, default_encode)
def getText():
#clear right text field
bottomtext.delete(1.0, END)
#var equals whats in left
var = middletext.get(1.0, 'end-1c')
#encode it
#insert encoded var in right
if process == encodehexstring:
bottomtext.insert(INSERT, var + process)
else:
bottomtext.insert(INSERT, process(var))
mapping = {'encode_b64': base64.encodestring, 'encode_url': urllib.quote_plus, 'encode_hex': lambda s: s.encode('hex')}
Edit: Or without the lambda (means exactly the same, it's just more to write):
def hexencode(s):
return s.encode("hex")
mapping = {'encode_b64': base64.encodestring, 'encode_url': urllib.quote_plus, 'encode_hex': hexencode}