Skip to content

Instantly share code, notes, and snippets.

View wonderbeyond's full-sized avatar
🎯
Focusing

Wonder wonderbeyond

🎯
Focusing
View GitHub Profile
@wonderbeyond
wonderbeyond / set-apt-proxy.md
Last active August 20, 2026 18:06
[ubuntu][socks5][proxy] Set proxy for apt

Writing an apt proxy conf file /etc/apt/apt.conf.d/proxy.conf as below.

Acquire::http::Proxy "socks5h://127.0.0.1:1080";
Acquire::https::Proxy "socks5h://127.0.0.1:1080";
Acquire::socks::Proxy "socks5h://127.0.0.1:1080";

And the proxy settings will be applied the next time we run apt.

@wonderbeyond
wonderbeyond / locate_line.py
Created July 21, 2025 06:44
[python] Locate line match some pattern in file
"""
Help me implement the Python function,
def locate_line(filename, pattern: str, after: str|None, until: str|None) -> int: ...
It finds the first line in file matching the re pattern and returns the 1-based lineno, 0 means nothing found. The after and until params specify the optional searching range.
Please write concise, clear but readable code.
"""
@wonderbeyond
wonderbeyond / fields.py
Last active December 14, 2024 10:26
Generic foreign key field for peewee based on postgresql's jsonb type, with Inter-Model-Identifier(IMID) support.
import six
from peewee import Model, FieldAccessor
from playhouse.postgres_ext import BinaryJSONField
class GForeignKeyAccessor(FieldAccessor):
def get_rel_instance(self, instance):
value = instance.__data__.get(self.name)
model_name = value['model']
rel_model = self.field.allowed_types[model_name]
@wonderbeyond
wonderbeyond / graceful_shutdown_tornado_web_server.py
Last active October 23, 2024 16:13 — forked from mywaiting/graceful_shutdown_tornado_web_server.py
The example to how to shutdown tornado web server gracefully...
#!/usr/bin/env python
"""
How to use it:
1. Just `kill -2 PROCESS_ID` or `kill -15 PROCESS_ID`,
The Tornado Web Server Will shutdown after process all the request.
2. When you run it behind Nginx, it can graceful reboot your production server.
"""
import time
@wonderbeyond
wonderbeyond / python-simple-requestor.py
Created March 29, 2023 06:40
Simple Requestor - A easy wrapper over python's urllib.request
from typing import cast
import logging
import json
import urllib.request
import http.client
import urllib.parse
logger = logging.getLogger(__name__)
@wonderbeyond
wonderbeyond / vsc-settings.py
Created February 25, 2019 09:11
script to export vsc settings
#!/usr/bin/env python3
from os import path
import tempfile
import subprocess
import datetime as dt
import click
@click.group()
def cli():
@wonderbeyond
wonderbeyond / get_guid.py
Last active August 2, 2024 07:56
Get a secret random string in python(Refer to django.utils.crypto.get_random_string)
import os
import binascii
import uuid
from xid import Xid
def get_guid(style='uuid'):
"""Get a globally unique string for identify things"""
if style == 'uuid':
return uuid.uuid4().hex
@wonderbeyond
wonderbeyond / rgetattr.py
Created April 13, 2018 08:07
Get attributes from nested objects
from functools import reduce
def rgetattr(obj, attr, *args):
"""See https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects"""
def _getattr(obj, attr):
return getattr(obj, attr, *args)
return reduce(_getattr, [obj] + attr.split('.'))
import os.path
@wonderbeyond
wonderbeyond / chunking.py
Created June 8, 2024 15:23
[Python]Split text into chunks with overlap.
def make_chunks(text, chunk_size: int, overlap: int = 0):
"""Split text into chunks with overlap."""
chunks = []
len_ = len(text)
for i in range(0, len_, chunk_size - overlap):
chunks.append(text[i: i + chunk_size])
if i + chunk_size >= len_:
break
return chunks