0

this is my code but i couldnt get phase 2 to print out can anyone help me please? thank you!

import sys
import string
NUM_ARGS = 3
GLOBALSTACK = []
TAG_STACK = []

# getFile()  Gets a valid file name
# Input:     None
# Output:    fileName, the name of the file
def getFile():
    # gets file name
    fileName = sys.argv[1]
    return fileName

# printGreeting() prints a greeting to the user
# Input:          None
# Output          None
def printGreeting():
    # greets user
    print 'Hello, welcome to my HTML checking program.'
    print 'This program will check that you have finished your tags in part 1.'
    print 'In part 2 it will check to see if each tag has a match if it',
    print ' needs one.'
    
# readFile() higher level function reads the file, finds the tags, and then
#            handles the processing and output dealing with reading the file
#            and creating a queue
# Input:     fileName, the name of the file
# Output:    tagQueue, a queue made of tags
def readFile(fileName):
    # initilizations
    numValid =0
    tagQueue = []
    
    # opens file and reads it
    infile = open(fileName, 'r')
    for line in infile:
        if line.count('<') == 0:
            line 
        elif len(line.split('>')) == 0:
            print 'Error in formating'
            sys.exit()
        else:
            frag1 = line.split('<')[1]
            try:
                frag2= frag1.split('>')
                tag = frag2[0]
            except IndexError:
                print'Bad Tag'
            
            print '<' ,tag ,'>'
            validTag(tag)

    print 'Phase 1: Commplete and correct!'
    tagQueue = enqueue(tag,tagQueue)
        
    # compairs number of valid tags to number of tags in queue and prints
    # ending of phase 1
    compareLengths(numValid, len(tagQueue))

    # If phase one completes return queue
    return tagQueue
        
# validTag() checks to see if the tag is in the proper set up
# Input:     tag
# Output:    True or False
def validTag(tag):
    #This is no longer needed#
    return

# enqueue() adds the tags into a queue
# Input:    tag
#           tagQueue
# Output:   tagQueue
def enqueue(tag, tagQueue):
    # adds the tags to a queue
    tagQueue. append(tag)
    return tagQueue
                                                                                    
# compareLengths() compairs the the length of the queue with the number of
#                  valid tags
# Input:           numValid, the number of valid tags
#                  lengQ, the lenght of tagQueue
# Output:          None
def compareLengths(numValid, lengQ):
    # Not Needed#
    return

# readQueue() a higher level function that handles the processing and output
#             dealing with reading the queue and creating a stack
# Input:      tagQueue
# Output:     None
def readQueue(tagQueue):
    TAG_STACK = []
    while len(tagQueue) != 0:
        tag = dequeue(tagQueue)
        TAG_STACK = push(tag, TAG_STACK)

# dequeue() takes the queue and takes the elements off
# Input:    tagQueue, queue made of tags
# Output:   tag
def dequeue(tagQueue):
    # find the sie of the Queue
    size = len(tagQueue)

    # runs if queue isnt empty
    if size > 0:
        # removes the first item off the queue and returns it
        tag = tagQueue[0]
        del(tagQueue[0])
        return tag
    else:
        return 'empty queue'
    
# push()  pushes the tags onto a stack
# Input:  tags
# Output: tagStack, a stack made of tags
def push(tags, stack):
    stack.append(tags)
    return stack

# readStack() is a higher level function that handles the prossessing and
#             output dealing with reading and analyzing the stack
# Input:      tagStack, a stack of tags
# Output:     None
def readStack(stack):
    while len(TAG_STACK) != 0:
        tag = pop(TAG_STACK)
        typeTag(tag)
    print 'Phase 2: tags all match in this document'
    
    
# pop()   a fuction that a tag off of a stack
# Input:  a stack
# Output: a tag
def pop(stack):
    size = len(TAG_STACK)
    if size > 0:
        popped = TAG_STACK[-1]
        del(TAG_STACK[-1])
        return popped
    else:
        return 'empty stack'

# typeTag(): a function that looks at the tag it is given and finds weither
#            it is a self closing, opening, or closing tag, and carries out the
#            correct task
# Input:     tag
# Output:    None
def typeTag(tag):
    if tag[-3] == ' ':
        print tag,
        print 'is self closing'
        return
    elif tag[1] != '/':
        GLOBESTACK = push(tag, GLOBALSTACK)
        return
    elif tag[1] == '/':
        openTag = pop(GLOBALSTACK)
        set = validSet(openTag, tag)
        if set == True:
            print '%s matches %s' %(openTag, tag)
        else:
            print 'Phase 2: there is an inccorct tag combonation.'
            sys.exit()

# cleanString() takes a tag and removes the mark-ups (<, >, \)
# Input:        tag
# Output:       a stripped tag
def cleanString(tag):
    #### Not Needed####
    return

# validSet() takes a stipped opening and a closing tag and compairs the set to
#            see if they are the same string
# Input:     cleanOpen, a stripped opening tag
#            cleanClose, a stripped closing tag 
# Output:    True or Flase
def validSet(cleanOpen, cleanClose):
    if cleanOpen == cleanClose:
        return True
    else:
        return False

def main():
   fileName = getFile()
   printGreeting()
   tagQueue = readFile(fileName)
   readQueue(tagQueue)
   readStack(TAG_STACK)

main()

the output should look like this :


linuxserver1.cs.umbc.edu[116] python proj1.py xhtml.dat
Your greeting/instructions go here!
<html>
<head>
<title>
</title>
</head>
<body>
<center>
<h1>
</h1>
</center>
<b>
</b>
<P>
</P>
<P>
<br />
<br />
<br />
</P>
<hr />
<P>
<foo>
</foo>
</P>
<P>
<br />
<tag1>
<br />
<tag2>
<br />
<tag3>
<br />
</tag3>
<br />
</tag2>
<br />
</tag1>
<br />
</P>
<P>
</P>
</body>
</html>
Phase 1: End of file was reached for xhtml.dat with no errors

<title> matches </title>
<head> matches </head>
<h1> matches </h1>
<center> matches </center>
<b> matches </b>
<P> matches </P>
<br /> is self-closing
<br /> is self-closing
<br /> is self-closing
<P> matches </P>
<hr /> is self-closing
<foo> matches </foo>
<P> matches </P>
<br /> is self-closing
<br /> is self-closing
<br /> is self-closing
<br /> is self-closing
<tag3> matches </tag3>
<br /> is self-closing
<tag2> matches </tag2>
<br /> is self-closing
<tag1> matches </tag1>
<br /> is self-closing
<P> matches </P>
<P> matches </P>
<body> matches </body>
<html> matches </html>
Phase 2: The tags match in this document.
linuxserver1.cs.umbc.edu[117]

Edited by the_azn_invasio: n/a

2
Contributors
1
Reply
5
Views
5 Years
Discussion Span
Last Post by woooee
0

For starters, this does not make sense

if line.count('<') == 0:
            line

Here, you are splitting a list which should yield an error message that says you can only split strings, not lists.

else:
            frag1 = line.split('<')[1]
            try:
                frag2= frag1.split('>')

And why are you implementing all of the stack stuff? Do you want to implement your own stack code, or analyze an html file? It is not obvious which of the two you are trying to do. Something like this will work even though it is clumsy, and you will have to work out the counting-the-same-tag-multiple-times problem yourself.

test_file = """<html>
 <head>
 <title>
 </title>
 </head>
 <body>
 <center>
 <h1>
 </h1>
 </center>
 <b>
 </b>
 <P>
 </P>
 <P>
 <br />
 <br />
 <br />
 </P>
 <hr />
 <P>
 <foo>
 </foo>
 </P>
 <P>
 <br />
 <tag1>
 <br />
 <tag2>
 <br />
 <tag3>
 <br />
 </tag3>
 <br />
 </tag2>
 <br />
 </tag1>
 <br />
 </P>
 <P>
 </P>
 </body>
 </html> """

## convert the test data into a file read type object
tags = ["<"+t.strip() for t in test_file.split("<") if len(t)]
print tags

for tag in tags:
    ## eliminate closing and self-closing tags
    if (not tag.startswith("</")) and (not tag.endswith("/>")):
        tag_count = tags.count(tag)
        end = "</"+tag[1:]
        end_count = tags.count(end)
        print tag, tag_count, end, end_count

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.