0

Is there a suitably Pythonic way of determining if a file was created between particular times on a particular day (or days) of the week - e.g. 09:00 to 17:00 Monday to Friday?

At the moment I have:

def IsEligible(filename, between = None, weekdays = None):
    """
    Determines the elgibility of a file for processing based on 
    the date and time it was modified (mtime). 'weekdays' is a list 
    of day numbers (Monday = 0 .. Sunday = 6) or None to indicate ALL
    days of the week. 'between' is a tuple which contains the
    lower and upper limits (as datetime.time objects) of the periods 
    under consideration or None to indicate ALL times of day. 
    """

    modified = datetime.datetime.fromtimestamp(os.path.getmtime(filename))

    dow = modified.weekday()
    mtime = modified.time()

    if between is not None:
        earliest = min(between)
        latest   = max(between)
        if mtime < earliest or mtime >= latest:
            return False

    if weekdays is not None and dow not in weekdays:
        return False
    print(filename, modified)
    return True

which works fine, but I didn't know if there was something smarter. (I know it's a bit wordy, but hopefully it's more readable that way).

One final thing, I originally used ctime rather than mtime but it didn't produce the results I was after, and seemed just to return the current time, even though the files hadn't been modified or anything since creation. Under what circumstances does the ctime value get reset?

TimGJ
  • 1,584
  • 2
  • 16
  • 32
  • `ctime` is the creation time and `mtime` is the last time the file was modified, `ctime` never gets reset normally unless you change the attributes of the file. This depends on your operating system or more accurately the filesystem used. A simple google will give you a clue of what the three `atime` / `ctime` and `mtime` does: http://www.linux-faqs.info/general/difference-between-mtime-ctime-and-atime – Torxed Nov 15 '15 at 17:30
  • Since `datetime`s are comparable, you could reduce your within interval check logic to `earliest, latest = sorted(between)` then `if not (earliest <= mtime <= latest):`. Your checks for `not None` can be simplified to just `if between:` and `if weekdays and dow not in weekdays`. – martineau Nov 15 '15 at 17:51
  • @martineau I deliberately kept the "is None" verbose as I think it makes more sense to a casual reader. – TimGJ Nov 15 '15 at 19:08
  • @Torxed I've read up about `ctime` (principally as part of debugging the problem). I don't understand why it wasn't behaving as expected, although `mtime` seems to do the trick admirably. – TimGJ Nov 15 '15 at 19:10
  • For a reminder program I wrote once that did a lot of this sort of thing, so I wrote a generic `Interval` class to represent closed intervals The constructor would automatically sort the two values given (which can be any comparable type) and implemented a `__contains__()` method which allows me to write statements like `if value in interval_instance:`. It also had a `__iter__()` method that would `yield` all the intermediate values within the range. Might be over-kill for what you want to do, but since the class is reusable, might be worth the extra effort. – martineau Nov 15 '15 at 19:45
  • @martineau That is a _sweet_ idea. I will try doing that. Thank you. – TimGJ Nov 16 '15 at 08:07

0 Answers0