Skip to content

Commit 7e28948

Browse files
committed
Removed multi-object update/remove/write methods
1 parent 5bf78cb commit 7e28948

File tree

2 files changed

+16
-78
lines changed

2 files changed

+16
-78
lines changed

melkit/toolkit.py

Lines changed: 15 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, filename: str):
2727
self._fl_list = self._read_fls()
2828
self._cf_list = self._read_cfs()
2929

30-
#------------------------ OBJECT MANIPULATION TOOLS -----------------------#
30+
# ------------------------ OBJECT MANIPULATION TOOLS -----------------------#
3131

3232
def _read_object(self, id_regex: str) -> List[Object]:
3333
'''
@@ -57,7 +57,7 @@ def _read_fls(self) -> List[FL]:
5757
Looks for FLs in the input file and returns them as a list of FL objects.
5858
'''
5959
return self._read_object(r'\bFL\d{3}00\b')
60-
60+
6161
def _read_cfs(self) -> List[CF]:
6262
'''
6363
Looks for CFs in the input file and returns them as a list of CF objects.
@@ -198,7 +198,7 @@ def get_cf(self, cf_id: str) -> CF:
198198
Searches for a CF in the input file and returns it as a CF object.
199199
'''
200200
cf_data = {}
201-
201+
202202
arg_c = 0 # arg counter
203203

204204
with open(self._filename, 'r') as file:
@@ -216,20 +216,21 @@ def get_cf(self, cf_id: str) -> CF:
216216
record_data['CFTYPE'] = record[2]
217217
record_data['NCFARG'] = record[3]
218218
record_data['CFSCAL'] = record[4]
219-
record_data['CFADCN'] = record[5] if len(record) > 5 else 0.0
219+
record_data['CFADCN'] = record[5] if len(
220+
record) > 5 else 0.0
220221
elif termination == '01':
221222
if record[1] in ['.TRUE.', '.FALSE.']:
222223
record_data['LCFVAL'] = record[1]
223224
else:
224-
record_data['CFVALR'] = record[1]
225+
record_data['CFVALR'] = record[1]
225226
elif termination == '02':
226227
record_data['ICFLIM'] = record[1]
227228
if int(record[1]) in [1, 2, 3]:
228229
record_data['CFLIML'] = record[2]
229230
if int(record[1]) in [2, 3]:
230231
record_data['CFLIMU'] = record[3]
231232
elif match(r'0[3-4]', termination):
232-
record_data['FIELDS'] = record[1] # Fixable
233+
record_data['FIELDS'] = record[1] # Fixable
233234
elif termination == '05':
234235
record_data['CLASS'] = record[1]
235236
elif termination == '06':
@@ -262,7 +263,7 @@ def get_fl_list(self) -> List[FL]:
262263
Return the list of CVs in parsed file.
263264
'''
264265
return self._fl_list
265-
266+
266267
def get_cf_list(self) -> List[CF]:
267268
'''
268269
Return the list of CFs in parsed file.
@@ -286,27 +287,14 @@ def remove_object(self, obj_id: str, overwrite: bool = False, new_file: str = No
286287
remove(src_file)
287288
rename(new_file, src_file)
288289

289-
def remove_objects(self, obj_ids: List[str], new_file: str = None) -> None:
290-
'''
291-
Deletes a list of objects from the input file.
292-
'''
293-
294-
src_file = self._filename
295-
new_file = new_file or self._filename + '_NEW'
296-
297-
with open(src_file, 'r') as f1, open(new_file, 'w') as f2:
298-
for line in f1:
299-
if line[:5] not in obj_ids:
300-
f2.write(line)
301-
302290
def write_object(self, obj: Object, overwrite: bool = False, new_file: str = None) -> None:
303291
'''
304292
Writes a new object in the input file.
305293
'''
306294

307295
src_file = self._filename
308296
new_file = new_file or src_file + '_NEW'
309-
297+
310298
with open(src_file, 'r') as f1, open(new_file, 'w') as f2:
311299
written = False
312300
for line in f1:
@@ -320,26 +308,7 @@ def write_object(self, obj: Object, overwrite: bool = False, new_file: str = Non
320308
remove(src_file)
321309
rename(new_file, src_file)
322310

323-
def write_objects(self, obj_list: List[Object], new_file: str = None) -> None:
324-
'''
325-
Writes a new object in the input file.
326-
'''
327-
328-
src_file = self._filename
329-
new_file = new_file or self._filename + '_NEW'
330-
331-
with open(src_file, 'r') as f1, open(new_file, 'w') as f2:
332-
written = False
333-
for line in f1:
334-
if line.startswith('.') and not written:
335-
for obj in obj_list:
336-
f2.write('*\n' + str(obj) + '*\n')
337-
f2.write(line)
338-
written = True
339-
else:
340-
f2.write(line)
341-
342-
def update_object(self, obj: Object, overwrite : bool = False, new_file: str = None) -> None:
311+
def update_object(self, obj: Object, overwrite: bool = False, new_file: str = None) -> None:
343312
'''
344313
Updates object input information.
345314
'''
@@ -365,36 +334,7 @@ def update_object(self, obj: Object, overwrite : bool = False, new_file: str = N
365334
f2.write(line)
366335
remove(tmp_file)
367336

368-
def update_objects(self, obj_list: Object, src_file: str = None, new_file: str = None) -> None:
369-
'''
370-
Updates objects input information.
371-
'''
372-
373-
src_file = src_file or self._filename
374-
tmp_file = self._filename + '_TMP'
375-
new_file = new_file or self._filename + '_NEW'
376-
377-
obj_ids = [obj.get_id() for obj in obj_list]
378-
379-
self.remove_objects(obj_ids, new_file=tmp_file)
380-
self.write_objects(obj_list, src_file=tmp_file, new_file=new_file)
381-
382-
remove(self._filename + '_TMP')
383-
384-
def clear_objects(self, src_file: str = None, new_file: str = None) -> None:
385-
'''
386-
Removes every CV or FL in the input file.
387-
'''
388-
389-
src_file = src_file or self._filename
390-
new_file = new_file or self._filename + '_NEW'
391-
392-
with open(src_file, 'r') as f1, open(new_file, 'w') as f2:
393-
for line in f1:
394-
if line[:2] not in ['CV', 'FL', 'CF', 'TF']:
395-
f2.write(line)
396-
397-
#-------------------------------- EDF TOOLS -------------------------------#
337+
# -------------------------------- EDF TOOLS -------------------------------#
398338

399339
def get_edf_vars(self) -> List[str]:
400340
'''
@@ -442,7 +382,7 @@ def plot_edf(self, datafile: str, y_var: str) -> None:
442382
self.as_dataframe(datafile).plot(x='TIME', y=y_var)
443383
plt.show()
444384

445-
#----------------------------- CONNECTION TOOLS ---------------------------#
385+
# ----------------------------- CONNECTION TOOLS ---------------------------#
446386

447387
def get_fl_connections(self, cv_id: str) -> List[FL]:
448388
'''
@@ -468,7 +408,6 @@ def get_connected_cvs(self, cv_id: str) -> List[CV]:
468408
cv_connected.append(self.id_search(
469409
self._cv_list, 'CV' + fl.get_field('KCVFM')))
470410
return cv_connected
471-
472411

473412
def get_connected_cfs(self, obj_id: str) -> List[CF]:
474413
'''
@@ -491,15 +430,14 @@ def get_connected_cfs(self, obj_id: str) -> List[CF]:
491430
elif obj_id.startswith('CF'):
492431
cf = self.get_cf(obj_id)
493432
cf_values = findall(r'\bCFVALU\.\d+\b', dumps(cf.records))
494-
for value in cf_values:
433+
for value in cf_values:
495434
dot_pos = value.find('.')
496435
cf_id = 'CF' + value[dot_pos + 1:]
497436
cf_connected.append(self.get_cf(cf_id))
498437
cf_connected += self.get_connected_cfs(cf_id)
499438

500439
return cf_connected
501440

502-
503441
def create_submodel(self, cv_id: str, new_file: str = None) -> Union[List[CV], List[FL]]:
504442
'''
505443
Creates a submodel related to a given CV. Those neighbour CVs are made time-independent.
@@ -524,7 +462,7 @@ def create_submodel(self, cv_id: str, new_file: str = None) -> Union[List[CV], L
524462

525463
return sub_cvs, sub_fls
526464

527-
#------------------------------- AUX TOOLS --------------------------------#
465+
# ------------------------------- AUX TOOLS --------------------------------#
528466

529467
def get_used_ids(self, obj_list: List[Object]) -> List[str]:
530468
'''
@@ -559,7 +497,7 @@ def used_to_csv(self, obj_list: List[Object], title='./used.csv') -> DataFrame:
559497
df.to_csv(title, index=False)
560498

561499
return df
562-
500+
563501
def available_to_csv(self, obj_list: List[Object], title='./available.csv') -> DataFrame:
564502
'''
565503
Returns a single-column available IDs DataFrame from an object list. Also exports it as a CSV file.

melkit/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.4
1+
0.1.6

0 commit comments

Comments
 (0)