コード例 #1
0
class TestHour(unittest.TestCase):
    def setUp(self):
        self.h_1 = Hour("12:53:29")
        self.h_2 = Hour("5:55")

    def test_str(self):
        self.assertEqual(str(self.h_1), "12:53:29")
        self.assertEqual(str(self.h_2), "05:55")

    def test_eq(self):
        self.assertEqual(self.h_1, Hour("12:53:29"))
        self.assertEqual(self.h_2, Hour("5:55"))
        self.assertNotEqual(self.h_1, self.h_2)

    def test_hash(self):
        self.assertEqual(hash(self.h_1), hash(Hour("12:53:29")))
        self.assertEqual(hash(self.h_2), hash(Hour("05:55")))

    def test_add(self):
        h_3 = self.h_1 + self.h_2
        self.assertEqual(h_3, Hour("12:59:24"))

    def test_get_seconds(self):
        self.assertEqual(self.h_1.get_seconds(), 46409)
        self.assertEqual(self.h_2.get_seconds(), 355)

    def test_get_minutes(self):
        self.assertEqual(self.h_1.get_minutes(), 773)
        self.assertEqual(self.h_2.get_minutes(), 5)

    def test_get_hours(self):
        self.assertEqual(self.h_1.get_hours(), 12)
        self.assertEqual(self.h_2.get_hours(), 0)
コード例 #2
0
class Song:
    def __init__(self, title, artist, album, length_):
        self.title = title
        self.artist = artist
        self.album = album
        self.length_ = Hour(length_)

    def __str__(self):
        return "{} - {} from {} - {}".format(self.artist, self.title,
                                             self.album, self.length_)

    def __repr__(self):
        return self.__str__()

    def __eq__(self, other):
        return self.title == other.title\
            and self.artist == other.artist\
            and self.album == other.album\
            and self.length_ == other.length_

    def __hash__(self):
        return hash(self.title) * hash(self.artist) * hash(self.album) * hash(
            self.length_)

    def length(self, hours=False, minutes=False, seconds=False):
        if hours:
            return self.length_.get_hours()
        if minutes:
            return self.length_.get_minutes()
        if seconds:
            return self.length_.get_seconds()
        return str(self.length_)

    def get_title(self):
        return self.title

    def get_artist(self):
        return self.artist

    def get_album(self):
        return self.album

    def get_length(self):
        return self.length_