Skip to content

Instantly share code, notes, and snippets.

View P403n1x87's full-sized avatar
⚒️
Tinkering

Gabriele N. Tornetta P403n1x87

⚒️
Tinkering
View GitHub Profile
@P403n1x87
P403n1x87 / map.py
Created November 7, 2022 12:25
Elements of a free module over a ring
class Map(dict):
"""Element of a free module over a ring."""
def __add__(self, other):
m = Map(self)
for k, v in other.items():
n = m.setdefault(k, v.__class__()) + v
if not n and k in m:
del m[k]
continue
@P403n1x87
P403n1x87 / README.md
Created October 5, 2022 09:45
gevent patch poc

When run as

python -m main

the expected output is

[abu] patched dolly
[main] Abu

That's because we are patching the dolly module and the Dolly class is now the Abu class.

@P403n1x87
P403n1x87 / deps.py
Last active October 9, 2022 11:17
Install Kodi Python plugin dependencies from PyPI using pip
# Use as
#
# import deps
#
# deps.install(dict(requests="2.28.1"))
#
import json
import os
import site
@P403n1x87
P403n1x87 / wordle.py
Last active January 4, 2022 15:27
Wordle solver
import typing as _t
from collections import defaultdict
from enum import Enum
from random import choice
class TileColor(Enum):
GRAY = 0
YELLOW = 1
GREEN = 2
@P403n1x87
P403n1x87 / git-credential-helper-libsecret.sh
Created July 21, 2020 08:59
Store git credentials securely on Ubuntu
#!/bin/bash
set -e
sudo apt-get -y install libsecret-1-0 libsecret-1-dev libglib2.0-dev
sudo make --directory=/usr/share/doc/git/contrib/credential/libsecret
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
@P403n1x87
P403n1x87 / y_combinator.py
Created June 10, 2020 15:19
Y combinator in Python
TRUE = lambda x: lambda y: x
FALSE = lambda x: lambda y: y
PAIR = lambda a: lambda b: lambda f: f(a)(b)
D = lambda x: PAIR(x)(x)
FIRST = lambda p: p(TRUE)
SECOND = lambda p: p(FALSE)
@P403n1x87
P403n1x87 / wake_signal.py
Created May 18, 2020 11:13
Wake Control Signal. A timer with extensible timeout that performs actions at the rising and falling edges. The timeout is extended whenever the wake method is called while the signal is hot.
from threading import Thread, Event
class WakeSignal(Thread):
def __init__(self, interval, rising=None, falling=None):
super().__init__()
self._interval = interval
self._stopped = False
self._awake = False
class FiniteAutomaton:
def __init__(self):
self._states = {}
self._initial = None
self._current = None
def add_state(self, name, handler, initial=False):
if initial:
if self._initial:
raise RuntimeError("Initial state already added!")
from multiprocessing import Pool, Queue, Manager, cpu_count
from multiprocessing.queues import Empty
from threading import Thread
from tqdm import tqdm
def parallelize(func: callable, iterable: list, processes: int = None) -> list:
"""Parallelize the execution of a function over a list.
This method runs a given function over chunks of the given list in
@P403n1x87
P403n1x87 / net.py
Created December 25, 2018 00:33
Network Monitor Blighty widget
import socket
from time import sleep
import psutil
from attrdict import AttrDict
from blighty import CanvasGravity, TextAlign
from blighty.legacy import Graph
from blighty.x11 import Canvas, start_event_loop
from requests import get