Skip to content

Instantly share code, notes, and snippets.

View liuyix's full-sized avatar

Yi Liu liuyix

View GitHub Profile
@liuyix
liuyix / python-call-external-program.py
Last active December 21, 2015 08:18
python 调用外部程序,拿到返回值
# http://stackoverflow.com/a/707001/668223
import os
from subprocess import Popen, PIPE
process = Popen(["ls", "-la", "."], stdout=PIPE)
exit_code = os.waitpid(process.pid, 0)
output = process.communicate()[0]
# 更好的做法应该是
# http://docs.python.org/2/library/subprocess.html
def start_thread_wrapper(target_func,name='thread-', thread_number=1,daemon=True, args=()):
assert target_func != None and hasattr(target_func, '__call__')
for i in xrange(thread_number):
t = threading.Thread(target=target_func,name=name+str(i), args=args)
t.daemon = daemon
t.start()
@liuyix
liuyix / command.sh
Created August 27, 2013 03:01
android screenshot by adb command
adb shell screencap -p | sed 's/\r$//' > screen.png
@liuyix
liuyix / bash-read-line-by-line.sh
Created August 28, 2013 02:27
bash script: read line by line
#Credit: http://stackoverflow.com/a/4642213/668223
FILE=test
while read CMD; do
echo "$CMD"
done < "$FILE"
class QueueItem:
def __init__(self, data):
self.data = data
self.next = None
def __repr__(self):
string = "Data: {}".format(str(self.data))
return string
class LinkedQueue:
def __init__(self):
@liuyix
liuyix / py-stack-mid-expression.py
Created August 30, 2013 15:42
python stack 实现 + 中缀表达式计算
import logging
class Stack:
def __init__(self):
self.__space = []
self.cur = -1
def pop(self):
if self.cur == -1:
logging.warn("pop an empty stack")
return None
@liuyix
liuyix / show-filesize.sh
Created September 2, 2013 02:27
shell script print file size
#credit:http://www.cyberciti.biz/faq/bash-csh-sh-check-and-file-file-size/
stat -c %s $file
@liuyix
liuyix / make_ext4fs-parameters.md
Created September 2, 2013 03:16
android make_ext4fs parameters

make_ext4fs -l 512M -s -a system system.img ./system

  • -l partition size
  • -s sparse mode 压缩模式,将image文件压缩
  • -a system mount point make_ext4fs会根据private/android_filesystem_config.h里定义好的权限来给文件夹里的所有文件重新设置权限
  • ./system file directory
@liuyix
liuyix / file-exists.sh
Last active December 22, 2015 03:18
shell snippet detect file exists by regexp
function exist() {
if [ ! -z $1 ];then
if ls $1 &>/dev/null;then
echo 0
return 0
fi
fi
echo 1
return 1
}
#!/usr/bin/python
#-*- encoding:utf-8 -*-
class ClassTest:
FOO = 'foo'
__bar = 'secret!'
def __init__(self, **args):
self.__dict = {}
self.__dict[ClassTest.FOO] = args[ClassTest.FOO]
self.fill_dict(ClassTest.FOO, **args)