'''
DVC SCRIPT
Brett Paufler
Copyright (c) 2021-04-23/24
A Python Script,
which transforms dvc_transcript.txt
into HTML Formatted Sections
for use in a BrettRant
This is a one off script.
'''
#I have my preferred workflow
FILE_IN = "./input/dvc_transcript.txt"
FILE_OUT = "./output/dvc_formatted.txt"
#The input is read and saved to a working variable
with open(FILE_IN, 'r') as f:
raw_text = f.read()
#print raw_text
#Monolithic Text Split into Lines
lines = raw_text.split('\n')
#print lines #Endless Prints
#print len(lines) #I check as I go
#Trims Off The Front Matter, leaving CSV Data
#I almost always get the number by Plugging & Chugging
lines = lines[24:]
#print lines #Leading & Ending Values are Correct
#print len(lines) #64 is the Correct Number of Courses
#Normally, this would go at the top
#But this is where I added it, so here it goes
from collections import namedtuple
#These are the Final Fields I want
#They differ from the CSV Fields
Course = namedtuple('Course',
['catalog', 'section', 'name',
'grade', 'credit', 'term'])
#print Course
#Test Case without a Section Number
test_course_ns = Course(
'ECON-220', '', 'Principles of MacRoeconomics',
'A','3','1992SP')
#print test_course_ns
#Test Case with a Section Number
test_course = Course(
'HSCI-126', '8931', 'Stress Management and Health',
'A','3','2006SP')
#print test_course
#I'm going to Loop through the Lines
#And transform each line into a Course
#This is where I will store the Courses
course_list = []
#The Main Data Munging Loop
for line in lines: #What could be simpler?
#print line #let's see what we are working with
csv_values = line.split(',')
#print csv_values
#The End Values are Easier to Deal With
#So, I'll work from the back-end, popping values
#It seemed like a silly check at the time
#But I have an Error in "Magic, Witchcraft, & Religion"
#assert len(csv_values) == 4
#This Handles My One Length Exception
if len(csv_values) != 4:
#print csv_values
start_string = ','.join(csv_values[:3]) #Combine Three as One
start_string = start_string.replace('"', '') #Kill the added "'s
csv_values = [start_string] + csv_values[3:]
#print csv_values
assert len(csv_values) == 4
#Now, where was I?
#print csv_values
#It All Looks Good
#There are easier ways to do this
#But they require more thought
term = csv_values.pop()
credit = csv_values.pop()
grade = csv_values.pop()
#print grade, credit, term
#All that remains in csv_values is the start string
assert len(csv_values) == 1
#Yep, I was right
#I need to pull catalog, section, & name
cat_string = csv_values.pop()
assert len(csv_values) == 0 #Can't Hurt
string_pieces = cat_string.split()
catalog = string_pieces.pop(0) #easiest first
#If at first you don't succeed
#The exception code will run
try:
section = str(int(string_pieces[0])) #If Error (i.e. no Int Conversion)
string_pieces = string_pieces[1:] #Then this never fires
except:
section = ''
#Now, we just rejoin the pieces
name = ' '.join(string_pieces)
#print catalog, section, name
#It All Looks Good, So Let's Build a Named_Tupel
this_course = Course(
catalog, section, name,
grade, credit, term)
#print this_course #Looks Good (Well, it does now)
course_list.append(this_course)
#Credits is a Keyword, listing Python Credits
#I'd wanted to use that in my Tuple
#But nope, not going to happen
#print len(course_list) #64, which is correct
# # #
#The Munging Done (Data to Types)
#I will break here
#Output is next
#But it's time for a break
# # #
#Starting This Section
#All Other Prints Commented Out
#print len(course_list)
#print course_list[0]
#All Types are Strings
#Let's see if this works
test_first = "Carry S Through: %s %%s" % "First"
test_second = test_first % "Second"
#print test_first
#print test_second
#So, that Works
#Normally, I'd do that in a "Workspace"
#Or Delete it for clarity
#But this file is about My Workflow
#Let's Build Up a Wall of Text
text = ""
for c in course_list:
text += '\n\n
\n
\n
\n