Skip to content

Instantly share code, notes, and snippets.

View naufraghi's full-sized avatar

Matteo Bertini naufraghi

View GitHub Profile
@naufraghi
naufraghi / kmeans.py
Created June 29, 2010 15:47
kmeans algorithm in python + iris dataset (naive implementation)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010 Matteo Bertini <matteo@naufraghi.net>
# Licensed as http://creativecommons.org/licenses/BSD/
#
# This is a naive implementation of the k-means unsupervised clustering
# algorithm (http://en.wikipedia.org/wiki/K-means_clustering).
from __future__ import division
import sys
@naufraghi
naufraghi / sqlitedict.py
Created November 3, 2010 14:36
A persistent (sqlite3) dictionary
"""
A persistent (sqlite3) python dictionary
Based on:
http://erezsh.wordpress.com/2009/05/31/filedict-bug-fixes-and-updates/
Author: Erez Shinan
Date: 31-May-2009
Copyright 2010 Matteo Bertini <matteo@naufraghi.net>
Python Software Foundation License (PSFL)
@naufraghi
naufraghi / updatenx.py
Created November 24, 2010 12:10
Download latest nomachine nx debian packages
#!/usr/bin/python
import os
import sys
import urllib
import re
import glob
import platform
@naufraghi
naufraghi / as197.f
Created January 23, 2011 11:27
AS 197 A Fast Algorithm for the Exact Likelihood of Autoregressive-Moving Average Models (Melard 1984) [f2py]
SUBROUTINE FLIKAM(P,MP,Q,MQ,W,E,N,SUMSQ,FACT,VW,VL,
* MRP1,VK,MR,TOLER,IFAULT)
IMPLICIT DOUBLE PRECISION(A-H,O-Z)
C
Cf2py intent(in) P
Cf2py intent(hide) MP
Cf2py intent(in) Q
Cf2py intent(hide) MQ
Cf2py intent(in) W
Cf2py intent(out) E
@naufraghi
naufraghi / arima.c
Created April 9, 2011 10:20
R stats/arima.c
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 2002-2006 The R Development Core Team.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
@naufraghi
naufraghi / is_regular_file.py
Created May 10, 2011 09:29
Detect [non] regular files in python
#!/usr/bin/env python
"""
$ echo "ciao" | python is_regular_file.py - <(cat comments.vim) comments.vim
'<stdin>' is not a regular file
'/dev/fd/63' is not a regular file
'comments.vim' is a regular file
"""
import os
import sys
@naufraghi
naufraghi / ignore2recall.py
Created May 15, 2011 15:55
Convert .hgignore files to a local recoll skipped[Names|Paths] lines
import os
import sys
from collections import defaultdict
def hgignore2skipped(filename):
localdir = os.path.dirname(os.path.abspath(os.path.expanduser(filename)))
def _iter_skipped():
with open(filename) as lines:
for line in (l.strip() for l in lines):
if line.startswith("syntax:") or line.startswith("#") or not line:
@naufraghi
naufraghi / git-svn-utils.sh
Created August 12, 2011 09:20
git svn outgoing | rebase-all | dcommit
git-svn-outgoing() {
need_stash=$(git st --por | cut -c 1-2 | grep M | cat)
if [ "$need_stash" ]; then
git stash > /dev/null
fi
git svn dcommit -n | grep diff-tree | while read line; do git show $(echo $line | cut -d' ' -f 3); done
if [ "$need_stash" ]; then
git stash pop > /dev/null
fi
}
@naufraghi
naufraghi / find_implementing_classes.py
Created August 12, 2011 15:33
Given a method return all the classes the method is implemented in
def find_implementing_classes(method):
"""
Given a method return all the classes the method is implemented in.
a.aaa() -> A.aaa
a.bbb() -> A.bbb
b.aaa() -> A.aaa
b.bbb() -> B.bbb
A.aaa == B.aaa -> True
A.bbb == B.bbb -> False
find_implementing_classes(b.aaa) -> [<class '__main__.A'>]
@naufraghi
naufraghi / MyImageView.java
Created December 15, 2012 15:38
Move and zoom image with Gestures in and android View
package com.develer.circularsliderule;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;