Skip to content

Instantly share code, notes, and snippets.

View KentaYamada's full-sized avatar
🏠
Working from home

yamaken KentaYamada

🏠
Working from home
View GitHub Profile
@KentaYamada
KentaYamada / sample-plpgsql.sql
Last active July 31, 2017 14:54
PL/PgSQL example
-- clean up.
drop table if exists persons;
drop function if exists entry_person(text, integer);
-- initialize.
create table persons (
name text
,age integer
);
@KentaYamada
KentaYamada / from_json.php
Created May 11, 2017 09:39
JSONファイルを読み込んで配列にして返す
<?php
function from_json($filename) {
if(!file_exists($filename)) {
throw new Exception($filename.' not found.');
}
$json_text = file_get_contents($filename);
if(!$json_text) {
throw new Exception('Failed read '.$filename);
}
$json = mb_convert_encoding($json_text, 'UTF8', 'ASCII, JIS, UTF-8, EUC-JP,SJIS-WIN');
@KentaYamada
KentaYamada / output.c
Created April 1, 2017 13:24
Output ASCII with Hex(C / C++)
#include <stdio.h>
int main(void)
{
int i = 0;
const unsigned char text[] = "test\0";
while(text[i] != '\0') {
printf("%02X\n", text[i]);
i++;
@KentaYamada
KentaYamada / copy_str.cpp
Created March 7, 2017 02:20
stringデータをstdを使ってコピー
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::string text = "abcde";
std::string copy_text = "";
std::copy(text.begin(), text.begin()+3, std::inserter(copy_text, copy_text.begin()));
@KentaYamada
KentaYamada / Testdata.sample.json
Last active February 14, 2017 03:53
Unit testデータサンプル
{
"test_save_ok": {
"expected": true,
"data": [
]
},
"test_save_ng": {
"expected": true,
"data": [
@KentaYamada
KentaYamada / array_to_2dimention_array.py
Created January 17, 2017 05:26
1次元配列から2次元配列へ変換する関数
def to_2dimentional_list(arr, row_no, col_no):
res = []
offset = 0
for row in range(0, row_no):
res.append(arr[offset:col_no+offset])
offset = offset + col_no
return res
a = [1,2,3,4,5,6]
@KentaYamada
KentaYamada / index.html
Created January 2, 2017 13:33
Python Flask + JavaScript XMLHttpRequest
<!DOCTYPE html>
<html>
<head>
<title>Practice AJAX</title>
<script type="text/javascript">
function do_ajax() {
var req = new XMLHttpRequest();
var result = document.getElementById('result');
req.onreadystatechange = function()
{
@KentaYamada
KentaYamada / GetBits.cpp
Created November 5, 2016 04:39
バイトデータからビットデータ取り出し
#include <iostream>
int main()
{
char c = 0x05; //Binary: 00000101
for(int i = 0; i <= 2; i++) {
std::cout << (unsigned int)((c >> i) & 0x01) << std::endl;
}
return 0;
}
@KentaYamada
KentaYamada / byte2str.cpp
Last active October 22, 2016 12:31
文字列からバイトデータに変換
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <string>
#include <vector>
typedef struct {
std::string text;
char delimiter;
} data_t;
@KentaYamada
KentaYamada / json_io.py
Last active June 4, 2017 12:17
json file I/O for python3
# -*-coding: utf-8 -*-
import os
import json
def read_json(filename):
if not os.path.isfile(filename):
raise FileExistsError("[error] {0} not found.".format(filename))
with open(filename) as f:
return json.load(f)