-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
54 lines (46 loc) · 2.03 KB
/
utils.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
import sys
import array
import struct
__all__ = ('BytesList', 'ArraySave', 'ArrayLoad')
#------------------------------------------------------------------------------#
# Bytes List #
#------------------------------------------------------------------------------#
class BytesList (list):
count_header = struct.Struct ('!I')
def __init__ (self, items = tuple ()):
list.__init__ (self, items)
#--------------------------------------------------------------------------#
# Save | Load #
#--------------------------------------------------------------------------#
def Save (self, stream):
# header (count, sizes)
stream.write (self.count_header.pack (len (self)))
ArraySave (stream, array.array ('i', ((-1 if item is None else len (item)) for item in self)))
# items
for item in self:
if item is not None:
stream.write (item)
@classmethod
def Load (cls, stream):
sizes = ArrayLoad (stream, 'i', cls.count_header.unpack (stream.read (cls.count_header.size)) [0])
return cls ((stream.read (size) if size >= 0 else None) for size in sizes)
#------------------------------------------------------------------------------#
# Array (Save|Load) #
#------------------------------------------------------------------------------#
if sys.version_info [0] < 3:
# Python 2
def ArraySave (stream, array):
stream.write (array.tostring ())
def ArrayLoad (stream, type, count):
sizes = array.array (type)
sizes.fromstring (stream.read (sizes.itemsize * count))
return sizes
else:
def ArraySave (stream, array):
array.tofile (stream)
def ArrayLoad (stream, type, count):
sizes = array.array (type)
sizes.fromfile (stream, count)
return sizes
# vim: nu ft=python columns=120 :