Reputation: 3
Hello, so my issue is what I think is I can't call function twice. I want to loop through all saturdays in different years and use those dates to change date of reports.
Process gets through year 2020 but can't get through year 2021 for some reason. And I'm getting these errors:
Like I'm not able to rewrite d with different year
from datetime import date, timedelta
import datetime
monthsInYearList = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
yearsToDownload = [2020, 2021]
arrReportsList = ['COUNTRY_RETAILER_PET_DOG_PLUS','COUNTRY_RETAILER_PET_CAT_PLUS','COUNTRY_RETAILER_FOOD','COUNTRY_RETAILER_GRO','COUNTRY_RETAILER_CONF']
def allSundays(year):
d = date(year, 1, 1)
d += timedelta(days = 5 -d.weekday())
while d.year == year:
yield d
d += timedelta(days=7)
#Loop in all years
for processYear in yearsToDownload:
print ("Processing year:",processYear)
#All sundays in given year
AllSundaysDates = list(allSundays(processYear))
#Loop in months
for sMonth in monthsInYearList:
#index of month Jan=1 Feb=2
monthIndex = monthsInYearList.index(sMonth)+1
print("Processing month:",sMonth)
#Clear list
monthDatesList = []
#Loop trough all sundays
for d in AllSundaysDates:
devidedDate = datetime.datetime.strptime(str(d), "%Y-%m-%d")
#Process only given Month
if (monthIndex == devidedDate.month):
#Write given date to list
monthDatesList.append(d)
#Loop trough reports
for sReportName in arrReportsList:
#Write reportName
print("----------------------")
print(sReportName+": ")
print("----------------------")
#Write found dates
for date in monthDatesList:
print(date)
But when I tried to get rid of any unnecessary code it works just fine and I can get through 2020 and 2021 with no issue.
for processYear in yearsToDownload:
AllSundaysDates = list(allSundays(processYear))
for sundayDate in AllSundaysDates:
print (sundayDate)
My question is: Why process does not allow me to use different year in big code but when I use smaller one it works ?
If you got any suggestions how to fix this, please let me know. Thank you.
Wish you wonderful life, kind regards -lostDove 999
Upvotes: 0
Views: 643
Reputation: 721
In the last for loop of your code:
for date in monthDatesList:
print(date)
you are reassigning the datetime
module's date
function to an element in monthDatesList
. I would rename it to something like monthDate
to make sure you're not overwriting the date
function. Always be careful when naming variables to not overwrite anything you're importing
Upvotes: 2