Skip to content

Instantly share code, notes, and snippets.

@omsobliga
omsobliga / thread_with_join.py
Last active February 28, 2016 10:27
Child thread with join, main thread wait unit all child threads terminate.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Child threads with join
Main thread wait unit all child threads terminate.
"""
import threading
from time import sleep
@omsobliga
omsobliga / mylogger.py
Last active August 29, 2015 14:12
Get a logger that can print to file and stream in one time.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
def get_logger():
# Get a logger, default is the root logger.
logger = logging.getLogger('mylogger')
@omsobliga
omsobliga / unicode_escape.py
Last active June 19, 2021 05:54
Python 在什么情况下会输出 Unicode 字符串
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" 测试 Python 在什么情况下会输出 Unicode 字符串
需要首先理解在 Python 中 Unicode 类型和 Unicode 字符串指的不是同一个东西。
Unicode 字符串是 str 类型,但它的值的表现形式是 Unicode 编码形式。
"""
def printt(str):
@omsobliga
omsobliga / node-and-npm-in-30-seconds.sh
Last active February 19, 2016 07:42 — forked from isaacs/node-and-npm-in-30-seconds.sh
Use one of these techniques to install node and npm without having to sudo. Discussed in more detail at http://joyeur.com/2010/12/10/installing-node-and-npm/ Note: npm >=0.3 is *safer* when using sudo.
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl -L https://www.npmjs.org/install.sh | sh
@omsobliga
omsobliga / multiple_processes.sh
Created March 15, 2016 15:13
Start multiple processes in shell
# 查找出当前文件夹中以 x 开头的文件,文件名作为参数传给 python 脚本,然后放到后台执行
for i in x*
do
python t.py $i &
done
@omsobliga
omsobliga / transaction.py
Created July 20, 2016 14:26
SQLAlchemy Transaction
# http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Transaction
from sqlalchemy import create_engine
engine = create_engine("postgresql://scott:tiger@localhost/test")
connection = engine.connect()
trans = connection.begin()
connection.execute("insert into x (a, b) values (1, 2)")
trans.commit()
@omsobliga
omsobliga / transaction_in_concurrent.py
Last active August 15, 2016 02:56
用多进程模拟数据库事务在并发环境下的表现
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
用多进程模拟数据库事务在并发环境下的表现。
测试环境:
一个数据库 server, 4 核机器, 四个进程
运行结果::
run 0 process
run 1 process
@omsobliga
omsobliga / cors_server.py
Last active July 24, 2018 03:20 — forked from enjalot/cors_server.py
Allow CORS with python simple http server
# python cors_server.py
import SimpleHTTPServer
class CORSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def send_head(self):
"""Common code for GET and HEAD commands.
This sends the response code and MIME headers.
Return value is either a file object (which has to be copied
@omsobliga
omsobliga / aes.py
Last active March 21, 2018 06:26
对称加密 AES
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 参考资料:
# * https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
# * https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
#
# Show AES document:
# > from Crypto.Cipher import AES
# > help(AES)
@omsobliga
omsobliga / rsa.py
Last active September 28, 2016 11:28
公开密钥加密 RSA
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 参考资料:
# * https://en.wikipedia.org/wiki/RSA
#
# Show RSA document:
# > from Crypto.PublicKey import RSA
# > help(RSA)
from Crypto.PublicKey import RSA