Skip to content

Instantly share code, notes, and snippets.

View hideshi's full-sized avatar

Hideshi Ogoshi hideshi

View GitHub Profile
@hideshi
hideshi / here_document.py
Last active December 30, 2015 16:49
You can use here document like shell script.
html = '''
<html>
<body>
Hello world
</body>
</html>
'''
for i in html.split('\n'):
if i.find('bo') > 0:
@hideshi
hideshi / log_rotate.py
Created December 8, 2013 13:05
Log rotation script. This script would be called by cron at fixed intervals.
from os.path import getsize
from os import rename
from datetime import datetime
srcnames = ['./logfile1', './logfile2']
kb = 1024
mb = kb * 1024
def log_rotate():
@hideshi
hideshi / debug_log.py
Created December 8, 2013 13:06
The most simple way to write debug log.
debug = True
print('debug1') if debug else ''
debug = False
print('debug2') if debug else ''
@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))
@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 / 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 / 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 / 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 / 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 / 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] + "'")