Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7162643

Browse files
committed
mymax: step 01, misstep
1 parent44c97c7 commit7162643

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Q: Why not define ``Number`` as follows?
3+
4+
::
5+
Number = Union[float, Decimal, Fraction]
6+
7+
8+
A: Because ``Union`` is not helpful as a return type.
9+
10+
"Be conservative in what you send, be liberal in what you accept."
11+
—Postel's Law
12+
13+
"""
14+
15+
fromdecimalimportDecimal
16+
fromfractionsimportFraction
17+
fromtypingimportUnion
18+
19+
Number=Union[float,Decimal,Fraction]
20+
21+
22+
defmax(a:Number,b:Number)->Number:
23+
returnaifa>=belseb
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
fromdecimalimportDecimal
2+
fromfractionsimportFraction
3+
4+
importmymax
5+
frommymaximportNumber
6+
7+
importpytest
8+
9+
10+
@pytest.mark.parametrize(
11+
'a, b, expected',
12+
[
13+
(1,2,2),
14+
(-3,-1,-1),
15+
(4,4,4),
16+
],
17+
)
18+
deftest_two_ints(a:int,b:int,expected:int)->None:
19+
result=mymax.max(a,b)
20+
assertresult==expected
21+
22+
23+
deftest_two_numbers()->None:
24+
result=mymax.max(Fraction(1,3),Fraction(1,4))
25+
assertresult==Fraction(1,3)
26+
27+
28+
@pytest.mark.parametrize(
29+
'a, b, expected',
30+
[
31+
(1,2,2),
32+
(0.1,0.01,0.1),
33+
(Fraction(1,3),Fraction(1,2),Fraction(1,2)),
34+
(Decimal('-1.3'),Decimal('-1.2'),Decimal('-1.2')),
35+
],
36+
)
37+
deftest_two_numbers_params(a:Number,b:Number,expected:Number)->None:
38+
result=mymax.max(a,b)
39+
assertresult==expected
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mypy]
2+
python_version = 3.9
3+
warn_unused_configs = True
4+
disallow_incomplete_defs = True
5+
[mypy-pytest]
6+
ignore_missing_imports = True

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp