[python] Generate ranges from items

Some years ago, I added a script for updating headers for (C) in the python files I was developing for Risu. In this way, the header got the list of authors and years working on the files updated automatically. With the pass of the years, the list started to became a bit too long, so I worked on creating code for getting ranges instead. This is the code I used: def getranges(data): """ From list of strings representing numbers, get ranges and return list of strings :param data: list of strings representing numbers :return: list of strings with number ranges when > 1 """ # Convert to integers data = [int(i) for i in data] result = [] if not data: return result # Prepare iteration loop idata = iter(data) first = prev = next(idata) first = first prev = prev # Process next item for following in idata: if following - prev == 1: # Years are continuum, just update previous prev = following else: # Years are not continuum, end range and start again if first == prev: result.append(first) else: if first + 1 == prev: # Only one item in difference, append items individually result.append(first) result.append(prev) else: result.append("%s-%s" % (first, prev)) first = prev = following # Catchall for regular execution or last remaining range if first == prev: result.append(first) else: if first + 1 == prev: # Only one item in difference, append items individually result.append(first) result.append(prev) else: result.append("%s-%s" % (first, prev)) # Convert back to text result = [str(i) for i in result] return result With it, previous headers like: ...

November 25, 2022 · 2 min · Pablo Iranzo Gómez

Automate code build and deployment with ansible

Let’s say that we want to keep our system updated with some code which is not distributed as a regular package, but as a code in a repository (which unfortunately, it’s a pretty common situation). As a part of the ansible playbooks used for the hosts, I can add a snippet like this: gitrepos: - { url: "https://github.com/myrepo/repo.git", tag: "tagtocheckout", folder: "/root/path-for-check-out", chdir: "subdir to enter", build: "make build", exec: "build/mybuiltbinary", } With this definition in the host inventory, we can then in our playbook to perform several steps: ...

November 9, 2022 · 3 min · Pablo Iranzo Gómez

OpenShift's oc debug and parallel execution

A colleague reported some issues in the OpenShift troubleshooting and diagnosis scripts at OpenShift-checks. Some time ago I did contribute some changes to use functions and allow using the RISU wrapper to the scripts, helping consuming the results via RISU’s HTML interface. As my colleague reported, for some plugins, the output of the command was not shown in the HTML Interface. After some investigation, it was found that parallel execution for the plugins was causing no output to be shown, but when filtering to individual ones via risu -i XXXXXXX/plugin -l it was working fine… the problem was not the check itself, as both of them worked fine when executed individually but failed when executing them together. ...

November 3, 2022 · 2 min · Pablo Iranzo Gómez
This blog is a participant in the Amazon Associate Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.