Skip to content

Instantly share code, notes, and snippets.

@gyli
gyli / dynamic_access_dict.py
Created March 21, 2024 15:41
Accessing dict value with all keys in one string
class DynamicAccessDict(dict):
"""
A dict-like class that allows accessing LineCol object with multi-level keys in a list
Example:
config == {
'a': {
'b': 0, # int value
'c': [{'d': 3}] # list value
}
}
@gyli
gyli / git-log-summary.sh
Last active July 15, 2023 03:53
Generate git log summary in a markdown table
git log --pretty=format:'%h%x00%an%x00%ad%x00%s%x00' --since="2023-07-12" --until="2023-07-13" --date=format:'%Y-%m-%d' | \
jq -R -s '[split("\n")[:-1] | map(split("\u0000")) | .[] | {
"commit": .[0],
"author": .[1],
"date": .[2],
"summary": .[3]
}]' | \
jq -r '"||Commit||Author||Date||Summary||",
( .[] | "|\(.commit)|\(.author)|\(.date)|\(.summary)|" )'
@gyli
gyli / .zshrc
Last active March 28, 2024 02:34
alias
alias ga='git add'
alias gaa='git add .'
alias gb='git branch'
alias gc='git checkout'
alias gcb='git checkout -b'
alias gdc='git diff main...'
alias gps='git push'
alias gpc='git push origin $(git rev-parse --abbrev-ref HEAD)'
alias gpl='git pull'
alias gd='git diff'
@gyli
gyli / scantree.py
Created February 19, 2023 01:39
Scan dir recursively and efficiently with os.scandir
import os
def scantree(path):
"""Recursively yield DirEntry objects for given directory."""
for entry in os.scandir(path):
if entry.is_dir(follow_symlinks=True):
yield from scantree(entry.path)
else:
yield entry
@gyli
gyli / forbidden_properties_metaschema.json
Created February 16, 2023 22:57
Meta-schema of a jsonschema to disallow Python dictionary method names to be used as property names
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Meta-schema of a jsonschema to disallow Python dictionary method names to be used as property names",
"description": "Checks forbidden properties in json objects recursively. Jsonschema keywords need to be conditionally excluded.",
"type": "object",
"allOf": [
{
"$ref": "#/definitions/forbidden-properties"
}
],
@gyli
gyli / wechat_video.txt
Last active February 17, 2023 02:22
WeChat Video DNS Blocklist
! Video
||*.video.qq.com^
||wx.qlogo.cn^
||cwx.qlogo.cn^
||sgshort.wechat.com^
||sgminorshort.wechat.com^
||sglong.wechat.com^
||mmhead.c2c.wechat.com^
||snsvideo.c2c.wechat.com^
||www.qq.com^
@gyli
gyli / dict_obj_mapping.py
Last active November 14, 2023 17:18
Map dict to object attributes in Python
class DictObjMap(dict):
"""
Converts dict to object attributes for easier value fetching
It supports fetching nested dict and list value like `obj.array[0].item`
Invalid list index and non-existing key will throw out IndexError and KeyError like native list and dict
Can be migrated to dataclasses once Python is upgraded to >=3.7
Side effect:
Namespace of builtin dict method attributes is overridden with config keys, only other than get().
Use dict() when calling dict methods, like `dict(obj.array[0].item).items()`
@gyli
gyli / weee-better-style.js
Last active May 31, 2022 20:13
Weee better Style
// ==UserScript==
// @name Weee Better Style
// @namespace https://gist.github.com/gyli/2a49803463d72e7b34cfbecad936c0f1
// @version 0.1
// @description try to take over the world!
// @author Guangyang Li
// @match https://www.sayweee.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=sayweee.com
// @grant GM_addStyle
// @run-at document-start
@gyli
gyli / youtube-restyle.js
Created May 14, 2022 15:55
TamperMonkey Script: Youtube Restyle
// ==UserScript==
// @name Youtube Restyle
// @namespace https://gist.github.com/gyli/5ab596a955b8d31883f02fbefde1cfef
// @version 0.1
// @description Update Youtube player position to avoid unexpected scrolling
// @author Guangyang Li
// @match https://www.youtube.com/*
// @grant GM_addStyle
// @run-at document-start
// ==/UserScript==
@gyli
gyli / calculate_decimal_range.py
Created December 14, 2021 05:35
Calculate range of Decimal with precision and scale
from typing import Decimal, Tuple
def calculate_decimal_range(precision: int, scale: int) -> Tuple[Decimal, Decimal]:
"""
This method calculates the range of Decimal with given precision and scale.
:return: (min_value, max_value)
"""
precision, scale = Decimal(precision), Decimal(scale)
max_value = 10**(precision-scale) - 10**-scale
return -max_value, max_value