Skip to content

Instantly share code, notes, and snippets.

View mebusw's full-sized avatar

Jacky Shen mebusw

View GitHub Profile
@mebusw
mebusw / gist:535cc4477c6d945628a8
Created May 25, 2014 06:08
simulate "lazy map" of Clojure in Python, which can just iterate given times with an infinite loop
### In Clojure, it's written as "take 5 (nToTicket (iterate inc))"
def inc():
n = 0
while True:
yield n
n += 1
def lazyMap(f, xs, times=None):
time = 0
for x in xs():
@mebusw
mebusw / gist:48ef02090b2156407a1c
Created May 29, 2014 00:37
timezone and datetime in Django
from django.utils import timezone
# Suppose in settings.py, TIME_ZONE = 'Asia/Shanghai'
timezone.now()
# >>> datetime.datetime(2014, 5, 29, 0, 35, 19, 523838, tzinfo=<UTC>)
timezone.localtime(timezone.now())
# >>> datetime.datetime(2014, 5, 29, 8, 35, 29, 427944, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
@mebusw
mebusw / gist:6bfef02bfdeb7c071080
Created July 25, 2014 01:52
Programmatically saving image to Django ImageField
################# Solution1 ##############
# First, copy your image file to the upload path (assumed = 'path/' in following snippet).
#
# Second, use something like:
class Layout(models.Model):
image = models.ImageField('img', upload_to='path/')
layout = Layout()
class decoratorWithArguments(object):
def __init__(self, arg1, arg2, arg3):
"""
If there are decorator arguments, the function
to be decorated is not passed to the constructor!
"""
print "Inside __init__()"
self.arg1 = arg1
self.arg2 = arg2
class Pipe(object):
def __init__(self, func):
self.func = func
def __ror__(self, other):
def generator():
for obj in other:
if obj is not None:
yield self.func(obj)
return generator()
@mebusw
mebusw / N-ary_convert.py
Last active August 19, 2016 13:51
integer to N-ary 整数转为N进制
def xf(x,y):
l=[]
while True:
if x==0:
break
else:
z,h=divmod(x,y)
x=z
l.insert(0,h)
return ''.join(map(str, l))
@mebusw
mebusw / multi-derivation.py
Last active August 19, 2016 13:50
generate sequence with char array, like column names of MS Excel does.
def g(L):
for i in L:
yield i
for i in [ x+y for x in L for y in L]:
yield i
gg=g(['A', 'B', 'C'])
for i in xrange(15):
print next(gg)
@mebusw
mebusw / threading.py
Last active August 19, 2016 13:49
simple way of multi-threading in python
#coding=utf-8
#!/usr/bin/python
import thread
import time
# 为线程定义一个函数
def print_time( threadName, delay):
count = 0
while count < 5:
@mebusw
mebusw / map-multi-threading.py
Last active August 6, 2023 15:05
python multi-threading with map()
from multiprocessing import Pool
import time
def func(i):
time.sleep(1)
print i
pool = Pool(3)
pool.map(func, range(30))
pool.close()
#! encoding=utf8
import unittest
def maxProfit(prices):
if not prices:
return 0
currBuy = -prices[0] #表示当天最终未持股的情况下,当天结束后的累计最大利润
currSell = 0 #表示当天最终持股的情况下,当天结束后的累计最大利润