Skip to content

Instantly share code, notes, and snippets.

View acatton's full-sized avatar

Antoine Catton acatton

View GitHub Profile
@acatton
acatton / add.py
Created June 26, 2020 19:55
Is there any way to carry over 1?
#!/usr/bin/env python3
import typing as ty
import itertools
import random
import logging
MAX_COMPOSITE = 5000 # Maximum amount of numbers used to compose a number in gen()
MAX_NUM = 1000000 # Maximum number generated when composing a number in gen()
MAX_BASE = 1000 # Maximum base
@acatton
acatton / aws-docker.py
Created January 16, 2018 15:14
aws-docker
#!/usr/bin/env python3
# $ docker run --rm -it $(docker-aws) alpine
# / # env | grep -i aws
# AWS_DEFAULT_REGION=eu-central-1
# AWS_SESSION_TOKEN=...
# AWS_ACCESS_KEY_ID=...
# AWS_SECRET_ACCESS_KEY=...
#
# When you don't want to mount `~/.aws:/root/.aws` because SELinux prevents you to do so.
@acatton
acatton / r.py
Created October 11, 2017 08:42
Render data
def render_table(data: List[List[str]]) -> str:
"""Render table:
Example:
>>> print(render_table([['a', 'b'], ['c', 'd'], ['long', 'very long']]))
+------+-----------+
| a | b |
+------+-----------+
| c | d |
+------+-----------+
@acatton
acatton / polynomial.py
Last active July 11, 2017 19:49
Compute derivative polinomial
# coding: utf-8
# Copyright © 2017 Antoine Catton
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
"""This is inspired from some mythical interview question (Google/Amazon/Microsoft, who knows..)
The goal is "compute the derivative of a polynomial".
@acatton
acatton / monitor.py
Last active June 2, 2017 14:46
Monitor MySQL InnoDB locks
#!/usr/bin/env python
# coding: utf-8
# Copyright © 2017, figo GmbH
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See the COPYING file or http://www.wtfpl.net/
# for more details.
import argparse
@acatton
acatton / poly.hs
Last active August 29, 2015 14:25
Mixed types
import Data.Map.Strict as Map
import Data.List as List
import Control.Monad
data Transform = StringToInt (String -> Int)
| IntToString (Int -> String)
| IntToInt (Int -> Int)
| StringToString (String -> String)
@acatton
acatton / bad_class_monkeypatching.py
Created April 22, 2015 23:36
Here's how you shouldn't monkeypatch your class
class GrandParent(object):
@classmethod
def call(cls):
return 1
class Parent(GrandParent):
@classmethod
def call(cls):
return 1 + super(Parent, cls).call()
@acatton
acatton / imerge_sorted.py
Last active August 29, 2015 14:15
Merge sorted iterables
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright © 2015-present Antoine Catton
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details
def imerge_sorted(iterables, key=lambda x: x, reversed=False):
"""
@acatton
acatton / fib.py
Last active August 29, 2015 14:13
Crazy optimization
# This was a relevation for me:
# <http://kukuruku.co/hub/algorithms/automatic-algorithms-optimization-via-fast-matrix-exponentiation.html>
def fib(n):
u_n = 1
u_n1 = 0
for i in xrange(1, n):
u_n1, u_n = u_n, (u_n1 + u_n)
return u_n
@acatton
acatton / memoryleak.py
Last active July 5, 2021 06:51
For people saying there's no such thing as memory leak in python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import functools
def memoize(func):
CACHE = dict()
@functools.wraps(func)
def wrapper(*args, **kwargs):
key = (tuple(args), frozenset(kwargs.items()))