HOWTOlabs  
 Services     Software     Commentary     Design     Astral Musings   
Python
Setup and basic uses
Related
Elsewhere

Python, like PHP and perl, is a popular scripting language common on UNIX and Linux systems.  Some unique traits of Python are its use of white space to control program flow and its built in list processing.  The white space controlled program flow makes Python very consise and compact to read and write.

Quick Example

$ python nline.py

  Output n longest lines from file    
  Syntax: nline.py [n] [filename]

$ cat string.txt

  The quick
  brown fox
  jumps over
  the lazy
  dog

$ python nline.py 2 string.txt

  brown fox
  jumps over 
import sys

# offer command line help, parse command line arguments, 
if len(sys.argv) < 3:
    print "Output n longest lines from file"
    print "Syntax:", sys.argv[0], "[n] [filename]"
    sys.exit(2)
n = int(sys.argv[1])
fname = sys.argv[2]

# attempt to open file
try:
    file = open(fname, "r")
except:
    print "Error: can not open", fname,  "for reading"
    sys.exit(1)

# first pass, detect line length, build list[line #][line length]
list = []
fline_n = 0
while file:
    fline = file.readline()
    if fline == "":
        break;
    list.append((fline_n, len(fline)))
    fline_n = fline_n + 1

# sort by line length, requires swaping values for each list pair
list = [(v, k) for k, v in list]
list.sort()

# determine how many lines to skip in final output
if len(list) <= n:
    o = 0
else:
    o = len(list) - n
# remove one list element for each line to be skipped
while o:
    del list[0]
    o = o - 1
# sort by line number, requires swaping values for each list pair
list = [(v, k) for k, v in list]
list.sort()

# second pass, read file and only output lines that are still included in list
fline_n = 0
list_n = 0
file.seek(0)
while file:
    fline = file.readline()
    if fline == "":
        break;
    if (list_n < n) and (list[list_n][0] == fline_n):
        print fline,
        list_n = list_n + 1
    fline_n = fline_n + 1
file.close() 
 

Web Applications [ edit ]

Elsewhere

Where as PHP still support high performance module for Apache, Python hasn't really settled on a widely accepted web application interconnect with establish web servers (as of around 2017).

zap technologies
tablet | printable