A pseudo universal data encoder This is useful for web scraping and web crawling anything other than that idk. It seems to work as planned.

I'm curious as to how the community thinks about the way I break the code down into simple functions with descriptive names. My reasoning for writing so many trivial functions is that I think it makes the code read more like pseudo code. In my opinion it makes the code self documented and easier to read. It also adds a big reusability factor.

#-*-coding:utf8;-*-
#qpy:3
#qpy:console

def is_string(data):
    return isinstance(data, str)

def is_bytes(data):
    return isinstance(data, bytes)

def encodeable(data):
    return is_string(data) or is_bytes(data)

def encode_decode(data, *args, **kw):
    kw.setdefault('errors', 'ignore')
    return data.encode(*args, **kw).decode(*args, **kw)

def encode(data, encoding='ascii', **kw):
    if not encodeable(data):
        error_msg = ('Invalid data type',
                         'to_ascii can only encode',
                         'bytes, strings, and raw-strings',
                         'not {}'.format(type(data)))
        raise ValueError(error_msg)

    if is_string(data):
        data = encode_decode(data, encoding, **kw)

    elif is_bytes(data):
        data = data.decode(encoding, **kw)

    return data

def test_encode():
    #invalid = to_ascii({})
    s = str('spam')
    r = r'eaggs'
    b = b'slurm'
    print('Before encoding')
    print(s, r, b)
    print(type(s), type(r), type(b), '\n')

    print('After encoding')
    s, r, b = encode(s), encode(r), encode(b)
    print(encode(s), encode(r), encode(b))
    print(type(s), type(r), type(b))

if __name__ == '__main__':
    test_encode()
def is_string(data):
    return isinstance(data, str)

def is_bytes(data):
    return isinstance(data, bytes)


I personally think that there isn't much gained by adding these two functions, but others may disagree (your post suggests you might). In otherwords, I would just write isinstance(data, str) wherever I see is_string. However, I think encodeable is sufficiently complicated to be a good stand-alone utility function.

I would also move:

s = str('spam')
r = r'eaggs'
b = b'slurm'
print('Before encoding')
print(s, r, b)
print(type(s), type(r), type(b), '\n')

print('After encoding')
s, r, b = encode(s), encode(r), encode(b)
print(encode(s), encode(r), encode(b))
print(type(s), type(r), type(b))


To a doctest.