Technologies I’m Impressed by

Following is the list of technologies I’m Impressed by, a completely subjective metric. Since I’m not impressed often, these deserve to be listed.

Impressed

In arbitrary order:

  1. JetBrains IDEs. The thorough understanding of the code is top notch.
  2. Vim. Amazing productivity when editing text.
  3. Linux. No explanation required šŸ™‚
  4. FHIR. Very well thought through data exchange standard for healthcare. Extensible everywhere. Profiles to mandate constraints.
  5. AWS CDK. Amazing productivity gain over CloudFormation due to semantic involvement of the tool. It’s about expressing what you want to achieve, not assembling the solution from what you have.

Hope you have found something new to you in this list. In this case, I recommend taking a look at it.

Subjective Python or why I “hate” Python

Different people look at different facts and also interpret them differently. Following are my chosen facts about Python programming language and my subjective opinion about these facts, the Python language, and its use for DevOps.

Background

As I do from time to time, I post comparisons between Next Generation Shell (NGS), a shell I’m working on, and other languages, Python included. The comparisons are typically involving some small task that I consider a “typical DevOps task” (defined later). As you can imagine, in these comparisons NGS always wins. First, because NGS was specifically designed for and is actually better for many DevOps tasks. Second, because there is no reason for me to post a comparison where NGS is not better. Anyhow…

After such post on LinkedIn (“Still using #Python for #DevOps? You like to suffer?”), where I compared sample NGS and Python code, a friend asked me why I “hate” Python. My answer was that I’m feeling neutral about Python.

I found the topic of my perception of Python interesting and deserving elaboration.

Python

From my perspective, Python is okayish language, relative to other programming languages.

While I understand people that are annoyed by Python 2 to Python 3 migration and breakage, I also realize that it’s very hard to get things right from the start (or even later).

Few things in Python from the top of my head that are annoying for me:

  • Python does not encourage nor supports well basic functional programming: map() and filter(). Due to Python’s syntax, the callback lambda can only be single expression.
  • Instead, it encourages list comprehensions which act as both map() and filter(). I don’t like list comprehensions, they don’t align with how I think. Dict comprehensions too.
  • Typing is afterthought and therefore “The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.”
  • Conditional expressions (A if C else B, where C is the condition) are breaking my head.
  • if __name__ == "__main__" hack idiom is ugly as fuck.

There are annoying things in any language though.

My strongest negative feeling about Python, for which I should mostly blame myself, is parameters handling. While working on NGS, early on, I copied was inspired by parameters handling in Python. The thought was that parameter handling is such a basic thing that (A) I shouldn’t spend much time on it and (B) such established language like Python would get it right. Python didn’t get it right. And I copied that atrocity. They ugly-fixed it later. I’m still thinking how to do it right. The “right” for NGS is likely to differ from the “right” for Python. Also, I would like to avoid making the same mistake for the second time.

What do I actually “hate” about Python?

What I “hate” about Python is surprisingly not Python-specific. It’s the use of square pegs for round holes:

Using Python, Ruby, Go, and other languages that were not intended for “typical DevOps tasks” (looking at resulting code for that judgement) when you have clearly better matching alternatives, including NGS and some other modern shells.

“Typical DevOps tasks” include running external programs and small scale data manipulation (files, structured data, mapping, filtering, pattern matching).

Justifying the Square Pegs

This one is annoying, especially hearing the same arguments for 1000th time.

“But I can do it with libraries”. You can. Also you can do it in Assembler, C or FORTRAN with or without libraries. I do not except the argument. The solutions are not equal.

“It’s not that much of a difference”. Depends on the use case. Run external program in Python and handle exit codes properly and let’s talk then.

More legitimate arguments include:

  • “Our team uses Python and so we do DevOps in Python too”
  • “We are afraid we will not find Stack Overflow answers if we are stuck”
  • “We are afraid we won’t be able to hire developers in this new language”
  • “Where is the tooling?”

In my mind, the legitimacy of the arguments above is in reverse proportion with DevOps work volume. The more “DevOps” tasks you do as percentage of your total work, the more advantages of better suited programming languages shine through.

While sometimes the arguments are justified, other times it’s inertia which is holding us back. Like switching away from JavaScript which is not going according to the plan.

Programming Languages in General

Subpar. All of them. At least the ones that I have seen (dozens). Python included. NGS included. We as humanity didn’t figure that out yet. I mean programming languages. We are stuck in paradigms which hold us back.

NGS aims to be less bad for DevOps than the rest because it was designed for the niche. Almost round peg for round hole, way better than the square pegs.

Conclusion

While it might look like I “hate” Python,

  • it’s actually all programming languages
  • and even more so their inappropriate use and the worst is
  • justifying and perpetuating inappropriate use of programming languages

Thanks for reading, have a nice ${TIME_OF_DAY} !


For the curious ones, code comparison follows.

The Code Comparison

During my quest to understand CloudFormation better, I stumbled upon this code:

"Gets the name of the folder with the handler code"

import json
import sys

def main(rpdk_path):
    "Get the source folder from rpdk config"

    with open(rpdk_path) as f:
        obj = json.load(f)
        entrypoint = obj["entrypoint"]
        print(entrypoint.split(".")[0])

if __name__ == "__main__":
    main(sys.argv[1])

To which I responded with NGS version (renaming mostly meaningless obj to conf on the way):


#!/usr/bin/env ngs

# Gets the name of the folder with the handler code

F main(rpdk_path) {
	conf = read(rpdk_path).decode_json()
	echo(conf.entrypoint.split(".")[0])
}

To which my friend responded with another Python version, which was shorter and did not have main().

Command Line Arguments

Both the original and the shorter version in Python require import sys which shows that dealing with command line arguments is not as “important” in Python as in NGS.

The gap is between additional import in Python and out-of-the-box automatic parsing of command line arguments and passing them to main() in NGS.

JSON

Both Python versions require import json, showing again that it’s easier to deal with JSON in NGS than in Python.

The example would be even shorter if the use case was slightly more typical – the JSON file would have .json extension. read(rpdk_path).decode_json() would become fetch(rpdk_path), leaving Python far behind.


Amazing! You stayed here till the end. Congrats and thanks!

Readable TypeScript debug() Output for Protobuf Messages

For search engine indexing and for understanding: Protobuf is frequently used with GRPC. Protobuf is how the requests and responses are serialized.

The Problem

Look at the output below. Printing protobuf messages using debug(‘%j’) produces unreadable output (the first one below).

$ DEBUG=myapp npx ts-node tojson.ts 

  myapp My object without toJSON {"wrappers_":null,"arrayIndexOffset_":-1,"array":[null,null,null,null,null,"Session name 01"],"pivot_":1.7976931348623157e+308,"convertedPrimitiveFields_":{}} +0ms

  myapp My object with toJSON {"id":"","CENSORED4":"","CENSORED3":"","version":"","name":"Session name 01","CENSORED1":"","status":0,"CENSORED2":""} +1ms

Solution

Here is all you need to make it work. SessionResponse below is a sample message (export class SessionResponse extends jspb.Message in another file, after import * as jspb from "google-protobuf";)

// --- Setup ---
import {Message} from "google-protobuf";

import {
    SessionResponse
} from "./PATH/TO/YOUR_GENERATED_PROTOBUF_FILE_pb";

import debug0 from 'debug';
const debug = debug0('myapp');

const req = new SessionResponse().setName("Session name 01");

// --- Test before ---
debug('My object without toJSON %j', req)

// --- Add this to your code --- start ---

declare module "google-protobuf" {
    interface Message {
        toJSON(): any;
    }
}
Message.prototype.toJSON = function () {
    return this.toObject();
}

// --- Add this to your code --- end ---

// --- Test after ---
debug('My object with toJSON %j', req)

Hope this helps. Let me know if you have questions.