Just a comment [that might nudge your insight] regarding functions. From the 'good-ol-days'--a function was called a subroutine. They are the same thing. When my thinking starts to muddle (often and easily) that connection always helps.
From the virtual desk of Lowell Tackett --- On Wed, 4/22/09, Eduardo Vieira <eduardo.su...@gmail.com> wrote: From: Eduardo Vieira <eduardo.su...@gmail.com> Subject: [Tutor] Organizing my code, should I use functions? To: "tutor" <tutor@python.org> Date: Wednesday, April 22, 2009, 10:49 AM Hello! I’m not a programmer and am a beginner with Python, I would like suggestion about ways of tackling the task I had to do. I have bee been using the xlrd package to get data from some spreadsheets successfully. I have developed a simple report from a script that compares number of shipments recorded in 2 spreadsheets, with data from previous years and current year, respectively. Later on I was asked to include in the same report numbers from another branch of our company. So, basically all the code logic was the same, only two different files needed to be processed, and I wondered how would I save time avoiding repeating code, creating extra variables, etc. So, I simply decided to alter the code as little as possible and use the import statement. It's working this way. Sounds wise? I thought if I converted it to a function or a class would make it more flexible, but my understanding of classes are too basic yet, and am not sure if it's worth using it. I have noticed that in lot's of python code example, people make functions out of everything. I'm still too much attached to procedural, not functional programming, if I understand the terms correctly. I would appreciate any suggestions. Here is the code: ## This script creates a report of shipments ## comparing the numbers from the previous year with the current year. import xlrd # Package to read Excel files import os import glob import time import datetime import dbi import odbc thismonth = time.strftime('%B', time.gmtime()) # Get's a string with the name of the current month hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) # Two excel files to process with shipping information thisyearFile = r'c:\Shipping Totals - 2009\Shipping Totals 2009 West.xls' prevyearFile = r'c:\Shipping Totals\Shipping Totals - 2008\Shipping Totals 2008 West.xls' thisDay = time.gmtime()[:3] prevyear = datetime.datetime.today() - datetime.timedelta(days=365) aYearAgo = prevyear.timetuple()[:3] ###### --- Code to find the right cell with today's shipments book = xlrd.open_workbook(thisyearFile) # Opens excel file dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) #Puts the date in the Excel format, like 3991.0 sh = book.sheet_by_name(thismonth) # The sheet which has the name of the month: April, May, June, etc. firstCol = sh.col_values(0) # Retrieves the first column tperday = firstCol.index('TOTAL Per Day') # Finds the cell 'Total Per Day' shipindex = tperday + 1 # The next cell after 'TOTAL Per Day', which contains the info about shipments # Looks for the column whose header is today's date for cols in range(sh.ncols): for valores in sh.col_values(cols): if valores == dayexcel: # If it finds the column with today's date todaysList = sh.col_values(cols) totals = sh.row_values(shipindex, end_colx=cols + 1) # sum up to the current date break # Crosses rows with column to find the right cell with the shipments of today shippedToday = todaysList[shipindex] totalShipments = sum([a for a in totals if type(a) == type(2.0)]) # Sums all shipments # Check previous year's shipments processing the file with last year's data booktwo = xlrd.open_workbook(prevyearFile) dayexcel = xlrd.xldate.xldate_from_date_tuple(aYearAgo, book.datemode) sh = booktwo.sheet_by_name(thismonth) firstCol = sh.col_values(0) tperday = firstCol.index('TOTAL Per Day') shipindex = tperday + 1 for cols in range(sh.ncols): for valores in sh.col_values(cols): if valores == dayexcel: lastyearsList = sh.col_values(cols) totals = sh.row_values(shipindex, end_colx=cols + 1) # sum up to the current date break shippedLastYear = lastyearsList[shipindex] OldTotalShipments = sum([a for a in totals if type(a) == type(2.0)]) # Imports the information from the Eastern division. See code on the bottom import bizreportereast as bz report = """ ===== Shipments Alberta Warehouse ===== - Shipments today: %d - Shipments this month: %d - Shipments this day, last year: %d - Shipments this month, last year: %d ===== Shipments Ontario Warehouse ===== - Shipments today: %d - Shipments this month: %d - Shipments this day, last year: %d - Shipments this month, last year: %d """ % (shippedToday, totalShipments, shippedLastYear, OldTotalShipments, bz.shippedToday, bz.totalShipments, bz.shippedLastYear, bz.OldTotalShipments) print report logfile = open('c:/myscripts/logbizreport.log', 'a') #### Code found in bizreportereast.py #### import xlrd import os import glob import time import datetime ###### --- Code to find the right cell with today's shipments thismonth = time.strftime('%B', time.gmtime()) hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) thisyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - 2009\Shipping Totals 2009 East.xls' prevyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - 2008\Shipping Totals 2008 East.xls' thisDay = time.gmtime()[:3] prevyear = datetime.datetime.today() - datetime.timedelta(days=365) aYearAgo = prevyear.timetuple()[:3] book = xlrd.open_workbook(thisyearFile) dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) sh = book.sheet_by_name(thismonth) firstCol = sh.col_values(0) _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor