Logo for Not A Programmer

How to use a custom python module to simplify your work

Save time by reusing code through python modules. Create a module to store 20 functions and call them when writing a scripts for financial analysis.
Written by: Marco Cerrato

Overview

In this article, we will create a python module by writing and storing twenty functions into a file ending with the .py extension. The functions included in this file can calculate several types of financial ratios.

The objective of this article is to show the usefulness of python modules and how they can save time when reusing code. In this exercise, we will create a python module that contains several functions. Other programs and scripts can later call these functions to do specific tasks or calculations.

The python module created in this exercise can be useful when writing programs or scripts to automate or conduct financial analysis. You can see an example of how python modules can be helpful by doing the exercise "How to automate a liquidity ratio analysis using python".

What is a Python Module?

Before we begin creating the custom python module, we need to understand what a python module is. We can define a python module as a file with the extension .py that file stores code that can be reused to write other programs and scripts.

What is a Python Function?

A function is a block of related statements that run when called upon and performs a task. In this exercise, our functions will help us break our module into smaller, more manageable pieces of code. Here is an example of a python function:

def get_price_earning_ratio(share_price, earning_per_share):
    """Calculates the Price Earning Ratio. You need to pass two values. The first one is the value for share price and the second value is for the earning per share."""
    price_earning_ratio = share_price / earning_per_share
    return(price_earning_ratio)

A python function typically contains the following components:

  • A header that starts with the keyword def.
  • The keyword def is followed by the name we want to give the function. In our case this name is get_price_earning_ratio.
  • After the name of the function comes the arguments or parameters wrapped around parenthesis. (share_price, earning_per_share).
  • A colon (:) marks the end of the function header.
  • Next, it is considered good practice to use a docstring to describe what the function does.
  • Following the docstring, we add the block of code for our function. This block is nested within the function. One or more statements make this block.
  • You usually will end the function with a return statement. This statement might return a value or perform another function.

You can learn more about python functions and python modules by reading the python's documentation.

Task Requirement

We want to save time writing code when creating scripts that automate financial ratio analysis tasks. We want to be able to reuse code that helps us calculate a list of financial ratios. The list of financial ratios are:

Liquidity Ratios

  • Current Ratio
  • Quick Ratio
  • Cash Ratio
  • Operating Cashflow Ratio

Leverage Ratio

  • Debt-To-Equity
  • Debt-To-Total Assets
  • Coverage Ratio
  • Debt-To-Service Coverage Ratio

Efficiency Ratios

  • Asset Turnover Ratio
  • Inventory Turnover Ratio
  • Receivables Turnover Ratio
  • Days In Sales Inventory Ratio

Profitability

  • Gross Margin Ratio
  • Operating Margin Ratio
  • Return On Assets Ratio
  • Return On Equity Ratio

Market Value

  • Book Value Per Share Ratio
  • Dividend Yield Ratio
  • Earning Per Share Ratio
  • Price-Earning-Ratio

At the end of this exercise, we want to be able to call a function to calculate any one of these ratios when writing other programs or scripts.

Resources

The following resources are made available to help you create the financial ratios module:

Instructions

The instructions for this exercise are straightforward. You need to write a function for each financial ratio and then save the file with the file extension .py. Below the function for each financial ratio.

Functions for Liquidity Ratios

In this section, we will write the functions for the current ratio, quick ratio, cash ratio, and the operating cash flow ratio.

A function for the Current Ratio

The function for the current ratio can be written as follows:

def get_current_ratio(current_assets, current_liabilities):
    "Returns the current ratio. You will need to pass two values. The first value is for the parameter current assets and a second value for the parameter current liabilities"
    current_ratio = current_assets/current_liabilities
    return(current_ratio)
A function for the Quick Ratio or Acid Test

The function for the quick ratio or acid test can be written as follows:

def get_quick_ratio(current_assets, inventories, current_liabilities):
    "Returns the quick or acid test ratio. Your need to pass three values. The first value is for the parameter current assets, the second value for the parameter inventories, and a third value for the parameter current liabilities"
    quick_ratio = (current_assets - inventories)/current_liabilities
    return(quick_ratio)
A function for the Cash Ratio

The function for the cash ratio can be written as follows:

def get_cash_ratio(cash, current_liabilities, cash_equivalent=0):
    "Returns the cash ratio. Your need to pass atleast two values. The first value for the parameter cash account, the second value for parameter current liabilities, and an optional third value for the parameter cash equivalent assets."
    cash_ratio = (cash + cash_equivalent)/current_liabilities
    return(cash_ratio)
A function for the Operating Cash Flow Ratio

The function for the operating cash flow ratio can be written as follows:

def get_operating_cashflow_ratio(operating_cashflow, current_liabilities):
    "Returns the operating cashflow ratio. Your need to pass at least two values. The first value for the parameter operating cashflow and the second value for parameter current liabilities."
    operating_cashflow_ratio = operating_cashflow / current_liabilities
    return(operating_cashflow_ratio)

Functions for Leverage Ratios

In this section, we will write the functions for the Debt-To-Equity ratio, Debt-To-Total-Assets ratio, Interest Coverage Ratio, and the Debt Service Coverage Ratio.

A function for the Debt-to-Equity Ratio

The function for the debt-to-equity ratio can be written as follows:

def get_debt_to_equity_ratio(total_liabilities, shareholders_equity):
    "Calculates the debt to equity ratio. You need to pass atleast two values. The first one is the value for total liabilities and the second value is shareholders equity."
    debt_to_equity_ratio = total_liabilities / shareholders_equity
    return(debt_to_equity_ratio)
A function for the Debt-to-Assets Ratio

The function for the debt-to-assets ratio can be written as follows:

def get_debt_to_total_assets_ratio(total_liabilities, total_assets):
    "Calculates the debt to total assets ratio. You need to pass two values. The first one is the value for total liabilities and the second value is for total assets."
    debt_to_total_assets_ratio = total_liabilities / total_assets
    return(debt_to_total_assets_ratio)
A function for the Interest Coverage Ratio

The function for the interest coverage ratio can be written as follows:

def get_interest_coverage_ratio(operating_income, interest_expenses):
    "Calculates the interest coverage ratio. You need to pass two values. The first one is the value for operating income and the second value is for interest expenses."
    interest_coverage_ratio = operating_income / interest_expenses
    return(interest_coverage_ratio)
A function for the Debt Service Coverage Ratio

The function for the debt service coverage ratio can be written as follows:

def get_debt_service_coverage_ratio(operating_income, total_debt_service):
    "Calculates the service coverage ratio. You need to pass two values. The first value is for operating income and the second value is for total_debt_service."
    debt_service_coverage_ratio = operating_income / total_debt_service
    return(debt_service_coverage_ratio)

Functions for Efficiency Ratios

In this section, we will write the functions for the asset turnover ratio, inventory turnover ratio, receivable turnover ratio, and days in sales inventory ratio.

A function for the Asset Turnover Ratio

The function for the asset turnover ratio can be written as follows:

def get_asset_turnonver_ratio(net_sales, total_assets):
    "Calculates the asset turnover ratio. You need to pass two values. The first one is the value for net sales and the second value is for total assets."
    asset_turnover_ratio = net_sales / total_assets
    return(asset_turnover_ratio)
A function for the Inventory Turnover Ratio

The function for the inventory turnover ratio can be written as follows:

def get_inventory_turnonver_ratio(cost_of_good_sold, average_inventory):
    "Calculates the inventory turnover ratio. You need to pass two values. The first one is the value for cost of goods sold and the average of inventory."
    inventory_turnover_ratio = cost_of_good_sold / average_inventory
    return(inventory_turnover_ratio)
A function for the Receivable Turnover Ratio

The function for the receivable turnover ratio can be written as follows:

def get_receivable_turnonver_ratio(net_credit_sales, average_account_receivables):
    "Calculates the receivable turnover ratio. You need to pass two values. The first one is the value for Net Credit Sales and the second value is for average account receivables."
    receivable_turnover_ratio = net_credit_sales / average_account_receivables
    return(receivable_turnover_ratio)
A function for the Days In Sales Inventory Ratio

The function for the days in sales inventory ratio can be written as follows:

def get_day_sale_inventory_ratio(cost_of_good_sold, average_inventory):
    "Calculates the day sale inventory ratio. You need to pass two values. The first one is the value for cost of good sold and the second value is for average inventory."
    day_sale_inventory_ratio = 365 / \
        get_inventory_turnonver_ratio(cost_of_good_sold, average_inventory)
    return(day_sale_inventory_ratio)

Functions for Profitability Ratios

In this section, we will write the functions for the gross margin ratio, operating margin ratio, return on assets ratio, and the return on equity ratio.

A function for the Gross Margin Ratio

The function for the gross margin ratio can be written as follows:

def get_gross_margin_ratio(gross_profit, net_sales):
    "Calculates the gross margin ratio. You need to pass two values. The first one is the value for gross profit and the second value is for net sales."
    gross_margin_ratio = gross_profit / net_sales
    return(gross_margin_ratio)
A function for the Operating Margin Ratio

The function for the operating margin ratio can be written as follows:

def get_operating_margin_ratio(operating_income, net_sales):
    "Calculates the operating margin ratio. You need to pass two values. The first one is the value for operating income and the second value is for net sales."
    operating_margin_ratio = operating_income / net_sales
    return(operating_margin_ratio)
A function for the Return On Assets Ratio

The function for the return on assets ratio can be written as follows:

def get_return_on_asset_ratio(net_income, total_assets):
    "Calculates the return on asset ratio. You need to pass two values. The first one is the value for net income and the second value is for total assets."
    return_on_assets_ratio = net_income / total_assets
    return(return_on_assets_ratio)
A function for the Return On Equity Ratio

The function for the return on equity ratio can be written as follows:

def get_return_on_equity_ratio(net_income, shareholders_equity):
    "Calculates the return on equity ratio. You need to pass two values. The first one is the value for net income and the second value is for shareholders equity."
    return_on_equity_ratio = net_income / shareholders_equity
    return(return_on_equity_ratio)

Functions for Market Value Ratios

In this section, we will write the functions for the book value per share ratio, dividend yield ratio, earning per share ratio, and price-earning ratio.

A function for the Book Value Per Share Ratio

The function for the book value per share ratio can be written as follows:

def get_bookvalue__per_share_ratio(shareholders_equity, total_share_outstanding):
    "Calculates the Book Value per Share Ratio. You need to pass two values. The first one is the value for shareholders equity and the second value is for total shares outstanding."
    bookvalue_per_share_ratio = shareholders_equity / total_share_outstanding
    return(bookvalue_per_share_ratio)
A function for the Dividend Yield Ratio

The function for the dividend yield ratio can be written as follows:

def get_dividend_yield_ratio(dividend_per_share, share_price):
    "Calculates the Dividend Yield Ratio. You need to pass two values. The first one is the value for dividend per share and the second value is for the share price."
    dividend_yield_ratio = dividend_per_share / share_price
    return(dividend_yield_ratio)
A function for the Earning Per Share Ratio

The function for the earning per share ratio can be written as follows:

def get_earning_per_share_ratio(net_earning, total_share_outstanding):
    "Calculates the Earning per Share Ratio. You need to pass two values. The first one is the value for net earning and the second value is for the total sahres outstanding."
    earning_per_share_ratio = net_earning / total_share_outstanding
    return(earning_per_share_ratio)
A function for the Price-Earning Ratio

The function for the price-earning ratio can be written as follows:

def get_price_earning_ratio(share_price, earning_per_share):
    "Calculates the Price Earning Ratio. You need to pass two values. The first one is the value for share price and the second value is for the earning per share."
    price_earning_ratio = share_price / earning_per_share
    return(price_earning_ratio)

Final Thoughts

I hope you found this exercise useful. Please remember that there are other types of financial ratios that you can use, so feel free to expand this python module to your liking.

You can also practice using this python module by doing the exercise included in the article "Automate the Calculation of Liquidity Ratios in 10 Simple Steps Using Python".

You can also create other python modules for other financial calculations, different ratios, or indicators. Here are some ideas:

  • Create a Python module of key performance indicators
  • Create a Python module of financial formulas like present value, internal rate of return, etc.
  • Create a Python module of marketing indicators
  • Other repetitive calculations and tasks that can solve using Python modules and function

Leave a Reply

More Info

Topics: 
Technologies: Python
Python Libraries: None

Stay Up to Date

Get notify when I publish new content. Join my mailing list.
[sibwp_form id=1]

About Marco Cerrato:

I enjoy learning about technology, predictive modeling, machine learning, and their applications in business, economics, finance, operations, and marketing. I love having conversations about economic trends, strategic planning, and other topics related to businesses and macroeconomics. I' m fortunate to work doing what I love, and I like to share what I learn with other people, hence my reason for starting this site.
© 2026 Marco T. Cerrato
crossmenu