Python has a solid backbone of modules for various operations. It would be a pity if we had to write one-liner python programs just because we cannot avail those functionalities on the command line - wouldn’t it?
Day 1:
I wanted to query a heavily nested json for a specific key. Now jq does the job pretty well, but the easier way was to flatten the json and just grep out the required key. I had a flatten json python function already written. So all I did was to write a simple program to use that function and flatten the file passed as a command line argument. Good enough.
Day 2:
I wanted to find out if one file is a subset of the other. I looked it up and saw that this post describes a way to do so:
as both of the above results are same - a is a subset of b. Pretty neat but then I would need to compare the two results and write a shell script todo so. The python library provides the set data structure having the very intuitive issubset function which just does this. So all I did was to write a simple program to use that function and do the necessary check. Does the trick.
Day 3:
A colleague was mangling some text and asked me how to convert uppercase to lowercase. I showed him how:
He asked is there a similar functionality to swap case? I did not have a ready answer to do so on the command line but told him that he could write a one liner python script using string.swapcase to do so. So all I did…
Now wait a minute…
Why would I want to write separate programs to flatten a json, check if a file is a subset of the other or swap case when I could very written write one program which takes the target module, function to call, its arguments and does the right thing?
Enter pyfunc
Now all of the above problems are solved by doing:
I get everything in the standard library as a bonus:
For the commonly used pyfunc calls I convert them to aliases:
And now I have a richer command line:
More examples in the github repo - pyfunc
The only glitch that I faced while writing this was to accomodate different types of arguments being passed to the target functions like list, dict, etc. I added support for method signatures to do so, but now I feel it would be easier to have pyfunc functions to just take args and pass in a json from the command line. I will be adding that soon.
pip pyfunc up and bring the awesomeness of python on the command line!