Companionway

Meeting challenges with success

Parsing ansi Code

A function to extract ansi code ...

Dealing with strings that contain ansi codes for colorizing can be a challenge especially if you need the actual display length of the embedded strings.


Taking advantage of *args and **kwargs

Taking Advantage of *args and **kwargs

I consistently forget the option word I have included in a function and so I had to design a way to accept different keywords (option) names to trigger features with in a function.

As an example, assigning a color to a box border is very easy by just adding something like this to a function’s arguments:

def buildabox(msg, border_color="red"):
  """
  docs go here to put a box around msg
  """
  ...

But, if you accidenally call the function like this, it will fail. my_box = buildabox(“my message”, box_color=“white on back”): …


gtoolz

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


Python's all() function

Python all() function

Much of my coding efforts are directed towards analyzing financial data specifically targeting potential cash secured puts or covered calls. Recently, I had to refine some market overview displays. I needed a listing of the current market “gainers” and market “losers”. There is one site which I scrape using panda read_html function to grab a hadful of tables. My problem arises from two factors. One is there are no labels or titles to the tables and they can vary. The a table can arise from special (temporart) events and the weekends or holidays might call for the tables to re-arrange their order. This requires that I look directly at the panda table and derive which table I might be examining. One table I look for lists all the “gainers” currenly in the market. What I do know is that the third column of that table will always contain a plus sign (the % change of all the gainers). I had nested loops initially to do this but I refactored the code to use the “all()” function to discover which table has all “+” signs in the column 3. My standard caveat applies here - there is likely a better way to do this - so let me know.


Quick N Dirty Python Debug Function

Pdb is a great debugger for python but I like to use my own simple debug functions. Here is a simple debug function for python which could definitely be improved:


Sometimes It Is The Obvious

This was one of those “problems” that nagged me for many hours until the obvious dawned on me. I wrote a python script to grab the temperature and humidity from a DHT22 sensor and then write the output with the proper syntax to send to my monitoring program [xymon]. The wrapper bash script ran every 5 minutes and used a redirection to write out a file ie:

*/5 * * * * /usr/local/bin/temphum.sh >/tmp/temphum.dat

The wrapper script grabs output and sends it to the server. Having this wrapper script lets me run the python script independently for testing or for curiosity.



Credits

cos Companionway
ale vim ALE
netlify Deploy to Netlify
vim VIM the editor
gtoolz gtoolz
gtoolz python tools Google Gemini