Skip to content

Instantly share code, notes, and snippets.

Avatar

三月沙 zhyq0826

View GitHub Profile
@zhyq0826
zhyq0826 / get_week.py
Last active May 31, 2019
python 根据根据日期计算所在周的开始和结束
View get_week.py
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 Jul 23, 2017
go interface description
View interface.go
package main
import (
"fmt"
)
//1
type I interface {
Get() int
Set(int)
View go-type-inherit.go
package main
import (
"fmt"
)
type I interface {
Talk()
}
@zhyq0826
zhyq0826 / make_and_new.go
Created Jul 25, 2017
make 和 new 的区别
View make_and_new.go
package main
import (
"fmt"
)
type Foo struct {
age int
name string
}
@zhyq0826
zhyq0826 / python_consume_produce_thread.py
Last active Sep 8, 2016
python 生产者消费者模式使用condition
View python_consume_produce_thread.py
#!/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 Sep 5, 2016
python 多线程模拟竞态
View python_thread_race.py
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 Sep 5, 2016
python 多线程
View python_thread.py
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 Sep 4, 2016
python 迭代器代码示例
View python_iterator.py
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 Sep 4, 2016
使用装饰器创建单例模式
View singletion_with_decorator.py
def singleton(cls):
_instance = {}
def wrapper():
if cls not in _instance:
_instance[cls] = cls()
return _instance[cls]
return wrapper
View format_time_show.py
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'))