Skip to content

Instantly share code, notes, and snippets.

@pierric
pierric / lens.lhs
Last active October 21, 2020 11:44
lens magic
m = HashMap.fromList [
("X", HashMap.fromList [("a", 0), ("b", 1)]),
("Y", HashMap.fromList [("a", 1)]),
("Z", HashMap.fromList [("b", 2)])
]
I want to extract all value with key "a" in the nested HashMap, and keep the root key.
> m ^.. (itraversed <. at "a" . folded) . withIndex
@pierric
pierric / jq_search.sh
Created June 18, 2019 11:54
Find all node of some name, and deduplicated the values.
# list all the color node in the json file (recursively)
python -m jsmin <json_file> | jq '.. | .color? | select (. != null) | keys | .[]'
# list all and remove duplicated items.
python -m jsmin <json_file> | jq '[.. | .color? | select (. != null) | keys | .[]] | unique | .[]'
@pierric
pierric / replace_color.py
Created June 11, 2019 20:34
Replace all the black pixel to green.
arr = cv2.imread('img', -1)
arr[numpy.where((arr == [0,0,0]).all(axis=2))] = [0, 255, 0]
@pierric
pierric / mxnet_inference.py
Created December 30, 2018 22:10
Inference with saved model in mxnet
import mxnet as mx
import itertools
import numpy as np
from collections import namedtuple
ctx = mx.cpu()
def load(prefix):
symbol = mx.sym.load('%s.json' % prefix)
save_dict = mx.nd.load('%s.params' % prefix)
@pierric
pierric / dbpedia.py
Created April 16, 2018 13:05
Get a random article from the dbpedia
from SPARQLWrapper import SPARQLWrapper, JSON
import requests
from urllib.request import unquote
# PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
# PREFIX dbpprop: <http://dbpedia.org/property/>
# PREFIX dbres: <http://dbpedia.org/resource/>
#
# SELECT ?abstract WHERE {
# dbres:Abijah_Beckwith_(Wisconsin) dbpedia-owl:abstract ?abstract.
@pierric
pierric / foreach.sh
Last active April 12, 2018 14:03
Do some command for each address in the 'node' file
#! /bin/bash
includes=()
excludes=()
notMember () { for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 1; done; return 0; }
while getopts ":e:i:" opt; do
case $opt in
e)
@pierric
pierric / cabal-init-with-stack.md
Last active March 17, 2018 07:11
Call cabal init from stack env
jiasen@jiasen-home:~/Workspace/sandbox$ stack exec -- cabal init
cabal: Use of GHC's environment variable GHC_PACKAGE_PATH is incompatible with
Cabal. Use the flag --package-db to specify a package database (it can be used
multiple times).
jiasen@jiasen-home:~/Workspace/sandbox$ stack exec --no-ghc-package-path -- cabal init
Warning: The package list for 'hackage.haskell.org' does not exist. Run 'cabal
@pierric
pierric / etcdctl.sh
Last active March 13, 2018 09:37
check etcd status in a RKE k8s cluster
./etcdctl --endpoints "https://127.0.0.1:2379" \
--ca-file /etc/kubernetes/ssl/kube-ca.pem \
--cert-file /etc/kubernetes/ssl/kube-node.pem \
--key-file /etc/kubernetes/ssl/kube-node-key.pem \
cluster-health
@pierric
pierric / nvme ssd.md
Last active March 7, 2018 04:12
some troubleshooting when install linux with nvme ssd

turn off journal of ext4

sudo tune2fs -O ^has_journal $SSD_DRIVE

enable periodical trim

cp /usr/share/doc/util-linux/examples/fstrim.{service,timer} /etc/systemd/system
systemctl enable fstrim.timer
@pierric
pierric / reset-iptables.sh
Created February 15, 2018 14:41
reset iptables' rules.
iptables --policy INPUT ACCEPT;
iptables --policy OUTPUT ACCEPT;
iptables --policy FORWARD ACCEPT;
iptables -Z; # zero counters
iptables -F; # flush (delete) rules
iptables -X; # delete all extra chains
systemctl restart docker;