This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
#!/usr/bin/python | |
import thread | |
import time | |
# 为线程定义一个函数 | |
def print_time( threadName, delay): | |
count = 0 | |
while count < 5: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
import time | |
def func(i): | |
time.sleep(1) | |
print i | |
pool = Pool(3) | |
pool.map(func, range(30)) | |
pool.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! encoding=utf8 | |
import unittest | |
def maxProfit(prices): | |
if not prices: | |
return 0 | |
currBuy = -prices[0] #表示当天最终未持股的情况下,当天结束后的累计最大利润 | |
currSell = 0 #表示当天最终持股的情况下,当天结束后的累计最大利润 |
OlderNewer