-
Notifications
You must be signed in to change notification settings - Fork 2
/
vozbase.py
67 lines (59 loc) · 2.28 KB
/
vozbase.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
55
56
57
58
59
60
61
62
63
64
65
66
67
import jsonpickle
import copy
import settings
def serialize(obj, use_deep_copy=True, pretty_serializer=settings.SERIALIZE_PRETTY_JSON_BY_DEFAULT):
if pretty_serializer:
jsonpickle.set_encoder_options('json', sort_keys=True, indent=4)
encoding_method = lambda i:jsonpickle.encode(i,make_refs=False)
else:
encoding_method = jsonpickle.encode
return encoding_method(obj)
def serialize_to_file(obj,file_name,use_deep_copy=True,pretty_serializer=settings.SERIALIZE_PRETTY_JSON_BY_DEFAULT):
open(file_name,'w').write(serialize(obj,use_deep_copy,pretty_serializer))
def unserialize(json):
return jsonpickle.decode(json)
def unserialize_file(file_name):
return unserialize(open(file_name).read())
class VozContainer(object):
def __init__(self):
self.id = id(self)
def _compute_caches(self,parent):
pass
def _clear_caches(self,parent):
pass
def _compute_caches(self,parent):
pass
def serialize(self,use_deep_copy=True,pretty_serializer=settings.SERIALIZE_PRETTY_JSON_BY_DEFAULT):
if pretty_serializer:
jsonpickle.set_encoder_options('json', sort_keys=True, indent=4)
encoding_method = lambda i:jsonpickle.encode(i,make_refs=False)
else:
encoding_method = jsonpickle.encode
if use_deep_copy:
self._prepare_copy()
obj = copy.deepcopy(self) #type: VozContainer
obj._clear_caches(obj)
return encoding_method(obj)
else:
self._clear_caches(self)
ret = encoding_method(self)
self._compute_caches(self)
return ret
def serialize_to_file(self,file_name,use_deep_copy=True, pretty_serializer=settings.SERIALIZE_PRETTY_JSON_BY_DEFAULT):
open(file_name,'w').write(self.serialize(use_deep_copy,pretty_serializer))
def __repr__(self):
return "%s %d" % (self.__class__.__name__, self.id)
def _prepare_copy(self):
pass
class VozTextContainer(object):
def __init__(self,id,offset,length):
"""
:param id: int
:param offset: int
:param length: int
"""
self.id = id
self.offset = offset
self.len = length
def __repr__(self):
return "%s %d" % (self.__class__.__name__, self.id)