Modules

Requirements:

  • Linux Distribution
  • python3
  • A Shell Interface

My Setup:

  • Debian GNU/Linux and Arch Linux
  • python3
  • BASH

During multiple examples , I have shown an import keyword that imports a module into the code. Modules are separate files that have their own functions in them. There may come a time where you may want or need to clean your code and separate functionality into different files. For this, you can import and use the functions. Creating a module is simple. In the next example, I will create a file called operate.py, and add function definitions to it:

def add(x, y):
    return x + y

def sub(x, y):
    return x - y

def mult(x, y):
    return x * y

def divide(x, y):
    if y == 0 :
        return "N/A"
    else:
        return x/y

Next I will create another file called run.py

import operate

firstInt = int(input("Insert the first number: "))
secondInt = int(input("Insert the second number: "))

#use the module name, the dot operator, and then the function name
print("Adding the two : " , operate.add(firstInt,secondInt))

Finally , I will call it within the interpreter:

clim@debian:~/Desktop/tests/pytests/modules$ python3 run.py
Insert the first number: 15
Insert the second number: 35
Adding the two :  50
clim@debian:~/Desktop/tests/pytests/modules$

You can also use an alias for your module by doing the following:

import operate as op

#to use:
op.add(x,y)

You can also directly import and use without needing the module name by importing the function or variable specifically.

from operate import add

#use as normal
add(x,y)

Python provides several modules that we will be going over in the future. You can also find a list at: https://docs.python.org/3/library/

Python Project : Temperature converter

In my other tutorial series, i create a temperature converter to show the logical steps to create a program. To start off, let us define some parameters. The program will take in either a -c or -f to determine the temperature you would want to convert it to. For example, if you want to convert a Fahrenheit temperature to Celsius, you would use the -c flag. For example :

[clim@archnet:~/Desktop/cspcsite/PythonTutorials/examples/3]$ python convert.py -c TEMP_IN_FAHRENHEIT
TEMP_IN_CELSIUS

In order to accomplish this, we will include the sys module. This will allow us to parse arguments that are passed in from the command line (the -c and temperature). There are multiple ways this program can be accomplished. For example, I could just write it all in one block and execute the code, but I want the code to look a bit more organized, so I am going to create 2 functions that will handle the conversions for each temperature type, and then just print the variable that it returns depending on the type flag.

#import the sys module
import sys

#helper functions
def printUsage():
	print("Usage:\npython3 convert.py [FLAG] [TEMP]")
	print("FLAGS:")
	print("\t\t-c : convert to Celsius")
	print("\t\t-f : convert to Fahrenheit")


def toCelsius(fahrenheit):
	celsius = (fahrenheit - 32) * 5/9
	return celsius

def toFahrenheit(celsius):
	fahrenheit = (celsius * 9/5) + 32
	return fahrenheit

#main function

#sys.argv[0] is the name of the python script
#we will use sys.argv[1] and sys.argv[2]

if len(sys.argv) < 2:
	printUsage()
else:
	conversion=sys.argv[1]
	temp=float(sys.argv[2])

	if conversion == '-c':
		print(" {} Fahrenheit is {} Celsius".format(temp,toCelsius(temp)))
	elif conversion == '-f':
		print(" {} Celsius is {} Fahrenheit".format(temp,toFahrenheit(temp)))
	else:
		printUsage()
[clim@archnet:~/Desktop/cspcsite/PythonTutorials/examples/6]$ python3 convert.py
Usage:
python3 convert.py [FLAG] [TEMP]
FLAGS:
		-c : convert to Celsius
		-f : convert to Fahrenheit
[clim@archnet:~/Desktop/cspcsite/PythonTutorials/examples/6]$ python3 convert.py -c 100
 100.0 Fahrenheit is 37.77777777777778 Celsius
[clim@archnet:~/Desktop/cspcsite/PythonTutorials/examples/6]$ python3 convert.py -f 37.77777777777
 37.77777777777 Celsius is 99.99999999998599 Fahrenheit
[clim@archnet:~/Desktop/cspcsite/PythonTutorials/examples/6]$

Leave a Reply