Skip to content

Instantly share code, notes, and snippets.

@muff1nman
Created October 9, 2012 02:07
Show Gist options
  • Save muff1nman/3856162 to your computer and use it in GitHub Desktop.
Save muff1nman/3856162 to your computer and use it in GitHub Desktop.
file wrapper class with Parser
#Python: Just wanted to share the how I used a custom file wrapper class for my Parser class to use so that I could avoid reading in the entire file and open and close it
#once (definitions omitted)
class FileHandler:
"""A class to handle file opening and closing."""
def __init__(self, filename):
def getPermissions( self) :
def openFileWithPermissions(self, permissionString):
def getFileObject(self):
def closeFile(self):
def getLine(self):
def writeLine(self, string):
def isEndOfFile(self):
def length(self):
def saveSpot(self):
def resumeFromSave(self) :
def reset(self):
#With this I was able to do a nifty thing with my hasMoreCommands function. I could just perform a lookahead for any more legit commands.
def hasMoreCommands(self):
"""Are there more commands in the input?"""
# if we are at the end of the file then no
if self.file.isEndOfFile() :
return 0
else :
# else we need to go see. Be right back
self.file.saveSpot()
while( not self.file.isEndOfFile() ) :
testLine = self.file.getLine()
testLine = self.preprocess(testLine)
# yep found a command
if self.isCommand(testLine):
# I'll put your file back to where you where
self.file.resumeFromSave()
return 1
# no command found
self.file.resumeFromSave()
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment