Skip to content

Instantly share code, notes, and snippets.

View aldanor's full-sized avatar

Ivan Smirnov aldanor

  • Dublin, Ireland
View GitHub Profile
@aldanor
aldanor / gist:4193087
Created December 3, 2012 06:14
canonical cards sorting / suit wildcards
def sort_cards(cards, mask_suits=False):
if isinstance(cards, str):
cards = cards.split(' ')
suits, ranks = 'cdhs', '23456789TJQKA'
ranks_rev = dict((ranks[i], i) for i in range(13))
cards = [(s, tuple(sorted(ranks_rev[c[0]] for c in cards
if c[1] is s))) for s in suits]
cards = [c for c in cards if c[1] != ()]
cards.sort(key=lambda c: (-len(c[1]), c[1]))
if mask_suits:
@aldanor
aldanor / pandas_hist_demo.py
Created December 5, 2012 06:27
Pandas histogram demo
import pandas as pd
def parse_to_df(files):
rank_values = dict(zip(tuple('23456789TJQKA'), range(13)))
records = []
for hand in h_iterators.HandsIterator(
hh_iterator=h_iterators.HandHistoryIterator(files)):
for i, card in enumerate(hand['hero']['cards'].split(' ')):
records.append({'card_rank': (rank_values[card[0]], card[0]),
# -*- coding: utf-8 -*-
from collections import namedtuple
Vertex = namedtuple('Vertex', 'id, edges')
Edge = namedtuple('Edge', 'edge_from, edge_to, label, weight')
def load_graph(filename):
with open(filename) as f:
@aldanor
aldanor / graphsearch.hs
Created May 22, 2013 21:57
obscure graph hackage
import Data.List (unfoldr, minimumBy)
import Data.Char (isSpace)
import Data.Function (on)
import Data.Maybe (mapMaybe, fromMaybe)
import System.Environment (getArgs)
import qualified Data.ByteString.Char8 as L
import qualified Data.IntMap as IM
type GraphData = (IM.IntMap (IM.IntMap [(Int, Int)]), Int)
@aldanor
aldanor / error_wrap.d
Created December 12, 2014 16:03
Template for return value based error checking
import std.traits;
import std.exception;
bool maybeError(T)(T v) {
static if (is(T == function) || is (T : void *))
return v is null;
else static if (isIntegral!T)
static if (isSigned!T)
return v < 0;
else
import std.traits;
import std.string;
import std.typetuple;
template ID(alias T) {
alias ID = T;
}
private bool _propertyNameMatches(alias parent, string suffix, alias name)() {
static if (!__traits(compiles, __traits(getMember, parent, name)))
@aldanor
aldanor / rc_proxy.d
Last active August 29, 2015 14:13
refcounted class proxy
import std.typecons;
import std.stdio;
import std.string;
import std.conv;
struct Box(T) if (is(T == class))
{
@disable this();
this(Args...)(Args args) {
@aldanor
aldanor / hdf5_mutex.rs
Last active August 29, 2015 14:13
hdf5 thread safety in rust
#[allow(unstable)]
extern crate libc;
#[allow(unstable)]
mod test {
use libc::c_int;
use std::sync::{StaticMutex, MUTEX_INIT};
static LOCK: StaticMutex = MUTEX_INIT;
use std::cell::RefCell;
use std::rc::Rc;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
#[derive(Debug)]
struct Handle {
id: Rc<RefCell<i32>>,
}
@aldanor
aldanor / search-hashing.callgrind.rs
Last active September 11, 2015 21:32
search-hashing.callgrind.log
--------------------------------------------------------------------------------
Ir
--------------------------------------------------------------------------------
158,791,478,749 PROGRAM TOTALS
--------------------------------------------------------------------------------
Ir file:function
--------------------------------------------------------------------------------
10,231,292,690 collections::hash::map::robin_hood
9,273,922,532 collections::hash::table::RawBucket<_>::offset