This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MIT License | |
# | |
# Copyright (c) 2021 Paolo Lammens | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Algorithm to recursively apply a function to a composite object. | |
Dependencies: | |
- multimethod (https://pypi.org/project/multimethod/) | |
""" | |
# MIT License | |
# | |
# Copyright (c) 2020 Paolo Lammens |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Metaclass for inner classes in Python. | |
""" | |
# MIT License | |
# | |
# Copyright (c) 2020 Paolo Lammens | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal |
NewerOlder