Women Who Code – In Honor of Ada Lovelace’s Birthday

I was reminded today by Google that it is the 197th birthday of Ada Lovelace, the first computer programmer, who assisted Charles Babbage in the development of his Analytical Engine.

Ada Lovelace Google Doodle on December 10, 2012
Ada Lovelace Google Doodle on December 10, 2012

I’ve put together a list of women coders / developers / hackers / techies on Twitter who are worth following:

Developer/Hacker Women

Sending an Email with Attachments using Python – Archiving Functionality Added

Python logo

(This article has an update.)

I have added code to the program I wrote in my previous post that now allows for folders to exist in the path.  This one does not pull files out of the subfolders, however.  Also, after being attached to the email, the files are renamed with the current date at the end of the filename and moved into an archive folder underneath the original path.  If the archive folder does not already exist, it will be created at runtime.

# Adapted from https://gist.github.com/4009671 and other sources by David Young
# added directory searching functionality to add all files in folder
# disabled username / password logon for use in our Exchange environment
######### Setup your stuff here #######################################

path='<<file path>>' # location of files
archiveFolderName = 'archive' # name of folder under path where files will be archived
host = 'smtp.whatever.com' # specify port, if required, using a colon and port number following the hostname

fromaddr = '[email protected]' # must be a vaild 'from' address in your environment
toaddr  = ['user'[email protected]','[email protected]','[email protected]'] # list of email addresses
replyto = fromaddr # unless you want a different reply-to

# username = 'username' # not used in our Exchange environment
# password = 'password' # not used in our Exchange environment

msgsubject = 'This is the email subject'

htmlmsgtext = "<h2>Email body here</h2>" # text with appropriate HTML tags

######### In normal use nothing changes below this line ###############

import smtplib, os, sys, shutil
from datetime import date
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE
from email import Encoders
from HTMLParser import HTMLParser

archivePath = os.path.join(path, archiveFolderName) # full path where files will be archived

if not os.path.exists(archivePath): # create archive folder if it doesn't exist
os.makedirs(archivePath)
print 'Archive folder created at ' + archivePath + '.'

# A snippet - class to strip HTML tags for the text version of the email

class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)

def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()

########################################################################

try:
# Make text version from HTML - First convert tags that produce a line break to carriage returns
msgtext = htmlmsgtext.replace('</br>',"r").replace('<br />',"r").replace('</p>',"r")
# Then strip all the other tags out
msgtext = strip_tags(msgtext)

# necessary mimey stuff
msg = MIMEMultipart()
msg.preamble = 'This is a multi-part message in MIME format.n'
msg.epilogue = ''

body = MIMEMultipart('alternative')
body.attach(MIMEText(msgtext))
body.attach(MIMEText(htmlmsgtext, 'html'))
msg.attach(body)
attachments = os.listdir(path)

if 'attachments' in globals() and len('attachments') > 0: # are there attachments?
for filename in attachments:
if os.path.isfile(os.path.join(path, filename)):
f = os.path.join(path, filename)
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)

msg['From'] = fromaddr
msg['To'] = COMMASPACE.join(toaddr)
msg['Subject'] = msgsubject
msg['Reply-To'] = replyto

print 'To addresses follow:'
print toaddr

# The actual email sendy bits
server = smtplib.SMTP(host)
server.set_debuglevel(False) # set to True for verbose output

# Comment this block and uncomment the below try/except block if TLS or user/pass is required.
server.sendmail(fromaddr, toaddr, msg.as_string())
print 'Email sent.'
server.quit() # bye bye

# try:
# # If TLS is used
# server.starttls()
# server.login(username,password)
# server.sendmail(msg['From'], [msg['To']], msg.as_string())
# print 'Email sent.'
# server.quit() # bye bye
# except:
# # if tls is set for non-tls servers you would have raised an exception, so....
# server.login(username,password)
# server.sendmail(msg['From'], [msg['To']], msg.as_string())
# print 'Email sent.'
# server.quit() # bye bye

try:
if 'attachments' in globals() and len('attachments') > 0: # are there attachments?
for filename in attachments:
if os.path.isfile(os.path.join(path, filename)):
f1 = os.path.join(path, filename)
x = filename.find('.')
filename2 = filename[:x] + '_' + str(date.today()) + filename[x:]
f2 = os.path.join(path, filename2)
os.rename(f1, f2)
print "File " + filename + " renamed to " + filename2 + "."
shutil.move(f2, archivePath)
print "File " + filename2 + " moved to " + archivePath + "."

except:
print "Files not successfully renamed and/or archived."

except:
print "Email NOT sent to %s successfully. ERR: %s %s %s " % (str(toaddr), str(sys.exc_info()[0]), str(sys.exc_info()[1]), str(sys.exc_info()[2]) )
#just in case

Troubleshooting an SSRS Error

SSRS logo

SQL Server Reporting Services is a great tool that comes with SQL Server, and it is the tool I prefer to use when a report is needed.

I have built and continue to maintain many such reports, and once the reports are built, they don’t often need updating, unless the database schema changes or the customer’s requirements change.  As a result, once they’re deployed, the reports don’t often require a lot of minding.

However, just as with any application, unforeseen events can cause the reports to fail.  In my case today, bad data was allowed to be inserted into the database that was the source for one of my reports.  The mechanism that inserted the data allowed the data in, but the code used to built the result set for the report threw an exception when it encountered the data.  For security reasons, that exception was caught and a generic error appeared in the Web browser when attempting to run the report:

  • An error has occurred during report processing.
    • Query execution failed for data set <>
      • For more information about this error navigate to the report server on the local server machine, or enable remote errors

To find out what the source of the error was, I opened the report in the BI Development Server from SQL Server 2005.  The data source was a stored procedure.  When executing the SP in Query Analyzer, I got this error:

“Msg 8115, Level 16, State 6, Line 49
Arithmetic overflow error converting float to data type numeric.”

My next step was to get the source for the SP, commenting out the CREATE statement, and declaring and setting the parameters as variables to change the SP source code into ad hoc executable statements.  By plugging the dates in question into these variables, I got the same error as above.

The line causing problem in the stored proc updated a table variable based on values from a static table.  Since I could not directly determine what values in the static table were causing the SP to throw the error, I read in all of the values that were in the column of the table variable that were normally joined to the static table into a cursor.

I then scrolled through the cursor, using SELECT statements that contained a modified version of the expression that had been in the UPDATE statement, until I found out which value caused the problem.  Also, I added two SELECT identical statements to the cursor, one before and one after the SELECT statement that had been modified from the original UPDATE statement.  These two SELECT statements simply read “SELECT <<cursor variable>>” to indicate which value was being used for each iteration through the cursor.

When the value causing the problem was reached, the error statement appeared in the Messages pane of the Query Analyzer between two lines, each of these lines showing the value that caused the error.  By then using that value in the WHERE clause of a SELECT statement against the static table, I was able to find out what bad data had been entered.