Skip to content

Instantly share code, notes, and snippets.

View hideshi's full-sized avatar

Hideshi Ogoshi hideshi

View GitHub Profile
@hideshi
hideshi / doctest_sample.py
Last active January 1, 2016 10:49
Python standard library Doctest sample.
def three_times(num):
"""
>>> three_times(-1)
-3
>>> three_times(0)
0
>>> three_times(1)
3
>>> three_times(3)
9
@hideshi
hideshi / jobnet.xml
Created December 24, 2013 15:35
Simple job scheduler with Apache Ant.
<?xml version="1.0" ?>
<project default="main" basedir="/path/to">
<target name="main" depends="begin, A0001, B0001, C0001, end">
</target>
<target name="begin">
<echo>
Begin execute jobnet.
</echo>
</target>
@hideshi
hideshi / show_process_id.py
Created December 8, 2013 13:44
You can get process id with keyword.
from subprocess import *
import sys, re
keyword = sys.argv[1]
(stdo, stde) = Popen(['ps'], stdout=PIPE).communicate()
lines = [j for i,j in enumerate(stdo.decode().split('\n')) if i > 0]
r = re.compile(r'\s+')
print('keyword', keyword)
for line in lines:
l = r.split(line.strip())
if len(l) > 1 and l[3].find(keyword) >= 0:
@hideshi
hideshi / make_insert.py
Created December 8, 2013 13:43
Script for making insert statement.
# coding: utf-8
from sys import argv
f = open(argv[1], 'r')
table_name = f.readline().strip()
col_names = map(lambda x: x.strip(), f.readline().split(','))
types = map(lambda x: x.strip(), f.readline().split(','))
def func(t):
if t[1] == 's':
return (t[0], "'" + t[2] + "'")
@hideshi
hideshi / ltsv_parser.awk
Created December 8, 2013 13:41
Script for parsing LTSV formatted log.
$ cat ltsv.log
host:127.0.0.1 user:- time:[08/Feb/2013:14:17:53 +0900] req:GET / HTTP/1.1 status:200 size:6 referer:- ua:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 taken:41
host:127.0.0.1 user:- time:[08/Feb/2013:14:17:53 +0900] req:GET /favicon.ico HTTP/1.1 status:404 size:6 referer:- ua:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 taken:36
$ awk -F \t '{print "---";for(i=1;i<=NF;i++) {match($i,":");print substr($i,0,RSTART-1) "\t", substr($i,RSTART+1);}}' ltsv.log
$ awk -F \t '/status:404/ {print "---";for(i=1;i<=NF;i++) {match($i,":");print substr($i,0,RSTART-1) "\t", substr($i,RSTART+1);}}' ltsv.log
@hideshi
hideshi / delete_file.py
Created December 8, 2013 13:28
If you want to delete file which was passed for certain days since it was modified last time, you can use this script.
import sys
import datetime
import os
# Get arguments without file name.
args = sys.argv[1:]
# Get current date time.
now = datetime.date.today()
for i in args:
@hideshi
hideshi / sqlite3_console.py
Last active October 22, 2023 04:15
Simple SQLite3 console. You can use DML (SELECT, INSERT, UPDATE, DELETE).
from cmd import Cmd
from sys import argv
import sqlite3
import traceback
class SqliteCmd(Cmd):
dml = ['SELECT', 'INSERT', 'UPDATE', 'DELETE']
def __init__(self, dbfile):
super().__init__()
self.conn = sqlite3.connect(dbfile)
@hideshi
hideshi / cat_grep.py
Created December 8, 2013 13:09
Show file and filter by keyword like cat and grep.
import sys
import os
def main(args):
directory = args[1]
if len(args) > 2:
keyword = args[2]
else:
keyword = ''
file_list = os.listdir(directory)
@hideshi
hideshi / sqlite_sample.py
Created December 8, 2013 13:08
How to use SQLite3 in Python3.
import sqlite3
conn = sqlite3.connect("sqlite3.db")
sql = "drop table test"
conn.execute(sql)
sql = """create table test (
key int,
value varchar(20)
)"""
@hideshi
hideshi / twitter_post.py
Last active December 30, 2015 16:49
How to post message to your twitter account through twitter api.
from twitter import Twitter, OAuth, TwitterHTTPError
# If you have twitter account,
# Register your app on https://dev.twitter.com so that you can get these information.
OAUTH_TOKEN = 'xxxx'
OAUTH_SECRET = 'xxxx'
CONSUMER_KEY = 'xxxx'
CONSUMER_SECRET = 'xxxx'
api = Twitter(auth = OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET))