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
忽略抛出的异常: | |
常用的写法: | |
try: | |
os.remove('somefile.tmp') | |
except OSError: | |
pass | |
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
//JavaScript callee 用法示例 | |
function factorial(num) { | |
if (num <= 1) { | |
return 1; | |
} else { | |
return num * arguments.callee(num - 1); | |
} | |
} | |
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
# via: http://www.oschina.net/code/snippet_16840_1568 | |
1.生成随机数 | |
import random #这个是注释,引入模块 | |
rnd = random.randint(1,500)#生成1-500之间的随机数 | |
2.读文件 | |
f = open("c:\\1.txt","r") | |
lines = f.readlines()#读取全部内容 |
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
# This is an asynchronous task scheduler based on coroutines | |
import socket | |
import select | |
from collections import deque | |
class YieldPoint: | |
def yield_task(self, task): | |
pass | |
def resume_task(self, task): | |
pass |
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
""" | |
This is a RPC server/client implementation using socket | |
* It authenticate the client with a secret shared by client/server | |
* It uses JSON to serialize the function call payload | |
""" | |
import socket | |
import random, string | |
import hmac | |
import json | |
import threading |
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 datetime import date | |
import os | |
# A simple script to open a new markdown todo file every day. | |
year = date.today().year | |
month = date.today().strftime('%b').lower() | |
day = date.today().day | |
filename = os.path.join(str(year) + '_' + month, str(day) + '.md') |
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
import re | |
s = "1991-02-28" | |
re.sub(r"(\d{4})-(\d{2})-(\d{2})", r'\1/\2/\3') | |
Out[6]: '1991/02/28' |
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 future.builtins.disabled import * |
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
set1 = {1,2,3,4} | |
set2 = {3,4,5,6} | |
set1.intersection(set2)#交集 | |
set1.difference(set2)#差集 |
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 itertools import groupby | |
def height_class(h): | |
if h> 180: | |
return "tall" | |
elif h<160: | |
return "short" | |
else: | |
return "middle" | |
OlderNewer