-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
201 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import struct | ||
import base64 | ||
|
||
# constants | ||
ITEM_HEADER_MAGIC = struct.pack("I", 11223344) | ||
ITEM_HEADER_MAGIC_AND_VERSION = ITEM_HEADER_MAGIC + struct.pack("I", 1) | ||
|
||
OPERAND_TYPE_LONG = 259 | ||
OPERAND_TYPE_DOUBLE = 261 | ||
|
||
|
||
def encode_list(list_value): | ||
typecode = 'l' | ||
if len(list_value) and isinstance(list_value[0], float): | ||
typecode = 'd' | ||
|
||
return encode_array(list_value, typecode) | ||
|
||
|
||
def encode_array(array_value, typecode): | ||
num_items = len(array_value) | ||
operand_type = OPERAND_TYPE_LONG if typecode == 'l' else OPERAND_TYPE_DOUBLE | ||
|
||
encoded_array = ITEM_HEADER_MAGIC_AND_VERSION + struct.pack('II' + typecode * num_items, | ||
num_items * 8, | ||
operand_type, | ||
*array_value) | ||
|
||
return base64.b64encode(encoded_array) | ||
|
||
|
||
def decode(encoded_array): | ||
static_header_len = len(ITEM_HEADER_MAGIC_AND_VERSION) | ||
|
||
# do a quick peek before we decode | ||
if len(encoded_array) <= static_header_len or not encoded_array.startswith(ITEM_HEADER_MAGIC_AND_VERSION): | ||
raise ValueError('Not an encoded array') | ||
|
||
# get header (which contains number of items and type | ||
header = encoded_array[static_header_len:static_header_len+8] | ||
values = encoded_array[static_header_len+len(header):] | ||
|
||
# unpack the header to get the size and operand | ||
unpacked_header = struct.unpack('II', header) | ||
|
||
# get the typecode and number of items | ||
typecode = 'l' if unpacked_header[1] == OPERAND_TYPE_LONG else 'd' | ||
num_items = int(unpacked_header[0] / 8) | ||
|
||
# decode the values | ||
return list(struct.unpack(typecode*num_items, values)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import sys | ||
import datetime | ||
|
||
# used only n py2 | ||
BASE_DATETIME = datetime.datetime(1970, 1, 1) | ||
|
||
|
||
def _get_timestamp_from_datetime_py3(dt): | ||
return dt.replace(tzinfo=datetime.timezone.utc).timestamp() | ||
|
||
|
||
def _get_timestamp_from_datetime_py2(dt): | ||
return (dt - BASE_DATETIME).total_seconds() | ||
|
||
|
||
# _get_timestamp_from_datetime is python version specific. resolve this once | ||
if sys.version_info[0] >= 3: | ||
_get_timestamp_from_datetime = _get_timestamp_from_datetime_py3 | ||
else: | ||
_get_timestamp_from_datetime = _get_timestamp_from_datetime_py2 | ||
|
||
|
||
def encode(dt): | ||
timestamp = _get_timestamp_from_datetime(dt) | ||
|
||
# get integer and float part of nanoseconds | ||
seconds, nanoseconds_float = divmod(timestamp, 1) | ||
nanoseconds = int(nanoseconds_float * 10e9) | ||
|
||
return '{}:{}'.format(int(seconds), nanoseconds) | ||
|
||
|
||
def decode(encoded_dt): | ||
seconds_str, nanoseconds_str = encoded_dt.split(':') | ||
|
||
timestamp = int(seconds_str) + (int(nanoseconds_str) / 10e9) | ||
|
||
return datetime.datetime.utcfromtimestamp(timestamp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters