forked from mshuaic/Blockchain_med
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
124 lines (96 loc) · 3.43 KB
/
util.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from Savoir import Savoir
from config import DELIMITER
def getAPI(auth):
api = Savoir(auth["rpcuser"], auth["rpcpasswd"],
auth["rpchost"], auth["rpcport"], auth["chainname"])
return api
def encoder(line):
fields = line.split(DELIMITER)
activity = fields[ATTRIBUTE_INDEX["Activity"]]
resource = fields[ATTRIBUTE_INDEX["Resource"]]
fields[ATTRIBUTE_INDEX["Activity"]] = activities.setdefault(
activity, str(len(activities)))
fields[ATTRIBUTE_INDEX["Resource"]] = resources.setdefault(
resource, str(len(resources)))
temp = 'f'.join(fields)
# padding to hex string format
if len(temp) % 2 != 0:
temp += 'f'
data.append(temp)
from timeit import default_timer as timer
from config import NUM_NODE, ATTRIBUTE_INDEX, ATTRIBUTE_NAME, ATTRIBUTE
import re
ENCODE_FORMAT = 'ascii'
def getAPI(config, num_node):
api = [None] * num_node
for i in range(NUM_NODE):
api[i] = Savoir(config["rpcuser"], config["rpcpasswd"],
config["rpchost"], str(int(config["rpcport"])+i), config["chainname"])
# print(api[i].getinfo())
return api
def createStream(masternode, streamPrefix):
streams = masternode.liststreams()['result']
if streamPrefix not in [item["name"] for item in streams]:
masternode.create('stream', streamPrefix, True)
def measure(func, *args, time=1):
elapsed = timer()
for i in range(time):
func(*args)
elapsed = timer() - elapsed
# print("average insertion time: %f" % (elapsed / time))
return elapsed / time
def display(result):
for item in result:
print(bytes.fromhex(item['data']).decode(ENCODE_FORMAT))
def getData(result, isHex=False):
data = []
if result is None:
return []
for item in result:
if isHex:
data.append(item['data'])
else:
data.append(bytes.fromhex(item['data']).decode(ENCODE_FORMAT))
return data
class Database:
__DB = []
__table = {}
def buildFromFiles(self, files):
for f in files:
self.__DB += [line.rstrip() for line in open(f)]
self.__db2Table(ATTRIBUTE_NAME)
def __len__(self):
return len(self.__DB)
def __getitem__(self, index):
if type(index) is int:
return self.__DB[index]
else:
return self.__table[index]
def isExist(self, values):
if isinstance(values, list):
for v in values:
if v not in self.__DB:
return False
else:
return values in self.__DB
return True
def validate(self, lines, stream, key, verbose=False):
att_dict = {short: full for short,
full in zip(ATTRIBUTE, ATTRIBUTE_NAME)}
if set(self.__table[stream][key]) != set(lines):
if verbose:
print("attribute: %s %s" % (stream, key))
print("lines: \n", *map(repr, lines), sep='\n')
print(
"truth: \n", *map(repr, self.__table[stream][key]), sep='\n')
input()
return False
return True
def __db2Table(self, attributes):
for att in attributes:
self.__table[att] = {}
for data in self.__DB:
values = data.split(DELIMITER)
self.__table[att].setdefault(
values[ATTRIBUTE_INDEX[att]], []).append(data)
database = Database()