Why the < operator does not work as expected in Python?

Viewed 53
import os
import re
files = ['/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i1/log', '/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i2/log', '/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i3/log', '/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i3/continue/log', '/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i3/continue_2/log', '/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i4/log', '/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i5/log']
path = "i*"
cwd = "/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon"
log = "log"
cont = "continue"

repath = path.replace("*", "[0-9]+")
for f1 in files:
    for f2 in reversed(files):
        match1 = re.search(f"({cwd}{os.sep}{repath}{os.sep}{cont}_*[0-9]*{os.sep}{log})", f1)
        match2 = re.search(f"({cwd}{os.sep}{repath}{os.sep}{log})", f2)
        match3 = re.search(f"({cwd}{os.sep}({repath}){os.sep}{cont}_*([0-9]*){os.sep}{log})", f1)
        match4 = re.search(f"({cwd}{os.sep}({repath}){os.sep}{cont}_*([0-9]*){os.sep}{log})", f2)

        print(f1, f2,
              "-" if match3 is None else match3[2],
              "-" if match4 is None else match4[2],
              "-" if match3 is None else ("1" if match3[3] == "" else match3[3]),
              "-" if match4 is None else ("1" if match4[3] == "" else match4[3]),
              )

        if (match1 is not None and match2 is not None and
            (re.search(f"{os.sep}i[0-9]+", f1)[0] == re.search(f"{os.sep}i[0-9]+", f2)[0]
             and f1 == match1.groups(0)[0] and f2 == match2.groups(0)[0])) or \
                (match3 is not None and match4 is not None and
                 (match3[2] == match4[2] and
                  int("1" if match3[3] == "" else match3[3]) < int("1" if match4[3] == "" else match4[3]))):
            files.remove(f2)

I am trying to remove /home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i3/log and /home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i3/continue/log. I can remove the first one but I can not remove the second. The part int("1" if match3[3] == "" else match3[3]) < int("1" if match4[3] == "" else match4[3]) seems not working as expected. Changing < to > removes /home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i3/continue_2/log, which is not desired. Why is it not working? What is the correct code?

1 Answers

I've picked a different algorithm, with one pattern match. it makes the /continue optional, as is the continue_# is also optional.

One you do that, I keep track of the current log file for each instance, if there is a better current log file, then I set the older file to delete. This code works when the log files are encountered in any order (current log first, or current log last). I use a simple scheme which is i#/log is zero, continue/log is 1, and continue_#/log is #. obviously the higher number determines which is the most current.

This has the advantage of scanning the list once, since I use a hash to keep track of what I've already found.


import os
import re

files = [
    "/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i1/log",
    "/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i2/log",
    "/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i3/log",
    "/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i3/continue/log",
    "/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i3/continue_2/log",
    "/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i4/log",
    "/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon/i5/log",
]
path = "i*"
cwd = "/home/egezer/Desktop/SIESTAstepper_tutorial/Carbon"
log = "log"
cont = "continue"
repath = path.replace("*", "[0-9]+")

active_log = {}
to_remove = []
for filename in files:
    logmatch = re.search(
            f"({cwd}{os.sep}({repath})({os.sep}{cont}(_([0-9]+)){{0,1}}){{0,1}}{os.sep}{log})", filename
        
    )
    if not logmatch:
        continue
    _, instance, extended, _, increment = logmatch.groups()
    lognumber = 0
    if extended is not None:
        lognumber = 1 if increment is None else int(increment)
    if instance not in active_log:
        active_log[instance] = (lognumber, filename)
    else:
        if active_log[instance][0] > lognumber:
            to_remove.append(filename)
        else:
            to_remove.append(active_log[instance][1])
            active_log[instance] = (lognumber, filename)

for filename in to_remove:
    print(f"need to remove {filename}")
Related