Skip to content

Instantly share code, notes, and snippets.

View tai2's full-sized avatar
🐢

Taiju Muto tai2

🐢
View GitHub Profile
@tai2
tai2 / gist:1104424
Created July 25, 2011 15:45
enumerate all subsequences in a sequence
# enumerate all subsequences in a sequence
def subsequences(s):
for i in range(len(s)):
for j in range(len(s) - i):
yield s[j:j + i + 1]
if __name__ == '__main__':
assert list(subsequences([])) == []
assert list(subsequences([1])) == [[1]]
@tai2
tai2 / gist:1104429
Created July 25, 2011 15:46
UPnP Port Mapper
# UPnP Port Mapper
import os
import sys
import re
import urllib2
from xml import sax
from xml.sax import handler
from xml.sax.handler import feature_namespaces
import socket
@tai2
tai2 / gist:1104608
Created July 25, 2011 17:13
connect system call with blocking timeout.
// connect system call with blocking timeout.
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
@tai2
tai2 / Unzip.java
Created November 2, 2011 10:16
Unzip an archive.
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Unzip {
@tai2
tai2 / gist:1604340
Created January 13, 2012 02:54
Remove a file or directory including its contents recursively
import java.io.File;
import java.io.IOException;
public class Rmr {
public static void rmr(String path) {
File target = new File(path);
rmr(target);
}
@tai2
tai2 / gist:1670883
Created January 24, 2012 16:11
TypedValue#applyDimension
public static float applyDimension(int unit, float value,
DisplayMetrics metrics)
{
switch (unit) {
case COMPLEX_UNIT_PX:
return value;
case COMPLEX_UNIT_DIP:
return value * metrics.density;
case COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
@tai2
tai2 / ZipCursor.java
Created March 26, 2012 01:22
Merge cursors horizontally. Combining cursor A which has column (a1, a2) and B which has column (b1, b2) results a new cursor which has column (a1, a2, b1, b2).
import android.database.AbstractCursor;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
public class ZipCursor extends AbstractCursor {
private DataSetObserver default_observer = new DataSetObserver() {
@Override
@tai2
tai2 / SplitCursor.java
Created March 26, 2012 03:03
Split one cursor into multiple columns. A single column cursor (a1, a2, a3, a4) split into a double column cursor ((a1, a2), (a3, a4)).
import android.database.AbstractCursor;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
public class SplitCursor extends AbstractCursor {
private DataSetObserver default_observer = new DataSetObserver() {
@Override
public void onChanged() {
@tai2
tai2 / abspath.sh
Last active October 4, 2015 04:37
echo self absolute pth
#!/bin/sh
THIS=${0#$(dirname $0)/}
echo $PWD/$THIS
@tai2
tai2 / gist:2836138
Created May 30, 2012 12:56
Test wheter an activity is the main or not.
private boolean isMain() {
boolean result = false;
Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
List<ResolveInfo> info = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (!info.isEmpty()) {
result = info.get(0).activityInfo.name.equals(getComponentName().getClassName());
}
return result;