Skip to content

Instantly share code, notes, and snippets.

Avatar

Arnau Orriols arnauorriols

View GitHub Profile
View python_typings_are_a_joke.py
from typing import NamedTuple
from mypy_extensions import TypedDict
A = TypedDict("A", { # error: Recursive types not fully supported yet, nested types replaced with "Any"
"b": B,
"c": C
})
B = TypedDict("B", {
@arnauorriols
arnauorriols / merge_sort.rs
Created Oct 23, 2015
Merge-sort implementation in Rust
View merge_sort.rs
fn mergesort(unsorted: &[u32]) -> Vec<u32> {
let n = unsorted.len();
if n < 2 {
return unsorted.to_vec();
}
let sorted_child1 = mergesort(&unsorted[..n/2]);
let sorted_child2 = mergesort(&unsorted[n/2..]);
let mut ptr1 = 0;
let mut ptr2 = 0;
let mut sorted = vec![];
@arnauorriols
arnauorriols / diamond_kata.js
Created Feb 2, 2015
Diamond kata with a TDD approach, in Javascript. Javascript noob, any feedback is most than welcome! Particularly looking for 'idiomatic' corrections.
View diamond_kata.js
// Diamond Kata with a TDD approach. First attempt
'use strict';
module.exports.Diamond = Diamond;
// Encapsulate line printing
function Letter(char, spaces) {
this._char = char;
this._spaces = spaces;
@arnauorriols
arnauorriols / coroutine_maker.py
Last active Jul 22, 2018
(Tornado) Convert a callback-based async function to a coroutine, configuring the position/keyword the callback is expected. This can be used, for example, to convert the callback-based interface of the pika.TornadoConnection to a coroutine-based one, which is the standard async pattern suggested by Tornado nowadays.
View coroutine_maker.py
from functools import wraps
from tornado.concurrent import return_future
class CoroutineMaker(object):
""" Convert a callback-based async function to a coroutine.
The Tornado utility gen.Task is quite useful to convert callback-based
async functions to coroutines, but has an important drawback: it expects
the callback to be passed to the function in a keyword parameter
@arnauorriols
arnauorriols / Encode.py
Created Feb 18, 2014
Python replacement for Encode.java used in OpenAM
View Encode.py
# Python replacement for Encode.java used in OpenAM
#
# Author: Arnau orriols
from hashlib import sha1
from base64 import b64encode
from sys import argv
def encode(string):
return b64encode(sha1(string).digest())