Package: pr
prA high-level shorthand print formatter for Python 3 or aboveOverviewThe objective of pr is to streamline the debugging process by allowing developers to implement complex print formatting with minimal effort.InstallationRunpip3 install prArgumentsvarnametypedefaultcontentContentstr / list / dict / tuple""tTabsint0laLines Afterint0lbLines Beforeint0tlTab Lengthint4bBulletboolFalsebcBullet Characterstr"•"cColorstrNonecsColor SpanboolFalseipIn PlaceboolFalsehHeadingboolFalsehrHorizontal RuleboolFalsehrcHorizontal Rule Characterstr"-"hrlHorizontal Rule Lengthint72dhrlDynamic Horizontal Rule LengthboolFalserRowboolFalserlRow ListboolFalsepPaddingint20aAlignmentstr"left"sbStatus BoxboolFalsestStatus Typestr"i"dbgDebugboolTrueSee also:Pretty PrintingExamplesImporting the Modulefrom pr import prPrinting a Stringpr("Hello world")Hello worldEquivalent to:print("Hello world")Printing an Empty Stringpr()Equivalent to:print("\n")orprint("")Using New Lines and Tabspr("Hello world", t=1, la=1, lb=1)orpr("Hello world", 1, 1, 1)Hello worldYou may also specify custom tab lengths with the tl argument.pr("Hello world", t=1)
pr("Hello world", t=1, tl=8)Hello world
        Hello worldWhere:t = number of tabs before the printed stringintla = number of lines after the printed stringintlb = number of lines before the printed stringinttl = number of spaces in a single tabintEquivalent to:print(\n\tHello world\n)Using Bulletsshopping_list = [
    "eggs",
    "bacon",
    "cucumbers",
    "bottled water",
    "fresh straberries",
    "fine cheese",
    "pasta",
]for item in shopping_list:
    pr(item, b=True)• eggs
• bacon
• cucumbers
• bottled water
• fresh straberries
• fine cheese
• pastafor item in shopping_list:
  pr(item, b=True, bc="@")@ eggs
@ bacon
@ cucumbers
@ bottled water
@ fresh straberries
@ fine cheese
@ pastaWhere:b = indicates whether to add a bullet point before the contentboolbc = desired bullet characterstrEquivalent to:for item in shopping_list:
  print(f"{•} {item}")for item in shopping_list:
  print(f"{@} {item}")Using Colorspr("Hello world", c="r")Hello word  <-- appears red in terminalWhere:c = desired color of textstrEquivalent to:print(\u001b[31mHello world\u001b[0m)Accepted colors include:Black --> ba, blackBlue --> bu, blueCyan --> c, cyanGreen --> g, greenMagenta --> m, magentaRed --> r, redWhite --> w, whiteYellow --> y, yellowUsing Color Spanscolor_span = [
    "Roses are ",
    ("red", "r"),
    " and violets are ",
    ("blue", "bu")
]

pr(color_span, cs=True)Roses are [red] and violets are [blue]  <--  words in square brackets appear in their respective colorsWhere:c = indicates whether to treat the iterable as spans of colored textboolEquivalent to:print("Roses are \u001b[31mred\u001b[0m and violets are \u001b[34mblue\u001b[0m")Printing In PlacePrinting in place avoids the default Python behavior of printing content on a new line for each call to the print function. Instead, printing in place displays content on the same line, flushing the contents of the previous print statement each time. Such functionality is extremely useful within large loops.for i in range(101):
  pr(f"Percent complete: {i}%", ip=True)Percent complete: {1-100}%Where:ip = indicates whether content should be printed in placeboolEquivalent to:for i in range(101):
    print(f"\r\033[KPercent complete: {i}%", end="", flush=True)Printing Headingspr("Hello world", h=True)------------------------------------------------------------------------
Hello world
------------------------------------------------------------------------pr("Hello world", h=True, hrl=20)--------------------
Hello world
--------------------pr("Hello world", h=True, dhrl=True)
pr("Hello world, goodbye Mars", h=True, dhrl=True)------------
Hello world
------------

--------------------------
Hello world, goodbye Mars
--------------------------Where:h = indicates whether content should be printed as a headingboolhrl = desired length of horizontal rule above and below the heading stringintdhrl = indicates whether the length of the horizontal rule above and below the heading string should be dynamic, i.e. match the length of the contentboolEquivalent to:print("-" * 72)
print("Hello world")
print("-" * 72)print("-" * 20)
print("Hello world")
print("-" * 20)content = "Hello world"
length = len(content) + 1

print("-" * length)
print(content)
print("-" * length)Printing Horizontal Rulespr(hr=True)------------------------------------------------------------------------pr(hr=True, hrl=20, hrc="=")====================Where:hr = indicates whether to print a horizontal ruleboolhrl = desired length of horizontal ruleinthrc = desired character used to construct the horizontal rulestrEquivalent to:print("-" * 72)print("=" * 20)Tabulating Dataheader = ["Name", "Age", "Location", "Gender"]
rows = [
    ("Joey", 32, "London, UK", "M"),
    ("Penny", 27, "Beijing, China", "F"),
    ("Caroline", 30, "Austin, USA", "F"),
    ("Diego", 24, "Lima, Peru", "M"),
    ("Donny", 29, "La Linea de la Concepcion, Spain", "M")
]pr(header, r=True, h=True)
for row in rows:
    pr(row, r=True)orpr(header, r=True, h=True)
pr(rows, rl=True)orheader_and_rows = [header] + rows

pr(header_and_rows, rl=True, h=True)Name                 | Age                  | Location             | Gender
----------------------------------------------------------------------------------------
Joey                 | 32                   | London, UK           | M
Penny                | 27                   | Beijing, China       | F
Caroline             | 30                   | Austin, USA          | F
Diego                | 24                   | Lima, Peru           | M
Donny                | 29                   | La Linea de la Con   | Mpadding = 25

pr(header, r=True, h=True, hrc="=", p=padding, a="c")
for row in rows:
    pr(row, r=True, p=padding)orpadding = 25

pr(header, r=True, h=True, hrc="=", p=padding, a="c")
pr(rows, rl=True, p=padding)Name           |            Age            |         Location          |          Gender
============================================================================================================
Joey                      | 32                        | London, UK                | M
Penny                     | 27                        | Beijing, China            | F
Caroline                  | 30                        | Austin, USA               | F
Diego                     | 24                        | Lima, Peru                | M
Donny                     | 29                        | La Linea de la Concepci   | MWhere:r = indicates whether the content should be treated as a table rowboolrl = indicates whether the content should be treated as a list of rowsboolh = indicates whether the content should be treated as a table header, i.e. a row with a horizontal rule underneathboolhrc = desired character used to construct the horizontal rulestrp = desired padding (width) of each columninta = desired alignment of each table cellstrAccepted alignment options include:Left --> l, leftCenter --> c, centerRight --> r, rightRemarks:Avoid using colors with tabulated data for the time being as Python's string formatter does not play well with with ANSI color codesUsing Status Boxespr("Initializing launch sequence...", sb=True, la=1)
pr("Thrusters activated", t=1, sb=True, st="s")
pr("Satcom operational", t=1, sb=True, st="s")
pr("Communications online", t=1, sb=True, st="s")
pr("Major Tom present", t=1, sb=True, st="f")
pr("Aborting launch sequence...", lb=1, sb=True)[i] Initializing launch sequence...


	[✔] Thrusters activated
	[✔] Satcom operational
	[✔] Communications online
	[✘] Major Tom present


[i] Aborting launch sequence...Where:sb = indicates whether to include a status box before the printed contentboolst = desired status type to display in the status boxstrAccepted status types include:Info --> i, infoSuccess --> s, successFail --> f, failPretty Printing DataPretty printing is enabled by default when a structure such as a dictionary, list, or tuple is passed into pr. This will print the data over multiple lines if necessary rather than attempt to print everything on a single line as the default print statement will do.d = {
    "personal_info": {"name": "Jose", "age": "23", "location": "Christchurch, New Zealand", "gender": "M"},
    "hobbies": ["running", "swimming", "reading", "photography", "birdwatching", "surfing"]
}pr(d){'hobbies': ['running',
             'swimming',
             'reading',
             'photography',
             'birdwatching',
             'surfing'],
 'personal_info': {'age': '23',
                   'gender': 'M',
                   'location': 'Christchurch, New Zealand',
                   'name': 'Jose'}}Equivalent to:from pprint import pprint

pprint(d)Using DebugThe use case of the debug argument is rather interesting here. In short, if debug is set to False, the pr function will return immediately without printing any output passed into it. So why have this feature at all? Here's an example.Let's say we have some function that we intend to run in a production environment, where the complexity of the function requires extensive debugging in a local environment. You might introduce a debug argument in your function that will tell it to print debug messages only when debug is set to True, i.e. in the local environment. The function might look something like this:def my_func(num, dbg=False):
  """ Adds 5 and squares the result """

  # Add 5 to num
  num = num + 5

  # Print debug message
  if dbg is True:
    print("Added 5 to num!")

  # Square the result
  num = num * num

  # Print debug message
  if dbg is True:
    print("Squared num!")

  # Return the squared result
  return numClearly, it becomes a tedious process to implement an if statement at every point in your function where would like to print a debug message. Using pr, the same can be accomplished in a single line to save space and improve readability.def my_func(num, dbg=False):
  """ Adds 5 and squares the result """

  # Add 5 to num
  num = num + 5

  # Print debug message
  pr("Added 5 to num!", dbg=dbg)

  # Square the result
  num = num * num

  # Print debug message
  pr("Squared num!", dbg=dbg)

  # Return the squared result
  return num