Created
April 17, 2017 01:04
-
-
Save frankmeola/54d579fe44e98fd16b04167723b537de to your computer and use it in GitHub Desktop.
Self Explaining Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Self Explaining Code Sample</title> | |
<!-- Google Fonts --> | |
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic"> | |
<!-- CSS Reset --> | |
<link rel="stylesheet" href="//cdn.rawgit.com/necolas/normalize.css/master/normalize.css"> | |
<!-- Milligram CSS minified --> | |
<link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css"> | |
<style> | |
.explanation-messages{ | |
padding: 15px; | |
margin-bottom: 20px; | |
border: 1px solid transparent; | |
border-radius: 4px;i | |
color: #31708f; | |
background-color: #d9edf7; | |
border-color: #bce8f1; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="row"> | |
<div class="column"> | |
<h3>Amount of loan taken: {{ loanHistory.originalLoanAmount }}</h3> | |
<table> | |
<thead> | |
<tr> | |
<th>Date</th> | |
<th>Description</th> | |
<th>Amount</th> | |
</tr> | |
</thead> | |
{% for item in loanHistory.transactions %} | |
<tr> | |
<td>{{ item.transactionDate }}</td> | |
<td>{{ item.description }}</td> | |
<td>{{ item.amount }}</td> | |
</tr> | |
{% endfor %} | |
<tfoot> | |
<tr> | |
<td></td> | |
<th>Outstanding Loan Amount</th> | |
<th>{{ loanHistory.outstandingBalance }}</th> | |
</tr> | |
</tfoot> | |
</table> | |
{% if 'explanation' in loanHistory and loanHistory.explanation.messages %} | |
<div class="explanation-messages"> | |
<h2>{{ loanHistory.explanation.context }}</h2> | |
<ul> | |
{% for item in loanHistory.explanation.messages %} | |
<li>{{ item }}</li> | |
</ul> | |
{% endfor %} | |
</div> | |
{% endif %} | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, request | |
app = Flask(__name__) | |
class Explanation: | |
def __init__(self): | |
self._context = '' | |
self._messages = [] | |
def explaining(self, context): | |
self._context = 'Explanation for : ' + context | |
def given(self, msg): | |
self._messages = self._messages + ['Given the following: ' + msg] | |
def because(self, dueTo, therefore): | |
self._messages = self._messages + ['The following occurred: ' + dueTo + ' which resulted in ' + therefore] | |
def becauseThen(self, dueTo, therefore, nextSteps): | |
self._messages = self._messages + ['The following occurred: ' + dueTo + ' which resulted in ' + therefore + '. Next steps: ' + nextSteps] | |
def getExplanation(self): | |
return {'context':self._context, 'messages':self._messages } | |
def getUserLoanHistory(userId, needExplanation): | |
explainer = Explanation() | |
if needExplanation: explainer.explaining('getUserLoanHistory for user ' + str(userId)) | |
startingBalance = 1000.0 | |
if needExplanation: explainer.given('The starting balance of the loan is ' + str(startingBalance)) | |
repaymentsMade = [{'transactionDate':'2/14/2017','description':'Loan Repayment','amount':25.0, 'status':'POSTED'}, | |
{'transactionDate':'3/15/2017','description':'Loan Repayment','amount':25.0, 'status':'POSTED'}, | |
{'transactionDate':'4/14/2017','description':'Loan Repayment','amount':25.0, 'status':'PENDING'}] | |
balanceRemaining = startingBalance | |
for repayment in repaymentsMade: | |
if repayment['status'] == 'POSTED': | |
balanceRemaining -= repayment['amount'] | |
if needExplanation: | |
explainer.because('A loan repayment was posted on ' + repayment['transactionDate'] + ' with an amount of ' + str(repayment['amount']), | |
"the loan's remaining balance became " + str(balanceRemaining)) | |
else: | |
if needExplanation: | |
explainer.becauseThen('A loan repayment was received with a status of '+ repayment['status'] +' on ' + repayment['transactionDate'] + ' with an amount of ' + str(repayment['amount']), | |
"the repayment has not posted so the loan's remaining balance is still " + str(balanceRemaining), | |
"No action is needed. When the repayment is posted it will reduce the loan's remaining balance.") | |
return { 'originalLoanAmount': startingBalance, 'transactions' : repaymentsMade, 'outstandingBalance': balanceRemaining, 'explanation':explainer.getExplanation()} | |
@app.route("/") | |
def loanPayments(): | |
shouldIExplain = request.args.get('why', False) | |
viewModel = { 'hello': 'world', 'loanHistory': getUserLoanHistory(123, shouldIExplain) } | |
return render_template('loan_repayments.html', **viewModel) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=8988, debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template_string, render_template | |
app = Flask(__name__) | |
def getUserLoanHistory(userId): | |
startingBalance = 1000.0 | |
repaymentsMade = [{'transactionDate':'2/14/2017','description':'Loan Repayment','amount':25.0, 'status':'POSTED'}, | |
{'transactionDate':'3/15/2017','description':'Loan Repayment','amount':25.0, 'status':'POSTED'}, | |
{'transactionDate':'4/14/2017','description':'Loan Repayment','amount':25.0, 'status':'PENDING'}] | |
balanceRemaining = startingBalance | |
for repayment in repaymentsMade: | |
if repayment['status'] == 'POSTED': | |
balanceRemaining -= repayment['amount'] | |
return { 'originalLoanAmount': startingBalance, 'transactions' : repaymentsMade, 'outstandingBalance': balanceRemaining } | |
@app.route("/") | |
def loanPayments(): | |
viewModel = { 'hello': 'world', 'loanHistory': getUserLoanHistory(123) } | |
return render_template('loan_repayments.html', **viewModel) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=8989, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment