summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-10-11 14:23:49 (GMT)
committerGeorg Brandl <georg@python.org>2009-10-11 14:23:49 (GMT)
commit393c82324521be08f21583ecb34b761f1eb3ec3f (patch)
treeccc9a8cce076bcb531ed674e989879f59c7b5035
parent8ec30e835f6062dc4a465f97de6f8477e6b8b983 (diff)
downloadcpython-393c82324521be08f21583ecb34b761f1eb3ec3f.zip
cpython-393c82324521be08f21583ecb34b761f1eb3ec3f.tar.gz
cpython-393c82324521be08f21583ecb34b761f1eb3ec3f.tar.bz2
Update lpwatch script.
-rwxr-xr-xDemo/scripts/lpwatch.py88
-rwxr-xr-xDemo/scripts/newslist.py2
-rwxr-xr-xDemo/scripts/queens.py6
3 files changed, 44 insertions, 52 deletions
diff --git a/Demo/scripts/lpwatch.py b/Demo/scripts/lpwatch.py
index 715cbb8..9d207b9 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 posix.environ.has_key('PRINTER'):
- printers = [posix.environ['PRINTER']]
+ if os.environ.has_key('PRINTER'):
+ 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 users.has_key(user):
- 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)
+ if fields and fields[0] != 'Rank':
+ 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)
- if totaljobs <> len(users):
- line = line + ' (%d jobs)' % totaljobs
+ line = '%d K' % ((totalbytes+1023) // 1024)
+ if totaljobs != len(users):
+ line += ' (%d jobs)' % totaljobs
if len(users) == 1:
- line = line + ' for %s' % (users.keys()[0],)
+ line += ' for %s' % (users.keys()[0],)
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/newslist.py b/Demo/scripts/newslist.py
index 7e0fade..a91e901 100755
--- a/Demo/scripts/newslist.py
+++ b/Demo/scripts/newslist.py
@@ -128,7 +128,7 @@ def makeleaf(tree,path):
j = path[0]
l = len(path)
- if not tree.has_key(j):
+ if j not in tree:
tree[j] = {}
if l == 1:
tree[j]['.'] = '.'
diff --git a/Demo/scripts/queens.py b/Demo/scripts/queens.py
index 74756be..866a7b2 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