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
4af8e745
Kaydet (Commit)
4af8e745
authored
Nis 24, 2009
tarafından
Mark Dickinson
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #5812: The two-argument form of the Fraction constructor
now accepts arbitrary Rational instances.
üst
90d47cb4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
13 deletions
+32
-13
fractions.py
Lib/fractions.py
+21
-12
test_fractions.py
Lib/test/test_fractions.py
+7
-1
NEWS
Misc/NEWS
+4
-0
No files found.
Lib/fractions.py
Dosyayı görüntüle @
4af8e745
...
...
@@ -56,7 +56,7 @@ class Fraction(Rational):
__slots__
=
(
'_numerator'
,
'_denominator'
)
# We're immutable, so use __new__ not __init__
def
__new__
(
cls
,
numerator
=
0
,
denominator
=
1
):
def
__new__
(
cls
,
numerator
=
0
,
denominator
=
None
):
"""Constructs a Fraction.
Takes a string like '3/2' or '1.5', another Fraction, or a
...
...
@@ -65,8 +65,13 @@ class Fraction(Rational):
"""
self
=
super
(
Fraction
,
cls
)
.
__new__
(
cls
)
if
type
(
numerator
)
not
in
(
int
,
long
)
and
denominator
==
1
:
if
isinstance
(
numerator
,
basestring
):
if
denominator
is
None
:
if
isinstance
(
numerator
,
Rational
):
self
.
_numerator
=
numerator
.
numerator
self
.
_denominator
=
numerator
.
denominator
return
self
elif
isinstance
(
numerator
,
basestring
):
# Handle construction from strings.
m
=
_RATIONAL_FORMAT
.
match
(
numerator
)
if
m
is
None
:
...
...
@@ -93,18 +98,22 @@ class Fraction(Rational):
if
m
.
group
(
'sign'
)
==
'-'
:
numerator
=
-
numerator
elif
isinstance
(
numerator
,
Rational
):
# Handle copies from other rationals. Integrals get
# caught here too, but it doesn't matter because
# denominator is already 1.
other_rational
=
numerator
numerator
=
other_rational
.
numerator
denominator
=
other_rational
.
denominator
else
:
raise
TypeError
(
"argument should be a string "
"or a Rational instance"
)
elif
(
isinstance
(
numerator
,
Rational
)
and
isinstance
(
denominator
,
Rational
)):
numerator
,
denominator
=
(
numerator
.
numerator
*
denominator
.
denominator
,
denominator
.
numerator
*
numerator
.
denominator
)
else
:
raise
TypeError
(
"both arguments should be "
"Rational instances"
)
if
denominator
==
0
:
raise
ZeroDivisionError
(
'Fraction(
%
s, 0)'
%
numerator
)
numerator
=
operator
.
index
(
numerator
)
denominator
=
operator
.
index
(
denominator
)
g
=
gcd
(
numerator
,
denominator
)
self
.
_numerator
=
numerator
//
g
self
.
_denominator
=
denominator
//
g
...
...
Lib/test/test_fractions.py
Dosyayı görüntüle @
4af8e745
...
...
@@ -60,13 +60,19 @@ class FractionTest(unittest.TestCase):
self
.
assertEquals
((
7
,
15
),
_components
(
F
(
7
,
15
)))
self
.
assertEquals
((
10
**
23
,
1
),
_components
(
F
(
10
**
23
)))
self
.
assertEquals
((
3
,
77
),
_components
(
F
(
F
(
3
,
7
),
11
)))
self
.
assertEquals
((
-
9
,
5
),
_components
(
F
(
2
,
F
(
-
10
,
9
))))
self
.
assertEquals
((
2486
,
2485
),
_components
(
F
(
F
(
22
,
7
),
F
(
355
,
113
))))
self
.
assertRaisesMessage
(
ZeroDivisionError
,
"Fraction(12, 0)"
,
F
,
12
,
0
)
self
.
assertRaises
(
TypeError
,
F
,
1.5
)
self
.
assertRaises
(
TypeError
,
F
,
1.5
+
3
j
)
self
.
assertRaises
(
TypeError
,
F
,
F
(
1
,
2
),
3
)
self
.
assertRaises
(
TypeError
,
F
,
"3/2"
,
3
)
self
.
assertRaises
(
TypeError
,
F
,
3
,
0
j
)
self
.
assertRaises
(
TypeError
,
F
,
3
,
1
j
)
def
testFromString
(
self
):
self
.
assertEquals
((
5
,
1
),
_components
(
F
(
"5"
)))
...
...
Misc/NEWS
Dosyayı görüntüle @
4af8e745
...
...
@@ -244,6 +244,10 @@ Core and Builtins
Library
-------
- Issue #5812: For the two-argument form of the Fraction constructor,
Fraction(m, n), m and n are permitted to be arbitrary Rational
instances.
- Issue #5812: Fraction('1e6') is valid: more generally, any string
that's valid for float() is now valid for Fraction(), with the
exception of strings representing NaNs and infinities.
...
...
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