gtoolz

Posted by geoff.mcnamara@gmail.com on Thursday, February 23, 2023

The python3 collection of tools called gtoolz has been released and can be downloaded from github or using:

pip install gtoolz

This is an overhaul of the deprecated gtoolz module. The name gtools was already taken in pypi so it has been renamed to gtoolz.

This is a collection of nearly 100 functions that make working with python easier to create command line based scripts or applications.

Features include:

  • a useful break-point type debugging tool is included (ie dbug(msg or variable… etc))
  • tools for colorizing text of boxes or tables
  • tools to build colorful tables data including pandas, list of lists, lists of dictionaries, csv files etc
  • tools to put (colorized) boxes around text or lines of text
  • tools to center output on the screen
  • tools to place shadows around boxes
  • tools for running shell commands with options to manipulate output
  • tools to pull data from files lines of output and build tables, lists of lists, or pandas, etc
  • rudimentary progress bars or percentage bar
  • spinners while others tasks are carried out
  • tools to read HTML tables and turn it into colorful ascii table with selected columns and/or filtered data
  • other tools to manipulate text or data
  • tools to build ascii dashboards or columns of boxes or blocks

and more.

Some short examples of other functions:

  • printit(msg): this is an extensively used function to replace the simple built in print function: here are some examples: printit(“This message will be boxed”, ‘boxed’) printit([“This message will be boxed”, “and centered with two lines”], ‘boxed’, ‘centered’) printit([“This message will be boxed”, “and centered with two lines”], ‘boxed’, ‘centered’, title=“My Title”, footer=dbug(‘here’), box_clr=“yellow! on black”) printit([“This message will be boxed”, “and centered with two lines”, “and now [red] some red[/] color”, “and it will be a shadowed box”], ‘boxed’, ‘centered’, ‘shadowed’, title=“My Title”, footer=dbug(‘here’), box_clr=“yellow! on black”)

  • dbug(var: str): I use this tool extensively and you will see it pop up anytime an argument to a function seems misused. This tool is available to display the contents of a variable and will list the file name, function name line number and value of the variable requested. Or it will display any string (message) you want. It can be centered, boxed, colored, or act as a break point by supplying the option ‘ask’. Also if the option ‘here’ is used it will not display the file, function, lineno, but instead return information (I use this when I request any type of boxed display by using boxed(my_message, title=“This is my Title”, footer=dbug(‘here’))

  • boxed(msg): puts the message which can be a string or lines (list of strings) in a box. You can center the box on the screen, or color it the way that you want.

  • centered(msg): centers msg on your terminal screen. msg can be a string or a list of lines (strings)

  • gclr(color): This is used by many functions to give color. Examples of color: gclr(“red! on black!”) The exclamation mark forces the named color to its extreme. Many terminals will normally display a “muted” red or a grayish black or brown yellow. Using the exclamation mark makes it a bright red, a true black, or a bright yellow. Or you can supply gclr(‘rgb(20,50,210’), or you can use tags within a string “This is my string with the [red]color red[/] in it”. This is similar to how the excellent “rich” modules handles colors.

  • remap_keys(data: dict, remap: dict): this will take an existing dictionary and change all the key names as declared in the remap dictionary ie: {orig_name1: new_name1, old_name2: new_name2, …}. A dictionary will be returned with the new key names - optionally you can request only the remap dictionary list of keys be returned (and the new dictionary will be in the orded of the remap dictionary)

  • usr_update(data: dict): this function wil take any supplied dictionary and will allow the user to edit each value of the dictionary

    • or you can supply an optional ’to_update’ list and only the “keys” in the list will be asked for user updates.
  • gcolumnize([box1, box2, box3], [box4, box5, box6], [box7, box8, box9]])

  • gblock(boxed(“My message”), ‘boxed’, ‘centered’, width=40, height=10, fill=“X”, pos=6, prnt=True, title=“boxed msg in position=6”, footer=dbug(‘here’)) will produce something like this: all of the options can be changed to your liking and their are options for colors etc from gtoolz import boxed, gblock gblock(boxed(“My message”), ‘boxed’, ‘centered’, width=40, height=10, fill=“X”, pos=6, prnt=True, title=“boxed msg in position=6”, footer=dbug(‘here’)) ┌─────────boxed msg in position=6──────────┐ │XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX│ │XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX│ │XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX│ │XXXXXXXXXXXXXXXXXXXXXXXXXXX┌────────────┐X│ │XXXXXXXXXXXXXXXXXXXXXXXXXXX│ My message │X│ │XXXXXXXXXXXXXXXXXXXXXXXXXXX└────────────┘X│ │XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX│ │XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX│ │XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX│ │XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX│ └──────DEBUG: [t.py:gblock_tst:3403]───────┘

  • do_logo(), and do_close() can be used to display branding info or closing messages: (but with the declared colors of course) from gtoolz import do_logo, do_close do_logo(“The Majick Company”, ‘figlet’, box_clr=“yellow! on black”) do_close(“Programmer needed”, box_clr=“red! on black!”) ┌─────────────────────────────────────────────────────────────────────────────────────────────────┐ │ _____ _ __ __ _ _ _ ____ │ │ |_ | |_ ___ | / | __ _ (|) | | __ / | _ __ ___ _ __ __ _ _ __ _ _ │ │ | | | ‘ \ / _ \ | |/| |/ || | |/ __| |/ / | | / _ \| '_ _ | ‘ \ / ` | ‘ | | | | │ │ | | | | | | __/ | | | | (| || | | (| < | || () | | | | | | |) | (| | | | | || | │ │ || || |_|_| || ||_,|/ |_|_||_\ __/|| || |_| ./ _,|| ||__, | │ │ |/ || |_/ │ └─────────────────────────────────────────────────────────────────────────────────────────────────┘ ┌───────────────────┐ │ Programmer needed │ └───────────────────┘

Contact me if you have any questions.

Enjoy!


comments powered by Disqus