Please use reply-all so a copy goes to the list.
Eduardo Vieira wrote:
On Wed, Apr 22, 2009 at 9:47 AM, bob gailer <bgai...@gmail.com> wrote:
Eduardo Vieira wrote:
Hello! I’m not a programmer
Do you mean that you were not a programmer and are now becoming one?
In a way yes, I am aspiring to be one, but because of circumstances in
my life I won't be going to take schooling for that.
To convert to a function ask: - what are the inputs? - what are the outputs?
The input I see is the excel file name. The outputs I see are # shipped
today and total # shipped.
That leads to a function:
def getShipmentInfo(xlfilename): # specify the input(s) here
###### --- Code to find the right cell with today's shipments
book = xlrd.open_workbook(xlfilename)
thisDay = time.gmtime()[:3]
dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode)
thismonth = time.strftime('%B', time.gmtime()) sh =
book.sheet_by_name(thismonth) firstCol = sh.col_values(0) tperday =
firstCol.index('TOTAL Per Day') shipindex = tperday + 1 for valores in
sh.col_values(cols):
if valores == dayexcel: todaysList = sh.col_values(cols)
totals = sh.row_values(shipindex, end_colx=cols + 1)
break
shipped = todaysList[shipindex]
total = sum([a for a in totals if type(a) == type(2.0)])
return shipped, total # specify the output(s) here
Eliminate unused statements and whitespace.
Put the main program in a main function (reason 3) above)
The program now looks like:
## 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 time import datetime def
main():
# 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\ShippingTotals
2008 West.xls'
shippedToday, totalShipments = getShipmentInfo(thisyearFile)
shippedLastYear, OldTotalShipments = getShipmentInfo(prevyearFile)
# Imports the information from the Eastern division. See code on the
bottom
.... rest of main program
def getShipmentInfo(xlfilename): # specify the input(s) here
.... rest of function code from above
if __name__ == "__main__":
main()
Thank you for your help. I guess actually I will have to create 2
functions: one for this year, and other for the previous year, right?
Because the calculations are different, as you can see in my original
code.
The ONLY difference I can see is the use of current vs prior year, which
I missed.
I also noticed that my code got clobbered, as did yours, by the email
program.
I will attempt to get it right this time and fix the year problem.
add to main program:
thisDay = time.gmtime()[:3]
aYearAgo = prevyear.timetuple()[:3]
modify function:
def getShipmentInfo(xlfilename, day): # specify the input(s) here
###### --- Code to find the right cell with today's shipments
book = xlrd.open_workbook(xlfilename)
thisDay = time.gmtime()[:3]
dayexcel = xlrd.xldate.xldate_from_date_tuple(day, book.datemode)
thismonth = time.strftime('%B', time.gmtime())
sh = book.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:
todaysList = sh.col_values(cols)
totals = sh.row_values(shipindex, end_colx=cols + 1)
break
shipped = todaysList[shipindex]
total = sum([a for a in totals if type(a) == type(2.0)])
return shipped, total # specify the output(s) here
modify calls to function:
shippedToday, totalShipments = getShipmentInfo(thisyearFile, thisDay)
shippedLastYear, OldTotalShipments = getShipmentInfo(prevyearFile, aYearAgo)
Now the 1 function should do what you want.
This is not the only way to use functions or improve things.
I leave dealing with the east data to someone else, but I think we can apply
the same principles.
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
--
Bob Gailer
Chapel Hill NC
919-636-4239
--
Bob Gailer
Chapel Hill NC
919-636-4239
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor