get week list of an year

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2006
Posts: 18
Reputation: sharma_vivek82 is an unknown quantity at this point 
Solved Threads: 0
sharma_vivek82 sharma_vivek82 is offline Offline
Newbie Poster

get week list of an year

 
0
  #1
Jan 17th, 2007
  1. #Auther : vivek sharma
  2. #date : 06-11-2006
  3. #Description : This script is used to get the list of week in given year, user have to input the year ,
  4.  
  5.  
  6. def WeekFinderFromYear(year):
  7. """ will return all the week from selected year """
  8. import datetime
  9.  
  10. WEEK = {'MONDAY':0,'TUESDAY':1,'WEDNESDAY':2,'THURSDAY':3,'FRIDAY':4,'SATURDAY':5,'SUNDAY':6}
  11. MONTH = {'JANUARY':1,'FEBRUARY':2,'MARCH':3,'APRIL':4,'MAY':5,'JUNE':6,'JULY':7,'AUGUST':8,'SEPTEMBER':9,'OCTOBER':10,'NOVEMBER':11,'DECEMBER':12}
  12.  
  13. year=int(year)
  14. month= MONTH['JANUARY']
  15. day=WEEK['MONDAY']
  16. dt = datetime.date(year,month,1)
  17. dow_lst = []
  18.  
  19. while dt.weekday() != day:
  20. dt = dt + datetime.timedelta(days=1)
  21.  
  22. lst_month = MONTH.values()
  23. lst_month.sort()
  24. for mont in lst_month:
  25. while dt.month == mont:
  26. dow_lst.append(dt)
  27. dt = dt + datetime.timedelta(days=7)
  28.  
  29. #for each in dow_lst:
  30. # print each
  31. return dow_lst
  32.  
  33. year=raw_input("Enter the year:")
  34. week_list = WeekFinderFromYear(year)
  35. for each in week_list:
  36. print each
Last edited by stymiee; Jan 17th, 2007 at 1:37 pm. Reason: added code tags
Best wishes,
Vivek Sharma
Zope/Python Developer
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,141
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 947
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: get week list of an year

 
0
  #2
Jan 17th, 2007
What is the question?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: get week list of an year

 
0
  #3
Jan 19th, 2007
So your script prints out all the mondays of a year, right?
Here's an easier version (well, maybe not really easier, but with less lines )
  1. import calendar
  2.  
  3. year = int(raw_input("Enter year: "))
  4.  
  5. for month in range(1, 13):
  6. mondays = [
  7. day.split()[0] for day in calendar.month(year, month).split("\n")[2:-1]
  8. if not day.startswith(" ")]
  9. for monday in mondays:
  10. print "%i-%02i-%02i" % (year, int(month), int(monday))

PS: I love list comprehensions

EDIT: You may have to write calendar.setfirstweekday(0) to make Monday the first day in the week. What is nice is that if you e.g. want to know all the Tuesdays of a year, just write calendar.setfirstweekday(1).
Last edited by mawe; Jan 19th, 2007 at 12:20 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 3096 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC