improved example

This commit is contained in:
Will McGugan 2020-10-23 17:20:05 +01:00
parent a9abd8ae26
commit 813610df3e
3 changed files with 25 additions and 24 deletions

View file

@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [9.1.0] - Unreleased
## [9.1.0] - 2020-10-23
### Added

View file

@ -1,35 +1,36 @@
"""
Basic example to show how to print an traceback of an exception
"""
from typing import List, Tuple
from rich.console import Console
from rich.panel import Panel
console = Console()
def zero(number: int) -> int:
same_number = number
result = same_number / 0
def divide_by(number: float, divisor: float) -> float:
"""Divide any number by zero."""
# Will throw a ZeroDivisionError if divisor is 0
result = number / divisor
return result
if __name__ == "__main__":
console.print(Panel("[i] Print exception traceback[/i]"))
def divide_all(divides: List[Tuple[float, float]]) -> None:
"""Do something impossible every day."""
try:
zero(10)
except:
console.print_exception()
console.print("[red]Exception catched")
console.print(
Panel(
"[i] Print exception traceback with 5 extra lines and locals[/i]",
style="yellow",
)
)
try:
zero(20)
except:
for number, divisor in divides:
result = divide_by(number, divisor)
console.print(f"{number} divided by {divisor} is {result}")
except Exception:
console.print_exception(extra_lines=5, show_locals=True)
console.print("[red]Exception also catched")
DIVIDES = [
(1000, 200),
(10000, 500),
(0, 1000000),
(3.1427, 2),
(2 ** 32, 2 ** 16),
(1, 0),
]
divide_all(DIVIDES)

View file

@ -2,7 +2,7 @@
name = "rich"
homepage = "https://github.com/willmcgugan/rich"
documentation = "https://rich.readthedocs.io/en/latest/"
version = "9.0.1"
version = "9.1.0"
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
authors = ["Will McGugan <willmcgugan@gmail.com>"]
license = "MIT"