Skip to content

Instantly share code, notes, and snippets.

View zh4n7wm's full-sized avatar
🎯
Focusing

Hydra zh4n7wm

🎯
Focusing
View GitHub Profile
@zh4n7wm
zh4n7wm / copy.py
Created May 4, 2019 03:59 — forked from vpontis/copy.py
Copy to clipboard from iPython on Mac OS X
"""
Add copy to clipboard from IPython!
To install, just copy it to your profile/startup directory, typically:
~/.ipython/profile_default/startup/
Example usage:
%copy hello world
# will store "hello world"
@zh4n7wm
zh4n7wm / python-random-str.md
Created May 4, 2019 08:58
Python 产生随机字符串,尽可能保证唯一性

测试结论:

  • random_str = lambda length: ''.join(random.choice(letters) for x in range(length)) 产生的随机字符串当然是会出现重复的
  • secrets.randbits 产生随机的 xx位的整数,然后将整数映射到字符串中,产生随机字符串 也是会重复的
  • 两个重复的概率差不多(手动小范围测试,一次产生 100 组,每组一万个随机字符串;大概会有 4 个重复的)
  • secrets.randbits的方法要比 random_str 快很多,大概两、三倍 (分别产生长度为 8、10的随机字符串测试)

secrets.randbits 生成随机字符串:

def urandom_str(length=8):

@zh4n7wm
zh4n7wm / tornado-web-tips.md
Created May 4, 2019 09:14
tornado web tips

记录 tornado web 开发 tips

提前终止 request 流程

tornado web 可以通过 self.write 调用多次,将数据发送到客户端,如果想提前终止,最容易想到的方法肯定是 return self.write。这会产生两个问题:

  • pylint 提醒 A function or a method has inconsistent return statements
  • 如果想写个帮助函数,比如:自定义异常处理,但调用它后就不希望程序再继续往下运行了,这时没法直接在帮助函数中终止流行流程,需要手动调用 return help_method
  • 如果帮助函数是做些检查,达到某个条件时就终止程序继续运行,否则,继续。就只能检查该函数的返回值,然后再对不同条件做不同处理。每个调用它的函数中都需要如此这般,非常麻烦
@zh4n7wm
zh4n7wm / sqlalchemy-tips.md
Last active May 26, 2019 06:03
SQLAlchemy Tips

SQLAlchemy Tips

查询返回的数据集非常大

文档

q = sess.query(Object).yield_per(100).enable_eagerloads(False)
for x in q:
    pass
@zh4n7wm
zh4n7wm / mac-enable-nfs-server.md
Created May 15, 2019 06:42
Mac OSX enable NFS Server

创建 nfs 目录

$ mkdir /data/nfs-data

配置 nfs

$ cat /etc/exports
/data/nfs-data -maproot=root -alldirs -rw

启动 nfs

@zh4n7wm
zh4n7wm / bash-script-tips.md
Last active March 10, 2020 03:51
Bash script tips
@zh4n7wm
zh4n7wm / Docker-Tips.md
Last active August 3, 2021 06:39
Docker Tips

Ubuntu 安装 docker/docker-compose

sudo apt-get upgrade -yq
sudo apt-get remove -yq docker docker-engine docker.io containerd runc
sudo apt-get install -yq apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update -yq
sudo apt-get install -yq docker-ce docker-ce-cli containerd.io
@zh4n7wm
zh4n7wm / sentry-disk-cleanup.md
Last active March 23, 2020 04:55
sentry disk cleanup

释放 Sentry 部署机器的磁盘空间,准确的说应该是释放 PostgreSQL 服务的磁盘空间。(安装Sentry时选用PostgreSQL作为数据库)

删除 issue

如果某个接口错误,有可能频繁的产生大量的 issue,删除这些误报的 issue。

< 10000 可以手动 在Web页面 删除,更多的可以命令行调用 curl 删除 /api/0/organizations/sentry/issues/?query=is%3Aunresolved&project={project-id},具体命令可以从 Chrome DevTool 中拷贝。

只保留指定天数的 issue

@zh4n7wm
zh4n7wm / PostgreSQL-tips.md
Last active January 21, 2022 00:39
PostgreSQL Tips

mac OSX upgrade PostgreSQL

error message:

waiting for server to start....2020-10-22 09:27:22.125 CST [40884] FATAL:  database files are incompatible with server
2020-10-22 09:27:22.125 CST [40884] DETAIL:  The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.0.
 stopped waiting
pg_ctl: could not start server
Examine the log output.
@zh4n7wm
zh4n7wm / java-reverse-engineering.md
Created October 20, 2019 12:53
Java逆向工程

Java 逆向工程

Java反编译

注:反编译后的 java 代码只能阅读来查看代码逻辑,不能保障能再通过 javac 编译会 class