Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Mailroom with sqlite db is mostly done #96

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
133 changes: 133 additions & 0 deletions mmf69/session07/Mailroom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/python

from prettytable import PrettyTable
import sqlite3
import locale

locale.setlocale(locale.LC_ALL, '')
'English_United States.1252'
locale.currency(188518982.18)
'$188518982.18'

conn = sqlite3.connect('donor.db')


# print("Opened database successfully")


def add_donation():
conn.execute("INSERT INTO DONORS (DONOR_ID,DONOR_NAME,DONATION_AMOUNT) \
VALUES (3, 'Tim', 100.00)")


def donor_list():
print("********DONORS***********")
cursor = conn.execute("SELECT DISTINCT DONOR_ID as 'Donor ID',"
"DONOR_NAME as 'Donor Name' from donors order by DONOR_ID")
col_names = [cn[0] for cn in cursor.description]
rows = cursor.fetchall()
x = PrettyTable(col_names)
x.align[col_names[0]] = "c"
x.align[col_names[1]] = "l"
x.padding_width = 1
for row in rows:
x.add_row(row)
print(x)


def donor_report():
print("*********************************DONOR REPORT***************************************")
cursor = conn.execute("SELECT DONOR_ID as 'Donor ID',DONOR_NAME as 'Donor Name',"
"total(DONATION_AMOUNT) AS 'Total Donations',"
"count(DONOR_ID) AS 'Number of Donations',"
"total(DONATION_AMOUNT)/count(DONOR_ID) AS 'Average Donation'"
" from donors group by DONOR_ID,DONOR_NAME")
col_names = [cn[0] for cn in cursor.description]
rows = cursor.fetchall()
x = PrettyTable(col_names)
x.align[col_names[1]] = "l"
x.align[col_names[2]] = "r"
x.align[col_names[3]] = "c"
x.align[col_names[4]] = "r"
x.padding_width = 1
for row in rows:
x.add_row(row)
print(x)


def thank_you():
cursor = conn.execute("SELECT DONOR_NAME as 'Donor Name',"
"total(DONATION_AMOUNT) AS 'Total Donations'"
" from donors group by DONOR_ID,DONOR_NAME")
rows = cursor.fetchall()
for row in rows:
with open('mailroom-thankyou-{}.txt'.format(row[0]), 'w') as f:
f.write('Dear ' + str(row[0]) + ', \n'
'Thank you for you generous donation of ' +
locale.currency(row[1], 'True') + '.\n'
'Your continued support of my fundraising efforts for the Big Climb keeps me motivated. \n'
'Together we WILL find a cure for Blood related cancers! \n'
'Thank you, \n'
'Matt')


def list_donor_files():
import glob
print(glob.glob("mailroom-thankyou-*.txt"))


def menu_options():
print("""
Please Select an option:
1) Add a Donor/Donation
2) View Donor List
3) Print Report
4) Send "Thank You"
5) Save all tasks to the Donor file and exit!
""")


def main():
"""Coordinates I/O and actions"""
try:
# Display a menu of choices to the user
# and Process user I/0
while True:
menu_options()
str_choice = str(input("Which option would you like to perform? [1 to 5]"))
# Need to add if/elif for adding donor/donation
if str_choice == '1': # 1) Add a new item.
new_donor = str(input("What is the donor's name?"))
contribution = int(input("What is the amount?"))
add_donation()
# donor_report(gTodoTasks)
continue

elif str_choice == '2': # Print List
donor_list()

elif str_choice == '3': # Print Report
donor_report()
continue
elif str_choice == '4':
list_donor_files()
continue
elif str_choice == '5':
# Save (commit) the changes
conn.commit()
conn.close()
print("Operation done successfully")
exit()
else:
print(Exception(
"\nThat isn't a valid input. Please select a number from [1 to 5]")) # Message in case user enters the incorrect number
# donor_report(gTodoTasks)
continue
except IOError as error: # Handles any Python errors
print("Hmmm something isn't right...")
print("pythons error info: ")
print(error)


# start the program
main() # Call the Main function at the start of the script
61 changes: 61 additions & 0 deletions mmf69/session07/database_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('donor.db')

print("Opened database successfully")


def drop_table():
try:
conn.execute("drop table if exists donors")
except IOError as error:
print(error)


def create_table():
try:
conn.execute('''CREATE TABLE DONORS
(DONOR_ID INT NOT NULL,
DONOR_NAME TEXT NOT NULL,
DONATION_AMOUNT REAL);''')
print("Table created successfully")
except IOError as error:
print(error)


def insert_data():
try:
conn.execute("INSERT INTO DONORS (DONOR_ID,DONOR_NAME,DONATION_AMOUNT) \
VALUES (1, 'Sara', 100.00)")

conn.execute("INSERT INTO DONORS (DONOR_ID,DONOR_NAME,DONATION_AMOUNT) \
VALUES (2, 'Starla', 50.00)")

conn.execute("INSERT INTO DONORS (DONOR_ID,DONOR_NAME,DONATION_AMOUNT) \
VALUES (3, 'Tim', 100.00)")

conn.execute("INSERT INTO DONORS (DONOR_ID,DONOR_NAME,DONATION_AMOUNT) \
VALUES (4, 'Ruby', 50.00)")

conn.execute("INSERT INTO DONORS (DONOR_ID,DONOR_NAME,DONATION_AMOUNT) \
VALUES (4, 'Ruby', 25.00)")

conn.commit()
print("Records created successfully")
except IOError as error:
print(error)


def menu_options():
"""Make a function for the code that allows the user to Add or Remove tasks from the list,
plus save the tasks in the List tasks-priorities using numbered choices."""
print("""
Please Select an option:
1) Drop Table
2) Create Table
3) Insert Initial Data
4) Exit Tool
""")

Binary file modified mmf69/session07/donor.db
Binary file not shown.
28 changes: 28 additions & 0 deletions mmf69/session07/donor_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/python

from prettytable import PrettyTable
import sqlite3

conn = sqlite3.connect('donor.db')
print("Opened database successfully")


def donor_list():
print("********DONORS***********")
cursor = conn.execute("SELECT DISTINCT DONOR_ID as 'Donor ID',"
"DONOR_NAME as 'Donor Name' from donors order by DONOR_ID")
col_names = [cn[0] for cn in cursor.description]
rows = cursor.fetchall()
x = PrettyTable(col_names)
x.align[col_names[0]] = "c"
x.align[col_names[1]] = "l"
x.padding_width = 1
for row in rows:
x.add_row(row)
print(x)

donor_list()

print("Operation done successfully")

conn.close()
34 changes: 34 additions & 0 deletions mmf69/session07/donor_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/python

from prettytable import PrettyTable
import sqlite3

conn = sqlite3.connect('donor.db')
print("Opened database successfully")


def donor_report():
print("*********************************DONOR REPORT***************************************")
cursor = conn.execute("SELECT DONOR_ID as 'Donor ID',DONOR_NAME as 'Donor Name',"
"total(DONATION_AMOUNT) AS 'Total Donations',"
"count(DONOR_ID) AS 'Number of Donations',"
"total(DONATION_AMOUNT)/count(DONOR_ID) AS 'Average Donation'"
" from donors group by DONOR_ID,DONOR_NAME")
col_names = [cn[0] for cn in cursor.description]
rows = cursor.fetchall()
x = PrettyTable(col_names)
x.align[col_names[1]] = "l"
x.align[col_names[2]] = "r"
x.align[col_names[3]] = "c"
x.align[col_names[4]] = "r"
x.padding_width = 1
for row in rows:
x.add_row(row)
print(x)


donor_report()

print("Operation done successfully")

conn.close()
45 changes: 40 additions & 5 deletions mmf69/session07/donordb_select.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
#!/usr/bin/python

from prettytable import PrettyTable
import sqlite3


conn = sqlite3.connect('donor.db')
print("Opened database successfully")

cursor = conn.execute("SELECT DONOR_ID,DONOR_NAME,DONATION_AMOUNT from DONORS")
cursor = conn.execute("SELECT DONOR_ID,DONOR_NAME,DONATION_AMOUNT from DONORS")
for row in cursor:
print("DONOR_ID = ", row[0])
print("DONOR_NAME = ", row[1])
print("DONATION_AMOUNT = ", row[2], "\n")
print(row)


cursor = conn.execute("SELECT DONOR_ID,DONOR_NAME,total(DONATION_AMOUNT),"
"count(distinct DONATION_AMOUNT),total(DONATION_AMOUNT)/count(distinct DONATION_AMOUNT)"
" from donors group by DONOR_ID,DONOR_NAME")
'''for row in cursor:
print("DONOR_ID = ", row[0])
print("DONOR_NAME = ", row[1])
print("TOTAL_DONATIONS = ", row[3])
print("DONATION_AMOUNT = ", row[2])
print("AVERAGE_DONATION = ", row[4], "\n")
'''


print("*********************************DONOR REPORT***************************************")

cursor = conn.execute("SELECT DONOR_ID as 'Donor ID',DONOR_NAME as 'Donor Name',"
"total(DONATION_AMOUNT) AS 'Total Donations',"
"count(DONOR_ID) AS 'Number of Donations',"
"total(DONATION_AMOUNT)/count(DONOR_ID) AS AVERAGE_DONATION"
" from donors group by DONOR_ID,DONOR_NAME")

col_names = [cn[0] for cn in cursor.description]
rows = cursor.fetchall()

x = PrettyTable(col_names)
x.align[col_names[1]] = "l"
x.align[col_names[2]] = "r"
x.padding_width = 1
for row in rows:
x.add_row(row)

print(x)


print("Operation done successfully")
conn.close()

conn.close()
6 changes: 6 additions & 0 deletions mmf69/session07/mailroom-thankyou-Ruby.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dear Ruby,
Thank you for you generous donation of $150.00.
Your continued support of my fundraising efforts for the Big Climb keeps me motivated.
Together we WILL find a cure for Blood related cancers!
Thank you,
Matt
6 changes: 6 additions & 0 deletions mmf69/session07/mailroom-thankyou-Sara.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dear Sara,
Thank you for you generous donation of $200.00.
Your continued support of my fundraising efforts for the Big Climb keeps me motivated.
Together we WILL find a cure for Blood related cancers!
Thank you,
Matt
6 changes: 6 additions & 0 deletions mmf69/session07/mailroom-thankyou-Starla.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dear Starla,
Thank you for you generous donation of $100.00.
Your continued support of my fundraising efforts for the Big Climb keeps me motivated.
Together we WILL find a cure for Blood related cancers!
Thank you,
Matt
6 changes: 6 additions & 0 deletions mmf69/session07/mailroom-thankyou-Tim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dear Tim,
Thank you for you generous donation of $200.00.
Your continued support of my fundraising efforts for the Big Climb keeps me motivated.
Together we WILL find a cure for Blood related cancers!
Thank you,
Matt
43 changes: 43 additions & 0 deletions mmf69/session09/Circle_Class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from math import pi, sqrt


class Circle(object):

def __init__(self, radius):
self._radius = radius

@staticmethod
def radius_to_area(radius):
return pi * radius ** 2

@classmethod
def from_diameter(cls, diameter):
return cls(diameter/2)

@classmethod
def from_area(cls, area):
return cls(sqrt(area/pi))

@property
def radius(self):
return self._radius

@radius.setter
def radius(self, radius):
self._radius = radius

@property
def diameter(self):
return self._radius * 2

@diameter.setter
def diameter(self, diameter):
self._radius = diameter / 2

# print(Circle.radius_to_area(1))
c = Circle.from_diameter(8)
print(c.diameter)
print(c.radius)