Python Programming

Part 1

Problem

Develop a simple Python program to demonstrate understanding of using the Tkinter module to design a simple widget and configure it using the layout manager. Then add an event handler that will accept text input to the button and display the content of the text when using the mouse to click on the “click here” button.

The program must have the following:

Demonstration of using the Tkinter module to construct a simple widget and configure it using the layout manager.
A GUI application as the output with a nonfunctioning button.
Demonstration of using Tkinter to create an event handler that displays the text content of the button after executing the “click here” button.

Documentation Guidelines:

Use good programming style (e.g., indentation for readability) and document each of your program parts with the following items (the items shown between the ‘<‘ and ‘>’ angle brackets are only placeholders. You should replace the placeholders and the comments between them with your specific information). Your cover sheet should have some of the same information, but what follows should be at the top of each program’s sheet of source code. Some lines of code should have an explanation of what is to be accomplished, this will allow someone supporting your code years later to comprehend your purpose. Be brief and to the point. Start your design by writing comment lines of pseudocode. Once that is complete, begin adding executable lines. Finally run and test your program.

Deliverable(s):

Your deliverable should be a Word document with screenshots showing the source code and running results, and discuss the issues that you had for this project related to AWS and/or Python IDE and how you solved them for all of the programs listed above as well as the inputs and outputs from running them. Submit a cover sheet with the hardcopy of your work.

part 2

For this assignment, you will develop working examples of a graphical user interface (GUI) and event handling and that demonstrate the following:

Working code with screenshots of a Python GUI application that includes 5 design widgets of your choosing
Working code with screenshots of event handling in Python based on 3 events of your choosing
Be sure to include a brief narrative of your code where you explain what the code is doing.

Documentation Guidelines:

Use good programming style (e.g., indentation for readability) and document each of your program parts with the following items (the items shown between the ‘<‘ and ‘>’ angle brackets are only placeholders. You should replace the placeholders and the comments between them with your specific information). Your cover sheet should have some of the same information, but what follows should be at the top of each program’s sheet of source code. Some lines of code should have an explanation of what is to be accomplished, this will allow someone supporting your code years later to comprehend your purpose. Be brief and to the point. Start your design by writing comment lines of pseudocode. Once that is complete, begin adding executable lines. Finally run and test your program.

Deliverable(s):

Your deliverable should be a Word document with screenshots showing the GUI and event handling you have created. Also, discuss the issues that you had for this project related to AWS and/or Python IDE and how you solved them for all of the programs listed above as well as the inputs and outputs from running them. Submit a cover sheet with the hardcopy of your work.

Part 3

Write a Python program using the Python IDE based on recursion with trees that is both depth first and breadth first searches.

The Python program to be developed will demonstrate the use of both depth-first (DFS) and breadth-first (BFS) searches. A tree node structure will be developed that will be used both for DFS and BFS searches. The program will apply recursion both for the DFS and BFS searches to completion of the entire DFS and BFS. Also, the Python program will provide as output some of the intermediate nodes that are transverse during the execution of the DFS and BFS. This output of the intermediate nodes searched will demonstrate the different paths executed during a DFS versus BFS.

Problem

Develop functions to demonstrate understanding of implementing a recursive depth first search (DFS) and an iterative breadth first search (BFS) in Python using a simple graph made of nodes. This example will use nodes A, B, C, D, and E connected as follows:

A —–
/ \ |
B-D-C |
\ | / |
E —–

The program must have the following:

Demonstration of writing a recursive depth first search (DFS) function.
Demonstration of writing an iterative breadth first search (BFS) function.
Documentation Guidelines:

Use good programming style (e.g., indentation for readability) and document each of your program parts with the following items (the items shown between the ‘<‘ and ‘>’ angle brackets are only placeholders. You should replace the placeholders and the comments between them with your specific information). Your cover sheet should have some of the same information, but what follows should be at the top of each program’s sheet of source code. Some lines of code should have an explanation of what is to be accomplished, this will allow someone supporting your code years later to comprehend your purpose. Be brief and to the point. Start your design by writing comment lines of pseudocode. Once that is complete, begin adding executable lines. Finally run and test your program.

Deliverable(s):

Your deliverable should be a Word document with screenshots showing the source code and running results, and discuss the issues that you had for this project related to AWS and/or Python IDE and how you solved them for all of the programs listed above as well as the inputs and outputs from running them. Submit a cover sheet with the hardcopy of your work.

Part 4

Two user-defined classes are recommended: class Transaction, and class BankStatement. A main() function will be required that declares a BankStatement object and Transaction objects and then performs operations as shown in the example driver main function below. The BankStatement object (called myStatement in the example main() shown later) contains a container of Transaction objects along with other record-keeping data fields. This is yet another example of the Containment/Composition (a.k.a., “Has-A”) relationship that can exist between classes/objects.

The Transaction class is used to create deposit and withdrawal objects. It contains a constructor for initialization. This constructor has three defaulted parameters to facilitate the user declaring transactions and passing appropriate initial data member values as parameters to this constructor or accepting one or more of its defaulted initializer values. It is certainly legal, and perhaps desirable, to also have a LoadTransaction() method that would take inputs from the keyboard on an interactive basis. This, in conjunction with a menu, is a desirable addition but not required for this exercise. The main() driver function shows a partial batch (i.e., hard-coded) implementation rather than the, perhaps, more desirable interactive implementation. See your instructor for any specific additional requirements.

# Python model of a bank transaction which can be either

# A deposit or a withdraw

#

# Filename: transaction.py

class Transaction:

def __init__(self, inAmount = 0.0, inCode = ‘D’, inNote = “No note”):

self.__Amount = inAmount if inAmount >= 0.0 else 0.0

self.__Code = inCode if inCode == ‘D’ or inCode == ‘W’ else ‘D’

self.__Note = inNote if len(inNote) > 0 else “No note”

def setAmount(self, newAmount):

self.__Amount = newAmount if newAmount >= 0.0 else self.__Amount

def getAmount(self):

return self.__Amount

def setCode(self, newCode):

self.__Code = newCode if newCode == ‘W’ or newCode == ‘D’ else self.__Code

def getCode(self):

return self.__Code

def setNote(self, newNote):

self.__Note = newNote if len(newNote) > 0 else self.__Note

def getNote(self):

return self.__Note

def loadTransaction(self):

self.setAmount(float(input(“Enter transaction amount(DD.CC), $ “)))

self.setCode(input(“Enter transaction code (‘W’ or ‘D’), “))

self.setNote(input(“Enter purpose of transaction, “))

The BankStatement class contains two list containers of Transaction objects, a list container of float values, some BankStatement support data fields, and the required methods to manipulate selected data fields, insert transactions, arrange (i.e., sort) the contained transaction objects, and print them.

# Python model of a bank statement capable of

# holding and managing multiple bank transactions

#

# Filename: bankStatement.py

from transaction import Transaction

class BankStatement:

def __init__(self, begBal = 0.0, endBal = 0.0):

self.__TransactionLog = []

self.__ArrangedLog = []

self.__RunningBalLog = []

self.__BegBal = begBal

self.__EndBal = endBal

self.__NumEntries = 0

self.__NumDeposits = 0

self.__NumWithdrawals = 0

def setBegEndBals(self, balance):

self.__BegBal = self.__EndBal = balance

def getBegBal(self):

return self.__BegBal

def getEndBal(self):

return self.__EndBal

def getNumEntries(self):

return self.__NumEntries

def getNumDeposits(self):

return self.__NumDeposits

def getNumWithdrawals(self):

return self.__NumWithdrawals

def insertTransaction(self, transaction):

self.__Transactionlog.append(transaction)

# Update __RunningBalLog, increment __NumEntries and increment either

# __NumDeposits or __NumWithdrawals depending upon whether transaction is a deposit

# or a withdrawal

def displayResults(self):

# Displays __BegBal, __TransactionLog list, __RunningBal list, and final stats (i.e., __EndBal, total transactions, number of deposits and number of withdrawls)

# See example output

def arrangeTransactions(self):

# Builds __ArrangedLog list from __TransactionLog list

def printArranged(self):

# Displays the __ArrangedLog list

The declared classes and their contents are a starting point for the program. You may not need all the class members described above. Do not feel bound to implement this program using the exact methods and/or data fields given. The primary objective is for you to solve the problem of providing a bank statement to the bank’s customers using O-O programming techniques. HOWEVER, if you deviate from the above design be sure to fully document your design! If in doubt as to whether your deviation violates the intent of this exercise, ask your instructor.

In the interest of sound software engineering practice, make sure to validate the values provided to the constructor method of class BankStatement. For invalid values you may choose to completely reject the passed in values and set the data fields to some default (but valid) values. In either case you should also display an error message.

Below is a non-interactive(i.e., batch), main driver test function that will confirm to you and your instructor that the program operates as expected. Use the transaction objects given as data for your final submission of the program.

def main(): # NOTE THIS IS A NON-INTERACTIVE DRIVER!

myStatement = BankStatement()

myStatement.setBegEndBals(15.92); # Sets beginning AND ending balance data fields

# Declare some transaction objects

T1 = Transaction() # TEST DATA

T1.setAmount (123.56)

T1.setCode(‘D’)

T1.setNote(“CTPay”)

T2 = Transaction(153.86, ‘W’,”Rent”)

T3 = Transaction()

T3.setAmount(75.56)

T3.setCode(‘D’)

T3.setNote(“Tips”)

T4 = Transaction(12.56, ‘D’,”Gift”)

T5 = Transaction()

T5.setAmount(73.74)

T5.setCode(‘W’)

T5.setNote(“Date”)

T6 = Transaction(145.75, ‘D’,”Loan”)

T7 = Transaction()

T7.setAmount(40.00)

T7.setCode(‘W’)

T7.setNote(“Loan Payment”)

T8 = Transaction(21.74, ‘W’, “Groceries”)

# Now insert the transaction objects into the bank statement

myStatement.enterTransaction(T1) # Enter transactions into the

myStatement.enterTransaction(T2) # BankStatement object

# Six more transactions entered……………………………………………………………

………………………………………………………………………………………………

# continue

# Manipulate the bank statement

myStatement.displayResults()

myStatement.arrangeTransactions()

myStatement.printArranged()

The following is a look at what the output might look like from the method, displayResults().

The beginning balance was: $15.92

Transaction: 1 was a D amount: $123.56 for CTPay

Running Bal: $139.48

Transaction: 2 was a W amount: $153.86 for Rent

Running Bal: $-14.38 OVERDRAWN

etc., for the other transactions……….……………………………………………………

…….……………………………………………………………………………………….

The ending balance is: $84.01

The number of Transactions is: 8

The number of Deposits is: 4

The number of Withdrawals is: 4

The following is the result after calling the arrangeTransactions() and printArranged() methods in the BankStatement class.

Printing the Deposits and Withdrawals as a group:

Transaction was a D amount: $123.56 for CTPay

Transaction was a D amount: $75.56 for Tips

Transaction was a D amount: $12.56 for Gift

Transaction was a D amount: $145.75 for Loan

Transaction was a W amount: $153.86 for Rent

Transaction was a W amount: $73.74 for Date

Transaction was a W amount:$40.00 for Loan Payment

Transaction was a W amount: $21.74 for Groceries

To build the ArrangedLog container in method, arrangeTransactions(), the following strategy is recommended:

1. Traverse the TransactionLog container checking each cell to determine if it is

a deposit (‘D’) or withdrawal (‘W’):

Loop for number of entries in the TransactionLog

if TransactionLog[i].getCode() == ‘D’:

append transaction in TransactionLog[i] to next open cell in list container,

ArrangedLog

2. In another loop (very similar to the loop above), go back to the beginning of the TransactionLog container and check for all the ‘W’s and copy (i.e., append) them to the ArrangedLog container following the deposits.

Now the method, printArranged(), just needs to loop through its entries and display the contents of the ArrangedLog container as shown above.

Notice that the methods of an object contained within a containment object are accessed with the selection operator just as though you had the name of the object. However, inside the BankStatement object (myStatement), all you have access to is a container of Transaction objects — not their individual names — hence the TransactionLog[i].getNote() notation.

Deliverable(s)

Your deliverable should be a Word document with screenshots showing the source code and running results. If the source code is too long, you may insert your source code files as Packages into the Word document. You may also login to the class by using a browser in the AWS, and upload your source code directly. Submit a cover sheet with the hardcopy of your work.

Last Updated on