Skip to content

Instantly share code, notes, and snippets.

@upsuper
upsuper / gist:5351509
Created April 10, 2013 03:22
Simplify IP segments by removing redundant segments and merging adjacent segments.
# coding: UTF-8
import socket
import struct
def aton(addr):
return struct.unpack('!l', socket.inet_aton(addr))
def ntoa(addr):
return socket.inet_ntoa(struct.pack('!l', addr))
@upsuper
upsuper / gist:5069549
Last active December 14, 2015 10:09
Counting non-printable characters in .js and .css files.
class FileInfo
def initialize(name, data)
@name = name
@length = data.length
@space = data.each_char.inject(0) { |n, c|
n += case c
when '\t' then 8
when /\s/ then 1
else 0
end
@upsuper
upsuper / mkramdisk
Created January 6, 2013 02:30
Script for OS X to create ramdisk & mount to a given place. This script is also enabled to resize the ramdisk it creates by mounting a new one and copying files into it.
#!/bin/zsh
INITIAL_SIZE=5
VOLUME_NAME='ramdisk'
MOUNTPOINT='/tmp/upsuper'
MOUNTPOINT_FILE='/tmp/ramdisk.mountpoint'
# find a non-existent initial mount point
mountpoint=$(mktemp -dt ramdisk)
# set ramdisk size
@upsuper
upsuper / corestatus.sh
Created December 28, 2012 04:43
Switch number of CPU cores in Linux
#!/bin/bash
cat /proc/cpuinfo | grep processor | wc -l
@upsuper
upsuper / sendkeys.py
Created December 23, 2011 14:04
Send keys to clipboard or input directly (now only for Mac OS X and partly Linux)
# - * - coding: UTF-8 - * -
import subprocess
def macosx_set_clipboard(text):
p = subprocess.Popen('pbcopy', stdin=subprocess.PIPE)
p.communicate(text)
def macosx_send_keys(text):
text = text.replace('\\', '\\\\').replace('"', '\\"')
@upsuper
upsuper / af_alternate.c
Created October 12, 2012 16:27
Interesting Audio Filters for MPlayer
/*
* Copyright (C) 2012 Xidorn Quan <quanxunzhen@gmail.com>
*
* This file is part of MPlayer.
*
* MPlayer 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.
*
@upsuper
upsuper / Makefile
Created August 22, 2012 09:23
"xor rax, rax" vs. "mov rax, 0"
all: mov xor
mov: mov.o
ld -o mov mov.o
xor: xor.o
ld -o xor xor.o
mov.o: mov.asm
yasm -f macho64 -o mov.o mov.asm
@upsuper
upsuper / update_self.sh
Created May 29, 2012 08:17
将 DNSPod 上指定子域名指向当前本机 IP 的脚本
#!/bin/bash
# 将 DNSPOD 上指定子域名指向当前本机 IP 的脚本
#
# 依赖:
# urlencode: http://www.shelldorado.com/scripts/cmds/urlencode
# jsawk: https://github.com/micha/jsawk
# 配置信息
EMAIL=""
@upsuper
upsuper / gist:2633348
Created May 8, 2012 07:51
HTML5 Canvas demo with lines smoothed using Bézier curve
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title></title>
<style type="text/css">
canvas { position: absolute; top: 0; left: 0; }
#c { background: #ccc; }
#ps { position: absolute; top: 0; right: 0; }
@upsuper
upsuper / mail.func.php
Created April 17, 2012 06:43
检验是否为有效的电子邮件地址的 PHP 函数
<?php
/**
* 检验是否为有效的电子邮件地址
*
* 根据 http://www.linuxjournal.com/article/9585 编写
*
* @param string $email
* @return bool
*/