Skip to content

Instantly share code, notes, and snippets.

View YanhaoYang's full-sized avatar

Yanhao Yang YanhaoYang

  • Berlin, Germany
View GitHub Profile
@YanhaoYang
YanhaoYang / rename-files.sh
Created September 10, 2017 06:47
Rename files by index
#!/bin/bash
files="*.mp4"
regexp="([0-9]+).mp4"
for i in $files
do
if [[ $i =~ $regexp ]]
then
idx="${BASH_REMATCH[1]}"
name=$(printf "%02d" $idx)
@YanhaoYang
YanhaoYang / and-import.rb
Last active February 12, 2018 15:30
Export data from PosgreSQL db in batch with pg gem
require 'pg'
conn = PG::Connection.open ENV['DATABASE_URL']
Dir.glob("tmp/exports/*.copy").sort.each do |f|
puts "#{Time.now} - batch #{f} ..."
rs = conn.copy_data "COPY some_table FROM STDOUT" do
File.open(f).each_line do |ln|
conn.put_copy_data ln
end
@YanhaoYang
YanhaoYang / sql-counts.sql
Last active February 21, 2018 15:02
SQL `count(1)` vs `count(*)` vs `count(id)`: only `count(id)` does not count `NULL` rows
CREATE TABLE categories ( id integer );
CREATE TABLE products ( id integer, cat_id integer );
INSERT INTO categories(id) VALUES(1);
INSERT INTO categories(id) VALUES(2);
INSERT INTO products(id, cat_id) VALUES(1, 1);
SELECT c.id, count(1) FROM categories c
@YanhaoYang
YanhaoYang / mock_indirectly_test.py
Last active April 18, 2018 07:20
Mock function indirectly with pytest
def test_something(mocker):
session = requests.Session()
resp = requests.Response()
resp._content = "response_body"
resp.status_code = 200
resp2 = requests.Response()
resp2._content = '{"results":[]}'.encode('utf-8')
resp2.status_code = 200
mocker.patch.object(session, 'get', side_effect=[resp, resp2])
mocker.patch('requests.Session', return_value=session)
@YanhaoYang
YanhaoYang / Finding local IP addresses using Python's stdlib.py
Created May 8, 2018 09:19
Finding local IP addresses using Python's stdlib
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
print(s.getsockname()[0])
s.close()
# https://stackoverflow.com/a/166589
@YanhaoYang
YanhaoYang / function.py
Last active May 8, 2018 11:38
Compare some details between Ruby and Python
>>> def hi():
... def h2():
... print('h2')
... h2()
...
>>> hi()
h2
>>> h2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
@YanhaoYang
YanhaoYang / docker-compose.yml
Created July 17, 2018 11:06
Docker compose with ssh-agent
version: '2'
services:
postgres:
image: postgres:9.6
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: a
@YanhaoYang
YanhaoYang / queries.sql
Created July 26, 2018 12:29
Some useful queries
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_relation_size(C.oid) DESC
LIMIT 200 -- bigtables;
SELECT
pg_stat_activity.pid,
@YanhaoYang
YanhaoYang / puma.rb
Created October 19, 2018 18:41
Print the call stacks in a multithread application, say puma, to know which threads are blocked
before_fork do
trap 'TTIN' do
Thread.list.each do |thread|
puts "\nThread TID-#{thread.object_id.to_s(36)}"
puts thread.backtrace.join("\n")
puts "\n"
end
end
end
@YanhaoYang
YanhaoYang / notes-for-vim.vim
Last active April 13, 2019 09:41
Some notes for vim
" Copy from Vim to clipboard
vmap <leader>x :w! ~/.vbuf<cr>:!cat ~/.vbuf \| xclip -in -sel clip<cr><cr>
" Strip rspec outputs
:%s/rspec \(.\{-}\) #.\+/\1 \\/gc