|
| 1 | +''' |
| 2 | +Binary.py |
| 3 | +Example websocket connection for price data |
| 4 | +Author: Dan Wallace |
| 5 | +Date: 6/5/2021 |
| 6 | +
|
| 7 | +-> Binary.py Example |
| 8 | + 1. This script contains two functions :: convert_to_binary(), unpack_binary_file(). |
| 9 | + -> convert_to_binary() :: Parses python dict(), where key's values are arrays, into a 1D binary file. |
| 10 | + -> convert_to_binary() :: This can be used to optimize large data storage |
| 11 | + -> unpack_binary_file() :: Unpacks the saved binary file back into a usable python dict() |
| 12 | + -> unpack_binary_file() :: Fast method to retrieve large datasets quickly |
| 13 | +
|
| 14 | + 2. For the example we will uses Numpy to create arrays |
| 15 | + -> Type 'pip install numpy' in your terminal if you don't have it already |
| 16 | +
|
| 17 | +-> Struct Notes |
| 18 | + 1. 'wb' overwrites binary file |
| 19 | + 2. 'ab' appends to existing binary file |
| 20 | +''' |
| 21 | + |
| 22 | +# Imports |
| 23 | +import struct |
| 24 | +import numpy as np |
| 25 | +import datetime as dt |
| 26 | +############################################################### |
| 27 | +# Saves python dict() to a binary file called :: binary_data |
| 28 | +############################################################### |
| 29 | +def convert_to_binary(data): |
| 30 | + |
| 31 | + # Print data passed into function |
| 32 | + print(str(dt.datetime.now())+' :: convert_to_binary() :: Converting '+str(data)+ ' to 1D array.') |
| 33 | + |
| 34 | + # Blank array for iterations to append to |
| 35 | + arr = [] |
| 36 | + |
| 37 | + # Loop through the data passed into the function and append to 'arr' |
| 38 | + # This will create a 1 dimensional array (all the data on the same line) |
| 39 | + [[arr.append(y) for y in data[x]] for x in data] |
| 40 | + |
| 41 | + # Insert the length of each dict() key so we can unpack the data later |
| 42 | + # This is needed because with the data flattend to one line, we need to know how to split it later |
| 43 | + arr.insert(0,len(data['key_1'])) |
| 44 | + arr.insert(1,len(data['key_2'])) |
| 45 | + |
| 46 | + # Print our 1 dimensional array |
| 47 | + print(str(dt.datetime.now())+' :: convert_to_binary() :: 1D array :: '+str(arr)+'.') |
| 48 | + |
| 49 | + # Save it to binary by passing the 1 dimensional array into struct |
| 50 | + with open('binary_data', "wb") as f: |
| 51 | + f.write(struct.pack('f' * len(arr), *arr)) |
| 52 | + f.close() |
| 53 | + |
| 54 | + # Print file save confirmation |
| 55 | + print(str(dt.datetime.now())+' :: convert_to_binary() :: Saved to binary file.') |
| 56 | + |
| 57 | +############################################################### |
| 58 | +# Unpacks data from binary file into python dict() |
| 59 | +############################################################### |
| 60 | +def unpack_binary_file(): |
| 61 | + |
| 62 | + # Open binary file and unpack to usable 1D array |
| 63 | + with open('binary_data', "rb") as file: |
| 64 | + f = file.read() |
| 65 | + data = list(struct.unpack('f' * int(len(f) / 4), f)) |
| 66 | + |
| 67 | + # Print raw python list from unpacked binary array |
| 68 | + print(str(dt.datetime.now())+' :: unpack_binary_file() :: Raw array from binary :: '+str(data)+'.') |
| 69 | + |
| 70 | + # Use first two values of struct array to find split point for data |
| 71 | + # The length of each dataset is appended to the 1D array by convert_to_binary() before it saves to binary |
| 72 | + key_1_len, key_2_len = int(data.pop(0)), int(data.pop(0)) |
| 73 | + |
| 74 | + # Split the 1D array into two arrays representing our original data |
| 75 | + key_1, key_2 = data[:key_1_len], data[key_2_len:] |
| 76 | + |
| 77 | + # Append the split unpacked arrays to a new python dict() |
| 78 | + unpacked_dict = {'key_1':key_1,'key_2':key_2} |
| 79 | + |
| 80 | + # Print final python dict() created from unpacked binary data |
| 81 | + print(str(dt.datetime.now())+' :: unpack_binary_file() :: Python dict from unpacked binary '+str(unpacked_dict)+'.') |
| 82 | + |
| 83 | +############################################################### |
| 84 | +# Main |
| 85 | +############################################################### |
| 86 | + |
| 87 | +# Dictionary where each key holds an array |
| 88 | +data = { |
| 89 | + 'key_1':[np.random.randint(100),np.random.randint(100)], |
| 90 | + 'key_2':[np.random.randint(100),np.random.randint(100)] |
| 91 | +} |
| 92 | + |
| 93 | +# Save data dict() to binary file |
| 94 | +convert_to_binary(data) |
| 95 | + |
| 96 | +# Unpack binary file to python dict |
| 97 | +unpack_binary_file('binary_data') |
0 commit comments