Ceci est une ancienne révision du document !
Greetings fellow beings. It’s already the end of summer (well, here where I live), and it can’t come soon enough. Days with highs of over 107° F (42° C) air temperature in the shade for over 30 days in a row with no rain at all, plays havoc with simple tasks of everyday life like mowing the grass, going to the grocery, and simply taking a walk – a real task that is dangerous for many people. Luckily, with the beginning of Fall, that should come to an end.
Anyway, about this month’s project. A few years ago, my son asked for a way for him to watch television on his computer without having to get a cable drop in his home office, or having to purchase another television. Here in the U.S., we have OTA (Over The Air) television that is free from the local stations. All you need is an antenna and a TV that has a tuner. That’s pretty easy to do, since most televisions have tuners built into them, and I have an antenna that’s designed for indoor installation. I use that for the television in the living room.
After a little research, I found a device called HDHomeRun that holds multiple tuners and when plugged into an ethernet cable and an antenna, will send the OTA signals to any computer that is connected to the home network. Where we are located, we can get over 80 channels of television shows throughout the house, and any computer or smartphone in the house can watch any show while others can watch something else. This is great for my son, since his computer in his office has three monitors and runs Windows. The great news is that there are viewers for Android, IOS, Windows and Mac. The bad news is that Linux is not supported for the viewers.
So one of my current projects is to create a Linux based viewer (that can also work on Mac and Windows), written of course in Python (and Tkinter for the UI). The device has an API built in that you can query to get various information for things like the channel lineup and the EPG (Electronic Program Guide) – all of which comes in JSON format (as well as other formats) to find out what you want to watch, and then you simply use the VLC or MPV python plugins to view the stream.
When you query the API for the channel list, the JSON data looks something like that shown above.
That’s easy enough to deal with in Python, but when you get to over 80 channels, that’s a bunch of data and the EPG is even worse. Of course, the number of data fields for each channel can change depending if the channel is High Definition, HEVC (the new ATSC format the U.S. is moving to), if the channel is marked as a Favorite channel, and so on. But visualizing this data is hard when it is in the flat format that comes in directly from the API. Especially, if you don’t have much in the way of documentation so you can write a program to handle all the possible values. It would be so much easier to see the data like this (next page, top right).
Even if there are over 80 sets of the data, you can find what you want to, especially if there is something unexpected that came down in the stream. Yes, you can send the data to a file and hand break it down, but python gives us a tool to do all that.
Enter PPRINT. It’s part of the standard library set and many of us have used it, but not necessarily to the full effectiveness of the library.
Let’s look at the basic syntax for pprint.
class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True, underscore_numbers=False)
There are a number of parameters that are helpful, but without any context, you might not know how to leverage them for the best results. I’ll try to break down each of them.
Indent - Default = 1 - The number of spaces added for each level.
Width - Default = 80 - The number of characters per line for the output. If the data can’t fit in the space allowed, the library will make a best effort.
Depth - Default - None - The number of nested levels for the formatted output. If the number is too low, the next level will be replaced with “…”
Stream - Default = sys.stdout - This is a file-like object that will be written to by the write function of the class. If stream and sys.stdout are both None, pprint will simply return nothing.
Compact - Boolean - This determines how long structures like tuples, lists, sets, etc, should be formatted. If False, the structure is broken into separate lines. If True, the structure will be output as single lines – up to width constraints.
Sort_dicts - Boolean - If True (default), dictionaries will be sorted by their keys, otherwise they will be displayed in the insertion order.
Underscore_numbers - If True, integers will have the underscore ( _ ) character as a thousands separator. If False (default), underscores won’t be printed.
There are also two methods that control the formatted data output.
Pprint.pformat - Returns a formatted object as a string. The syntax is:
output=pprint.pformat(dataobject,*args)
Pprint.pp - Prints the formatted dataobject followed by a newline character.
pprint.pp(dataobject,*args)
Pprint.pprint - Prints the formatted dataobject on stream followed by a newline character. If stream is None, sys.stdout is used.
pprint.pprint(dataobject, stream, *args)
Real World Examples
All of this information is wonderful, but without seeing the parameters in action, it really doesn’t help. So here we go (next few pages). I’m going to concentrate on the .pformat method in the following examples. The width parameter is fairly easy to understand, so I won’t bore you with it.
That’s it for this time. I didn’t bother to set up a repository since the data would be fairly large and useless to anyone but me. I’m sure you can handle things from here.
Until next time, as always; stay safe, healthy, positive and creative!
