Skip to content

Instantly share code, notes, and snippets.

View myuanz's full-sized avatar

provefar myuanz

View GitHub Profile
@myuanz
myuanz / df_cache.py
Created November 28, 2025 02:45
A type-safe file cache decorator snippet
# try it in pyright playground
# https://pyright-play.net/?code=GYJw9gtgBMCuB2BjALmMAbAzlAlhADmCMlAO4gCG%2BmAsAFCiRT4XIAW6OARrgUSQAVWbeo2jIAnvhzwA5r0LEoAYQrp0FLugCmAGigAZHMm2V0%2BgLJVpc-QPCpEGfQGVtAR1jakeqIgqYyPT0eIokLPAAJgFQMfiRIXxKhBog2HHoouDQKRRpAHTRyKxS2tih-FAAIqwUACqlsdgCGHmYNcUN%2BNrBdPSIGpjYqohs2pEdFABilBDaUwiIANoAVCsCALoAFPZgjhgAlABc9FBnUJHawFAA%2Bjf%2B6ndbmNrowPorebKYR1AC%2BV9MB8VgBrUiA37-MGAg5QAC0AD5mOh8pMZhQ5r98tjeucLlc-Do8vcKKNtM9Xu8oJ8QN9IQDaUDqaDwYz6dDGbDEVAAHJgeDaLE4voigYBYaksZVKYnOh4y7XO4yYxPF5vfRcALaG5FCg6nAgX6BEBQAA%2Bf2EUAAvFAAOS2rlIvkC2V4vFq4D5G6al462r6k02oTsLY%2B7W6gMHU5us4e-Jhv3FAP5CAgyIGrYsEDeZCYK11EBefTaAAeOECNzAIPzhe0UZFeIAAvhwN1iBJo2cFVAExH0yAKW9HRb2K6Y1Bs8hYCB4FA496tYm9f3cedG73-f38i9kCYQJ38dcN0n%2B4OqfgjcgTebg2xh87BQf3ZSvcflwbrSO2Jn6wfu3cHnQO5VnWbYn3OD1dHAs54AxQU5yvM0vyQ1R1E0HQliWKd8B0fR0xQDZ9GNQjoKgUtkF%2BIw9zUJZbUQTAADdbX0W0s08bRkFtDZPzoxjmNI4AiEQeCuDQdBPymNQXiguVx0wMkID1MAGNMEAcEuH4oCsfAbFkJZjTsVo0kmLptG4803HYnwlhaVJ2lqUzzN5fltE-B8ZLxYdUI0LRtEw7z0L8gR9Bw1FanROYkPiMLigiszCJUSVxjRW
@myuanz
myuanz / read_visible_rows_to_dataframe.py
Created August 26, 2024 08:50
read xlsx with excel filter conditions.
def read_visible_rows_to_dataframe(file_path: str, sheet_name: str|None=None) -> pd.DataFrame:
"""
Reads the specified sheet of an Excel file, ignores all hidden rows, and returns a pandas DataFrame.
:param file_path: Path to the Excel file
:param sheet_name: Name of the sheet to read. If None, reads the active sheet.
:return: A pandas DataFrame containing data from visible rows
"""
wb_info = load_workbook(filename=file_path, read_only=False)
from pathlib import Path
import re
import html
posts = list(Path('_posts').glob('*.md'))
for post_p in posts:
content = post_p.read_text(encoding='utf-8')
content = content.replace('<pre><code>', '<pre><code class="language-text">')
content = content.replace('&quot;', '"')
@myuanz
myuanz / dynamic-enum.py
Last active July 23, 2022 04:23
Dynamic enum with FastApi
from random import randint
from fastapi import FastAPI
from enum import Enum
app = FastAPI()
class DynamicEnum(Enum):
pass
@myuanz
myuanz / calculator.py
Created November 15, 2020 10:49
简单的 Python 带括号四则运算计算器 / Simple Python elementary arithmetic calculator
def merge(ops: list[str], values: list[int], final_merger=False):
priority = {'*': 1, '/': 1, '+': 2, '-': 2, '$': 9999}
while True:
if len(ops) >= 2:
last, last_last = [ops.pop() for _ in range(2)]
if final_merger:
last_last, last = last, last_last
elif len(ops) >= 1 and len(values) >= 2:
last_last, last = ops.pop(), '$'
else:
@myuanz
myuanz / compressed_image.py
Last active May 25, 2020 09:04
Image to ndarray; Image to bytes buffer; Image to buffer pickle; Pickle to Image. Pickle to ndarray
from __future__ import annotations
import io
from functools import lru_cache
from PIL import Image
import numpy as np
import pickle
import os
@myuanz
myuanz / dataclasses.py
Created April 21, 2020 04:19
Dataclasses 的一个轮子 / Homemade Dataclasses
class DataclassesBase:
def __init__(self, **data):
self._annotations = self.__annotations__ if hasattr(self, "__annotations__") else {}
annotations = self._annotations.copy()
for key, value in data.items():
if key in annotations:
if isinstance(value, annotations[key]):
self.__dict__[key] = value
annotations.pop(key)
@myuanz
myuanz / .sh
Last active May 20, 2020 09:22
新Ubuntu配置镜像和公钥
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
" > /etc/apt/sources.list
@myuanz
myuanz / .sh
Created January 2, 2020 02:53
Linux 解压
tar –xvf file.tar //解压 tar包
tar -xzvf file.tar.gz //解压tar.gz
tar -xjvf file.tar.bz2 //解压 tar.bz2
tar –xZvf file.tar.Z //解压tar.Z
@myuanz
myuanz / .sh
Created January 2, 2020 02:51
View git add and delete lines / 查看 git 增删行数
git log --author="myuanz" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s|%.2f%%, removed lines: %s|%.2f%%, total lines: %s\n", add, add/(add + subs)*100, subs, subs/(add + subs)*100, loc }' -