Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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