Skip to content

Instantly share code, notes, and snippets.

@tanbro
tanbro / snippetfuncexecutor.py
Created January 15, 2024 04:16
exec and eval a Python code snippet with return expression, as if it's in a non-arguments function
class SnippetFuncExecutor:
"""
exec and eval a Python code snippet with return expression, as if it's in a non-arguments function
"""
def __init__(
self,
*,
prefix: Optional[str] = None,
optimize: int = -1,
@tanbro
tanbro / grab_source_files.py
Last active March 9, 2024 09:33
Export all source code files into a single HTML file with Pygments syntax highlight and remove all comments and empty lines.
#!/usr/bin/env python
"""
Export all source code files into a single HTML file with Pygments syntax highlight and remove all comments and empty lines.
"""
import argparse
import fnmatch
import os
import sys
#!/usr/bin/env python3
"""
# Report PyProject version
The tool print python source code project's version from pip install report with a dry-run.
The project MUST have a `pyproject.toml` file with metadata defined in it!
- On non-western language Windows, environment `PYTHONUTF8` may be in need .
@tanbro
tanbro / caseconvert.py
Last active June 16, 2023 03:07
Change dictionary's key naming style
"""
Change dictionary's key naming style
"""
from typing import (
Any,
Callable,
Iterable,
Mapping,
MutableMapping,
@tanbro
tanbro / captchaimage.py
Last active July 15, 2022 07:37
Generate a captcha image
from itertools import product
from pathlib import Path
from random import choice, randint, randrange, uniform
from typing import Any, BinaryIO, Mapping, Optional, Sequence, Union
from PIL import Image, ImageDraw, ImageFilter, ImageFont
__all__ = ['captcha_image']
@tanbro
tanbro / make_manylinux_wheels.py
Last active August 3, 2022 07:13
使用 quay.io 提供的 pypa/manylinux Docker 镜像构建该项目及其依赖软件的 Python Wheel 发布包
#!/usr/bin/env python3
"""
使用 quay.io 提供的 pypa/manylinux Docker 镜像构建该项目及其依赖软件的 Python Wheel 发布包
"""
import argparse
import os
import os.path
import platform
#include <stddef.h>
#include <memory.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
/// 参考: https://www.openssl.org/docs/man1.0.2/man3/BIO_f_base64.html 以及
/// https://gist.github.com/barrysteyn/7308212
@tanbro
tanbro / namecase.py
Last active November 16, 2021 07:40
A python module snakelize or camelize string keys inside a dictionary
"""
Change dictionary's key naming style
"""
from typing import Iterable, Mapping
import stringcase
__all__ = ['convert', 'camelize', 'pascalize', 'snakelize']
@tanbro
tanbro / corenlp_ssplit.py
Last active March 21, 2019 08:31
使用 Web API 远程调用 CoreNLP Server 的 ssplit 进行分句和分词。
# -*- coding: utf-8 -*-
"""
使用 CoreNLP 进行汉语语料的分句和分词
"""
import os
import re
import unicodedata
import unittest