Created
March 27, 2015 20:15
-
-
Save josephmilla/181a0d4864e7ba709900 to your computer and use it in GitHub Desktop.
TimeObject
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
import unittest | |
# ************** To be implemented *************** | |
# The TimeObject has a constructor that takes a single string | |
# that will be parsed into components that we need to make a | |
# guess at what the user meant. | |
# | |
# The TimeObject has three properties: | |
# digits: a string representation of all the digits in the input | |
# period: 'am' or 'pm'. Can also be None if 24-hour time | |
# colon_index: The index of a colon in relation to the digits | |
# in the string. | |
class TimeObject: | |
def __init__(self, string): | |
self.digits = ''.join(c for c in string if c.isdigit()) | |
if (string.find('a') != -1) and (string.find('pm') == -1): | |
self.period = 'am' | |
elif (string.find('p') != -1) and (string.find('a') == -1): | |
self.period = 'pm' | |
elif (string.find('a') != -1) and (string.find('p') != -1) and (string.find('pm') == -1): | |
self.period = 'am' | |
elif (string.find('pm') != -1): | |
self.period = 'pm' | |
try: | |
self.colon_index = (''.join(c for c in string if c.isdigit() or c == ':')).index(':') | |
except ValueError: self.colon_index = None | |
class TestParseInputString(unittest.TestCase): | |
# TimeObject.digits should be a string of all the digit | |
# characters in the input. | |
def test_returns_digits(self): | |
time_object = TimeObject('11as23fg44') | |
self.assertEqual(time_object.digits, '112344') | |
# When an 'a' appears in the input, but no 'pm' is | |
# found, the time is considered 'am'. | |
def test_is_am(self): | |
time_object = TimeObject('12kls38akllo') | |
self.assertEqual(time_object.period, 'am') | |
# When a 'p' appears in the input, but no 'a' is | |
# found, the time is considered 'pm'. | |
def test_is_pm(self): | |
time_object = TimeObject('22kljp11kjd') | |
self.assertEqual(time_object.period, 'pm') | |
# When an 'a' appears with a 'p', but NO 'pm' is found, | |
# the time is considered 'am'. | |
def test_is_am_includes_p(self): | |
time_object = TimeObject('11lkjaw33lkjp') | |
self.assertEqual(time_object.period, 'am') | |
# When a consecutive 'p' and 'm' are found, they | |
# trump everything else. The time is considered | |
# 'pm'. | |
def test_is_pm_includes_pm_and_a(self): | |
time_object = TimeObject('11lkjaw33lpmkj') | |
self.assertEqual(time_object.period, 'pm') | |
# The colon index should be the index of a colon | |
# in relation to the digits in the string disregarding | |
# the letters. | |
def test_correct_colon_index_at_0(self): | |
time_object = TimeObject(':11lkjs234kkdj') | |
self.assertEqual(time_object.colon_index, 0) | |
def test_correct_colon_index_at_2(self): | |
time_object = TimeObject('11lk:js234kkdj') | |
self.assertEqual(time_object.colon_index, 2) | |
def test_correct_colon_index_at_5(self): | |
time_object = TimeObject('11lkjs234kkdj:') | |
self.assertEqual(time_object.colon_index, 5) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment