Skip to content

Instantly share code, notes, and snippets.

View dyno's full-sized avatar
🏠
Working from home

Dyno Fu dyno

🏠
Working from home
View GitHub Profile
@dyno
dyno / build.gradle
Created September 4, 2020 18:00
Make intelliJ to resolve python symbols
// https://www.jetbrains.com/help/idea/content-roots.html
// https://intellij-support.jetbrains.com/hc/en-us/community/posts/360003198659-PyCharm-Fails-to-resolve-references-to-installed-modules-and-even-built-ins
sourceSets {
main {
resources {
srcDir file('.venv/lib/python3.6/site-packages')
}
}
}
@dyno
dyno / peotry_vs_pipenv.sh
Last active April 27, 2020 04:55
poetry vs pipenv
# install development packages
pipenv install --dev
poetry install
# install packages without development
pipenv install
poetry install --no-dev
# shell into virtualenv
pipenv shell
@dyno
dyno / Makefile
Created January 7, 2020 00:21
Makefile to bootstrap ammonite scala shell
SHELL = /bin/bash
# https://get-coursier.io/docs/cli-launch.html#java-options
define AMM_SCRIPT
#!/bin/bash
command -v scala &>/dev/null || {
echo "cannot execute scala. is it installed?"
exit 1
}
@dyno
dyno / load_spark_jars.scala
Created January 6, 2020 23:59
AmmoniteSparkSession adds SPARK_DIST_CLASSPATH jars
/**
* ## Load jars ##
*/
import ammonite.ops._
// e.g. /opt/.ivy2/local/sh.almond/ammonite-spark_2.12/0.8.0+16-fc59944f-SNAPSHOT/jars/ammonite-spark_2.12.jar => ammonite-spark
val toPackageName = (jarPath: Path) => jarPath.baseName.split("-").dropRight(1).mkString("-")
// { "[0-9]$".r findFirstIn _._2.baseName isEmpty }
val jarFilter = (jarPath:Path) => jarPath.ext == "jar" && !List("-tests", "-javadoc", "-sources", "jre7", "empty-to-avoid-conflict-with-guava").exists(jarPath.baseName.endsWith)
@dyno
dyno / cattrs_serde.py
Created December 26, 2019 21:56
demonstrate the incompleteness of serialize/deserialize solutions in attrs
#!/usr/bin/env python3
import attr
import cattr
from attrs_serde import serde
name_path = ["contact", "personal", "Name"]
phone_path = ["contact", "Phone"]
@dyno
dyno / plantuml_server_url_decode.py
Created December 20, 2019 08:10
plantuml server url decoder encoder
# https://plantuml.com/text-encoding
# https://github.com/dougn/python-plantuml/blob/master/plantuml.py#L64
import zlib
import base64
maketrans = bytes.maketrans
plantuml_alphabet = string.digits + string.ascii_uppercase + string.ascii_lowercase + '-_'
base64_alphabet = string.ascii_uppercase + string.ascii_lowercase + string.digits + '+/'
@dyno
dyno / nginx.conf
Created October 15, 2019 16:50
put streamlit/jupyterlab behind nginx
# for fancyindex
include /etc/nginx/modules-enabled/*.conf;
# https://github.com/alibaba/tengine/issues/818
events {
}
http {
map $http_upgrade $connection_upgrade {
default upgrade;
@dyno
dyno / h2o_model_version.py
Created August 1, 2019 14:40
Detect H2O native model version
#!/usr/bin/env python3
# https://github.com/h2oai/h2o-3/blob/master/h2o-core/src/main/java/water/AutoBuffer.java#L269
from struct import unpack
with open("x.bin", "rb") as f:
magic = unpack(">h", f.read(2))[0]
assert magic == 0x1CED
@dyno
dyno / date.libsonnet
Created May 31, 2019 05:33
date library for jsonnet
// ported date utility function from http://howardhinnant.github.io/date_algorithms.html
// @param z Days since epoch
// @return Civic date tuple [y, m, d]
local civil_from_days(z) = (
local z1 = z + 719468;
local era = std.floor((if z1 >= 0 then z1 else z1 - 146096) / 146097);
local doe = (z1 - era * 146097); // [0, 146096]
local yoe = std.floor((doe - std.floor(doe / 1460) + std.floor(doe / 36524) - std.floor(doe / 146096)) / 365); // [0, 399]
local y = yoe + era * 400;
@dyno
dyno / replace_or_append.sh
Created April 4, 2019 06:37
replace_or_append
#!/usr/bin/env bash
#-------------------------------------------------------------------------------
# Patch target file with content from content file
#
# If begin/end marks exist, replace the content in between, otherwise append the section.
# It's not a function, because sometime we need to run it with sudo.
#-------------------------------------------------------------------------------
target_file="$1"