コード例 #1
0
ファイル: PrintQueue.py プロジェクト: pywaker/PythonProblems
def simulation(numseconds, pagesperminute):
    labprinter = Printer(pagesperminute)
    printerqueue = Queue()
    waitingtimes = []
    longestqueue = 0

    for currentsecond in range(numseconds):

        if newprinttask():
            newtask = Task(currentsecond)
            printerqueue.enqueue(newtask)
            if printerqueue.size() > longestqueue:
                longestqueue = printerqueue.size()

        if not labprinter.busy() and printerqueue.has_items():
            nexttask = printerqueue.dequeue()
            waitingtimes.append(nexttask.waittime(currentsecond))
            labprinter.startnext(nexttask)

        labprinter.tick()

    averagewait = sum(waitingtimes) / len(waitingtimes)
    print "Average Wait %6.2f secs %3d tasks remaining." % (
        averagewait, printerqueue.size())
    print "The longest the queue got to was %d tasks. \n" % (longestqueue)
コード例 #2
0
 def test_tick(self):
     # Create a new printer, give it a task, check if ticking down to 0 works
     laserjet = Printer(20)
     laserjet.currenttask = True
     laserjet.timeremaining = 10
     self.assertEqual(laserjet.timeremaining, 10)
     laserjet.tick()
     self.assertEqual(laserjet.timeremaining, 9)
     for i in range(9):
         laserjet.tick()
     self.assertEqual(laserjet.currenttask, None)
     self.assertEqual(laserjet.timeremaining, 0)
コード例 #3
0
 def test_tick(self):
     # Create a new printer, give it a task, check if ticking down to 0 works
     laserjet = Printer(20)
     laserjet.currenttask = True
     laserjet.timeremaining = 10
     self.assertEqual(laserjet.timeremaining, 10)
     laserjet.tick()
     self.assertEqual(laserjet.timeremaining, 9)
     for i in range(9):
         laserjet.tick()
     self.assertEqual(laserjet.currenttask, None)
     self.assertEqual(laserjet.timeremaining, 0)
コード例 #4
0
def simulation(numseconds, pagesperminute):
    labprinter = Printer(pagesperminute)
    printerqueue = Queue()
    waitingtimes = []
    longestqueue = 0

    for currentsecond in range(numseconds):

        if newprinttask():
            newtask = Task(currentsecond)
            printerqueue.enqueue(newtask)
            if printerqueue.size() > longestqueue:
                longestqueue = printerqueue.size()

        if not labprinter.busy() and printerqueue.has_items():
            nexttask = printerqueue.dequeue()
            waitingtimes.append(nexttask.waittime(currentsecond))
            labprinter.startnext(nexttask)

        labprinter.tick()

    averagewait = sum(waitingtimes)/len(waitingtimes)
    print "Average Wait %6.2f secs %3d tasks remaining." % (averagewait, printerqueue.size())
    print "The longest the queue got to was %d tasks. \n" % (longestqueue)
コード例 #5
0
 def test_busy(self):
     # Create a printer, give it a task, check if busy
     canon = Printer(12)
     self.assertEqual(canon.busy(), False)
     canon.currenttask = True
     self.assertEqual(canon.busy(), True)
コード例 #6
0
 def test_init(self):
     # Create a printer, check if initial values match
     inkjet = Printer(6)
     self.assertEqual(inkjet.pagerate, 6)
     self.assertEqual(inkjet.currenttask, None)
     self.assertEqual(inkjet.timeremaining, 0)
コード例 #7
0
 def test_busy(self):
     # Create a printer, give it a task, check if busy
     canon = Printer(12)
     self.assertEqual(canon.busy(), False)
     canon.currenttask = True
     self.assertEqual(canon.busy(), True)