Skip to content

Instantly share code, notes, and snippets.

@ChenyangGao
ChenyangGao / str2num.py
Created July 24, 2020 08:02
English numerals to Arabic numerals
import re
from types import MappingProxyType
__all__ = ['str2num']
NUMBase = MappingProxyType({
'one': 1,
'two': 2,
@ChenyangGao
ChenyangGao / ufcs.py
Last active July 1, 2024 13:42
Uniform Function Call Syntax (UFCS) for Python
'''
This module implements UFCS for Python.
[Uniform Function Call Syntax (UFCS)](https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs)
UFCS is a key feature of D and enables code reusability and scalability through well-defined encapsulation.
UFCS allows any call to a free function fun(a) to be written as a member function call a.fun().
If a.fun() is seen by the compiler and the type doesn't have a member function called fun(),
@ChenyangGao
ChenyangGao / currying.py
Created June 23, 2021 08:02
Python functional programming: currying
#!/usr/bin/env python3
# coding: utf-8
'''
4 decorators (as below) are provided to implement `currying` for Python functions:
- currying 👍
- partial_currying
- fast_currying
- Currying 👍
'''
@ChenyangGao
ChenyangGao / bracket_simple_pattern.py
Created September 18, 2021 09:26
Python bracket pattern [simple version]
#!/usr/bin/env python3
# coding: utf-8
__author__ = 'ChenyangGao <https://chenyanggao.github.io/>'
__version__ = (0, 0, 4)
__all__ = [
'BRACKET_PATTERN', 'bracket_pattern', 'translate', 'compile',
'replace', 'get_dict_of_match', 'template_replace',
]
__doc__ = '''
@ChenyangGao
ChenyangGao / html_img_src2datauri.py
Last active August 27, 2022 13:08
Python 把 HTML 中的 img 元素的 src 转换成data URL | Convert the src link of img tag of HTML into data URL
#!/usr/bin/env python3
# coding: utf-8
__author__ = "ChenyangGao <https://chenyanggao.github.io/>"
__version__ = (0, 1)
__all__ = ["process"]
if __name__ == "__main__":
from argparse import ArgumentParser
@ChenyangGao
ChenyangGao / mod-proxy.sh
Last active August 3, 2023 03:22
Shell脚本实现在Linux命令行开启代理
#? 设置代理 ip,请运行 proxy# 获取帮助信息
# AUTHOR='ChenyangGao <https://chenyanggao.github.io/>''
# VERSION=0.2
if [ -n "${MOD_PROXY_sourced+x}" ]; then
return 0
fi
# inetutils (apt)
@ChenyangGao
ChenyangGao / config_props.py
Created August 27, 2022 13:12
Python在命令行修改Properties配置文件
#!/usr/bin/env python3
# coding: utf-8
assert __name__ == "__main__", "不能被引入模块"
__author__ = "ChenyangGao <https://chenyanggao.github.io/>"
__version__ = (0, 1, 1)
from argparse import ArgumentParser, RawTextHelpFormatter
from sys import argv
@ChenyangGao
ChenyangGao / func_cmdline.py
Created August 27, 2022 13:14
Python函数作为命令行使用
#!/usr/bin/env python3
# coding: utf-8
__author__ = "ChenyangGao <https://chenyanggao.github.io/>"
__version__ = (0, 1, 1)
__all__ = ["func_cmdline"]
# Inspired by:
# - https://pypi.org/project/typer/
@ChenyangGao
ChenyangGao / dictattr.py
Last active August 30, 2022 01:43
Python脚本作为配置文件加载
#!/usr/bin/env python3
# coding: utf-8
__author__ = "ChenyangGao <https://chenyanggao.github.io/>"
__version__ = (0, 1, 1)
__all__ = ["AttrDict", "DictAttr", "Properties"]
import builtins
from pathlib import Path
@ChenyangGao
ChenyangGao / share_ssh_pubkey.py
Created August 27, 2022 14:12
在Linux主机间共享ssh公钥
#!/usr/bin/env python3
# coding: utf-8
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
__version__ = (0, 1)
__all__ = ["login", "get_pubkey"]
# TODO: 可能需要检测 ssh 时是否远程服务器冲突,可通过运行如下命令修复
# ssh-keygen -R ${host}