Set of custom python packages for various odds and ends.
- mcol defines a set of colours as ANSI escape strings.
- module provides the module() function that calls lmod to persistently load softare on EUREKA.
- mout provides a set of functions for formatted console output.
- mplot provides a variety of plotting functions using matplotlib.
Provides ANSI escape strings for colourised terminal output that fits into a colour scheme. I.e. green success messages, red errors, orange warnings, yellow filenames etc...
For example to output an error message:
import mcol
print(mcol.error + "Error: Something went wrong!" + mcol.clear)
Please note that for most cases the functions in mout will achieve the same functionality with less code:
import mout
mout.errorOut("Something went wrong!")
Colour strings in the scheme:
varName
Variable names, turqoisevarType
Variable types/units, pinkfunc
Function/script name, underlined turqoisefile
Filename/path, yellowerror
For error messages, bold redsuccess
For success messages, bold greenwarning
For warning messages, bold greenresult
For results/variable values, bluearg
For function arguments, lime
General colour strings:
-
clear
-
bold
-
inverse
-
underline
-
red
-
green
-
yellow
Access the unix modulecmd
from within python. Load and unload packages, which will persist so that subsequest exec()
or os.system()
commands will see them. Comma delimited arguments instead of spaces:
import module
module.module('load','lammps/2018/mpi+intel-xe_2017_3')
Write formatted output to the console.
General console message
mout.out(string,printScript=False,end="\n")
string
The message to be displayedprintScript
Prepend the output with the name of the script?end
End the output with this string.
Output a variable and its value
def varOut(name, value, unit="",valCol="",precision=8,printScript=False,end="\n")
name
The name of the variablevalue
Value of the variableunit
Unit string for the variablevalCol
Custom colour string for the valueprecision
Significant figures of the value outputprintScript
Prepend the output with the name of the script?end
End the output with this string.
Warning message
def warningOut(string,printScript=False,code=None,end="\n")
string
The message to be displayedprintScript
Prepend the output with the name of the script?code
The warning codeend
End the output with this string.
Error message
def errorOut(string,printScript=False,fatal=False,code=None,end="\n"):
string
The message to be displayedprintScript
Prepend the output with the name of the script?fatal
Exit the script?code
The warning codeend
End the output with this string.
Functions for plotting data using matplotlib.
mplot
requires matplotlib
Plotting 2D data:
mplot.graph2D(xdata,ydata,ytitles=None,filename=None,show=True,xmin=None,xmax=None,ymin=None,ymax=None,xlab='x',ylab='y',title=None)
xdata
List of x-axis valuesydata
List of y-axis values or list of list of y-axis values.ytitles
List of titles for the different y-axis data-sets.filename
Output filename.show
Open a GUI window with the plot?xmin
Lower bound of x-axis.xmax
Upper bound of x-axis.ymin
Lower bound of y-axis.ymax
Upper bound of y-axis.xlab
x-axis label.xlab
y-axis label.title
Title of plot.verbosity
Verbosity level. Every nested function call will passverbosity=verbosity-1
.
Example:
import mplot
xdata = [1,2,3,4]
ydata1 = [1,2,3,4]
ydata2 = [3,4,5,6]
mplot.graph2D(xdata,ydata1,filename="test1.png",title="Test 1")
mplot.graph2D(xdata,[ydata1,ydata2],ytitles=["ydata1","ydata2"],filename="test2.png",title="Test 2")
Open figures in GUI windows
To show all the figures created in the interactive session:
mplot.show(verbosity=1)