{ "cells": [ { "cell_type": "markdown", "id": "eca1af78", "metadata": {}, "source": [ "## Logging and Debugging" ] }, { "cell_type": "markdown", "id": "21db435c", "metadata": {}, "source": [ "![](../img/logging_debugging.png)" ] }, { "cell_type": "markdown", "id": "64753789", "metadata": {}, "source": [ "Collections of tools for logging and debugging Python code. " ] }, { "cell_type": "markdown", "id": "9cb6752d", "metadata": {}, "source": [ "### rich.inspect: Produce a Beautiful Report on any Python Object" ] }, { "cell_type": "code", "execution_count": null, "id": "286f6971", "metadata": { "tags": [ "hide-cell" ] }, "outputs": [], "source": [ "!pip install rich " ] }, { "cell_type": "markdown", "id": "5e56b4ff", "metadata": {}, "source": [ "If you want to quickly see which attributes and methods of a Python object are available, use rich’s `inspect` method.\n", "\n", "rich’s `inspect` method allows you to create a beautiful report for any Python object, including a string." ] }, { "cell_type": "code", "execution_count": 6, "id": "0d7bdef7", "metadata": { "ExecuteTime": { "end_time": "2021-09-12T13:11:02.968719Z", "start_time": "2021-09-12T13:11:02.784658Z" } }, "outputs": [ { "data": { "text/html": [ "
╭────────────────────────────────────── <class 'str'> ──────────────────────────────────────╮\n", "│ str(object='') -> str │\n", "│ str(bytes_or_buffer[, encoding[, errors]]) -> str │\n", "│ │\n", "│ ╭───────────────────────────────────────────────────────────────────────────────────────╮ │\n", "│ │ 'hello' │ │\n", "│ ╰───────────────────────────────────────────────────────────────────────────────────────╯ │\n", "│ │\n", "│ capitalize = def capitalize(): Return a capitalized version of the string. │\n", "│ casefold = def casefold(): Return a version of the string suitable for caseless │\n", "│ comparisons. │\n", "│ center = def center(width, fillchar=' ', /): Return a centered string of length │\n", "│ width. │\n", "│ count = def count(...) S.count(sub[, start[, end]]) -> int │\n", "│ encode = def encode(encoding='utf-8', errors='strict'): Encode the string using the │\n", "│ codec registered for encoding. │\n", "│ endswith = def endswith(...) S.endswith(suffix[, start[, end]]) -> bool │\n", "│ expandtabs = def expandtabs(tabsize=8): Return a copy where all tab characters are │\n", "│ expanded using spaces. │\n", "│ find = def find(...) S.find(sub[, start[, end]]) -> int │\n", "│ format = def format(...) S.format(*args, **kwargs) -> str │\n", "│ format_map = def format_map(...) S.format_map(mapping) -> str │\n", "│ index = def index(...) S.index(sub[, start[, end]]) -> int │\n", "│ isalnum = def isalnum(): Return True if the string is an alpha-numeric string, False │\n", "│ otherwise. │\n", "│ isalpha = def isalpha(): Return True if the string is an alphabetic string, False │\n", "│ otherwise. │\n", "│ isascii = def isascii(): Return True if all characters in the string are ASCII, │\n", "│ False otherwise. │\n", "│ isdecimal = def isdecimal(): Return True if the string is a decimal string, False │\n", "│ otherwise. │\n", "│ isdigit = def isdigit(): Return True if the string is a digit string, False │\n", "│ otherwise. │\n", "│ isidentifier = def isidentifier(): Return True if the string is a valid Python │\n", "│ identifier, False otherwise. │\n", "│ islower = def islower(): Return True if the string is a lowercase string, False │\n", "│ otherwise. │\n", "│ isnumeric = def isnumeric(): Return True if the string is a numeric string, False │\n", "│ otherwise. │\n", "│ isprintable = def isprintable(): Return True if the string is printable, False │\n", "│ otherwise. │\n", "│ isspace = def isspace(): Return True if the string is a whitespace string, False │\n", "│ otherwise. │\n", "│ istitle = def istitle(): Return True if the string is a title-cased string, False │\n", "│ otherwise. │\n", "│ isupper = def isupper(): Return True if the string is an uppercase string, False │\n", "│ otherwise. │\n", "│ join = def join(iterable, /): Concatenate any number of strings. │\n", "│ ljust = def ljust(width, fillchar=' ', /): Return a left-justified string of │\n", "│ length width. │\n", "│ lower = def lower(): Return a copy of the string converted to lowercase. │\n", "│ lstrip = def lstrip(chars=None, /): Return a copy of the string with leading │\n", "│ whitespace removed. │\n", "│ maketrans = def maketrans(...) Return a translation table usable for str.translate(). │\n", "│ partition = def partition(sep, /): Partition the string into three parts using the │\n", "│ given separator. │\n", "│ replace = def replace(old, new, count=-1, /): Return a copy with all occurrences of │\n", "│ substring old replaced by new. │\n", "│ rfind = def rfind(...) S.rfind(sub[, start[, end]]) -> int │\n", "│ rindex = def rindex(...) S.rindex(sub[, start[, end]]) -> int │\n", "│ rjust = def rjust(width, fillchar=' ', /): Return a right-justified string of │\n", "│ length width. │\n", "│ rpartition = def rpartition(sep, /): Partition the string into three parts using the │\n", "│ given separator. │\n", "│ rsplit = def rsplit(sep=None, maxsplit=-1): Return a list of the words in the │\n", "│ string, using sep as the delimiter string. │\n", "│ rstrip = def rstrip(chars=None, /): Return a copy of the string with trailing │\n", "│ whitespace removed. │\n", "│ split = def split(sep=None, maxsplit=-1): Return a list of the words in the │\n", "│ string, using sep as the delimiter string. │\n", "│ splitlines = def splitlines(keepends=False): Return a list of the lines in the string, │\n", "│ breaking at line boundaries. │\n", "│ startswith = def startswith(...) S.startswith(prefix[, start[, end]]) -> bool │\n", "│ strip = def strip(chars=None, /): Return a copy of the string with leading and │\n", "│ trailing whitespace removed. │\n", "│ swapcase = def swapcase(): Convert uppercase characters to lowercase and lowercase │\n", "│ characters to uppercase. │\n", "│ title = def title(): Return a version of the string where each word is titlecased. │\n", "│ translate = def translate(table, /): Replace each character in the string using the │\n", "│ given translation table. │\n", "│ upper = def upper(): Return a copy of the string converted to uppercase. │\n", "│ zfill = def zfill(width, /): Pad a numeric string with zeros on the left, to fill │\n", "│ a field of the given width. │\n", "╰───────────────────────────────────────────────────────────────────────────────────────────╯\n", "\n" ], "text/plain": [ "\u001b[34m╭─\u001b[0m\u001b[34m───────────────────────────────────── \u001b[0m\u001b[1;34m<\u001b[0m\u001b[1;95mclass\u001b[0m\u001b[39m \u001b[0m\u001b[32m'str'\u001b[0m\u001b[1;34m>\u001b[0m\u001b[34m ─────────────────────────────────────\u001b[0m\u001b[34m─╮\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[1;35mstr\u001b[0m\u001b[1;36m(\u001b[0m\u001b[33mobject\u001b[0m\u001b[36m=\u001b[0m\u001b[32m''\u001b[0m\u001b[1;36m)\u001b[0m\u001b[36m -> str\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[1;35mstr\u001b[0m\u001b[1;36m(\u001b[0m\u001b[36mbytes_or_buffer\u001b[0m\u001b[1;36m[\u001b[0m\u001b[36m, encoding\u001b[0m\u001b[1;36m[\u001b[0m\u001b[36m, errors\u001b[0m\u001b[1;36m]\u001b[0m\u001b[1;36m]\u001b[0m\u001b[1;36m)\u001b[0m\u001b[36m -> str\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[32m╭───────────────────────────────────────────────────────────────────────────────────────╮\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[32m│\u001b[0m \u001b[32m'hello'\u001b[0m \u001b[32m│\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[32m╰───────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mcapitalize\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mcapitalize\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a capitalized version of the string.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mcasefold\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mcasefold\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a version of the string suitable for caseless \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mcomparisons.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mcenter\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mcenter\u001b[0m\u001b[1m(\u001b[0mwidth, \u001b[33mfillchar\u001b[0m=\u001b[32m' '\u001b[0m, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a centered string of length \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mwidth.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mcount\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mcount\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[1;2;35mS.count\u001b[0m\u001b[1;2m(\u001b[0m\u001b[2msub\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, start\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, end\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m -> int\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mencode\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mencode\u001b[0m\u001b[1m(\u001b[0m\u001b[33mencoding\u001b[0m=\u001b[32m'utf-8'\u001b[0m, \u001b[33merrors\u001b[0m=\u001b[32m'strict'\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mEncode the string using the\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mcodec registered for encoding.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mendswith\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mendswith\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[1;2;35mS.endswith\u001b[0m\u001b[1;2m(\u001b[0m\u001b[2msuffix\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, start\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, end\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m -> bool\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mexpandtabs\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mexpandtabs\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtabsize\u001b[0m=\u001b[1;36m8\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a copy where all tab characters are \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mexpanded using spaces.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mfind\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mfind\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[1;2;35mS.find\u001b[0m\u001b[1;2m(\u001b[0m\u001b[2msub\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, start\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, end\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m -> int\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mformat\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mformat\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[1;2;35mS.format\u001b[0m\u001b[1;2m(\u001b[0m\u001b[2m*args, **kwargs\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m -> str\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mformat_map\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mformat_map\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[1;2;35mS.format_map\u001b[0m\u001b[1;2m(\u001b[0m\u001b[2mmapping\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m -> str\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mindex\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mindex\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[1;2;35mS.index\u001b[0m\u001b[1;2m(\u001b[0m\u001b[2msub\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, start\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, end\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m -> int\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misalnum\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misalnum\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is an alpha-numeric string, \u001b[0m\u001b[2;3;91mFalse\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misalpha\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misalpha\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is an alphabetic string, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misascii\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misascii\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if all characters in the string are ASCII, \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2;3;91mFalse\u001b[0m\u001b[2m otherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misdecimal\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misdecimal\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is a decimal string, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misdigit\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misdigit\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is a digit string, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misidentifier\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misidentifier\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is a valid Python \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2midentifier, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m otherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mislower\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mislower\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is a lowercase string, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misnumeric\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misnumeric\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is a numeric string, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misprintable\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misprintable\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is printable, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misspace\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misspace\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is a whitespace string, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mistitle\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mistitle\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is a title-cased string, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33misupper\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31misupper\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn \u001b[0m\u001b[2;3;92mTrue\u001b[0m\u001b[2m if the string is an uppercase string, \u001b[0m\u001b[2;3;91mFalse\u001b[0m\u001b[2m \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2motherwise.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mjoin\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mjoin\u001b[0m\u001b[1m(\u001b[0miterable, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mConcatenate any number of strings.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mljust\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mljust\u001b[0m\u001b[1m(\u001b[0mwidth, \u001b[33mfillchar\u001b[0m=\u001b[32m' '\u001b[0m, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a left-justified string of \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mlength width.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mlower\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mlower\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a copy of the string converted to lowercase.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mlstrip\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mlstrip\u001b[0m\u001b[1m(\u001b[0m\u001b[33mchars\u001b[0m=\u001b[3;35mNone\u001b[0m, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a copy of the string with leading \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mwhitespace removed.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mmaketrans\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mmaketrans\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[2mReturn a translation table usable for \u001b[0m\u001b[1;2;35mstr.translate\u001b[0m\u001b[1;2m(\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mpartition\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mpartition\u001b[0m\u001b[1m(\u001b[0msep, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mPartition the string into three parts using the \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mgiven separator.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mreplace\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mreplace\u001b[0m\u001b[1m(\u001b[0mold, new, \u001b[33mcount\u001b[0m=\u001b[1;36m-1\u001b[0m, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a copy with all occurrences of \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2msubstring old replaced by new.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mrfind\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mrfind\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[1;2;35mS.rfind\u001b[0m\u001b[1;2m(\u001b[0m\u001b[2msub\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, start\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, end\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m -> int\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mrindex\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mrindex\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[1;2;35mS.rindex\u001b[0m\u001b[1;2m(\u001b[0m\u001b[2msub\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, start\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, end\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m -> int\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mrjust\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mrjust\u001b[0m\u001b[1m(\u001b[0mwidth, \u001b[33mfillchar\u001b[0m=\u001b[32m' '\u001b[0m, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a right-justified string of \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mlength width.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mrpartition\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mrpartition\u001b[0m\u001b[1m(\u001b[0msep, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mPartition the string into three parts using the \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mgiven separator.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mrsplit\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mrsplit\u001b[0m\u001b[1m(\u001b[0m\u001b[33msep\u001b[0m=\u001b[3;35mNone\u001b[0m, \u001b[33mmaxsplit\u001b[0m=\u001b[1;36m-1\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a list of the words in the \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mstring, using sep as the delimiter string.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mrstrip\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mrstrip\u001b[0m\u001b[1m(\u001b[0m\u001b[33mchars\u001b[0m=\u001b[3;35mNone\u001b[0m, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a copy of the string with trailing \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mwhitespace removed.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33msplit\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31msplit\u001b[0m\u001b[1m(\u001b[0m\u001b[33msep\u001b[0m=\u001b[3;35mNone\u001b[0m, \u001b[33mmaxsplit\u001b[0m=\u001b[1;36m-1\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a list of the words in the \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mstring, using sep as the delimiter string.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33msplitlines\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31msplitlines\u001b[0m\u001b[1m(\u001b[0m\u001b[33mkeepends\u001b[0m=\u001b[3;91mFalse\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a list of the lines in the string, \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mbreaking at line boundaries.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mstartswith\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mstartswith\u001b[0m\u001b[1m(\u001b[0m\u001b[33m...\u001b[0m\u001b[1m)\u001b[0m \u001b[1;2;35mS.startswith\u001b[0m\u001b[1;2m(\u001b[0m\u001b[2mprefix\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, start\u001b[0m\u001b[1;2m[\u001b[0m\u001b[2m, end\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m]\u001b[0m\u001b[1;2m)\u001b[0m\u001b[2m -> bool\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mstrip\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mstrip\u001b[0m\u001b[1m(\u001b[0m\u001b[33mchars\u001b[0m=\u001b[3;35mNone\u001b[0m, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a copy of the string with leading and \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mtrailing whitespace removed.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mswapcase\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mswapcase\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mConvert uppercase characters to lowercase and lowercase \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mcharacters to uppercase.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mtitle\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mtitle\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a version of the string where each word is titlecased.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mtranslate\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mtranslate\u001b[0m\u001b[1m(\u001b[0mtable, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReplace each character in the string using the \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2mgiven translation table.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mupper\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mupper\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mReturn a copy of the string converted to uppercase.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[3;33mzfill\u001b[0m = \u001b[3;96mdef \u001b[0m\u001b[1;31mzfill\u001b[0m\u001b[1m(\u001b[0mwidth, \u001b[35m/\u001b[0m\u001b[1m)\u001b[0m: \u001b[2mPad a numeric string with zeros on the left, to fill \u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m│\u001b[0m \u001b[2ma field of the given width.\u001b[0m \u001b[34m│\u001b[0m\n", "\u001b[34m╰───────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "from rich import inspect\n", "\n", "print(inspect('hello', methods=True))" ] }, { "cell_type": "markdown", "id": "1413fae1", "metadata": {}, "source": [ "### Rich’s Console: Debug your Python Function in One Line of Code" ] }, { "cell_type": "code", "execution_count": null, "id": "39316dda", "metadata": { "tags": [ "hide-cell" ] }, "outputs": [], "source": [ "!pip install rich " ] }, { "cell_type": "markdown", "id": "e591d55a", "metadata": {}, "source": [ "Sometimes, you might want to know which elements in the function created a certain output. Instead of printing every variable in the function, you can simply use Rich’s `Console` object to print both the output and all the variables in the function." ] }, { "cell_type": "code", "execution_count": 7, "id": "e728aac5", "metadata": { "ExecuteTime": { "end_time": "2021-09-12T13:12:24.610176Z", "start_time": "2021-09-12T13:12:24.341535Z" } }, "outputs": [ { "data": { "text/html": [ "
[08:12:24] a b 1165738010.py:14\n", " 0 45 4 \n", " 1 30 5 \n", " 2 75 6 \n", " ╭───── locals ─────╮ \n", " │ data = a b │ \n", " │ 0 45 4 │ \n", " │ 1 30 5 │ \n", " │ 2 75 6 │ \n", " │ var_1 = 45 │ \n", " │ var_2 = 30 │ \n", " │ var_3 = 75 │ \n", " ╰──────────────────╯ \n", "\n" ], "text/plain": [ "\u001b[2;36m[08:12:24]\u001b[0m\u001b[2;36m \u001b[0m a b \u001b]8;id=137856;file:///tmp/ipykernel_174022/1165738010.py\u001b\\\u001b[2m1165738010.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:14\u001b[0m\n", " \u001b[1;36m0\u001b[0m \u001b[1;36m45\u001b[0m \u001b[1;36m4\u001b[0m \n", " \u001b[1;36m1\u001b[0m \u001b[1;36m30\u001b[0m \u001b[1;36m5\u001b[0m \n", " \u001b[1;36m2\u001b[0m \u001b[1;36m75\u001b[0m \u001b[1;36m6\u001b[0m \n", " \u001b[34m╭─\u001b[0m\u001b[34m──── \u001b[0m\u001b[3;34mlocals\u001b[0m\u001b[34m ────\u001b[0m\u001b[34m─╮\u001b[0m \n", " \u001b[34m│\u001b[0m \u001b[3;33mdata\u001b[0m\u001b[31m =\u001b[0m a b \u001b[34m│\u001b[0m \n", " \u001b[34m│\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;36m45\u001b[0m \u001b[1;36m4\u001b[0m \u001b[34m│\u001b[0m \n", " \u001b[34m│\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;36m30\u001b[0m \u001b[1;36m5\u001b[0m \u001b[34m│\u001b[0m \n", " \u001b[34m│\u001b[0m \u001b[1;36m2\u001b[0m \u001b[1;36m75\u001b[0m \u001b[1;36m6\u001b[0m \u001b[34m│\u001b[0m \n", " \u001b[34m│\u001b[0m \u001b[3;33mvar_1\u001b[0m\u001b[31m =\u001b[0m \u001b[1;36m45\u001b[0m \u001b[34m│\u001b[0m \n", " \u001b[34m│\u001b[0m \u001b[3;33mvar_2\u001b[0m\u001b[31m =\u001b[0m \u001b[1;36m30\u001b[0m \u001b[34m│\u001b[0m \n", " \u001b[34m│\u001b[0m \u001b[3;33mvar_3\u001b[0m\u001b[31m =\u001b[0m \u001b[1;36m75\u001b[0m \u001b[34m│\u001b[0m \n", " \u001b[34m╰──────────────────╯\u001b[0m \n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from rich import console\n", "from rich.console import Console \n", "import pandas as pd \n", "\n", "console = Console()\n", "\n", "data = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})\n", "\n", "def edit_data(data):\n", " var_1 = 45\n", " var_2 = 30\n", " var_3 = var_1 + var_2\n", " data['a'] = [var_1, var_2, var_3]\n", " console.log(data, log_locals=True)\n", "\n", "edit_data(data)" ] }, { "cell_type": "markdown", "id": "62ffcf85", "metadata": {}, "source": [ "[Link to my article about rich](https://towardsdatascience.com/rich-generate-rich-and-beautiful-text-in-the-terminal-with-python-541f39abf32e).\n", "\n", "[Link to rich](https://github.com/willmcgugan/rich)." ] }, { "attachments": {}, "cell_type": "markdown", "id": "db50094b", "metadata": {}, "source": [ "### Simplify Python Logging with Loguru" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6f30417a", "metadata": {}, "source": [ "Are you struggling with the complexity of configuring a logger object before logging in Python? With Loguru, you can skip this step and use the logger object directly with pre-built color and format settings.\n", "\n", "Here is the comparison between the standard Python logging library and Loguru:" ] }, { "attachments": {}, "cell_type": "markdown", "id": "1658416e", "metadata": {}, "source": [ "Standard Python logging library:" ] }, { "attachments": {}, "cell_type": "markdown", "id": "5b665c1e", "metadata": {}, "source": [ "```python\n", "# loguru_vs_logging/logging_example.py\n", "\n", "import logging\n", "\n", "logging.basicConfig(format='%(asctime)s | %(levelname)s | %(module)s:%(funcName)s:%(lineno)d | %(message)s', level=logging.DEBUG)\n", "\n", "def main():\n", " logging.debug('This is a debug message')\n", " logging.info('This is an info message')\n", " logging.warning('This is a warning message')\n", " logging.error('This is an error message')\n", " logging.critical('This is a critical message')\n", "\n", "if __name__ == '__main__':\n", " main()\n", "```" ] }, { "attachments": {}, "cell_type": "markdown", "id": "2b17bb17", "metadata": {}, "source": [ "```bash\n", "$ python loguru_vs_logging/logging_example.py\n", "\n", "2023-03-13 08:46:30,802 | DEBUG | logging_example:main:6 | This is a debug message\n", "2023-03-13 08:46:30,802 | INFO | logging_example:main:7 | This is an info message\n", "2023-03-13 08:46:30,802 | WARNING | logging_example:main:8 | This is a warning message\n", "2023-03-13 08:46:30,802 | ERROR | logging_example:main:9 | This is an error message\n", "2023-03-13 08:46:30,802 | CRITICAL | logging_example:main:10 | This is a critical message\n", "```" ] }, { "attachments": {}, "cell_type": "markdown", "id": "65997789", "metadata": {}, "source": [ "Loguru:" ] }, { "attachments": {}, "cell_type": "markdown", "id": "7b9de68d", "metadata": {}, "source": [ "```python\n", "# loguru_vs_logging/loguru_example.py\n", "from loguru import logger\n", "\n", "def main():\n", " logger.debug(\"This is a debug message\")\n", " logger.info(\"This is an info message\")\n", " logger.warning(\"This is a warning message\")\n", " logger.error(\"This is an error message\")\n", " logger.critical(\"This is a critical message\")\n", "\n", "\n", "if __name__ == '__main__':\n", " main()\n", "```\n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "b6306bbc", "metadata": {}, "source": [ "```bash\n", "$ python loguru_vs_logging/loguru_example.py\n", "```\n", "![](../img/loguru_output.png)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "3a00f431", "metadata": {}, "source": [ "[Link to loguru](https://github.com/Delgan/loguru). " ] }, { "cell_type": "markdown", "id": "b8e85d9e", "metadata": {}, "source": [ "### loguru: Print Readable Traceback in Python\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b7e73e8b", "metadata": { "tags": [ "hide-cell" ] }, "outputs": [], "source": [ "!pip install loguru " ] }, { "attachments": {}, "cell_type": "markdown", "id": "232a4763", "metadata": {}, "source": [ "Loguru also allows you to print a readable traceback by adding decorator `logger.catch` to a function. " ] }, { "cell_type": "code", "execution_count": null, "id": "9f6a8fc2", "metadata": { "ExecuteTime": { "end_time": "2021-09-12T13:13:20.721670Z", "start_time": "2021-09-12T13:13:20.283582Z" }, "tags": [ "hide-output" ] }, "outputs": [], "source": [ "from sklearn.metrics import mean_squared_error\n", "import numpy as np\n", "from loguru import logger\n", "\n", "logger.add(\"file_{time}.log\", format=\"{time} {level} {message}\")\n", "\n", "@logger.catch\n", "def evaluate_result(y_true: np.array, y_pred: np.array):\n", " mean_square_err = mean_squared_error(y_true, y_pred)\n", " root_mean_square_err = mean_square_err ** 0.5\n", "\n", "y_true = np.array([1, 2, 3])\n", "y_pred = np.array([1.5, 2.2])\n", "evaluate_result(y_true, y_pred)" ] }, { "cell_type": "markdown", "id": "41e013ab", "metadata": {}, "source": [ "```bash\n", "> File \"/tmp/ipykernel_174022/1865479429.py\", line 14, in