Python Modules

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

Define a module

Create the file area.py

# module name called area
def circle(radius):
    result = 3.14 * radius * radius;
    return result;

def triangle(base, height):
    result = (base*height)/2;
    return result;

def square(a):
    return a*a;

Use a module

import area
area.circle(5)
Output
78.5

When a module named area is imported, the interpreter first searches for a built-in module with that name. If not found, then searches for a file named area.py in a list of directories given by the variable sys.path. initialized from these locations.

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

Re-naming a Module

Create an alias when you import a module, by using the as keyword:

import area as ar #It creates the alias for module area
ar.circle(5)
Output
78.5

Import all functions from module

 from area import * #It imports all functions present in module called area
 circle(5) #Directly can access the function without module name
 square(5)

Import specific or required function from module

from area import circle #It imports only a circle function present in module called area
circle(5) #Directly can access the function without module name

Standard or Built-in Modules

Modules which are built into the interpreter called standard or Built-in modules.

OS

It provides a way of using operating system dependent functionality.

import os #This module which contains the functions related to operating system.

os.getcwd() #This function is used to get the current working directory.

os.chdir("C:/Users/SimLabAutomation/") #It sets the current working directory to the specified path.

os.mkdir ("C:/Users/SimLabAutomation/Script",mode=777) #It creates the directory in specified path with permission read, write and execute, mode argument is ignored on windows.

os.remove("c:/Users/SimLabAutomation/Output/MeshModel.gda") #It deletes the specified file.

os.rmdir("c:/Users/SimLabAutomation/Output/") #It deletes the specified empty directory.

os.path.isdir("c:/Users/SimLabAutomation/Output/") # This function specifies whether the path is existing directory or not. It returns True if given path is a directory else False.

os.path.exists("c:/Users/SimLabAutomation/Output/") #It returns True whether the specified path is exists else False.

os.path.basename(path) #It returns base name of specified path.

os.path.dirname (path) #It returns the directory name of specified path.

os.path.isabs(path) #It returns True if specified path is absolute path else False.

os.system() #It executes the given shell commands.

os.rename("c:/Users/SimLabAutomation/Output","c:/Users/SimLabAutomation/Result") #Rename a file or directory.

os.environ["SL_MIN_AVG_SIZE"]="2.0" #It set the specified value to the given environment variable.

os.environ["SL_MIN_AVG_SIZE"] #It returns the environment variable as string.

Shutil

Utility functions for copying and archiving files and directory trees.

rmtree()

shutil.rmtree(path, ignore_errors=False, onerror=None)

it deletes the files and directory recursively from given path.

import shutil
shutil.rmtree("d:/myscr/out")

copy()

shutil.copy(src, dst, *, follow_symlinks=True)

it copies the file from source to destination. The destination may be a directory. Returns the path to the newly created file.

import shutil
shutil.copy("d:/myscr/param.xml", "d:/myscr/input")

copytree()

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)

Recursively copy an entire directory tree from source to destination. The destination directory, named by dst, must not already exist;

Scipy

The SciPy package of key algorithms and functions core to Python's scientific computing capabilities.

spatial: distance functions

from scipy.spatial import distance
node1 = [-41.155580, 112.866900, 18.242380]
node2 = [-46.057850, 109.711600, 18.156810]
dist = distance.euclidean(node1,node2)
print(dist);
Output
5.830565278581485