Companionway

Meeting challenges with success

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”): …


Personal Coding Workflow

Some thoughts on personal coding workflow

My coding is rudimentary at best but the lessons I have learned along the way save me from constant anguish.


gtoolz

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


Fixing Flipped or Reversed Web Images

The tool of choice is mogrify which come from the imagemagic package on your linux platform.

I had one image that looked fine when I displayed it using display tools in linux but when placed into a webpage the image would be reversed.


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.


Managing Vim and Essential Plugins

Our last post discussed using fisa-vimrc and appreciating the simple installation and ease of use. This post will explore, briefly, some tweaks and a short “how to” for each of the loaded plugins. This exercise will be worth your time if you have not explored these tools before.


Vim-Plug Quick Setup

I want you to try this .vimrc setup.

I have had my own .vimrc for years. I recently came across fisa-vim-config and ended up dropping my extensive .vimrc, adopting the one above and adding a few tweaks to make it friendly to me. I was actually looking for more ALE-fixers when I found this gem.

It is simple, self installing and very powerful due to the vim Plugins that are installed and loaded.


Quick Note about fabric and pipenv

I have posted several articles about python fabric and yet I failed to warn you about the dangers of version and module chaos.

Initially, fabric came as a python2 only application. They added support for python3 later. Here is the version I am using in a virtual environment (more about that in a minute). From within my pipenv project directory with pipenv shell loaded this is what I get running fab –version

fab --version
Fabric3 1.14.post1
Paramiko 2.4.2

Here is how I created my virtual environment. I have used almost all of python’s virtual environment tools (and there a quite a few). And there has been a steady progression of improvements in those tools. My current goto for a python virtual environment is pipenv. It is simple, clean, and almost fool proof which is essential for me.


Python fabric - Dynamically Discover Hosts

Python fabric is a library tool for executing ssh commands remotely and responding as you desire. I use python fabric for promoting code and web content. Essentially it is my devops tool to satisfy the single most important devops rule: never log into a server! I use this over ansible or some of the other devop tools because fabric only requires knowning one langauge and that is python.

My network changes a lot as I take servers down or stand them up frequently. Normally that would require changing the environment roles (host lists) in my fabfile.py with all my changes. In the past I set aside certain IPs in my network for each role. For example all my raspberry pies get named before I ever introduce them to the network. Using the option –skip_bad_hosts with fabric is one way to do this but I really want more control.


Replace PHP with WSGI python

This post is a brief description of replacing PHP code with python using WSGI (Web Server Gateway Interface). The journey here comes from how I monitor my servers (mostly raspberry pi’s). I used a PHP script on all my servers that delivered the hostname and basic server information and I use xymon monitoring to poll that PHP script on all the servers and test to see if the hostname is contained in the returned page. My focus lately has been on python and my interest in PHP has waned. The goal was to replace my PHP script with a python script. The end result provides a simple means of delivering dynamic web pages. I will be using bottlepy as the wsgi web-framework. My development was done on an ubuntu 18.10 (“cosmic” release).