Emphasize Print Statement
✨ emphasize texts with bold/colors in CLI/notebook ✨
Attention
Source Code: GH/prettify
A Set of Simple Function(s) to Emphasize Print Statements
A collection of functions that modifies the inbuilt print()
statement of Python language. The code follows camelCasing and
provide features like aligning text, text colors, etc.
- prettify.markdownCodeBlock(code: str, language: str, display: bool = False) str
Convert any Code/Statement into a Markdown Code Block
The function simply encloses a code piece with a tilde character which can be used to display the same as a code block. The function is useful when used inside Jupyter Notebook to provide a string that can be directly used to display markdown code block for understanding like:
from IPython.display import Markdown, display # let's use the function to print a code block for SQL statement = "SELECT * FROM myTable" display(Markdown(markdownCodeBlock(statement, language = "sql")))
The function is designed to be simple and efficient, the following paramters are available:
- Parameters:
code (str) – Code block which is enclosed inside tilde that can be used to display markdown.
language (str) – Supports any of the language dialect as accepted by the Markdown code block. For more information, and a list of accepted language check reference links below.
display (bool) – Display the content using the
IPython.displaymodule. Defaults to False. Setting this value to True will only work inside a Jupyter Notebook environment and may raise an error outside.
Error Guidelines
- Raises:
ModuleNotFoundError – The error is raised when the function is called from outside the scope of the Jupyter Notebook or the
IPython.displaymodule is not installed in the system.ImportError – The
ModuleNotFoundErrorerror is a subclass of the theImportErrorthe only difference being that the module might be available but other errors are raised. Typically, this error may be raised if the Jupyter Notebook is not properly configured.
Supported Languages
Basic programming language like
shell,python, etc. is supported by most of the rendering engines, however platform specific rendering engines may vary. List of supported language by platforms are as follows:
- prettify.textAlign(value: str, align: str = 'center', verbose: bool = True, **kwargs) str
Text Align to the Left/Right/Center of the Screen basis Size
By default all the text in terminal/notebook is left aligned in most programming languages. However, python provides simple utility function like
.center()to align text to the center of the line of a specified length (size).The function is also adjusted to right align the text based on the conversion of size and length of the value to be printed on the screen. Please check example use cases for more details.
- Parameters:
value (str) – The value to be printed on the screen. The argument is enclosed within the
print()function.align (str) – Type of alignment, can be {
center,right} of the screen based onsize. Defaults tocenteralignment. Text alignment toleftis valid, however it does not have any significance and defaults toprint()usage (kept for legacy codes).verbose (bool) – The function has the ability to print the value directly, or can be encapsulated in other functions like
pprintexternally. Defaults toTrue.
Keyword Arguments
The function accepts all keyword arguments as accepted by the print() <https://docs.python.org/3/library/functions.html#print>_ method, and defaults to as in documentation. The keyword arguments are only usable if
verbose = Trueelse ignored. For example,sep (str) : seperator
end (str) : end line character
file (str) : output file name
flush (bool) : force flush to file
In addition, the function has the following configurable keyword arguments as below:
size (int): The total length of the line, which is by default a dynamic calculated field of the terminal size, i.e. the display screen length.
A Wrapper for Functions to Emphasize/Control Print Statements
During development we often uses a simple “print” statement rather than developing a full fledged logging module. This module provides ways to wrap a function’s print statement for various purposes.
@author: Debmalya Pramanik @version: v0.0.2
- pwrapper.suppressPrint(func) callable
A Function to Suppress Print Statements for a Function
The function suppresses all print statements from a function, and channels them to a blank fileas python allow to overwrite the standard output (
stdout) with any object.- Parameters:
func (function) – Function to be wrapped, on using this, all the print statement for the function will be disabled and enabled after the function execution is completed.
Example Usage
The wrapper can be used to suppress print statements for a function as shown below:
from wrappers import suppressPrint @suppressPrint def myFunction(): print("Hello World") myFunction() # print is diabled print("Hello World") >> Hello World # print is restored
- rtype:
function
- return:
Wrapped function with its’ output while the print statement are disbled.