python - 测试抽象数据类型

标签 python class module abstract-data-type

我创建了一个名为“Time”的 ADT,需要创建一个新的 python 文件来测试它。以下是我所拥有的:

下面的代码使用 datetime 模块的时间功能实现时间 ADT。

import datetime

class Time :
     # Creates an new time instance and initializes it with the given time.
    def __init__ (self, hours, minutes, seconds):
        self.hr = hours
        self.min = minutes
        self.sec = seconds

    def hour(self):
        # Returns the hour as an integer between 0 to 23.
        return int(self.hr)

    def min(self):
        # Returns the minute as an integer between 0 to 59.
        return int(self.min)

    def sec(self):
        # Returns the seconds as an integer between 0 to 59.
        return int(self.sec)

    def numSec(self, otherTime):
        # Returns the number of seconds elapsed between this time and the otherTime.
        return abs(self.sec - otherTime.seconds)

    def advanceBy(self, numSeconds):
        # Advances the time by the given number of seconds.
        if (numSeconds + self.sec) < 60:
             seconds = seconds + numSeconds
        if ((numSeconds / 60) + self.min) < 60:
                minutes = minutes + int(numSeconds / 60)
                seconds = seconds + numSeconds % 60
        else:
                hours = hours + int(numSeconds / 3600)
                minutes = minutes + (numSeconds % 3600)
                seconds = seconds + numSeconds % 60

    def isPM(self):
        # Returns a Boolean indicating if this time is at or after 12 o'clock noon.
        if self.hr > 11:
            return True
        else:
            return False

    def comparable(self, otherTime):
        # Compares this time to the otherTime to determine their logical ordering.
        if datetime.time(otherTime) > datetime.time(self.hr, self.min, self.sec):
            return otherTime + "is after" + (self.hr, self.min, self.sec)
        else:
            return otherTime + "is before"+ (self.hr, self.min, self.sec)

    def toString(self):
        # Returns "HH:MM:SS XX", in the 12-hr format where XX is either AM/PM.
        if self.hr > 11:
            return "%d:%d%d PM" % (hours, minutes, seconds)
        else:
            return "%d:%d%d AM" % (hours, minutes, seconds)

我创建了另一个文件,该文件应该导入此模块(另存为 timeADT.py)并测试每个操作。但是,我不知道如何正确调用每个函数。我想将时间设置为(小时、分钟、秒),但是当我调用 timeADT.hour(5,12,49) 时,它会给我一个错误,并说我应该只有一个参数。

import timeADT

timeADT.hour(5,49,13)

我是 python 新手,对于我从这里去往何处感到非常困惑。任何有关如何创建此“测试”文件的帮助将不胜感激!

谢谢。

最佳答案

您应该首先构造该对象,然后使用它。 试试这个:

from timeADT import Time
time = Time(5,49,13)
print time.hour, time.min, time.sec

关于python - 测试抽象数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584408/

相关文章:

javascript - 如何手动 bundle 两个javascript文件?

Python:删除数据框中除 > < 之间的信息之外的所有字符串

Python - 对方法和 while 循环非常困惑

python - 如何访问用于在 Google App Engine for python 中存储数据的低级 API

java - 如何影响一个类的所有对象? ( java )

javascript - 模块解析失败错误 Unexpected token } - Webpack

python - 我尝试抓取网站,但不断收到 404 错误

PHP 类::为什么声明为新的?

java - 调用函数时数组为空(java)

c# - PRISM 模块 app.config 配置设置