Skip to content

Instantly share code, notes, and snippets.

View jizhang's full-sized avatar

Jerry jizhang

View GitHub Profile
@jizhang
jizhang / crossdomain.py
Created July 19, 2012 16:29
simple socket server
import SocketServer
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
self.request.sendall("""
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy><site-control permitted-cross-domain-policies="all"/>
@jizhang
jizhang / md5.clj
Created December 18, 2012 07:14
Clojure - Calculate MD5 hash of a given string.
(import 'java.security.MessageDigest
'java.math.BigInteger)
(defn md5 [s]
(let [algorithm (MessageDigest/getInstance "MD5")
size (* 2 (.getDigestLength algorithm))
raw (.digest algorithm (.getBytes s))
sig (.toString (BigInteger. 1 raw) 16)
padding (apply str (repeat (- size (count sig)) "0"))]
(str padding sig)))
@jizhang
jizhang / 4clojure-85-power-set.clj
Created April 21, 2013 06:27
Write a function which generates the power set of a given set. The power set of a set x is the set of all subsets of x, including the empty set and x itself.
; 4clojure.com - 85. Power Set
; http://www.4clojure.com/problem/85
(fn [s]
(set (for [i (range 0 (apply * (repeat (count s) 2)))]
(set (for [index-base (range 0 (count s))
:let [index (apply * (repeat index-base 2))]
:when (pos? (bit-and i index))]
(nth (seq s) index-base))))))
@jizhang
jizhang / hadoop-count.clj
Last active December 16, 2015 16:49
Simple filter-count hadooop map-reduce job written in Java vs. Clojure.
(ns uv.raw
(:require [clojure-hadoop.gen :as gen]
[clojure-hadoop.imports :as imp])
(:import [org.apache.hadoop.util Tool]
[com.hadoop.mapreduce LzoTextInputFormat]))
(imp/import-conf)
(imp/import-fs)
(imp/import-io)
(imp/import-mapreduce)
@jizhang
jizhang / zabbix_image.py
Last active December 18, 2015 07:49
1. Login to Zabbix; 2. Fetch image; 3. Send email.
# -*- coding: utf-8 -*-
import json
import urllib2
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
data = {
@jizhang
jizhang / log4j.properties
Created September 25, 2013 01:20
basic log4j.properties
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c{1} - %m%n
@jizhang
jizhang / pom-package-plugin.xml
Last active December 25, 2015 13:59
A sample Maven configuration for packaging. I usually use assembly, but when it comes to spring framework, you have to use shade.
<!-- project / build / plugins/ plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
@jizhang
jizhang / wordstream.py
Created January 18, 2015 06:28
simple word stream
import SocketServer
import random
import time
words = ('cat', 'dog', 'monkey', 'horse', 'rabbit')
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
while True:
self.request.sendall(' '.join(random.sample(words, 3)) + '\n')
@jizhang
jizhang / JsonUtils.scala
Created January 26, 2015 02:53
json4s opt methods
import org.json4s._
object JsonUtils {
implicit class AugmentJValue(val jvalue: JValue) {
implicit val formats = DefaultFormats
def getString(key: String): String = {
@jizhang
jizhang / config
Last active January 3, 2017 12:40
GFW
# Linux
Host github.com
ProxyCommand socat STDIO PROXY:192.168.1.101:%h:%p,proxyport=8118
# Mac
Host github.com
ProxyCommand nc -X 5 -x 127.0.0.1:1086 %h %p
ServerAliveInterval 10