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 / 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 / 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 / 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 / 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 / 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 / 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 / array-of-different-types.go
Created August 23, 2017 10:28
Array of different types
package main
import (
"fmt"
)
type A struct{}
func (A) hi() string {
return "hi A"
@YanhaoYang
YanhaoYang / rc.local
Created July 14, 2017 01:30
/etc/rc.local for Mint on Mac
# fix keyboard
echo 0 > /sys/module/hid_apple/parameters/iso_layout
# prevent wakeup from usb
echo XHC1 > /proc/acpi/wakeup
exit 0
@YanhaoYang
YanhaoYang / golang-polymorphism.go
Created July 5, 2017 07:22
golang-polymorphism
// https://play.golang.org/p/CpPW7CxYBV
package main
import (
"fmt"
)
type A struct {
X string
@YanhaoYang
YanhaoYang / bm-slice.go
Created June 27, 2017 09:38
[Golang] make a slice and populate it by index or with `append`?
package main
import (
"fmt"
"testing"
)
func BenchmarkAppendFloat(b *testing.B) {
benchmarks := []int{10, 100, 1000, 10000, 1e5}
for _, bm := range benchmarks {