Skip to content

Instantly share code, notes, and snippets.

@Martin91
Martin91 / mime_types.json
Created July 20, 2018 02:40
incomplete MIME types
// refer to https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
{
".aac": "audio/aac",
".abw": "application/x-abiword",
".arc": "application/octet-stream",
".avi": "video/x-msvideo",
".azw": "application/vnd.amazon.ebook",
".bin": "application/octet-stream",
".bmp": "image/bmp",
".bz": "application/x-bzip",
@Martin91
Martin91 / paramsconvert.py
Last active May 17, 2018 10:05
param convert according to json schema in python
from lib.errors import TypeConvertInvalid
SIMPLE_VALUE_TYPES = [str, int, float]
BOOLABLE_TYPES = SIMPLE_VALUE_TYPES[:]
BOOLABLE_TYPES.append(bool)
def convert(value, convert_func, parse_path, convertable_types=SIMPLE_VALUE_TYPES):
if type(value) in convertable_types:
try:
return convert_func(value)
@Martin91
Martin91 / dict_sort.py
Created May 8, 2018 14:44
python sort dictionary with value in desc order
import collections
mydict = {
'a': 49023,
'b': 39201,
'c': 49021,
'd': 90201,
'e': 49012,
'f': 49401,
'g': 10039,
@Martin91
Martin91 / omg-git.git-show.sh
Last active November 20, 2017 16:05
search sensitive words in git history
#!/usr/bin/env bash
NOT_GIT_REPO=1
if $(git rev-list --all &>/dev/null)
then
echo "Detect git repo, continue now..."
else
echo "Could not find any git repo here!"
exit $NOT_GIT_REPO
@Martin91
Martin91 / hash_addition.rb
Created June 2, 2017 02:42
ruby hashes addition
hash1 = {a: 100, b: [1, 2], c: "hello"}
hash2 = {a: 300, b: [3, 4, 5], c: " world", d: "fh"}
hash1.merge(hash2) { |key, val_f, val_r| val_f + val_r }
# => {:a=>400, :b=>[1, 2, 3, 4, 5], :c=>"hello world", :d=>"fh"}
@Martin91
Martin91 / application.rb
Created March 10, 2017 07:49
rails application decorator mechanism
config.to_prepare do
# Load application's model / class decorators
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
@Martin91
Martin91 / factory_girl_model_generator_decorator.rb
Last active March 2, 2017 03:08
FactoryGirl model generator to support automatically add columns
# generate factory with columns for model
#
# $ bundle exec rails g factory_girl:model User
require 'factory_girl'
require 'generators/factory_girl/model/model_generator'
FactoryGirl::Generators::ModelGenerator.class_eval do
alias :command_line_attributes :attributes
@Martin91
Martin91 / install_nokogiri.sh
Created March 1, 2017 00:59
install nokogiri on mac os
gem install nokogiri -v '1.7.0.1' -- --with-xml2-include=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libxml2 --use-system-librarie
@Martin91
Martin91 / hotp.rb
Last active January 29, 2022 14:16
OTP algorithms in Ruby
require 'openssl'
def hotp(secret, counter, digits = 6)
hash = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), secret, int_to_bytestring(counter))
"%0#{digits}i" % (truncate(hash) % 10**digits)
end
def truncate(string)
offset = string.bytes.last & 0xf
partial = string.bytes[offset..offset+3]
@Martin91
Martin91 / explain_sqls.sql
Last active March 2, 2020 14:53
MySQL ORDER BY primary key which is not in WHERE CLAUSE performs slowly
/* use condition > */
mysql> EXPLAIN SELECT * FROM test WHERE `test`.`activeday` > 10000 AND `test`.`deleted` = 0 ORDER BY id ASC LIMIT 20\G;
*************************** 1. row ***************************
possible_keys: index_on_test_to_activeday
key: PRIMARY
key_len: 4
rows: 40
filtered: 5.00
Extra: Using where