Skip to content

Instantly share code, notes, and snippets.

View zhyq0826's full-sized avatar

三月沙 zhyq0826

View GitHub Profile
@zhyq0826
zhyq0826 / get_week.py
Last active May 31, 2019 07:04
python 根据根据日期计算所在周的开始和结束
import datetime
def get_week():
today = datetime.date.today()
month = today.month
year = today.year
day = today.day
weekday = today.weekday()
start = today + datetime.timedelta(0-weekday)
@zhyq0826
zhyq0826 / interface.go
Created July 23, 2017 05:54
go interface description
package main
import (
"fmt"
)
//1
type I interface {
Get() int
Set(int)
@zhyq0826
zhyq0826 / go-type-inherit.go
Created July 28, 2017 05:19
go type inherit
package main
import (
"fmt"
)
type I interface {
Talk()
}
@zhyq0826
zhyq0826 / make_and_new.go
Created July 25, 2017 23:31
make 和 new 的区别
package main
import (
"fmt"
)
type Foo struct {
age int
name string
}
@zhyq0826
zhyq0826 / python_consume_produce_thread.py
Last active September 8, 2016 08:25
python 生产者消费者模式使用condition
#!/usr/bin/env python
import time
import thread
from threading import Thread, Condition, Event, Lock, currentThread
from Queue import Queue
class ItemQ(object):
def __init__(self):
@zhyq0826
zhyq0826 / python_thread_race.py
Created September 5, 2016 07:10
python 多线程模拟竞态
import urllib2
import time
from threading import Thread
#define a global variable
some_var = 0
class IncrementThread(Thread):
def run(self):
@zhyq0826
zhyq0826 / python_thread.py
Last active September 5, 2016 07:04
python 多线程
import urllib2
import time
from threading import Thread
def get_responses():
urls = ['http://www.baidu.com', 'http://www.amazon.cn', 'http://www.taobao.com', 'http://www.alibaba.com']
start = time.time()
for url in urls:
print url
@zhyq0826
zhyq0826 / python_iterator.py
Last active September 4, 2016 14:45
python 迭代器代码示例
class A():
def __init__(self):
self.v = [1,2,3,4,5,6]
self.step = 0
def __iter__(self):
return self
def next(self):
@zhyq0826
zhyq0826 / singletion_with_decorator.py
Created September 4, 2016 08:46
使用装饰器创建单例模式
def singleton(cls):
_instance = {}
def wrapper():
if cls not in _instance:
_instance[cls] = cls()
return _instance[cls]
return wrapper
@zhyq0826
zhyq0826 / format_time_show.py
Last active September 4, 2016 08:41
时间戳显示
from datetime import datetime
def format_show_time(v, start=None, end=None):
diff = datetime.now() - v
if diff.seconds < 60:
s = ('%s%s') % (diff.seconds, '秒前')
elif diff.seconds >= 60 and diff.seconds < 3600:
s = ('%s%s') % (diff.seconds / 60, '分钟前')
elif diff.seconds >= 3600 and (v > start and v < end):
s = ('%s%s') % ('今天', datetime.strftime(v, format='%H:%M'))