Skip to content

Instantly share code, notes, and snippets.

View txomon's full-sized avatar

Javier Domingo Cansino txomon

  • Germany/Koln, UK/London, Spain/Bilbao
View GitHub Profile
@txomon
txomon / client_disconnect_handler.py
Created February 1, 2025 18:16
Detect client-side disconnection of socket to raise an exception on the server to interrupt traffic
import ctypes
import enum
import logging
import os
import socket
import struct
import sys
try:
import tenacity.retry
@txomon
txomon / JSONL.jsonl.groovy
Last active April 8, 2025 20:58
Jetbrains IDEs (Pycharm, Intellij, etc.) data export to JSONL (json lines)
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
@txomon
txomon / debugging_exception.py
Last active January 1, 2024 19:38
Snippet to ease debugging exceptions
import sys, traceback
def exception_hook(e, value, tb):
for line in traceback.TracebackException(
type(value), value, tb, capture_locals=True
).format(chain=True):
print(line, end="")
sys.excepthook = exception_hook
@txomon
txomon / playbook.yaml
Created October 30, 2019 19:11
Example declarative metamodule for ansible
- name: Make sure only three files exist in /myfolder
declarative:
items:
- a
- b
- c
state:
command: find /myfolder/ | tail -n +2 | basename
extra_item:
file:
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import string
from weakref import WeakKeyDictionary
class SafeDict(dict):
def __missing__(self, key):
return '{' + key + '}'
@txomon
txomon / ratelimit.py
Created August 12, 2018 21:24
Rate limit python asyncio
import asyncio
import collections
async def ratelimit(*, max_request, in_interval):
slots = collections.deque()
while True:
slots.append(time() + in_interval)
yield
while len(slots) >= max_request:
left = slots[0] - time()
@txomon
txomon / test.py
Created August 4, 2018 04:54
Python 3.7 ContextVar example in asyncio
from contextvars import ContextVar
import asyncio
import random
cv = ContextVar('cv')
async def waiting_func(name):
print(f'{name} Before sleep: {cv.get() == name}')
async def message_handler(message: DubtrackMessage):
print(f'Received {message.text}')
# await message.channel.say('Bot speaking here')
def run_bot():
# Setup
bot = Bot()
dubtrack_backend = DubtrackBotBackend()
dubtrack_backend.configure()
WARNING:abot.bot:No message handler for <DubtrackUserUpdate <DubtrackEntity None#560b135c7ae1ea0300869b20 379/1680 0(S) ##0> skip#0 played#1680 queue#0 dubs#379>
WARNING:abot.bot:No message handler for <DubtrackDub#updub <DubtrackEntity txomon#560b135c7ae1ea0300869b20 379/1680 0(S) ##0> +1-0>
INFO:dubtrack.layer3:User updated 5628edc08d7d6a5600335d3d, skip 0, played 23898, queue 30, dubs 25899
INFO:dubtrack.layer3:Chat txomon: so I have been able to more or less finish the bindings
INFO:dubtrack:Now playing youtube#8-wIg90lagk: Batman Begins Soundtrack - Corynorhinus
INFO:dubtrack.layer3:Chat txomon: and I need to generate some actions more to be able to map all the possible ones
INFO:dubtrack.layer3:Chat txomon: so if you see I do strange stuff, it's true xD
INFO:dubtrack.layer3:Chat txomon: I am just testing
INFO:dubtrack.layer3:Chat eva_MoS: Short version:beware, he´s watching all of us
INFO:dubtrack.layer3:Chat eva_MoS: ;-P
INFO:dubtrack.layer3:User updated 562e462b888e6f1900ff20bc, skip 0, played 28988, queue 42, dubs 30136
INFO:dubtrack.layer3:Song updub by eva_MoS#562e462b888e6f1900ff20bc, total 1U/0D