Skip to content

Instantly share code, notes, and snippets.

@wjzhangq
wjzhangq / yield.py
Created February 17, 2011 12:59
python yield 用法
class Fib:
def __init__(self, max):
self.max = max
def __iter__(self):
self.a = 0
self.b = 1
return self
def next(self):
<?php
class Fib implements Iterator{
var $a = 0;
var $b = 1;
var $i = 0;
var $max = 100;
public function __construct($n=100){
$this->max = $n;
}
def cur_file_dir():
# 获取脚本路径
path = sys.path[0]
# 判断为脚本文件还是py2exe编译后的文件,如果是脚本文件,则返回的是脚本的目录,如果是py2exe编译后的文件,则返回的是编译后的文件路径
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.dirname(path)
@wjzhangq
wjzhangq / my_date.py
Created April 29, 2019 08:30
时间函数
def my_date(unixtime, format='%m/%d/%Y %H:%M'):
d = datetime.datetime.fromtimestamp(unixtime)
return d.strftime(format)
if __name__ == '__main__':
pid_file = cur_file_dir() + '/logstat.pid'
fp = open(pid_file, 'w')
try:
fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
# another instance is running
sys.exit(0)