Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dcd038f

Browse files
committed
nannified.
1 parent a0f0a33 commit dcd038f

1 file changed

Lines changed: 70 additions & 70 deletions

File tree

Demo/classes/Dates.py

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
# vi:set tabsize=8:
4040

4141
_MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May',
42-
'June', 'July', 'August', 'September', 'October',
43-
'November', 'December' ]
42+
'June', 'July', 'August', 'September', 'October',
43+
'November', 'December' ]
4444

4545
_DAY_NAMES = [ 'Friday', 'Saturday', 'Sunday', 'Monday',
46-
'Tuesday', 'Wednesday', 'Thursday' ]
46+
'Tuesday', 'Wednesday', 'Thursday' ]
4747

4848
_DAYS_IN_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
4949

@@ -56,114 +56,114 @@
5656

5757
_INT_TYPES = type(1), type(1L)
5858

59-
def _is_leap( year ): # 1 if leap year, else 0
59+
def _is_leap( year ): # 1 if leap year, else 0
6060
if year % 4 != 0: return 0
6161
if year % 400 == 0: return 1
6262
return year % 100 != 0
6363

64-
def _days_in_year( year ): # number of days in year
64+
def _days_in_year( year ): # number of days in year
6565
return 365 + _is_leap(year)
6666

67-
def _days_before_year( year ): # number of days before year
67+
def _days_before_year( year ): # number of days before year
6868
return year*365L + (year+3)/4 - (year+99)/100 + (year+399)/400
6969

70-
def _days_in_month( month, year ): # number of days in month of year
70+
def _days_in_month( month, year ): # number of days in month of year
7171
if month == 2 and _is_leap(year): return 29
7272
return _DAYS_IN_MONTH[month-1]
7373

74-
def _days_before_month( month, year ): # number of days in year before month
74+
def _days_before_month( month, year ): # number of days in year before month
7575
return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year))
7676

77-
def _date2num( date ): # compute ordinal of date.month,day,year
77+
def _date2num( date ): # compute ordinal of date.month,day,year
7878
return _days_before_year( date.year ) + \
79-
_days_before_month( date.month, date.year ) + \
80-
date.day
79+
_days_before_month( date.month, date.year ) + \
80+
date.day
8181

82-
_DI400Y = _days_before_year( 400 ) # number of days in 400 years
82+
_DI400Y = _days_before_year( 400 ) # number of days in 400 years
8383

84-
def _num2date( n ): # return date with ordinal n
84+
def _num2date( n ): # return date with ordinal n
8585
if type(n) not in _INT_TYPES:
86-
raise TypeError, 'argument must be integer: ' + `type(n)`
86+
raise TypeError, 'argument must be integer: ' + `type(n)`
8787

88-
ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj
88+
ans = Date(1,1,1) # arguments irrelevant; just getting a Date obj
8989
del ans.ord, ans.month, ans.day, ans.year # un-initialize it
9090
ans.ord = n
9191

92-
n400 = (n-1)/_DI400Y # # of 400-year blocks preceding
92+
n400 = (n-1)/_DI400Y # # of 400-year blocks preceding
9393
year, n = 400 * n400, n - _DI400Y * n400
9494
more = n / 365
9595
dby = _days_before_year( more )
9696
if dby >= n:
97-
more = more - 1
98-
dby = dby - _days_in_year( more )
97+
more = more - 1
98+
dby = dby - _days_in_year( more )
9999
year, n = year + more, int(n - dby)
100100

101-
try: year = int(year) # chop to int, if it fits
101+
try: year = int(year) # chop to int, if it fits
102102
except (ValueError, OverflowError): pass
103103

104104
month = min( n/29 + 1, 12 )
105105
dbm = _days_before_month( month, year )
106106
if dbm >= n:
107-
month = month - 1
108-
dbm = dbm - _days_in_month( month, year )
107+
month = month - 1
108+
dbm = dbm - _days_in_month( month, year )
109109

110110
ans.month, ans.day, ans.year = month, n-dbm, year
111111
return ans
112112

113-
def _num2day( n ): # return weekday name of day with ordinal n
113+
def _num2day( n ): # return weekday name of day with ordinal n
114114
return _DAY_NAMES[ int(n % 7) ]
115115

116116

117117
class Date:
118118
def __init__( self, month, day, year ):
119-
if not 1 <= month <= 12:
120-
raise ValueError, 'month must be in 1..12: ' + `month`
121-
dim = _days_in_month( month, year )
122-
if not 1 <= day <= dim:
123-
raise ValueError, 'day must be in 1..' + `dim` + ': ' + `day`
124-
self.month, self.day, self.year = month, day, year
125-
self.ord = _date2num( self )
119+
if not 1 <= month <= 12:
120+
raise ValueError, 'month must be in 1..12: ' + `month`
121+
dim = _days_in_month( month, year )
122+
if not 1 <= day <= dim:
123+
raise ValueError, 'day must be in 1..' + `dim` + ': ' + `day`
124+
self.month, self.day, self.year = month, day, year
125+
self.ord = _date2num( self )
126126

127127
# don't allow setting existing attributes
128128
def __setattr__( self, name, value ):
129-
if self.__dict__.has_key(name):
130-
raise AttributeError, 'read-only attribute ' + name
131-
self.__dict__[name] = value
129+
if self.__dict__.has_key(name):
130+
raise AttributeError, 'read-only attribute ' + name
131+
self.__dict__[name] = value
132132

133133
def __cmp__( self, other ):
134-
return cmp( self.ord, other.ord )
134+
return cmp( self.ord, other.ord )
135135

136136
# define a hash function so dates can be used as dictionary keys
137137
def __hash__( self ):
138-
return hash( self.ord )
138+
return hash( self.ord )
139139

140140
# print as, e.g., Mon 16 Aug 1993
141141
def __repr__( self ):
142-
return '%.3s %2d %.3s ' % (
143-
self.weekday(),
144-
self.day,
145-
_MONTH_NAMES[self.month-1] ) + `self.year`
142+
return '%.3s %2d %.3s ' % (
143+
self.weekday(),
144+
self.day,
145+
_MONTH_NAMES[self.month-1] ) + `self.year`
146146

147147
# Python 1.1 coerces neither int+date nor date+int
148148
def __add__( self, n ):
149-
if type(n) not in _INT_TYPES:
150-
raise TypeError, 'can\'t add ' + `type(n)` + ' to date'
151-
return _num2date( self.ord + n )
149+
if type(n) not in _INT_TYPES:
150+
raise TypeError, 'can\'t add ' + `type(n)` + ' to date'
151+
return _num2date( self.ord + n )
152152
__radd__ = __add__ # handle int+date
153153

154154
# Python 1.1 coerces neither date-int nor date-date
155155
def __sub__( self, other ):
156-
if type(other) in _INT_TYPES: # date-int
157-
return _num2date( self.ord - other )
158-
else:
159-
return self.ord - other.ord # date-date
156+
if type(other) in _INT_TYPES: # date-int
157+
return _num2date( self.ord - other )
158+
else:
159+
return self.ord - other.ord # date-date
160160

161161
# complain about int-date
162162
def __rsub__( self, other ):
163-
raise TypeError, 'Can\'t subtract date from integer'
163+
raise TypeError, 'Can\'t subtract date from integer'
164164

165165
def weekday( self ):
166-
return _num2day( self.ord )
166+
return _num2day( self.ord )
167167

168168
def today():
169169
import time
@@ -175,44 +175,44 @@ def test( firstyear, lastyear ):
175175
a = Date(9,30,1913)
176176
b = Date(9,30,1914)
177177
if `a` != 'Tue 30 Sep 1913':
178-
raise DateTestError, '__repr__ failure'
178+
raise DateTestError, '__repr__ failure'
179179
if (not a < b) or a == b or a > b or b != b:
180-
raise DateTestError, '__cmp__ failure'
180+
raise DateTestError, '__cmp__ failure'
181181
if a+365 != b or 365+a != b:
182-
raise DateTestError, '__add__ failure'
182+
raise DateTestError, '__add__ failure'
183183
if b-a != 365 or b-365 != a:
184-
raise DateTestError, '__sub__ failure'
184+
raise DateTestError, '__sub__ failure'
185185
try:
186-
x = 1 - a
187-
raise DateTestError, 'int-date should have failed'
186+
x = 1 - a
187+
raise DateTestError, 'int-date should have failed'
188188
except TypeError:
189-
pass
189+
pass
190190
try:
191-
x = a + b
192-
raise DateTestError, 'date+date should have failed'
191+
x = a + b
192+
raise DateTestError, 'date+date should have failed'
193193
except TypeError:
194-
pass
194+
pass
195195
if a.weekday() != 'Tuesday':
196-
raise DateTestError, 'weekday() failure'
196+
raise DateTestError, 'weekday() failure'
197197
if max(a,b) is not b or min(a,b) is not a:
198-
raise DateTestError, 'min/max failure'
198+
raise DateTestError, 'min/max failure'
199199
d = {a-1:b, b:a+1}
200200
if d[b-366] != b or d[a+(b-a)] != Date(10,1,1913):
201-
raise DateTestError, 'dictionary failure'
201+
raise DateTestError, 'dictionary failure'
202202

203203
# verify date<->number conversions for first and last days for
204204
# all years in firstyear .. lastyear
205205

206206
lord = _days_before_year( firstyear )
207207
y = firstyear
208208
while y <= lastyear:
209-
ford = lord + 1
210-
lord = ford + _days_in_year(y) - 1
211-
fd, ld = Date(1,1,y), Date(12,31,y)
212-
if (fd.ord,ld.ord) != (ford,lord):
213-
raise DateTestError, ('date->num failed', y)
214-
fd, ld = _num2date(ford), _num2date(lord)
215-
if (1,1,y,12,31,y) != \
216-
(fd.month,fd.day,fd.year,ld.month,ld.day,ld.year):
217-
raise DateTestError, ('num->date failed', y)
218-
y = y + 1
209+
ford = lord + 1
210+
lord = ford + _days_in_year(y) - 1
211+
fd, ld = Date(1,1,y), Date(12,31,y)
212+
if (fd.ord,ld.ord) != (ford,lord):
213+
raise DateTestError, ('date->num failed', y)
214+
fd, ld = _num2date(ford), _num2date(lord)
215+
if (1,1,y,12,31,y) != \
216+
(fd.month,fd.day,fd.year,ld.month,ld.day,ld.year):
217+
raise DateTestError, ('num->date failed', y)
218+
y = y + 1

0 commit comments

Comments
 (0)