def calculate_balance(balance, annualInterestRate, monthlyPaymentRate):
totalPaid = 0
remainingBalance = balance
for month in range(1,13):
monthlyInterestRate = annualInterestRate / 12.0
minimumMonthlyPayment = round((monthlyPaymentRate * remainingBalance),2)
totalPaid += minimumMonthlyPayment
monthlyUpaidBalance = remainingBalance - minimumMonthlyPayment
remainingBalance = round(monthlyUpaidBalance + (monthlyInterestRate * monthlyUpaidBalance),2)
print("Month: {}".format(month))
print("Minimum monthly payment: {}".format(minimumMonthlyPayment))
print("Remaining Balance: {}".format(remainingBalance))
print("Total paid: {}".format(totalPaid))
print("Remaining Balance: {}".format(remainingBalance))
calculate_balance(balance, annualInterestRate, monthlyPaymentRate)
Category Archives: Python
edX_MITx_6.00.1x/Problem_Set_2/Paying_Debt_Off_in_a_Year.py
def calculate_balance(balance, annualInterestRate):
monthlyInterestRate = annualInterestRate / 12.0
remainingBalance = balance
minimumMonthlyPayment = 0
while True:
minimumMonthlyPayment += 10
for _ in range(0,12):
monthlyUpaidBalance = remainingBalance - minimumMonthlyPayment
remainingBalance = round(monthlyUpaidBalance + (monthlyInterestRate * monthlyUpaidBalance),2)
if remainingBalance < 0:
break
else:
remainingBalance = balance
print("Lowest Payment: {}".format(minimumMonthlyPayment))
calculate_balance(balance, annualInterestRate)
edX_MITx_6.00.1x/Problem_Set_2/Bisection_Search.py
def calculate_balance(balance, annualInterestRate):
monthlyInterestRate = annualInterestRate / 12.0
lowerBound = balance / 12
upperBound = (balance * (1+ monthlyInterestRate)**12)/12
minimumMonthlyPayment = (upperBound + lowerBound)/2.0
while True:
remainingBalance = balance
for _ in range(0,12):
monthlyUpaidBalance = remainingBalance - minimumMonthlyPayment
remainingBalance = round(monthlyUpaidBalance + (monthlyInterestRate * monthlyUpaidBalance),2)
if remainingBalance <= 0 and remainingBalance >= -0.01:
break
else:
if remainingBalance > 0:
lowerBound = minimumMonthlyPayment
else:
upperBound = minimumMonthlyPayment
minimumMonthlyPayment = (upperBound + lowerBound)/2.0
print("Lowest Payment: {}".format(round(minimumMonthlyPayment,2)))
calculate_balance(balance, annualInterestRate)
edX_MITx_6.00.1x/Problem_Set_1/item_order.py
order = ""
def item_order(order):
salad = order.count("salad")
hamburger = order.count("hamburger")
water = order.count("water")
return("salad:{} hamburger:{} water:{}".format(salad, hamburger, water))
item_order(order)
edX_MITx_6.00.1x/Problem_Set_1/count_vowels.py
def count_vowels(s):
count = 0
for char in s:
if char in 'aeiou':
count += 1
print("Number of vowels: {}".format(count))
count_vowels(s)
edX_MITx_6.00.1x/Problem_Set_1/count_occurrence.py
def count_occurrence(s):
count = 0
pos = s.find("bob")
while pos != -1:
count += 1
pos = s.find("bob", pos+1)
print("Number of times bob occurs is: {}".format(count))
count_occurrence(s)
Automate Facebook Login and Status Update with Python Selenium
You can download the source code from my GitHub page
https://github.com/bikrammann/web_crawler/blob/master/Automate_Facebook_Login_and_Status_Update.py
Step 1 – Required imports
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
Step 2 – Create Firefox object and use get method then sleep for 5 seconds
driver = webdriver.Firefox() # Firefox Browser
driver.get('http://www.facebook.com')
time.sleep(5)
To use Google Chrome with Selenium, First install Google Chrome Driver from ‘https://sites.google.com/a/chromium.org/chromedriver/’. Then replace ‘C:/chrome/chromedriver.exe’ with your chrome driver path
# Replace
driver = webdriver.Firefox() # Firefox Browser
# With
driver = webdriver.Chrome('C:/chrome/chromedriver.exe') # Chrome Browser
Step 3 – Locate email address and password field on Facebook Login Page
username = driver.find_element_by_name("email")
password = driver.find_element_by_name("pass")
Step 4 – Add values to email address and password fields
username.send_keys("[email protected]")
password.send_keys("password123")
Step 5 – Sleep for 1 second, then press the login button
time.sleep(1)
driver.find_element_by_id("loginbutton").click()
Step 6 – Locate the text box where it says “What’s on your mind?”
message = driver.find_element(By.XPATH, "//textarea[@name='xhpc_message']")
Step 7 – Click the text box area to get it in focus where it says “What’s on your mind?”
ActionChains(driver) \
.key_down(Keys.CONTROL) \
.click(message) \
.key_up(Keys.CONTROL) \
.perform()
Step 8 – Message for status update
message.send_keys("Ha Ha")
Step 9 – Press the post button to submit the status update
driver.find_element(By.XPATH, '//button[text()="Post"]').click()
time.sleep(5)
Step 10 – Close the browser
driver.close()
Error: ‘xslt-config’ is not recognized as an internal or external command
Error: 'xslt-config' is not recognized as an internal or external command make sure the development packages of libxml2 and libxslt are installed
If you are getting this message when trying to pip install lxml or install lxml via Pycharm project interpreter, here the solution https://www.bikrammann.com/error-microsoft-visual-c-10-0-is-required-unable-to-find-vcvarsall-bat/
Error: Microsoft visual C++ 10.0 is required (Unable to find vcvarsall.bat)
I got this error message ” Error: Microsoft visual C++ 10.0 is required (Unable to find vcvarsall.bat) ” when I was trying to install lxml package on windows 10 using the command
pip install lxml
Here is the solution to fix this problem
Step 1 – Upgrade pip, setuptools and virtualenv
python -m pip install -U pip pip install -U setuptools pip install -U virtualenv
Step 2 – Download the latest version of lxml MS Windows installer from this link https://pypi.python.org/pypi/lxml/. Then move the downloaded file
lxml-3.6.0.win32-py3.2.exe to c:\python35\Scripts ( or where your python package is installed ).
Then use easy_install to install the windows installer
easy_install lxml-3.6.0.win32-py3.2.exe
Step 3 – Now you should be able to install the lxml package using easy_install
easy_install lxml
If everything works you will see a message similar to this
Installed python\lib\site-packages\lxml-3.6.0-py3.5-win32.egg Processing dependencies for lxml=3.6.0 Finished processing dependencies for lxml=3.6.0
CSV to Excel (.xlsx) Converter Python – xlsxwriter
CSV to Excel Converter program in Python
This program will convert .CSV ( Comma-Separated Values ) Files to Excel (.XLSX ) format using python XlsxWriter module. You can download the file directly from my GitHub page:
https://github.com/bikrammann/mannpetroleum/blob/master/csv_to_xlsx.py
Below is the documentation as how the program works.
Step 1 – Required modules for the program
import csv, os
from glob import glob
from xlsxwriter.workbook import Workbook
Step 2 – This for loop will give you list of csv files in the specified directory. Change ‘csvFiles/*.csv’ to whatever directory your files are in.
for csvfile in glob('csvFiles/*.csv'):
Step 3 – When you loop through the list you will get something like ‘csvFiles/report.csv’ as output if you print the variable csvfile. But we just want the file name without the full path so we are going to use os.path.basename(csvfile) which will give us ‘report.csv’ as output. Then we are going to split the file name and extension using the split function and we will get a list like [ ‘report’, ‘csv’ ]. Lastly we are going to use List Indices to access the first element in the sequence and store it in the variable name.
name = os.path.basename(csvfile).split('.')[-2]
Step 4 – Then we are going to make new instance of Workbook class and provide parameters to the constructor. Workbook constructor takes two parameters (filename, options). For complete list of options refer to the workbook class documentation . Then we are going to use the workbook object to add a new worksheet using the add_worksheet() method and store it in the variable worksheet.
workbook = Workbook('xlsxFiles/' + str(name) + '.xlsx', {'strings_to_numbers': True,
'constant_memory': True})
worksheet = workbook.add_worksheet()
Step 5 – Now we are going to open the csvfile in the read mode and get the reader object using csv.reader(f) and store it in the variable r. Once we have the reader object then we can loop through it using enumerate to get the row. Once we have row then we can loop through it using enumerate to get the columns. While we are inside the inner most loop we are going to use worksheet object to write data using the worksheet.write() method. write() method takes three + parameters worksheet.write(row, col, *args), for more information refer to the worksheet class documentation
with open(csvfile, 'r') as f:
r = csv.reader(f)
for row_index, row in enumerate(r):
for col_index, data in enumerate(row):
worksheet.write(row_index, col_index, data)
Step 6 – Finally we are going to close the Excel file using the close() method
workbook.close()
print("-------------------------------------------")
print(" .CSV to .XLSX Conversion Successful")
print("-------------------------------------------")
If everything works you should see this message “.CSV to .XLSX Conversion Successful “. If you are still having problems making it work, go ahead and leave me a comment with description of the problem. Will try to get back to you asap 🙂