# https://www.youtube.com/watch?v=eILeIEE3C8c&ab_channel=JackofSome

# pip install msgpack
# msgpack is serialization library for json




import msgpack
import json
import random


def msgpack_example_1():
    example_dict = {i: random.random() for i in range(10000)}

    with open('json_file.json', 'w') as f:
        json.dump(example_dict, f)
    with open('json_file.json') as f:
        back_from_json = json.load(f)

    # Saving and loading
    with open('msgpack_file.msgpack', 'wb') as f:
        # f.write(msgpack.packb(example_dict))
        f.write(msgpack.packb(example_dict, use_single_float=True))
    with open('msgpack_file.msgpack', 'rb') as f:
        back_from_msgpack = msgpack.unpackb(f.read())

    # Data integrity
    print(type(next(iter(back_from_json.keys()))))
    print(type(next(iter(back_from_msgpack.keys()))))


def msgpack_example_2():
    list_of_dicts = [{0: random.random()} for i in range(100)]
    with open('streamed.msgpack', 'wb') as f:
        for d in list_of_dicts:
            f.write(msgpack.packb(d))

    with open('streamed.msgpack', 'rb') as f:
        loaded_list_of_dicts = [item for item in msgpack.Unpacker(f)]

    print(list_of_dicts[3][0], loaded_list_of_dicts[3][0])

if __name__ == '__main__':
    # msgpack_example_1()
    msgpack_example_2()
# https://www.youtube.com/watch?v=eILeIEE3C8c&ab_channel=JackofSome

# pip install msgpack
# msgpack is serialization library for json




import msgpack
import json
import random


def msgpack_example_1():
    example_dict = {i: random.random() for i in range(10000)}

    with open('json_file.json', 'w') as f:
        json.dump(example_dict, f)
    with open('json_file.json') as f:
        back_from_json = json.load(f)

    # Saving and loading
    with open('msgpack_file.msgpack', 'wb') as f:
        # f.write(msgpack.packb(example_dict))
        f.write(msgpack.packb(example_dict, use_single_float=True))
    with open('msgpack_file.msgpack', 'rb') as f:
        back_from_msgpack = msgpack.unpackb(f.read())

    # Data integrity
    print(type(next(iter(back_from_json.keys()))))
    print(type(next(iter(back_from_msgpack.keys()))))


def msgpack_example_2():
    list_of_dicts = [{0: random.random()} for i in range(100)]
    with open('streamed.msgpack', 'wb') as f:
        for d in list_of_dicts:
            f.write(msgpack.packb(d))

    with open('streamed.msgpack', 'rb') as f:
        loaded_list_of_dicts = [item for item in msgpack.Unpacker(f)]

    print(list_of_dicts[3][0], loaded_list_of_dicts[3][0])

if __name__ == '__main__':
    # msgpack_example_1()
    msgpack_example_2()
# https://www.youtube.com/watch?v=eILeIEE3C8c&ab_channel=JackofSome # pip install msgpack # msgpack is serialization library for json # Saving and loading # f.write(msgpack.packb(example_dict)) # Data integrity # msgpack_example_1()