944,101 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4064
  • Python RSS
Jan 17th, 2007
0

get week list of an year

Expand Post »
Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sharma_vivek82 is offline Offline
18 posts
since Mar 2006
Jan 17th, 2007
0

Re: get week list of an year

What is the question?
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 19th, 2007
0

Re: get week list of an year

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 )
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python code for opening the web browser
Next Thread in Python Forum Timeline: a simple notepad in wxPython





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC