[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

In action...@redken_bot with examples

Redken in action with some examples Let’s learn about using Redken in Telegram or Discord with some examples…. but first, let’s add the bot to a new group based on the platform we’re using: Redken_bot in Telegram Redken in discord Let’s get hands on! First of all, some of the commands shown will require admin privileges in the chat (from the user point of view) and from Redken. Redken uses admin privileges to: ...

January 20, 2022 · 3 min · Pablo Iranzo Gómez

LDAP query from Python

Recently, some colleagues commented about validating if users in a Telegram group were or not employees anymore, so that the process could be automated without having to chase down the users that left the company. One of the fields that can be configured by each user, is the link to other platforms (Github, LinkedIn, Twitter, Telegram, etc), so querying an LDAP server could suffice to get the list of users. ...

October 19, 2021 · 2 min · Pablo Iranzo Gómez

Redken machine learning for spam detection

For some of the telegram groups I’m in, I’ve been detecting users that after some period of time, just publish spam messages of any topic. There are many bots for controlling when a user joins, by showing a CAPTCHA that user must resolve (either clicking a button, answering a mathematical operation, inputting a CAPTCHA image text, etc). Some time ago, a colleague was using Machine Learning and I wanted to have a look at it and it would make a good feature to implement. ...

June 24, 2021 · 8 min · Pablo Iranzo Gómez

Language detection in @redken_bot

Introduction Before the move to Python3, redken had per-group language configuration by using i18n, with the upgrade/rewrite of Python3 code there were some issues and I had to remove the support, defaulting everything to English (most of the outputs where already in English so not a great loss). On another side, having to manually configure each channel could be problematic as most users just add the bot to their groups but don’t care about other settings that might be useful like welcome message, inactivity for kicking out inactive users, etc. ...

March 25, 2020 · 6 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.