0

I'm looking to achieve a search of files based on user input on a date. For example, I'm attempting to write the script to ask user for a range in which to search (month to date, last full week, or specific day).

last full week needs to go backward, to the last full week - so if today is Wednesday, the script should go back to the previous (2)Sunday(s) as a start range to the Saturday that just past, while also accounting for what day it is currently:

Sun(start)---Mon---Tue---Wed---Thu---Fri---Sat(end)---Sun---Mon---Tue---Wed (today)

Howevver, it needs to also account for what day it is in relation to the above, meaning that regardless of what "today" is, the search criteria is always one full week behind (if its Sunday, it just goes to last sunday to 'yesterday, Saturday')

From some examples attempting similar things I've seen here and here, I've attempted to join, modify, and add over the last couple of days:

import datetime
import os

import dateutil.relativedelta

import timedelta


class AuditFileCheck():
    """File Compliance Checker."""

    def datechoice(self):
        """Select date."""
        print("Checking the Audit Files for compliance.")
        print("Today is", datetime.date.today().strftime(" %A."))
        print("\nI will check either for file compliances."
              "\nSearch criteria is either by MONTH to date, last full WEEK, "
              "or individual DAY: [M/W/D]")

        print(now.strftime('Week of %Y%m%d'))
        weekbefore = now - timedelta(days=6)
        print(
            "Week of {weekbefore:%A, %m-%d-%Y} to {now:%A, %m-%d-%Y}").format(**vars())

    input_search = input(
        "Enter search range: Month to date, Prior Week, or by day [M/W/D]")

    def search_month(d, w, m, weekday, month):
        """Establish search from month start, or prior month if today is first of current month."""
        if input_search.lower() == "m":
            current_month = datetime(today.month, 1, today.year)
            if current_month == datetime.today():
                current_month == dateutil.relativedelta.relativedelta(
                    months=-1)
            return m - datetime.timedelta(current_month)
        m = current_month()
        print(current_month)
        # TODO ensure the prior month is used if 'today' is before the end of
        # first full week in current month
        if input_search.lower() == "w":
            prior_week = weekday + d.weekday()
            if prior_week >= 0:  # Target day already happened this week
                prior_week -= 6
            return d - datetime.timedelta(prior_week)
        d = datetime.date.today()
        # 6 = Sunday, 0 = Monday, 1 = Tuesday...
        previous_monday = previous_weekday(d, 6)
        print(previous_monday)
        # TODO search files
        if input_search.lower() == "d":
            day_search = input(
                "Enter a specific date within to search [YYYYMMDD]")
            return d
        print("Searching through...")
        # TODO search files from set_day

This bit:

previous_sunday = previous_weekday(d, 8)

adjusting the integer adjusts how far back it looks.

I'm having some trouble with getting this to function properly. What am I doing wrong here? The more I attempt to play with it, the more confused I become and less it works...

Shawn Gordon
  • 173
  • 2
  • 10
  • What you need is for your datetime.date object to remove 7 days plus the difference of day between a sunday and the current day of the week. I believe you struggle with this part. This is the method weekday() that might help you. So the previous_weekday(d) function should search for the 7+d.weekday() before. You can search for each date with a for loop with a range of 7+d.weekday(). Is it what you need or am I missing the question? – RysDe May 26 '17 at 18:59
  • So, from what I gather in your comment here is that it needs to remove: prior_week -= 6? I'll try that. – Shawn Gordon May 26 '17 at 23:10
  • No. What I'm looking to do is write something that allows the user to input a date, and the script look s back to the most recent full week from Sun to Sat, but also wont allow None or future entry. – Shawn Gordon May 28 '17 at 15:04

0 Answers0