Skip to content

Instantly share code, notes, and snippets.

View plammens's full-sized avatar
🎯

Paolo Lammens plammens

🎯
View GitHub Profile
@plammens
plammens / method_decorator.py
Created June 25, 2021 11:25
Method Decorator
"""
Utility to make a method decorator, with one decorated function per instance.
Applying a regular decorator to a method applies it to the unbound function,
but in many situations what we really would like to do is to decorate each of the
bound instances of the method independently, that is, for each instance of the class.
Consider the method ``MyClass.foo(self, x)``; if we apply the ``functools.lru_cache()``
decorator to it, there will be only one cache for all `MyClass` instances, and it
will be indexed by ``(self, x)`` tuples instead of just ``x``. More probably, what we
@plammens
plammens / autorepr.py
Last active June 29, 2021 19:26
Generic __repr__ implementation based on the constructor's signature
import inspect
class AutoReprEqMixin:
"""
Adds a generic __repr__ and __eq__ implementation.
The __repr__ implementation is based on the constructor's signature and relies
on the existence of attributes with the same name as the parameters they were
initialised from.
@plammens
plammens / mergemetaclasses.py
Created June 30, 2021 15:13
Merge metaclasses ad-hoc to solve metaclass conflicts
from typing import Type, Optional
def merge_metaclasses(*bases: Type, metaclass: Optional[Type] = None) -> Type:
"""
Make an ad-hoc metaclass to resolve conflicts in class declarations.
Makes a metaclass that inherits from all of the metaclasses involved in a class
declaration: the metaclasses of the bases and the explicit metaclass. The
inheritance order is the explicit metaclass first and then the bases'
@plammens
plammens / main.dart
Created November 16, 2022 08:39
List slicing with step parameter in Dart
extension ListSlice<T> on List<T> {
List<T> slice([int start = 0, int? end, int step = 1]) {
end ??= this.length;
return [for (var i = start; i < end; i += step) this[i]];
}
}
void main() {
final list1 = [for (var i = 1; i <= 20; ++i) i];
@plammens
plammens / main.dart
Last active March 14, 2023 21:01
groupConsecutiveBy
extension MoreIterableTools<E> on Iterable<E> {
/// Group consecutive elements with the same key.
Iterable<MapEntry<K, Iterator<E>>> groupConsecutiveBy<K>(
K Function(E) keyOf,
) sync* {
final iterator = this.iterator;
bool moveNext = iterator.moveNext(), lastGroupConsumed = true;
while (moveNext) {
if (!lastGroupConsumed) {
@plammens
plammens / parker_pizza.py
Created May 20, 2023 09:22
Number of ways to cut pizza into identical-shaped pieces such that some do not touch the centre.
import itertools
import math
import typing
from collections import Counter
from sympy import factorint
def submultisets(multiset: Counter) -> typing.Iterable[Counter]:
for counts in itertools.product(*[range(count + 1) for count in multiset.values()]):
@plammens
plammens / factorio_ratios.py
Created February 24, 2024 10:47
Compute the minimal integral solution for producing in exact ratios in Factorio
import typing as t
from abc import ABCMeta, abstractmethod
from dataclasses import dataclass, field
from decimal import Decimal
from fractions import Fraction
from functools import cache
from math import lcm
from frozendict import frozendict