-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileproc.py
259 lines (210 loc) · 6.84 KB
/
fileproc.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import sys,os,imp,re,subprocess,json
def dedup_totalidenticals(FPIn,FPOut=None,Min=80,WindowSize=50000):
if not FPOut:
FPOut=FPIn+'.dedup'
Seen=[]; Sentl=False
with open(FPIn) as FSr:
with open(FPOut,'wt') as FSw:
while not Sentl:
LiNe=FSr.readline()
if not LiNe:
Sentl=True
continue
Line=LiNe.strip()
if Line and len(Line)>Min and Line in Seen:
print('dup found, "'+Line+'" at earlier by '+str(WindowSize-Seen.index(Line)))
else:
if Line:
Seen.append(Line)
if len(Seen)>WindowSize:
Seen.pop(0)
FSw.write(LiNe)
class JsonManip:
def __init__(self,FP,Stuff):
if FP.endswith('.json'):
FP=FP+'.json'
self.fp=FP
self.stuff=Stuff
def encode_stuff_if_nec(self,Stuff):
return Stuff.__dict__
def decode_stuff_if_nec(self,FP):
PureJ=json.loads(FP)
Decoded=dejsonify_diclist(PureJ)
return Decoded
def dump_json(self):
with open(FP,'wt') as FSw:
try:
json.dumps(self.stuff,FSw)
except TypeError:
json.dumps(self.serialise_stuff_if_nec(self.stuff))
def jsonable_p(Obj,DirectP=True):
JsonableAtoms=[ 'str', 'int', 'float' ]
# JsonableCollects=[ 'dict', 'list' ]
# IndirectlyJsonableCollects=[ 'tuple', 'set' ]
if type(Obj).__name__ in JsonableAtoms:
return True,DirectP
else:
if isinstance(Obj,dict):
Obj=list(Obj.keys())+list(Obj.values())
elif isinstance(Obj,tuple) or isinstance(Obj,set):
Obj=list(Obj)
DirectP=False
if isinstance(Obj,list):
List=Obj
for El in List:
(ObjP,DirectP)=jsonable_p(El,DirectP)
if not ObjP:
return False,DirectP
return True,DirectP
else:
return False,DirectP
def jsonify_diclist(DicList):
if isinstance(DicList,dict):
return jsonify_dic(DicList)
elif isinstance(DicList,list):
return jsonify_list(DicList)
def dejsonify_diclist(DicList):
if isinstance(DicList,dict):
return dejsonify_dic(DicList)
elif isinstance(DicList,list):
return dejsonify_list(DicList)
def jsonify_dic(Dic):
NewItems=[]
for Key,Value in Dic.items():
if isinstance(Key,tuple):
NewKey=stringify_halfjsonable(Key)
else:
NewKey=Key
Type=type(Value).__name__
if Type=='list' or Type=='dict':
NewVal=jsonify_diclist(Value)
elif Type=='tuple':
NewVal=stringify_halfjsonable(Value)
else:
NewVal=Value
NewItems.append((NewKey,NewVal,))
Dict={}
Dict.update(NewItems)
return Dict
def dejsonify_dic(Dic):
NewItems=[]
for Key,Value in Dic.items():
if Key.startswith('tuple|:|'):
NewKey=destringify_halfjsonable(Key)
else:
NewKey=Key
Type=type(Value).__name__
if Type=='list' or Type=='dict':
NewVal=dejsonify_diclist(Value)
elif Type=='str' and (Value.startswith('tuple|:|') or Value.startswith('set|:|')):
NewVal=destringify_halfjsonable(Value)
else:
NewVal=Value
NewItems.append((NewKey,NewVal,))
Dict={}
Dict.update(NewItems)
return Dict
def jsonify_list(L):
NewL=[]
for El in L:
Type=type(El).__name__
if Type=='list' or Type=='dict':
NewL.append(jsonify_diclist(El))
elif Type=='tuple':
NewL.append(stringify_halfjsonable(El))
else:
NewL.append(El)
return NewL
def dejsonify_list(L):
NewL=[]
for El in L:
Type=type(Value).__name__
if Type=='list' or Type=='dict':
NewL.append(dejsonify_diclist(El))
elif Type=='tuple':
NewL.append(destringify_halfjsonable(El))
else:
NewL.append(El)
return NewL
def stringify_halfjsonable(HalfJsonable):
Els=[]
for El in HalfJsonable:
if isinstance(El,str):
Els.append(El)
else:
Els.append(type(El).__name__+'|;|'+str(El))
return type(HalfJsonable).__name__+'|:|'+'|-|'.join(Els)
def destringify_halfjsonable(StringifiedTuple):
Type,StEls=StringifiedTuple.split('|:|')
Els=[]
for StEl in StEls.split('|-|'):
if '/=/' in StEl:
Type,St=StEl.split('|-|')
if Type=='int':
Els.append(int(St))
elif Type=='float':
Els.append(float(St))
else:
Els.append(StEl)
if Type=='set':
return set(Els)
elif Type=='tuple':
if Els==['']:
return ()
return tuple(Els)
def ask_filenoexist_execute_json(FP,Function,ArgsKArgs,Message='Use the old file',TO=10,DefaultReuse=True,Backup=True):
import json,myModule
Response=myModule.ask_filenoexist_execute(FP,Function,ArgsKArgs,Message=Message,TO=TO,DefaultReuse=DefaultReuse,Backup=Backup)
if Response is False:
PureJson=json.loads(open(FP,'rt').read())
Json=dejsonify_diclist(PureJson)
return Json
else:
(Bool,Level)=jsonable_p(Response)
if not Bool:
print('not jsonable, only returning the object')
elif Level=='direct':
ToJson=Response
elif Level=='indirect':
ToJson=jsonify_diclist(Response)
open(FP,'wt').write(json.dumps(ToJson))
return Response
def filelines2list(FP,Num=False):
L=[]
for Line in open(FP):
if Num:
L.append(int(Line.strip()))
else:
L.append(Line.strip())
return L
def extract_lines_numbers(FP,Nums,Delete=False,StdOut=True):
if not StdOut:
FSw=open(FP+'_extlines','wt')
for Cntr,Line in enumerate(open(FP)):
if Delete:
Cond=Cntr+1 not in Nums
else:
Cond=Cntr+1 in Nums
if Cond:
if StdOut:
sys.stdout.write(Line)
else:
FSw.write(Line)
if not StdOut:
FSw.close()
def filelines_extract(LineFP,FP,Delete=False,StdOut=False):
Lines=filelines2list(LineFP,Num=True)
extract_lines_numbers(FP,Lines,Delete=Delete,StdOut=StdOut)
def change_ext(FP,NewExt):
return get_stem_ext(FP)[0]+'.'+NewExt
def get_stem_ext(FN):
if '.' in FN:
Matches=re.match(r'(..*)\.(..*)',FN).groups()
else:
Matches=(FN,'')
return Matches
def get_linecount(FP):
print(FP+': counting lines...')
LineCnt=int(subprocess.check_output(['wc','-l',FP]).split()[0].decode())
print('...counted')
return LineCnt