implementing a calendar class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    #16
    Originally posted by bvdet
    Following is a class that does not use Python's datetime module:[code=Python]...
    [/code]
    It is a little complicated and It seems that there is something I don't know. Like I never read something that when defining a method, we don't use self._something

    assume that I have self._month, self._day, self._year has been property defined.
    I have two list of maximum day for regular year and leap year.
    I would like to write a nextDate() method to increase one day of current date, how can I write it?

    [code=python]
    class CalculationDate :
    daysList1 = [31,28,31,30,31, 30,31,31,30,31, 30,31]
    daysList2 = [31,29,31,30,31, 30,31,31,30,31, 30,31]
    def __ini__(m, d, yr):
    self._month...
    self._day...
    self._year... # all of these things are written properly if user enters invalid information date, it will ask to change the date.
    #now I would like to have a nextDay method:
    def nextDay(self):
    # how can I write this
    [/code]

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #17
      Originally posted by python101
      It is a little complicated and It seems that there is something I don't know. Like I never read something that when defining a method, we don't use self._something

      assume that I have self._month, self._day, self._year has been property defined.
      I have two list of maximum day for regular year and leap year.
      I would like to write a nextDate() method to increase one day of current date, how can I write it?

      [code=python]
      class CalculationDate :
      daysList1 = [31,28,31,30,31, 30,31,31,30,31, 30,31]
      daysList2 = [31,29,31,30,31, 30,31,31,30,31, 30,31]
      def __ini__(m, d, yr):
      self._month...
      self._day...
      self._year... # all of these things are written properly if user enters invalid information date, it will ask to change the date.
      #now I would like to have a nextDay method:
      def nextDay(self):
      # how can I write this
      [/code]
      The use of a leading underscore is for variables and methods intended to be non-public. I seldom make variables and methods non-public in the applications I have developed.

      Did you notice that I included a nextDay() method in my post? The key is in the __add__(), monthDayYr(), and dayNo() methods. Method dayNo() returns the day number in the current year and does input validation on the day of the month. Method monthDayYr() returns a month, day, year tuple given a number of days with respect to the current year. Example:
      [code=Python]>>> print a
      June 8, 2007
      >>> a.monthDayYr(a. dayNo())
      (6, 8, 2007)
      >>> a.monthDayYr(1)
      (1, 1, 2007)
      >>> a.monthDayYr(20 000)
      (10, 3, 2061)
      >>> a.dayNo()
      159
      >>> [/code]Method __add__() returns a Dates() object:[code=Python]....def __add__(self, days):
      return Dates('/'.join([str(item) for item in self.monthDayYr (self.dayNo() + days)]))[/code]The method nextDay() is now very simple:[code=Python]....def nextDay(self):
      return self+1[/code][code=Python]>>> print d
      December 31, 2000
      >>> d.nextDay()
      <__main__.Dat es object at 0x00DC70B0>
      >>> print d.nextDay()
      January 1, 2001
      >>> [/code]Following is an example using a Dates() object to parse the input and the Python datetime module:[code=Python]if __name__ == '__main__':
      import datetime
      d = Dates('6/8/2007')
      dt = datetime.dateti me(d.year, d.month, d.day)
      oneday = datetime.timede lta(days=1)
      newdate = dt + oneday
      print newdate[/code]Output: >>> 2007-06-09 00:00:00

      HTH

      Comment

      Working...