Skip to content

Instantly share code, notes, and snippets.

View stilllisisi's full-sized avatar
🎯
Focusing

stilllisisi

🎯
Focusing
View GitHub Profile
@stilllisisi
stilllisisi / exception.py
Last active February 18, 2020 03:14 — forked from banjin/优秀代码集合...
【python-异常处理】忽略异常的处理
忽略抛出的异常:
常用的写法:
try:
os.remove('somefile.tmp')
except OSError:
pass
@stilllisisi
stilllisisi / callee.js
Last active February 18, 2020 03:12 — forked from dreamsky/callee.js
【JavaScript-列表/字典处理】优秀的 JavaScript 代码片段集合
//JavaScript callee 用法示例
function factorial(num) {
if (num <= 1) {
return 1;
} else {
return num * arguments.callee(num - 1);
}
}
@stilllisisi
stilllisisi / gist:9409eda29e3a1828c2e8627b7b0501fa
Last active February 18, 2020 03:11 — forked from Quasimo/gist:1139189
【python-字符串/正则表达式】新手常用代码片段
# 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()#读取全部内容
@stilllisisi
stilllisisi / aync_sched.py
Last active February 18, 2020 03:09
【python-任务调度】使用协同程序实现的异步调度程序,原则上类似于 Tornado 的 ioloop
# 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
@stilllisisi
stilllisisi / rpc.py
Last active February 18, 2020 03:08
【python-网络通信】这是一个用socket实现的简单的python RPC服务器/客户端。* 通过客户端/服务器共用的密码认证客户端* 使用JSON序列化函数调用有效负载
"""
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
@stilllisisi
stilllisisi / open_todo.py
Last active February 18, 2020 03:07
【python-文件/目录处理】每天打开一个新的 markdown todo 文件的简单脚本。
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')
@stilllisisi
stilllisisi / resub.py
Last active February 18, 2020 03:06
【python-字符串/正则表达式】正则调整文本格式
import re
s = "1991-02-28"
re.sub(r"(\d{4})-(\d{2})-(\d{2})", r'\1/\2/\3')
Out[6]: '1991/02/28'
@stilllisisi
stilllisisi / python2to3
Last active February 18, 2020 03:05
【python-兼容】python2中有12个内置功能在Python3中已经被移除了。要确保在Python2代码中不要出现这些功能来保证对Python3的兼容。这有一个强制让你放弃12内置功能的方法:
from future.builtins.disabled import *
@stilllisisi
stilllisisi / sets.py
Last active February 18, 2020 03:04
【python-列表/字典处理】使用sets计算交集和差集
set1 = {1,2,3,4}
set2 = {3,4,5,6}
set1.intersection(set2)#交集
set1.difference(set2)#差集
@stilllisisi
stilllisisi / groupby.py
Last active February 18, 2020 03:04
【python-列表/字典处理】使用groupby将列表元素分类
from itertools import groupby
def height_class(h):
if h> 180:
return "tall"
elif h<160:
return "short"
else:
return "middle"