forked from OpenBazaar/OpenBazaar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
34 lines (24 loc) · 829 Bytes
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
__author__ = 'brianhoffman'
import threading
import math
def split_array_like(data, length, message_id=None, data_size=None):
length = length or 1
retval = []
blocks = int(math.ceil(len(data) / length))
blocks += 1
for i in range(0, blocks):
if message_id and data_size:
retval.append('%s|%s|%s' % (message_id, data_size, data[i*length:i*length + length]))
else:
retval.append(data[i*length:i*length + length])
return retval
def set_interval(func, sec=0, times=3):
def func_wrapper():
set_interval(func, sec, times-1)
func()
func()
timer = threading.Timer(sec, func_wrapper)
timer.start()
return timer
def sort_by_sequence(packet_a, packet_b):
return packet_a.get_sequence_number() - packet_b.get_sequence_number()