Python 3 formatting issue with '$' - python-3.x

I just started learning Python this week and I'm having an issue with some line formatting. I am trying to insert a '$' along with the calculated values, I've tried to insert the '$' with a new {} set, as well as here {{$:>{}.2f}} (based on Google search suggestions), however I keep getting errors.
I'm using Google Collab to run the code.
def budget_calc(money, rent, food, phone, gas, extra_income):
width = 35
price_width = 10
item_width = width - price_width
income = sum(weekly_income) + extra_income
expenses = sum(credit_cards) + rent + phone + food + gas + utilities
after_tax =income*0.84
#after_tax_instr = str(after_tax)
gross = 'Money before tax:' #+ str(income)
post_tax= 'Post tax income:' #+after_tax_instr
tax_payment ='Taxes due:' #+str(income*.16)
savings = after_tax - expenses
total = 'Money left over:' #+'$'+str(savings)
expense_total = 'Total expenses' #+str([expenses])
line = "{{:{}}} {{:>{}.2f}}".format(item_width, price_width)
print(line.format(gross, income))
print(line.format(tax_payment, (income*0.16)))
print(line.format(post_tax, after_tax))
print(line.format(expense_total, expenses))
print(line.format(total, savings))
The output I get is:
Money before tax: 3300.00
Taxes due: 528.00
Post tax income: 2772.00
Total expenses 2190.00
Money left over: 582.00
Days until my Mac: 44.36
I would appreciate any pointers in the right direction.

I think the error it throws will be keyerror and it because of the messy string format
def budget_calc(money, rent, food, phone, gas, extra_income):
width = 35
price_width = 10
item_width = width - price_width
income = sum(weekly_income) + extra_income
expenses = sum(credit_cards) + rent + phone + food + gas + utilities
after_tax =income*0.84
#after_tax_instr = str(after_tax)
gross = 'Money before tax:' #+ str(income)
post_tax= 'Post tax income:' #+after_tax_instr
tax_payment ='Taxes due:' #+str(income*.16)
savings = after_tax - expenses
total = 'Money left over:' #+'$'+str(savings)
expense_total = 'Total expenses' #+str([expenses])
# You can specify the width integer instead of formating
line = "{0:<25} ${1:.2f}"
print(line.format(gross, income))
print(line.format(tax_payment, (income*0.16)))
print(line.format(post_tax, after_tax))
print(line.format(expense_total, expenses))
print(line.format(total, savings))
Note : since weekly_income and credit_cards are not provided i just
removed it from script in order to check the output
When `budget_calc(33000,500,10,100,152,510)` this is called the output
Output :
Money before tax: $33510.00
Taxes due: $5361.60
Post tax income: $28148.40
Total expenses $762.00
Money left over: $27386.40

Python 3.6+ String interpolation is recommended way of string formatting because of its high readabililty.
The best way would be:
print(f'Money before tax: {income}')
print(f'Taxes due:{income*0.16}')
print(f'Post tax income: {after_tax}')
print(f'Total expenses: {expenses}')
print(f'Money left over :{savings}')
But if you want to use $ String Formatting only , the you should try the answer by DaVinci

Related

How to make my code work more efficiently

I made this grade calculator that calculates the grade form my school website. There are some minor flaws that do not affect the code, but does bug me. For example, When I paste the grade from the website, I have to press enter twice, which people who are using my program for the first time get confused with. Also, I want to make it so that the code does not create errors when I press enter without any data given in input.
Not only, these, but I want some feedback on my code and how I can improve them.
This is the code I worked on so far.
class color:
reset = '\\033\[0m'
class fg:
green = '\\033\[32m'
orange = '\\033\[33m'
\#getting input from user
def multi_input():
try:
while True:
data=input(color.fg.orange)
if not data: break
yield data
except KeyboardInterrupt:
return
restart=1
while restart!="x":
score = \[\]
percent = \[\]
add = \[\]
print(color.fg.green + "Enter Grade" + color.reset)
data = 0
data = list(multi_input())
#filter data into percent and score
for i in range(3, len(data),4):
data[i] = data[i].split('\t')
try:
percent.append(data[i][3])
score.append(data[i][4])
except IndexError:
result = 0
#take out ungraded values
percent = [value for value in percent if value != '']
score = [value for value in score if value != '']
#refine percent data
for i in range(len(percent)):
try:
percent[i] = percent[i].replace('%', '')
percent[i] = float(percent[i])
except ZeroDivisionError:
result = 0
#refine score data
for i in range(len(score)):
score[i] = score[i].split('/')
for j in range(len(score[i])):
score[i][j] = float(score[i][j])
try:
score[i] = score[i][0]/score[i][1]*100
except ZeroDivisionError:
result = 0
#amount of assignments
print()
print(color.fg.green + "graded assignments: ", len(score))
#calculation
for i in range(len(score)):
add.append(score[i]*percent[i]/100)
print(color.fg.green + "Percentage: ", f"{sum(add)/sum(percent)*100:05.2f}" + color.reset)
restart = input(color.fg.green + "press any key to start again, or x to exit.")
print()
This is a sample grade copied from my school website so that you can test my code out.
Nov
02
Projects & Labs
4.2 If statements & 4.3 Comparison A 1% 100/100 11/2/22
Nov
02
Quiz
4.2 If statements & 4.3 Comparison Quizzes A 0.4% 100/100 11/2/22
Oct
31
Projects & Labs
4.1 Booleans Labs A 1% 100/100 10/31/22
Oct
31
Quiz
4.1 Boolean Quiz A 0.4% 100/100 10/31/22
Oct
24
Exams & Tests
Python Console & Interaction Module Test A 12.5% 200/200 Test 18/20 Programming: 100(Extra 10 points Extra credit) 10/24/22
Oct
24
Homework
Study for Python Console & Interaction Quiz & Programming Test: Ungraded (Ungraded)
Oct
21
Projects & Labs
3.6 Comments Quiz & Lab C 1% 75/100 Quiz = 1/2 10/26/22
Oct
21
Projects & Labs
3.5 String Operators Labs A 2% 200/200 no screenshot of recipe 10/24/22

Compare QuantLib bond pricing with Excel functions YIELD and PRICE when doing stress testing

I calculated bond price and stressed bond price (shocking up yield) in both Excel and Python Quantlib. As the following table shows, weird results were generated: base bond price matches well between Excel and Quantlib but the stressed bond prices have more gaps (1.5% relative difference). Yields also show some gaps.
Can you please provide some comments?
Excel Code:
Base Bond Price=PRICE("2021-8-19","2025-8-19",4.5%,YIELD("2021-8-19","2025-8-
19",4.5%,95,100,4,0),100,4,0)
Stress Bond Price=PRICE("2021-8-19","2025-8-19",4.5%,YIELD("2021-8-19","2025-8-
19",4.5%,95,100,4,0)+662/10000,100,4,0)
Python code:
import datetime
import QuantLib as ql
settlement_date = ql.Date(19,8,2021)
valuation_date = ql.Date(19,8,2021)
issue_date = ql.Date(19,8,2021)
maturity_date = ql.Date(19,8,2025)
tenor = ql.Period(4)
calendar = ql.UnitedStates()
business_convention = ql.Following
date_generation = ql.DateGeneration.Backward
end_month = False
face_value = 100
coupon_rate = 450/10000
day_count = ql.Thirty360(ql.Thirty360.USA)
redemption_value = 100
schedule = ql.Schedule(issue_date, maturity_date, tenor, calendar, business_convention, business_convention, date_generation, end_month)
bond = ql.FixedRateBond(settlement_date-valuation_date, face_value, schedule, [coupon_rate], day_count, business_convention, redemption_value, issue_date)
target_price = 95
bond_yield = bond.bondYield(target_price, day_count, ql.Compounded, 4, ql.Date(), 1.0e-8,1000)
bond_price = bond.cleanPrice(bond_yield, day_count, ql.Compounded, 4)
STRESS = 662
stress_bond_yield = bond_yield+STRESS/10000
stress_bond_price = bond.cleanPrice(stress_bond_yield, day_count, ql.Compounded, 4)
excel_base_bond_price = 99.50
excel_stress_bond_price = 75.02971569
print('Base bond price from excel is', excel_base_bond_price )
print('Base bond price from Quantlib is', bond_price)
print('Stressed bond price from excel is',excel_stress_bond_price)
print('Stressed bond price from Quantlib is',stress_bond_price)
You need to add
ql.Settings.instance().evaluationDate = valuation_date
before the calculation. If you don't do so, you'll be calculating the yield and price as of today instead of as of the valuation date. Once you do that, the yields and prices match a lot better.

How can I make the trailing sell move up with price and not go down when price reverse without using the high of the current candle in PineScript

How can I make the trailing sell move up with price and not go down when price reverse without using the high of the current candle in PineScript
//#version=4
study("Trailing Sell ", overlay=true)
//--------------------------------------------TRAILING SELL-------------------------------------------
//TRAILING SELL LINE
show_drop_labels = input(true, title="Show Labels Trailing Below")
trailing_sell_offset = input(-0.2, title="Red Trailing Sell Line % Set Above Current Price", maxval=100, step=0.1)
src = close // should trail up behind current price and not the candle(high). should trail up and stop if price reverse.
sumbars2 = 1
color_sell = color.red
per_calc = (sum(src,sumbars2)/sumbars2)*(1+(trailing_sell_offset/100))
trailing_sell_line = line.new(x1=bar_index, y1=per_calc,
x2=bar_index[13], y2=per_calc, width=1, color=color_sell)
line.set_extend(trailing_sell_line, extend.none)
line.delete(trailing_sell_line[1])
//TRAILING SELL LABEL
var line l12 = na
if show_drop_labels
label trailing_sell_label = label.new(bar_index[12] , per_calc, "Trailing Sell Line", textcolor=color.white, style=label.style_label_right, color=color_sell, size=size.tiny )
line.delete(l12[1])
label.delete(trailing_sell_label[1])

Parsing heterogenous data from a text file in Python

I am trying to parse raw data results from a text file into an organised tuple but having trouble getting it right.
My raw data from the textfile looks something like this:
Episode Cumulative Results
EpisodeXD0281119
Date collected21/10/2019
Time collected10:00
Real time PCR for M. tuberculosis (Xpert MTB/Rif Ultra):
PCR result Mycobacterium tuberculosis complex NOT detected
Bacterial Culture:
Bottle: Type FAN Aerobic Plus
Result No growth after 5 days
EpisodeST32423457
Date collected23/02/2019
Time collected09:00
Gram Stain:
Neutrophils Occasional
Gram positive bacilli Moderate (2+)
Gram negative bacilli Numerous (3+)
Gram negative cocci Moderate (2+)
EpisodeST23423457
Date collected23/02/2019
Time collected09:00
Bacterial Culture:
A heavy growth of
1) Klebsiella pneumoniae subsp pneumoniae (KLEPP)
ensure that this organism does not spread in the ward/unit.
A heavy growth of
2) Enterococcus species (ENCSP)
Antibiotic/Culture KLEPP ENCSP
Trimethoprim-sulfam R
Ampicillin / Amoxic R S
Amoxicillin-clavula R
Ciprofloxacin R
Cefuroxime (Parente R
Cefuroxime (Oral) R
Cefotaxime / Ceftri R
Ceftazidime R
Cefepime R
Gentamicin S
Piperacillin/tazoba R
Ertapenem R
Imipenem S
Meropenem R
S - Sensitive ; I - Intermediate ; R - Resistant ; SDD - Sensitive Dose Dependant
Comment for organism KLEPP:
** Please note: this is a carbapenem-RESISTANT organism. Although some
carbapenems may appear susceptible in vitro, these agents should NOT be used as
MONOTHERAPY in the treatment of this patient. **
Please isolate this patient and practice strict contact precautions. Please
inform Infection Prevention and Control as contact screening might be
indicated.
For further advice on the treatment of this isolate, please contact.
The currently available laboratory methods for performing colistin
susceptibility results are unreliable and may not predict clinical outcome.
Based on published data and clinical experience, colistin is a suitable
therapeutic alternative for carbapenem resistant Acinetobacter spp, as well as
carbapenem resistant Enterobacteriaceae. If colistin is clinically indicated,
please carefully assess clinical response.
EpisodeST234234057
Date collected23/02/2019
Time collected09:00
Authorised by xxxx on 27/02/2019 at 10:35
MIC by E-test:
Organism Klebsiella pneumoniae (KLEPN)
Antibiotic Meropenem
MIC corrected 4 ug/mL
MIC interpretation Resistant
Antibiotic Imipenem
MIC corrected 1 ug/mL
MIC interpretation Sensitive
Antibiotic Ertapenem
MIC corrected 2 ug/mL
MIC interpretation Resistant
EpisodeST23423493
Date collected18/02/2019
Time collected03:15
Potassium 4.4 mmol/L 3.5 - 5.1
EpisodeST45445293
Date collected18/02/2019
Time collected03:15
Creatinine 32 L umol/L 49 - 90
eGFR (MDRD formula) >60 mL/min/1.73 m2
Creatinine 28 L umol/L 49 - 90
eGFR (MDRD formula) >60 mL/min/1.73 m2
Essentially the pattern is that ALL information starts with a unique EPISODE NUMBER and follows with a DATE and TIME and then the result of whatever test. This is the pattern throughout.
What I am trying to parse into my tuple is the date, time, name of the test and the result - whatever it might be. I have the following code:
with open(filename) as f:
data = f.read()
data = data.splitlines()
DS = namedtuple('DS', 'date time name value')
parsed = list()
idx_date = [i for i, r in enumerate(data) if r.strip().startswith('Date')]
for start, stop in zip(idx_date[:-1], idx_date[1:]):
chunk = data[start:stop]
date = time = name = value = None
for row in chunk:
if not row: continue
row = row.strip()
if row.startswith('Episode'): continue
if row.startswith('Date'):
_, date = row.split()
date = date.replace('collected', '')
elif row.startswith('Time'):
_, time = row.split()
time = time.replace('collected', '')
else:
name, value, *_ = row.split()
print (name)
parsed.append(DS(date, time, name, value))
print(parsed)
My error is that I am unable to find a way to parse the heterogeneity of the test RESULT in a way that I can use later, for example for the tuple DS ('DS', 'date time name value'):
DATE = 21/10/2019
TIME = 10:00
NAME = Real time PCR for M tuberculosis or Potassium
RESULT = Negative or 4.7
Any advice appreciated. I have hit a brick wall.

Vanilla Interest Rate Swap Valuation in Python using QuantLib

I am valuing a Vanilla Interest Rate Swap as at 31 January 2017 (Valuation Date) but the effective date of the Vanilla Interest Rate Swap is 31 December 2016 (Start Date). Firstly, I would like to know how I can adjust for my valuation date and start date in the code below;
import QuantLib as ql
startDate = ql.Date(31,12,2016)
valuationDate = ql.Date(31,1,2017)
maturityDate = ql.Date(30,9,2019)
calendar = ql.SouthAfrica()
bussiness_convention = ql.Unadjusted
swapT = ql.VanillaSwap.Payer
nominal = 144000000
fixedLegTenor = ql.Period(3, ql.Months)
fixedSchedule = ql.Schedule(startDate, maturityDate,
fixedLegTenor, calendar,
ql.ModifiedFollowing, ql.ModifiedFollowing,
ql.DateGeneration.Forward, False)
fixedRate = 0.077
fixedLegDayCount = ql.Actual365Fixed()
spread = 0
floatLegTenor = ql.Period(3, ql.Months)
floatSchedule = ql.Schedule(startDate, maturityDate,
floatLegTenor, calendar,
ql.ModifiedFollowing, ql.ModifiedFollowing,
ql.DateGeneration.Forward, False)
floatLegDayCount = ql.Actual365Fixed()
discountDates = [ql.Date(31,1,2017),ql.Date(7,2,2017),ql.Date(28,2,2017),
ql.Date(31,3,2017),ql.Date(28,4,2017),ql.Date(31,7,2017),
ql.Date(31,10,2017),ql.Date(31,1,2018),ql.Date(31,1,2019),
ql.Date(31,1,2020),ql.Date(29,1,2021),ql.Date(31,1,2022),
ql.Date(31,1,2023),ql.Date(31,1,2024),ql.Date(31,1,2025),
ql.Date(30,1,2026),ql.Date(29,1,2027),ql.Date(31,1,2029),
ql.Date(30,1,2032),ql.Date(30,1,2037),ql.Date(31,1,2042),
ql.Date(31,1,2047)]
discountRates = [1,0.9986796,0.99457,0.9884423,0.9827433,0.9620352,0.9420467,0.9218714,
0.863127,0.7993626,0.7384982,0.6796581,0.6244735,0.5722537,0.5236629,
0.4779477,0.4362076,0.3619845,0.2795902,0.1886847,0.1352048,0.1062697]
jibarTermStructure = ql.RelinkableYieldTermStructureHandle()
jibarIndex = ql.Jibar(ql.Period(3,ql.Months), jibarTermStructure)
jibarIndex.addFixings(dtes, fixings)
discountCurve = ql.DiscountCurve(discountDates,discountRates,ql.Actual365Fixed())
swapEngine = ql.DiscountingSwapEngine(discountCurve)
interestRateSwap = ql.VanillaSwap(swapT, nominal, fixedSchedule,
fixedRate,fixedLegDayCount,jibarIndex,spread,floatSchedule,
floatLegDayCount,swapEngine,floatSchedule.convention)
Secondly, I would like to know how best I can incorporate jibarIndex in interestRateSwap =ql.VanillaSwap() or else how can I use my Discount Factors and Discount Dates to calculate the value of the Interest Rate Swap
ql.Settings.instance().setEvaluationDate(today)
sets the evaluation date or valuation date.
I don't see a problem with your jibarIndex index in the VanillaSwap code. You've given the object to QuantLib, the library will use it as a forward curve to price your floating leg and thus the swap.

Resources