From ec8aab6e4c992d155ba389e5574842c43a323b0e Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 11 Oct 2009 14:27:51 +0000 Subject: [PATCH] Merged revisions 75354 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r75354 | georg.brandl | 2009-10-11 16:23:49 +0200 (So, 11 Okt 2009) | 1 line Update lpwatch script. ........ --- Demo/scripts/lpwatch.py | 84 +++++++++++++++++++---------------------- Demo/scripts/queens.py | 6 +-- 2 files changed, 41 insertions(+), 49 deletions(-) diff --git a/Demo/scripts/lpwatch.py b/Demo/scripts/lpwatch.py index 262e562ea93..90b3ecf0ede 100755 --- a/Demo/scripts/lpwatch.py +++ b/Demo/scripts/lpwatch.py @@ -3,10 +3,9 @@ # Watch line printer queue(s). # Intended for BSD 4.3 lpq. -import posix +import os import sys import time -import string DEF_PRINTER = 'psc' DEF_DELAY = 10 @@ -14,94 +13,87 @@ DEF_DELAY = 10 def main(): delay = DEF_DELAY # XXX Use getopt() later try: - thisuser = posix.environ['LOGNAME'] + thisuser = os.environ['LOGNAME'] except: - thisuser = posix.environ['USER'] + thisuser = os.environ['USER'] printers = sys.argv[1:] if printers: # Strip '-P' from printer names just in case # the user specified it... - for i in range(len(printers)): - if printers[i][:2] == '-P': - printers[i] = printers[i][2:] + for i, name in enumerate(printers): + if name[:2] == '-P': + printers[i] = name[2:] else: - if 'PRINTER' in posix.environ: - printers = [posix.environ['PRINTER']] + if 'PRINTER' in os.environ: + printers = [os.environ['PRINTER']] else: printers = [DEF_PRINTER] - # - clearhome = posix.popen('clear', 'r').read() - # - while 1: + + clearhome = os.popen('clear', 'r').read() + + while True: text = clearhome for name in printers: - text = text + makestatus(name, thisuser) + '\n' + text += makestatus(name, thisuser) + '\n' print(text) time.sleep(delay) def makestatus(name, thisuser): - pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r') + pipe = os.popen('lpq -P' + name + ' 2>&1', 'r') lines = [] users = {} aheadbytes = 0 aheadjobs = 0 - userseen = 0 + userseen = False totalbytes = 0 totaljobs = 0 - while 1: - line = pipe.readline() - if not line: break - fields = string.split(line) + for line in pipe: + fields = line.split() n = len(fields) if len(fields) >= 6 and fields[n-1] == 'bytes': - rank = fields[0] - user = fields[1] - job = fields[2] + rank, user, job = fields[0:3] files = fields[3:-2] - bytes = eval(fields[n-2]) + bytes = int(fields[n-2]) if user == thisuser: - userseen = 1 + userseen = True elif not userseen: - aheadbytes = aheadbytes + bytes - aheadjobs = aheadjobs + 1 - totalbytes = totalbytes + bytes - totaljobs = totaljobs + 1 - if user in users: - ujobs, ubytes = users[user] - else: - ujobs, ubytes = 0, 0 - ujobs = ujobs + 1 - ubytes = ubytes + bytes + aheadbytes += bytes + aheadjobs += 1 + totalbytes += bytes + totaljobs += 1 + ujobs, ubytes = users.get(user, (0, 0)) + ujobs += 1 + ubytes += bytes users[user] = ujobs, ubytes else: if fields and fields[0] != 'Rank': - line = string.strip(line) + line = line.strip() if line == 'no entries': line = name + ': idle' elif line[-22:] == ' is ready and printing': line = name lines.append(line) - # + if totaljobs: - line = '%d K' % ((totalbytes+1023)//1024) + line = '%d K' % ((totalbytes+1023) // 1024) if totaljobs != len(users): - line = line + ' (%d jobs)' % totaljobs + line += ' (%d jobs)' % totaljobs if len(users) == 1: - line = line + ' for %s' % (list(users.keys())[0],) + line += ' for %s' % next(iter(users)) else: - line = line + ' for %d users' % len(users) + line += ' for %d users' % len(users) if userseen: if aheadjobs == 0: - line = line + ' (%s first)' % thisuser + line += ' (%s first)' % thisuser else: - line = line + ' (%d K before %s)' % ( - (aheadbytes+1023)//1024, thisuser) + line += ' (%d K before %s)' % ( + (aheadbytes+1023) // 1024, thisuser) lines.append(line) - # + sts = pipe.close() if sts: lines.append('lpq exit status %r' % (sts,)) - return string.joinfields(lines, ': ') + return ': '.join(lines) if __name__ == "__main__": try: diff --git a/Demo/scripts/queens.py b/Demo/scripts/queens.py index 820c9add4b1..726433c5514 100755 --- a/Demo/scripts/queens.py +++ b/Demo/scripts/queens.py @@ -19,8 +19,8 @@ class Queens: def reset(self): n = self.n - self.y = [None]*n # Where is the queen in column x - self.row = [0]*n # Is row[y] safe? + self.y = [None] * n # Where is the queen in column x + self.row = [0] * n # Is row[y] safe? self.up = [0] * (2*n-1) # Is upward diagonal[x-y] safe? self.down = [0] * (2*n-1) # Is downward diagonal[x+y] safe? self.nfound = 0 # Instrumentation @@ -50,7 +50,7 @@ class Queens: self.up[x-y] = 0 self.down[x+y] = 0 - silent = 0 # If set, count solutions only + silent = 0 # If true, count solutions only def display(self): self.nfound = self.nfound + 1