Skip to content

Instantly share code, notes, and snippets.

@KuRRe8
Last active May 6, 2025 01:56
Show Gist options
  • Save KuRRe8/26bb1fd09b3e272c01fbe91a20dd3343 to your computer and use it in GitHub Desktop.
Save KuRRe8/26bb1fd09b3e272c01fbe91a20dd3343 to your computer and use it in GitHub Desktop.
small repo for personal use, welcome to contribute
import logging
import os
from datetime import datetime
# Define a global logger
def setup_logger(log_file="app.log", level=logging.INFO):
"""Setup and return a logger instance."""
logger = logging.getLogger()
logger.setLevel(level)
# Create file handler
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(level)
# Create console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
# Define formatter and add it to handlers
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
logger.info('###Logger initialized.###')
return logger
# Initialize the global logger
current_date = datetime.now().strftime("%Y-%m-%d")
LOG_FILE_PATH = os.path.join(os.path.dirname(__file__), "logs", f"app.{datetime.now().strftime('%Y-%m-%d_%H-%M')}.log")
os.makedirs(os.path.dirname(LOG_FILE_PATH), exist_ok=True)
logger = setup_logger(log_file=LOG_FILE_PATH,level=config.settings.LOGGER_LVL)
# Example usage
if __name__ == "__main__":
logger.debug('Test the debug level log.')
logger.info('Test the info level log.')
logger.warning('Test the warning level log.')
logger.error('Test the error level log.')
logger.critical('Test the critical level log.')
import sys
before = [str(m) for m in sys.modules]
import yourmoudule
after = [str(m) for m in sys.modules]
set([m.split('.')[0] for m in after if not m in before and not m.startswith('_')])
# use as a class method decorator, @classonlymethod def mymethod: pass
class classonlymethod(classmethod):
"""
decorator for class methods that should only be called on the class, not on instances.
"""
def __get__(self, instance, cls=None):
if instance is not None:
raise AttributeError("This method is available only on the class, not on instances.")
return super(classonlymethod, self).__get__(instance, cls)
def clear_screen():
import sys
sys.stdout.write("\033[2J\033[1;1H")
sys.stdout.flush()
#change the working dir to current file dir
import os
import sys
if "__file__" in globals():
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# set random generator seed
def make_training_deterministic(seed: int = 0):
'''Set random seed for reproducibility'''
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
os.environ['PYTHONHASHSEED'] = str(seed)

Python Snippets

Useful code for daily development.

import threading
class SingletonMeta(type):
"""自定义元类"""
def __init__(cls, *args, **kwargs):
cls.__instance = None
cls.__lock = threading.RLock()
super().__init__(*args, **kwargs)
def __call__(cls, *args, **kwargs):
if cls.__instance is None:
with cls.__lock:
if cls.__instance is None:
cls.__instance = super().__call__(*args, **kwargs)
return cls.__instance
class President(metaclass=SingletonMeta):
"""总统(单例类)"""
pass
# 上面是使用metaclass的单例,下面是使用装饰器的做法
from functools import wraps
from threading import RLock
def singleton(cls):
"""线程安全的单例装饰器"""
instances = {}
locker = RLock()
@wraps(cls)
def wrapper(*args, **kwargs):
if cls not in instances:
with locker:
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return wrapper
# time profiling
import time
class Timer:
def __init__(self, name="Timer"):
self.name = name
def __enter__(self):
self.start = time.perf_counter()
return self
def __exit__(self, *args):
elapsed = time.perf_counter() - self.start
print(f"[{self.name}] Elapsed: {elapsed:.6f} seconds")
# 用法
with Timer("Sum 1 million"):
sum(range(1000000))
import os
def print_tree(path, prefix=""):
files = sorted(os.listdir(path))
for i, name in enumerate(files):
full_path = os.path.join(path, name)
connector = "└── " if i == len(files) - 1 else "├── "
print(prefix + connector + name)
if os.path.isdir(full_path):
extension = " " if i == len(files) - 1 else "│ "
print_tree(full_path, prefix + extension)
print_tree("/your/path/here")
import os
import json
from IPython.display import display, JSON
def get_directory_structure_with_human_readable_sizes(path):
"""
递归地构建目录结构,包含人类可读的文件大小。
Args:
path (str): 要遍历的路径。
Returns:
dict: 目录结构的 JSON 格式表示,人类可读的文件大小作为 value。
"""
try:
tree = {}
for entry in os.listdir(path):
full_path = os.path.join(path, entry)
if os.path.isfile(full_path):
try:
size_bytes = os.path.getsize(full_path)
tree[entry] = human_readable_size(size_bytes) # 获取并格式化文件大小
except OSError as e:
tree[entry] = f"Error getting size: {e}" # 处理无法获取大小的情况
elif os.path.isdir(full_path):
tree[entry] = get_directory_structure_with_human_readable_sizes(full_path) # 递归调用
else:
tree[entry] = "unknown"
except OSError as e:
return f"Error: {e}" # 处理权限等错误
return tree
def human_readable_size(size_bytes):
"""
将字节数转换为人类可读的格式(KB、MB、GB 等)。
Args:
size_bytes (int): 文件大小(以字节为单位)。
Returns:
str: 人类可读的文件大小字符串。
"""
if size_bytes == 0:
return "0 Bytes"
size_name = ("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return f"{s} {size_name[i]}"
# 确保math模块被导入
import math
# 获取根目录的结构,包含人类可读的文件大小
root_structure = get_directory_structure_with_human_readable_sizes("/")
# 使用 IPython.display.JSON 显示
display(JSON(root_structure, expanded=False)) # 默认折叠以提高可读性
# 可选:将 JSON 字符串打印出来,以便在不支持 display 的情况下查看
# print(json.dumps(root_structure, indent=2))
import os
from IPython.display import display
import ipywidgets as widgets
def list_dir(path, show_hidden=False):
try:
items = sorted(os.listdir(path))
except PermissionError:
items = []
if not show_hidden:
items = [item for item in items if not item.startswith('.')]
return items
def make_browser(path, show_hidden):
box = widgets.VBox()
label = widgets.HTML(f"<b>{path}</b>")
children = []
for name in list_dir(path, show_hidden):
full_path = os.path.join(path, name)
if os.path.isdir(full_path):
toggle = widgets.ToggleButton(value=False, description=name, layout=widgets.Layout(width='auto'))
subbox = widgets.VBox()
def on_toggle_change(change, path=full_path, subbox=subbox):
if change['new']:
subbox.children = [make_browser(path, show_hidden)]
else:
subbox.children = []
toggle.observe(on_toggle_change, names='value')
children.append(widgets.VBox([toggle, subbox]))
else:
children.append(widgets.Label("📄 " + name))
box.children = [label] + children
return box
# 控制开关 + 文件浏览器绑定逻辑
def interactive_browser(start_path="/your/path/here"):
show_hidden_toggle = widgets.Checkbox(value=False, description="显示隐藏文件(.开头)")
output = widgets.Output()
def update_browser(change=None):
output.clear_output()
with output:
display(make_browser(start_path, show_hidden_toggle.value))
show_hidden_toggle.observe(update_browser, names='value')
update_browser() # 初始加载
display(widgets.VBox([show_hidden_toggle, output]))
# 调用函数启动浏览器
interactive_browser("/your/path/here")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment