Wednesday, August 22, 2007

Text to HTML Table Converter 1.2

Now has unbuffered reads and better flag support


Unbuffered Input Reading


I added an pseudo-unbuff_read() function to the program (pseudo because it is really a sentinel-terminated loop concatenating a buffered read to a string)

def unbuff_read(END):
""" Reads one char at a time from stdin until reaches a "stop" character """
str = ""

while 1: # no do-whiles or assignments-in-conditionals in python :`(
# Compare the end of the string (same length as END) to END
if str[ len(END) * -1 :] == END:
break
str += sys.stdin.read(1)

return str


The parameter that you pass into this function called END breaks out of the read loop and returns the str read in up to that point. The default END is '\n.\n' which is a newline followed by a period and then another newline. This syntax is the same as the one used to stop reading the message body(DATA) when talking to smtp mail servers.

To quit the program just type in the END variable by itself.



Better flag support


Function argsContain():

def argsContain(flag):
""" Flag is the letter/word/etc that follows the '-' or '--' in the options """
# search for flag in options part of args i.e. before filename
for arg in sys.argv[1:len(sys.argv)-1]:
if arg.find(flag) != -1:
return True
else:
return False


This function goes through each argument before the filename and searches for a specific flag like "f" or "v". The two instances that used to search for flags have been replaced with a call to this method enabling syntax like
$ python txt2html-table-1.2.py -vf input.txt

and

$ python txt2html-table-1.2.py -fv input.txt



Download


This program is Copyright 2007 to Boris Joffe and licensed under GPL 3.0. Open the file to read more of the terms that I released it under. Download Text to HTML Table (txt2html-table) Version 1.2



Debugging


I kept my debugging code which consists of a function called debug() and a commented call to it after main() to make it easier to play around with the code by just uncommenting the call(and maybe commenting main()) and putting code in the debug() function.

No comments: