summaryrefslogtreecommitdiffstats
path: root/Demo/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'Demo/scripts')
-rw-r--r--Demo/scripts/README18
-rwxr-xr-xDemo/scripts/fact.py45
-rwxr-xr-xDemo/scripts/from.py25
-rwxr-xr-xDemo/scripts/lpwatch.py111
-rwxr-xr-xDemo/scripts/pi.py30
-rwxr-xr-xDemo/scripts/primes.py26
6 files changed, 255 insertions, 0 deletions
diff --git a/Demo/scripts/README b/Demo/scripts/README
new file mode 100644
index 0000000..7a2b837
--- /dev/null
+++ b/Demo/scripts/README
@@ -0,0 +1,18 @@
+Contents of this directory:
+
+byteyears.py Print product of a file's size and age
+eptags.py Create Emacs TAGS file for Python modules
+fact.py Factorize numbers
+findlinksto.py Find symbolic links to a given path (prefix)
+from.py Summarize mailbox
+lfact.py Factorize long numbers
+lpwatch.py Watch BSD line printer queues
+mkreal.py Turn a symbolic link into a real file or directory
+objgraph.py Print object graph from nm output on a library
+pdeps.py Print dependencies between Python modules
+pi.py Print digits of pi (uses arbitrary precision integers)
+primes.py Print prime numbers
+ptags.py Create vi tags file for Python modules
+suff.py Sort a list of files by suffix
+which.py Find a program in $PATH
+xxci.py Wrapper for rcsdiff and ci
diff --git a/Demo/scripts/fact.py b/Demo/scripts/fact.py
new file mode 100755
index 0000000..ba75a04
--- /dev/null
+++ b/Demo/scripts/fact.py
@@ -0,0 +1,45 @@
+#! /usr/local/python
+
+# Factorize numbers, slowly.
+# This version uses plain integers and is thus limited to 2**31-1.
+
+import sys
+from math import sqrt
+
+error = 'fact.error' # exception
+
+def fact(n):
+ if n < 1: raise error # fact() argument should be >= 1
+ if n = 1: return [] # special case
+ res = []
+ # Treat even factors special, so we can use i = i+2 later
+ while n%2 = 0:
+ res.append(2)
+ n = n/2
+ # Try odd numbers up to sqrt(n)
+ limit = int(sqrt(float(n+1)))
+ i = 3
+ while i <= limit:
+ if n%i = 0:
+ res.append(i)
+ n = n/i
+ limit = int(sqrt(float(n+1)))
+ else:
+ i = i+2
+ res.append(n)
+ return res
+
+def main():
+ if len(sys.argv) > 1:
+ for arg in sys.argv[1:]:
+ n = int(eval(arg))
+ print n, fact(n)
+ else:
+ try:
+ while 1:
+ n = int(input())
+ print n, fact(n)
+ except EOFError:
+ pass
+
+main()
diff --git a/Demo/scripts/from.py b/Demo/scripts/from.py
new file mode 100755
index 0000000..20771a0
--- /dev/null
+++ b/Demo/scripts/from.py
@@ -0,0 +1,25 @@
+#! /usr/local/python
+
+# Print From and Subject of messages in $MAIL.
+# Extension to multiple mailboxes and other bells & whistles are left
+# as exercises for the reader.
+
+import posix
+
+# Open mailbox file. Exits with exception when this fails.
+
+mail = open(posix.environ['MAIL'], 'r')
+
+while 1:
+ line = mail.readline()
+ if not line: break # EOF
+ if line[:5] = 'From ':
+ # Start of message found
+ print line[:-1],
+ while 1:
+ line = mail.readline()
+ if not line: break # EOF
+ if line = '\n': break # Blank line ends headers
+ if line[:8] = 'Subject:':
+ print `line[9:-1]`,
+ print
diff --git a/Demo/scripts/lpwatch.py b/Demo/scripts/lpwatch.py
new file mode 100755
index 0000000..294028f
--- /dev/null
+++ b/Demo/scripts/lpwatch.py
@@ -0,0 +1,111 @@
+#! /ufs/guido/bin/sgi/python
+#! /usr/local/python
+
+# Watch line printer queue(s).
+# Intended for BSD 4.3 lpq.
+
+import posix
+import sys
+import time
+import string
+
+DEF_PRINTER = 'psc'
+DEF_DELAY = 10
+
+def main():
+ delay = DEF_DELAY # XXX Use getopt() later
+ try:
+ thisuser = posix.environ['LOGNAME']
+ except:
+ thisuser = posix.environ['USER']
+ printers = sys.argv[1:]
+ if not printers:
+ if posix.environ.has_key('PRINTER'):
+ printers = [posix.environ['PRINTER']]
+ else:
+ printers = [DEF_PRINTER]
+ #
+ clearhome = posix.popen('clear', 'r').read()
+ #
+ while 1:
+ # Pipe output through cat for extra buffering,
+ # so the output (which overwrites the previous)
+ # appears instantaneous.
+ sys.stdout = posix.popen('exec cat', 'w')
+ sys.stdout.write(clearhome)
+ for name in printers:
+ pipe = posix.popen('lpq -P' + name + ' 2>&1', 'r')
+ showstatus(name, pipe, thisuser)
+ sts = pipe.close()
+ if sts:
+ print name + ': *** lpq exit status', sts
+ sts = sys.stdout.close()
+ time.sleep(delay)
+
+def showstatus(name, pipe, thisuser):
+ lines = 0
+ users = {}
+ aheadbytes = 0
+ aheadjobs = 0
+ userseen = 0
+ totalbytes = 0
+ totaljobs = 0
+ while 1:
+ line = pipe.readline()
+ if not line: break
+ fields = string.split(line)
+ n = len(fields)
+ if len(fields) >= 6 and fields[n-1] = 'bytes':
+ rank = fields[0]
+ user = fields[1]
+ job = fields[2]
+ files = fields[3:-2]
+ bytes = eval(fields[n-2])
+ if user = thisuser:
+ userseen = 1
+ 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
+ users[user] = ujobs, ubytes
+ else:
+ if fields and fields[0] <> 'Rank':
+ if line[-1:] = '\n':
+ line = line[:-1]
+ if not lines:
+ print name + ':',
+ else:
+ print
+ print line,
+ lines = lines + 1
+ if totaljobs:
+ if lines > 1:
+ print
+ lines = lines+1
+ print (totalbytes+1023)/1024, 'K',
+ if totaljobs <> len(users):
+ print '(' + `totaljobs` + ' jobs)',
+ if len(users) = 1:
+ print 'for', users.keys()[0],
+ else:
+ print 'for', len(users), 'users',
+ if userseen:
+ if aheadjobs = 0:
+ print '(' + thisuser + ' first)',
+ else:
+ print '(' + `(aheadbytes+1023)/1024`,
+ print 'K before', thisuser + ')'
+ if lines:
+ print
+
+try:
+ main()
+except KeyboardInterrupt:
+ pass
diff --git a/Demo/scripts/pi.py b/Demo/scripts/pi.py
new file mode 100755
index 0000000..5e19db6
--- /dev/null
+++ b/Demo/scripts/pi.py
@@ -0,0 +1,30 @@
+#! /usr/local/python
+
+# Print digits of pi forever.
+#
+# The algorithm, using Python's 'long' integers ("bignums"), works
+# with continued fractions, and was conceived by Lambert Meertens.
+#
+# See also the ABC Programmer's Handbook, by Geurts, Meertens & Pemberton,
+# published by Prentice-Hall (UK) Ltd., 1990.
+
+import sys
+
+def main():
+ k, a, b, a1, b1 = 2l, 4l, 1l, 12l, 4l
+ while 1:
+ # Next approximation
+ p, q, k = k*k, 2l*k+1l, k+1l
+ a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
+ # Print common digits
+ d, d1 = a/b, a1/b1
+ #print a, b, a1, b1
+ while d = d1:
+ # Use write() to avoid spaces between the digits
+ sys.stdout.write(`int(d)`)
+ # Flush so the output is seen immediately
+ sys.stdout.flush()
+ a, a1 = 10l*(a%b), 10l*(a1%b1)
+ d, d1 = a/b, a1/b1
+
+main()
diff --git a/Demo/scripts/primes.py b/Demo/scripts/primes.py
new file mode 100755
index 0000000..487acef
--- /dev/null
+++ b/Demo/scripts/primes.py
@@ -0,0 +1,26 @@
+#! /usr/local/python
+
+# Print prime numbers in a given range
+
+def main():
+ import sys
+ min, max = 2, 0x7fffffff
+ if sys.argv[1:]:
+ min = int(eval(sys.argv[1]))
+ if sys.argv[2:]:
+ max = int(eval(sys.argv[2]))
+ primes(min, max)
+
+def primes(min, max):
+ if 2 >= min: print 2
+ primes = [2]
+ i = 3
+ while i <= max:
+ for p in primes:
+ if i%p = 0 or p*p > i: break
+ if i%p <> 0:
+ primes.append(i)
+ if i >= min: print i
+ i = i+2
+
+main()