Skip to content

Instantly share code, notes, and snippets.

View mmasashi's full-sized avatar

Masashi mmasashi

View GitHub Profile
@mmasashi
mmasashi / test_patch.py
Last active August 22, 2023 12:33
Behavior of mock, patch, pytest mocker, examples
import contextlib
import pytest
from unittest.mock import patch
class A(object):
def say(self, msg):
return f'A say {msg!r}.'
@contextlib.contextmanager
@mmasashi
mmasashi / cprofile_template.py
Created March 29, 2019 19:27
Python cProfile template
import contextlib
import cProfile
import datetime
import io
import pstats
import time
def log(msg):
cur_datetime_str = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
@mmasashi
mmasashi / clean_install_high_sierra.md
Created February 18, 2018 20:12
Clean Install High Sierra (High Sierra のクリーンインストール)

Prepare for OS update (事前準備)

  • Backup data to Time machine drive
  • Backup some important data to HDD drive manually
  • Make a list which directory should be\ copied into the new OS
  • Export vicosity config into a file

Clean Install Sierra (High Sierra のクリーンインストール)

docker rmi $(docker images -q)
@mmasashi
mmasashi / cleanup_unused_docker_images.sh
Created October 5, 2017 18:43
Clean up docker images
#!/bin/bash
docker rmi $(docker images -q)
@mmasashi
mmasashi / setup_logging.py
Created May 9, 2017 08:14
Initialize logging for Flask
def setup_logging():
import logging
import os
from logging.handlers import RotatingFileHandler
from logging import Formatter
handler = RotatingFileHandler('log/flask.log', maxBytes=10000, backupCount=1)
process_id = os.getpid()
handler.setFormatter(Formatter(
#'[%(asctime)s] [%(levelname)s]: %(message)s [in %(pathname)s:%(lineno)d]' # for debug
'[%(asctime)s #' + str(process_id) + '] [%(levelname)s]: %(message)s'
@mmasashi
mmasashi / generate_sec_key.py
Created May 8, 2017 18:47
Generate a secret key in python
import os
os.urandom(24)
require 'msgpack'
require 'json'
def usage
puts <<EOT
usage: ruby msgpack_util.rb <option> <input-file-path> [<output-file-path>]
option:
-d, --decode: Decode msgpack formatted data into JSON formatted data
-e, --encode: Encode JSON formatted data into msgpack formatted data
EOT
@mmasashi
mmasashi / creaet_read_only_user.sql
Created November 14, 2016 20:24
Create read-only user on MySQL
CREATE USER r_only_user identified by 'password';
GRANT SELECT, SHOW VIEW, PROCESS, REPLICATION CLIENT ON *.* TO 'r_only_user'@'%' IDENTIFIED BY 'password';
GRANT SELECT, SHOW VIEW, PROCESS, REPLICATION CLIENT ON *.* TO 'r_only_user'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
@mmasashi
mmasashi / kill_old_redshift_sessions.rb
Created September 15, 2016 21:48
Script to kill old connections(sessions) on Redhsift
require 'pg'
require 'socket'
require 'pp'
TIMEOUT = 5
IDLE_CONN_TIMEOUT_MINUTES = 30 # 30 minutes
#IDLE_CONN_TIMEOUT_MINUTES = 1 # 1 minute for testing
CURRENT_SESSIONS_QUERY = "select * from stv_sessions where user_name = '%{user}' and starttime < CURRENT_TIMESTAMP - INTERVAL '%{timeout} minutes' order by starttime;"
KILL_CONNECTION_QUERY = "select pg_terminate_backend(%{pid})"