-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from datacamp/feat/add-messaging-utils
Add counting helpers
- Loading branch information
Showing
3 changed files
with
74 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
def get_ord(num): | ||
assert num > 0, "use strictly positive numbers in get_ord()" | ||
nums = { | ||
1: "first", | ||
2: "second", | ||
3: "third", | ||
4: "fourth", | ||
5: "fifth", | ||
6: "sixth", | ||
7: "seventh", | ||
8: "eight", | ||
9: "ninth", | ||
10: "tenth", | ||
} | ||
if num in nums: | ||
return nums[num] | ||
else: | ||
return ( | ||
{1: "{}st", 2: "{}nd", 3: "{}rd"}.get( | ||
num if (num < 20) else (num % 10), "{}th" | ||
) | ||
).format(num) | ||
|
||
|
||
def get_times(num): | ||
nums = {1: "once", 2: "twice"} | ||
if num in nums: | ||
return nums[num] | ||
else: | ||
return "%s times" % get_num(num) | ||
|
||
|
||
def get_num(num): | ||
nums = { | ||
0: "no", | ||
1: "one", | ||
2: "two", | ||
3: "three", | ||
4: "four", | ||
5: "five", | ||
6: "six", | ||
7: "seven", | ||
8: "eight", | ||
} | ||
if num in nums: | ||
return nums[num] | ||
else: | ||
return str(num) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pytest | ||
|
||
from protowhat.utils_messaging import get_ord, get_times, get_num | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input, output", [(1, "first"), (2, "second"), (3, "third"), (11, "11th")] | ||
) | ||
def test_get_ord(input, output): | ||
assert get_ord(input) == output | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input, output", [(1, "one"), (2, "two"), (3, "three"), (11, "11")] | ||
) | ||
def test_get_num(input, output): | ||
assert get_num(input) == output | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input, output", [(1, "once"), (2, "twice"), (3, "three times"), (11, "11 times")] | ||
) | ||
def test_get_times(input, output): | ||
assert get_times(input) == output |