-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_updated_format_new.py
181 lines (155 loc) · 6.8 KB
/
merge_updated_format_new.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
from os import listdir
from os.path import isfile, join
import sys
import pickle
sender_final_stat = {}
receiver_final_stat = {}
userfields = [ 'ADULT_CONTENT', 'HEALTH', 'DRUGS_ALCOHOL_GAMBLING', 'RACE', 'VIOLENCE_CRIME', 'POLITICS', 'RELATION', 'LOCATION','AC','E','I','PH','AD','TO','O','S','P','T','A']
#userfields = ['A','AL', 'C', 'E' ,'ET','OE', 'NC', 'L1', 'L2' , 'L3' , 'L4' , 'L5' , 'L6']
NUMBER_1 = 5 #set this to any number of your choice between 1 and 9 of your choice
NUMBER_2 = 371 #set this to any number of your choice between 1 and 999 of your choice
NUMBER_3 = 4 #set this to any number of your choice between 1 and 9 of your choice
NUMBER_4 = 4 #set this to any number of your choice between 1 and 1000000 of your choice
if(len(sys.argv) != 3):
print("==================================================================================")
print("SORRY!! Please provide the paths to the directories for reading and writing ")
print("==================================================================================")
print("Example: python3 merge_updated_format.py checkpoint_folder_path output_file_path ")
print("==================================================================================")
sys.exit()
read_path = sys.argv[1] # i.e. '/venmo/read/'
write_path = sys.argv[2] # i.e. '/venmo/write/'
def updateStat(username, joined, sender, receiver):
try:
if sender:
if username not in sender_final_stat:
sender_final_stat[username] = {'joined': joined, 'dates': {}}
for dt in sender: #date (year-month)
if dt not in sender_final_stat[username]['dates']:
sender_final_stat[username]['dates'][dt] = {col:0 for col in userfields}
for k in sender[dt]: #'S': sensitive, 'P': personal, 'T': total of senstive + personal, 'A': total notes
if k not in sender_final_stat[username]['dates'][dt]:
continue
sender_final_stat[username]['dates'][dt][k] += sender[dt][k]
if receiver:
if username not in receiver_final_stat:
receiver_final_stat[username] = {'joined': joined, 'dates': {}}
for dt in receiver: #date (year-month)
if dt not in receiver_final_stat[username]['dates']:
receiver_final_stat[username]['dates'][dt] = {col:0 for col in userfields}
for k in receiver[dt]: #'S': sensitive, 'P': personal, 'T': total of senstive + personal, 'A': total notes
if k not in receiver_final_stat[username]['dates'][dt]:
continue
receiver_final_stat[username]['dates'][dt][k] += receiver[dt][k]
except Exception as e:
print(e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
#===============================================================#
# READ & MERGE #
#===============================================================#
files = [f for f in listdir(read_path) if isfile(join(read_path, f))]
for f in files:
print(f)
print("==============================")
print(" PROCESSING INFORMATION ")
print("==============================")
for f in files:
if f[:11] == 'sender.txt.': # 'sender.txt.xxxx'
print(f)
with open(join(read_path, f), "rb") as myFile:
sender_dict = pickle.load(myFile)
for username in sender_dict:
joined = ""
if 'joined' in sender_dict[username]:
joined = sender_dict[username]['joined']
dates = sender_dict[username]['dates']
updateStat(username, joined, dates, {})
if f[:13] == 'receiver.txt.': # 'receiver.txt.xxxx'
print(f)
with open(join(read_path, f), "rb") as myFile:
receiver_dict = pickle.load(myFile)
for username in receiver_dict:
joined = ""
if 'joined' in receiver_dict[username]:
joined = receiver_dict[username]['joined']
dates = receiver_dict[username]['dates']
updateStat(username, joined, {}, dates)
#===============================================================#
# WRITE #
#===============================================================#
# SENDER #
outputfile = open(write_path + 'sender_summary.txt', "w")
scnt = -1
for k,v in sender_final_stat.items():
if(v is None):
continue
x = ""
y = 1
for c in k:
x += str((int(int(ord(c))/NUMBER_1) + NUMBER_2))
y = int(x)*NUMBER_3
scnt = y + NUMBER_4
s = ""
try:
s = str(scnt) + "|"
if('joined' in sender_final_stat[k]):
s = s + str(sender_final_stat[k]['joined'])
s = s + "|"
if('dates' in sender_final_stat[k]):
for kk,vv in sender_final_stat[k]['dates'].items():
s = s + str(kk)
for kkk,vvv in sorted(vv.items()):
s = s + "," + str(vvv)
s = s + ";"
s = s + "|"
if(k in receiver_final_stat and 'dates' in receiver_final_stat[k]):
for kk,vv in receiver_final_stat[k]['dates'].items():
s = s + str(kk)
for kkk,vvv in sorted(vv.items()):
s = s + "," + str(vvv)
s = s + ";"
outputfile.write(s + "\n")
except Exception as e:
print(e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
continue
outputfile.close()
##############
# RECEIVER #
outputfile1 = open(write_path + 'receiver_summary.txt', "w")
rcnt = -1
for k,v in receiver_final_stat.items():
if(v is None or k in sender_final_stat):
continue
x = ""
y = 1
for c in k:
x += str((int(int(ord(c))/NUMBER_1) + NUMBER_2))
y = int(x)*NUMBER_3
rcnt = y + NUMBER_4
s = ""
try:
s = str(rcnt) + "|"
if('joined' in receiver_final_stat[k]):
s = s + str(receiver_final_stat[k]['joined'])
s = s + "|"
if('dates' in receiver_final_stat[k]):
for kk,vv in receiver_final_stat[k]['dates'].items():
s = s + str(kk)
for kkk,vvv in sorted(vv.items()):
s = s + "," + str(vvv)
s = s + ";"
outputfile1.write(s + "\n")
except Exception as e:
print(e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
continue
outputfile1.close()
##############