Skip to content

Instantly share code, notes, and snippets.

View bxt's full-sized avatar

Bernhard Häussner bxt

View GitHub Profile
@bxt
bxt / once.js
Created September 8, 2012 08:38
JavaScript の once よくつかうので (I actually don't use it that often)
function once (fn) {
var done = false, result;
function _once_ () {
if(!done) {
result = fn.apply(this,Array.prototype.slice.call(arguments))
done = true;
}
return result;
}
return _once_;
@bxt
bxt / webmsg.py
Created August 27, 2012 19:11
Really simple python web server to serve a maintenance message to the web
import string,cgi,time, datetime
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
self.send_response(503) # let bots know whats up
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write('<!DOCTYPE html>\n<meta charset=utf-8 />\n<title>Notification page</title>\n')
@bxt
bxt / hextime.sh
Created July 22, 2012 13:18
Print the current time as 4 byte hex timestamp
#!/bin/bash
printf '0x%08x' $(date +%s)
@bxt
bxt / Util.java
Created March 25, 2012 12:31
Java utility class for comparable and collections
package de.bxt.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@bxt
bxt / bottom.java
Created March 22, 2012 09:43
Java generic "bottom" value
public abstract class Bottom {
public static <E> E get() {
return null;
}
}
@bxt
bxt / md2html
Created November 10, 2011 23:50
Bash script for invoking MarkdownJ to convert Markdown to HTML on CLI
#!/bin/bash
usestdi=off
filename=$1
if [ $1 = "-h" ] ; then
echo "Usage: $0 mdfile [outfile]"; exit
fi
if [ $1 = "-" ] ; then
usestdi=on
@bxt
bxt / filename-utils.js
Created October 12, 2011 20:46
Javascript batch processing filename utilities
/**
* Return something as filename-save string.
*
* Result will contain only 0-9, a-z and "-".
* It converts german umlauts, you might want
* to add some conversions for your target
* language.
*/
function filesc(str) {
@bxt
bxt / percentage-mysql-avg.sql
Created September 29, 2011 17:57
MySQL - Calculate percentage of rows/records matching predicate using IF() and AVG
-- Tip: You can use IF() and AVG() together to get a percentage value
-- without doing subquerys
-- -
-- Basic usage:
-- SELECT AVG(IF( predicate ,100,0)) as percentage FROM table
-- Example:
-- -
SELECT
AVG(IF(f.mid,100,0)) as `percentage filed`
FROM
@bxt
bxt / jquery-eventhacks.js
Created September 24, 2011 21:33
jQuery binding event handlers to non-DOM objects Javascipt code examples
// This is a little demo File on how to bind event listeners
// to own, non-DOM objects with jQuery.
// Since calling the default action is calling the identically
// named methods of our custom action and that would usually
// mean triggering the event again we use this handy callback:
function preventSelfcallCallback (event) {
event.preventDefault();
}
@bxt
bxt / csv-export.sh
Created September 13, 2011 20:58
Exporting whole MySQL databse to CSV files
#!/bin/bash
# USAGE: Will create a .tar.gz with CSVs of all tables in schema.
# Configure below and run as root (i.e. the user mysql runs as)
#
# The script will (or should) SELECT * INTO OUTFILE your tables
# and save them into csv files under /tmp dir first, then name them
# like the tables and move them thogether into a directory. Then
# it will tar everything together and chown you the tarball.
# Schema to export: