Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base64 encoding vs pickling on python objects

I have not used pickling and encoding much with Python. But, just as I came to know about them, I was thinking that I could perform the same operation of converting a python object to a string using 2 different ways.

#1: Using pickle module

>>> encoded = pickle.dumps(range(10))
>>> pickle.loads(encoded)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

#2: Using Base64 Encoding

# First I'l define encode and decode functions for convenience.

def encode(obj):
    string = json.dumps(obj)
    return base64.b64encode(string)

def decode(string):
    decoded_string = base64.b64decode(string)
    return json.loads(decoded_string)

# Encode and Decode.
>>> encoded = encode(range(10))
>>> decode(encoded)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

base64

What are their typical use cases and which method is more efficient in its performance?

UPDATE:

I understand now that my question does not make much sense. Comparing json and pickle would be a better question. Base64 encoding can be performed on both json or pickled strings and is used for creating an ASCII only string encoding; example: for passing data as GET parameter in urls.

like image 554
Pranjal Mittal Avatar asked Mar 21 '23 05:03

Pranjal Mittal


1 Answers

Base64 is a way to encode binary data into a printable string. There are 256 possible values in a byte (8 bits) and less than half of them are printable characters. You have, for instance, line down (0xa), carriage return (0xc), Null-terminator (0x0) and so on. When text-based protocols (namely SMTP) found the need to transfer binary data - a need arose to encode this data into a text format. That's Base64 or MIME encoding. You use 64 printable characters (6 bits) to encode all the binary data - which means that you inflate your data by 33%. It's very inefficient, but it works.

Pickle is Pythons way to serialize (the process of transforming runtime objects into bit-stream objects) its objects. I'd trust the Engineers who developed the system to make it relatively efficient.

Base64 was not designed for that. Base64 is just an inefficient method to store binary data.

like image 53
immortal Avatar answered Apr 26 '23 13:04

immortal