base65536 icon indicating copy to clipboard operation
base65536 copied to clipboard

KeyError on Python 3.7.7

Open Filco306 opened this issue 5 years ago • 2 comments

Python version: Python 3.7.7 Used pip3 to install the package.

Traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/base65536/core.py", line 117, in encode
    code_point = BLOCK_START[b2] + b1
KeyError: 's'

Reproduce:

import base65536
base65536.encode('asdsgsdfhsfgdhsdghsrdt')
# OR 
a = 'asdsgsdfhsfgdhsdghsrdt'
base65536.encode(a)

What is the problem here?

Filco306 avatar Jul 07 '20 23:07 Filco306

In core.py the comments say: "Encodes bytes to a base65536 string." and "Decodes bytes from a base65536 string." The input would need to be bytes, rather than a string. This article from AskPython helps clarify how to use python's built-in encode/decode functions. The encoding can be specified, such as utf-8, though I believe it defaults to bytes, which is what to use here. For Example:

import base65536
input_string = "Hello World"
b65k_string = base65536.encode(input_string .encode())
print(b65k_string)

Here is another example, using both encoding and decoding in a single line: print(base65536.decode(base65536.encode("Hello World".encode())).decode()) That is encoding "Hello World' first from plaintext string into bytes, then from bytes into a base65536 string, then decoding the b65k string back into bytes (which would look like b'Hello World' if printed) then decoded back from bytes into a regular string again, and finally printing the string as it was when first input.

Here is one way to implement it into simple functions that automatically encode/decode:

def b65ke(xyz):
	zyx = base65536.encode(yzx.encode())
	return zyx

def b65kd(zyx):
	xzy = base65536.decode(zyx).decode()
	return xyz

tl;dr? the problem is inputting a string, not bytes.

Artifact0 avatar Jul 19 '20 04:07 Artifact0

Oh, now I feel a bit stupid. Of course, I see the b in front of the b"Hello world!" in the docs now.

Perhaps there might be a value of adding a function able to encode strings directly, or even integrate it into the encoding function? It is of course just one simple command, but still more comfortable for the user.

Filco306 avatar Jul 22 '20 20:07 Filco306