Line indexing in Python

Rory Campbell-Lange rory at campbell-lange.net
Fri Dec 18 16:22:54 EST 2009


On 18/12/09, seafoid (fitzpadj at tcd.ie) wrote:
> http://old.nabble.com/Parsing-file-format-to-ensure-file-meets-criteria-to26837682.html

Your specification is confusing. However I suggest you break it down
the code so that the steps in your programme are logical. Good luck.

# example psuedocode
headers = {}
header_clauses = {}
current_header = None

def header_parser (input):
    split input into parts
    make unique header desciptor
    check not in headers else abort with error (?)
    add descriptor to headers hash
    # eg descriptor 1 = [attrib1, attrib2, attrib3]
    return descriptor

def clause_parser (input, current_header):
    if current_header is None: abort
    split clause into parts
    store in array in header_clauses [current_header]
    # this will make a data structure like this:
    # header_clauses = {
    #       descriptor1 = {[ clause parts ], [ clause parts ], ... }
    #       descriptor2 = {[ clause parts ], [ clause parts ], ... }

def comment_parser (input)
    pass

# now run over the file
for l in lines:
    if l[0] == 'c':
        comment_parser(l)
    elif l[0] == 'p':
        current_header = header_parser(l)
    else:
        clause_parser(l, current_header)

# now that we have stored everything, check the data
for h in headers:
    attrib1, attrib2, attrib3  = headers[h]
    for c in header_clauses:
        iterate over the arrays of clause parts either adding them
        up or comparing them to the header attributes
        
-- 
Rory Campbell-Lange
Director
rory at campbell-lange.net

Campbell-Lange Workshop
www.campbell-lange.net
0207 6311 555
3 Tottenham Street London W1T 2AF
Registered in England No. 04551928



More information about the Python-list mailing list