Skip to content

Instantly share code, notes, and snippets.

@pgtwitter
pgtwitter / gist:c03c2572742c4931c41e
Created February 22, 2015 18:21
任意のカラムだけ抜き出したCSVファイルのバイト列を作る (OpenCSVを用いた)
//import au.com.bytecode.opencsv.CSVReader;
//import au.com.bytecode.opencsv.CSVWriter;
String encoding= "UTF-8";
Reader in = new InputStreamReader(new FileInputStream("/path/to/csvfile.csv"), encoding);
ByteArrayOutputStream ret = new ByteArrayOutputStream();
Writer out = new OutputStreamWriter(ret, encoding);
CSVWriter writer = new CSVWriter(out, ',', '"', '"', "\n");
CSVReader reader = new CSVReader(in);
@pgtwitter
pgtwitter / gist:ac0865d1a120e5bef0e3
Created February 22, 2015 18:25
setLocalInfileInputStraemを用いたLOAD DATA LOCAL INFILE (MySQL) (参考 http://d.hatena.ne.jp/sh2/20090528 )
byte[] buf= {};
Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASS);
//conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
String sql = "LOAD DATA LOCAL INFILE '' INTO TABLE thetable"
+ " FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\"'"
+ " LINES TERMINATED BY '\n'";
InputStream is = new ByteArrayInputStream(buf);
((com.mysql.jdbc.Statement) stmt).setLocalInfileInputStream(is);
stmt.execute(sql);
@pgtwitter
pgtwitter / gist:af5c6fb1b34220a638d6
Last active September 4, 2015 01:05
MailのSubjectをdecodeする
from email.parser import Parser
from email.header import decode_header
fp = open("/path/to/mail.eml", "rb")
parser = Parser()
message = parser.parse(fp)
fp.close()
strs = []
for pair in decode_header(message['Subject']):
@pgtwitter
pgtwitter / nasne_UsedVolumeSizeRatio.sh
Last active September 21, 2015 15:19
Used Volume Size Ratio of NASNE (on Mac OS X / マウント済みのケース)
#!/bin/bash
NAME=`smbutil statshares -a | awk 'BEGIN{F=0}/^nasne_home/{F=1}F==1 && /SERVER_NAME/ {print $2;exit}'`
IPADDR=`smbutil lookup ${NAME} | awk '/^IP address of /{print $NF}'`
URL="http://${IPADDR}:64210/status/HDDInfoGet?id=0"
curl -s ${URL} \
| awk '{gsub("[,}]", "\n");print $0}' \
| awk '/usedVolumeSize/{u=$NF}/totalVolumeSize/{t=$NF}END{printf "%.2f%%\n", u/t*100}'
@pgtwitter
pgtwitter / XML2Hashtable.java
Created October 21, 2015 19:00
XML String to Hashtable
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
@pgtwitter
pgtwitter / XPathSample.java
Last active October 21, 2015 19:17
XPath Sample
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
@pgtwitter
pgtwitter / Server.java
Last active October 29, 2015 02:00
SocketServer Template
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server extends Thread {
private static final int PORT = 8080;
@pgtwitter
pgtwitter / XOR.py
Created November 7, 2015 20:17
chainer で XOR
#! /usr/bin/env python
#encoding: utf-8
import numpy as np
import chainer.functions as F
from chainer import FunctionSet, Variable, optimizers
model= FunctionSet(
l1 = F.Linear(2, 2),
l2 = F.Linear(2, 1)
)
@pgtwitter
pgtwitter / XOR2.py
Created November 8, 2015 02:07
chainer で XOR の 出力 の 推移 を ffmpeg で mp4 にする
#! /usr/bin/env python
#encoding: utf-8
import numpy as np
import chainer.functions as F
from chainer import FunctionSet, Variable, optimizers
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
@pgtwitter
pgtwitter / XOR3.py
Created November 8, 2015 05:09
chainer で XOR の 出力 の 推移 を ffmpeg で mp4 にする (subplot有り)
#! /usr/bin/env python
#encoding: utf-8
import numpy as np
import chainer.functions as F
from chainer import FunctionSet, Variable, optimizers
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ims = []