Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

SQL Query Generator / Angel LMS / Calendar Usage - Python Code Bank


SQL Query Generator / Angel LMS / Calendar Usage
This is little program I wrote a little while ago that deals with the use of Python's Calendar module. I had to change survey dates to 14000+ courses in our Angel LMS. Since it was faster to run sql statements then API calls, I exported my results to a file. I made a little date checker program to get the correct date. The input was csv file containing 1 column of dates in mm/dd/yy format. Just wanted to share some real world applications of python.
                import calendar
def getStartDate(end_date):
    months = {1:31, 2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
    date = end_date.split('/')
    month = int(date[0])
    day = int(date[1])
    year = int(date[2])
    leapYear = calendar.isleap(year)
    startDate = 21
    
    #set the newday for the eval to start
    newday = day-startDate

    #if negative or zero, change month and set the date
    if newday <= 0:
        month-=1

        #if the year is a leap year add 1 to Feburary
        if leapYear:
            newday = months[month] + newday
        else:
            newday = (months[month]+ 1) + newday

    #if month is negative or zero, change year and set the month            
    if month <= 0:
        month = 12
        year-=1

    #print str(year), "is a leap year?:", leapYear
    return str(month)+'/'+str(newday)+'/'+str(year)

#test getStartDate() def:
#print getStartDate('3/22/2012')

#takes a csv file of one column of end dates: no header field
f = open('Fall 2011 Classes ending after 10232011.csv', 'r')
data = f.readlines()
f.close()
end_dates = []
for i in data:
    line = i.replace(',','').replace('\n','')
    if line not in end_dates:
        end_dates+=[line]

EvalParentEntryId = '3B79D15AFE1149FEACDAAD3265A0E434'
f = open('EVAL_QUERIES.txt', 'w')
for i in end_dates:
    start_date = getStartDate(i)

    class_ends = i
    f.write( '''update lsn_entries
set start_date = '%s'
where ParentEntryId = '%s' and course_id in (select courses.course_id 
from courses
join lsn_entries
on courses.course_id = lsn_entries.course_id
where class_ends ='%s 12:00:00 AM');

''' % (start_date, EvalParentEntryId, class_ends))
f.close()
print "Job Complete!"


            
Comments
Sorry but there are no comments to display