Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
bb3565d4
Kaydet (Commit)
bb3565d4
authored
Tem 03, 2010
tarafından
Alexander Belopolsky
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #9151: Demo/classes/Dates.py does not work in 3.x
Made minimal changes to make included test pass.
üst
6c96bfe0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
9 deletions
+10
-9
Dates.py
Demo/classes/Dates.py
+10
-9
No files found.
Demo/classes/Dates.py
Dosyayı görüntüle @
bb3565d4
...
@@ -39,7 +39,7 @@
...
@@ -39,7 +39,7 @@
# Note that as of Python 2.3, a datetime module is included in the stardard
# Note that as of Python 2.3, a datetime module is included in the stardard
# library.
# library.
# vi:set tabsize=8:
import
functools
_MONTH_NAMES
=
[
'January'
,
'February'
,
'March'
,
'April'
,
'May'
,
_MONTH_NAMES
=
[
'January'
,
'February'
,
'March'
,
'April'
,
'May'
,
'June'
,
'July'
,
'August'
,
'September'
,
'October'
,
'June'
,
'July'
,
'August'
,
'September'
,
'October'
,
...
@@ -57,8 +57,6 @@ for dim in _DAYS_IN_MONTH:
...
@@ -57,8 +57,6 @@ for dim in _DAYS_IN_MONTH:
dbm
=
dbm
+
dim
dbm
=
dbm
+
dim
del
dbm
,
dim
del
dbm
,
dim
_INT_TYPES
=
type
(
1
),
type
(
1
)
def
_is_leap
(
year
):
# 1 if leap year, else 0
def
_is_leap
(
year
):
# 1 if leap year, else 0
if
year
%
4
!=
0
:
return
0
if
year
%
4
!=
0
:
return
0
if
year
%
400
==
0
:
return
1
if
year
%
400
==
0
:
return
1
...
@@ -85,7 +83,7 @@ def _date2num(date): # compute ordinal of date.month,day,year
...
@@ -85,7 +83,7 @@ def _date2num(date): # compute ordinal of date.month,day,year
_DI400Y
=
_days_before_year
(
400
)
# number of days in 400 years
_DI400Y
=
_days_before_year
(
400
)
# number of days in 400 years
def
_num2date
(
n
):
# return date with ordinal n
def
_num2date
(
n
):
# return date with ordinal n
if
type
(
n
)
not
in
_INT_TYPES
:
if
not
isinstance
(
n
,
int
)
:
raise
TypeError
(
'argument must be integer:
%
r'
%
type
(
n
))
raise
TypeError
(
'argument must be integer:
%
r'
%
type
(
n
))
ans
=
Date
(
1
,
1
,
1
)
# arguments irrelevant; just getting a Date obj
ans
=
Date
(
1
,
1
,
1
)
# arguments irrelevant; just getting a Date obj
...
@@ -116,7 +114,7 @@ def _num2date(n): # return date with ordinal n
...
@@ -116,7 +114,7 @@ def _num2date(n): # return date with ordinal n
def
_num2day
(
n
):
# return weekday name of day with ordinal n
def
_num2day
(
n
):
# return weekday name of day with ordinal n
return
_DAY_NAMES
[
int
(
n
%
7
)
]
return
_DAY_NAMES
[
int
(
n
%
7
)
]
@functools.total_ordering
class
Date
:
class
Date
:
def
__init__
(
self
,
month
,
day
,
year
):
def
__init__
(
self
,
month
,
day
,
year
):
if
not
1
<=
month
<=
12
:
if
not
1
<=
month
<=
12
:
...
@@ -133,8 +131,11 @@ class Date:
...
@@ -133,8 +131,11 @@ class Date:
raise
AttributeError
(
'read-only attribute '
+
name
)
raise
AttributeError
(
'read-only attribute '
+
name
)
self
.
__dict__
[
name
]
=
value
self
.
__dict__
[
name
]
=
value
def
__cmp__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
cmp
(
self
.
ord
,
other
.
ord
)
return
self
.
ord
==
other
.
ord
def
__lt__
(
self
,
other
):
return
self
.
ord
<
other
.
ord
# define a hash function so dates can be used as dictionary keys
# define a hash function so dates can be used as dictionary keys
def
__hash__
(
self
):
def
__hash__
(
self
):
...
@@ -150,14 +151,14 @@ class Date:
...
@@ -150,14 +151,14 @@ class Date:
# Python 1.1 coerces neither int+date nor date+int
# Python 1.1 coerces neither int+date nor date+int
def
__add__
(
self
,
n
):
def
__add__
(
self
,
n
):
if
type
(
n
)
not
in
_INT_TYPES
:
if
not
isinstance
(
n
,
int
)
:
raise
TypeError
(
'can
\'
t add
%
r to date'
%
type
(
n
))
raise
TypeError
(
'can
\'
t add
%
r to date'
%
type
(
n
))
return
_num2date
(
self
.
ord
+
n
)
return
_num2date
(
self
.
ord
+
n
)
__radd__
=
__add__
# handle int+date
__radd__
=
__add__
# handle int+date
# Python 1.1 coerces neither date-int nor date-date
# Python 1.1 coerces neither date-int nor date-date
def
__sub__
(
self
,
other
):
def
__sub__
(
self
,
other
):
if
type
(
other
)
in
_INT_TYPES
:
# date-int
if
isinstance
(
other
,
int
)
:
# date-int
return
_num2date
(
self
.
ord
-
other
)
return
_num2date
(
self
.
ord
-
other
)
else
:
else
:
return
self
.
ord
-
other
.
ord
# date-date
return
self
.
ord
-
other
.
ord
# date-date
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment