1 Introduction
We often use print statements to get feedback on certain process steps or to present findigs. In this post, I want to show how to use print statements cleverly and make them more descriptive.
2 Loading the libraries and classes
import pandas as pd
import os
class Color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
3 Modified Print Statements
3.1 Print Statements with Variables
As we all know for sure, beyond the simple text output like
print('My Text')
you can also print the contents of variables.
3.2 Print Statements with compound Paths
Here we request the current working directory we are on:
root_directory = os.getcwd()
root_directory
Now we connect this to our destination folder:
new_path = root_directory + '\\' + 'Target_Folder'
print(new_path)
Even simpler, this is how it works:
new_path = os.path.join(root_directory, 'Target_Folder')
print(new_path)
3.3 Color Print Statements
To make print statements even more beautiful, we can have parts printed in color or bold. For this we use the Color-class created above.
print('Current Date: ' + Color.RED + today)
print(Color.BLUE + 'Current Date: ' +
Color.BOLD + Color.RED + today)
print(Color.RED + 'My '
+ Color.END +
'Calculation: ' + str(my_calculation))
print(Color.BLUE + 'My ' + Color.END +
Color.UNDERLINE + Color.GREEN + 'Calculation:' + Color.END +
' ' + Color.BOLD + Color.RED + str(my_calculation))