Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
ayuLiao / pythonjson.py
Last active June 15, 2018 08:48
json格式化时,添加自己自定义动作
import json
import datetime
# json格式化时自定义动作
class CJsonEncoder(json.JSONEncoder):
def default(self,obj):
if isinstance(obj,datetime.datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj,datetime.date):
return obj.strftime('%Y-%m-%d')
@ayuLiao
ayuLiao / sort_list_dict.py
Last active July 10, 2018 10:03
python中list的元素为dict,通过dict中具体某个元素,进行list排序
# 使用匿名函数进行排序,其中k就是list中的元素,也就是一个dict
newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
#使用itemgetter
from operator import itemgetter
# reverse=True表示降序
newlist = sorted(list_to_be_sorted, key=itemgetter('name'),reverse=True)
@ayuLiao
ayuLiao / get_url.js
Last active July 2, 2018 02:53
JavaScript获取url中的值
var getQueryString = function (name, url) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var search;
if (url === undefined) {
url = decodeURI(location.href);
}
var index = url.indexOf("?");
search = url.substring(index);
var r = search.substr(1).match(reg);
if (r != null)return unescape(r[2]);
@ayuLiao
ayuLiao / form.html
Created July 3, 2018 04:13
form表单提交,通过jquery简单实现
<form class='form-horizontal group-border-dashed' action="{{info['subUrl']}}" method='POST' id='broadcastForm' onSubmit='return false'>
<div class="form-group">
<label class="col-sm-3 control-label">人数</label>
<small>满多少人送红包</small>
<div class="col-sm-6">
<input style="width:100%;float:left;" class="form-control" type="text" name="peoplenum" id="peoplenum" >
<label class="hitLabel" for="peoplenum">*</label>
</div>
</div>
@ayuLiao
ayuLiao / tmux.sh
Last active July 6, 2018 08:37
tmux会开启一个新进程,在新进程中操作linux
#列出所有会话
$ tmux ls
#新建一个会话
$ tmux new -s session-name
# 新建会话但并不指定名字 (不推荐这样做)
$ tmux new
@ayuLiao
ayuLiao / num4file.py
Last active August 3, 2018 13:19
python统计文件夹中的文件数
#统计文件夹下文件的数目
import os
DIR = '/tmp' #要统计的文件夹
print(len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))]))
@ayuLiao
ayuLiao / rmrediskey.sh
Created August 14, 2018 06:04
删除redis中的key,其实也把redis中的值删除了,通过一句shell命令则可以做到
-a:redis密码
-n:选择Redis中哪个数据库
keys:找出符合语法的redis key
通过管道,使用xargs将搜索到的key传递给redis,进行删除
redis-cli -a Fkkg65NbRwQOnq01OGMPy5ZREsNUeURm -n 1 keys gold:canjoin:room:*:*:set | xargs redis-cli -a Fkkg65NbRwQOnq01OGMPy5ZREsNUeURm -n 1 del
@ayuLiao
ayuLiao / showimg.py
Last active September 11, 2018 15:14
matplotlib显示图像
import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
import numpy as np
'''
显示图片
'''
lena = mpimg.imread('lena.png') # 读取和代码处于同一目录下的 lena.png
@ayuLiao
ayuLiao / killpid2mac.sh
Last active May 19, 2020 02:35
查看Mac下占用端口的进程,然后关闭它
1.查看占用的端口
终端输入:lsof -i tcp:port
将port换成被占用的端口(如:8086、9998)
将会出现占用端口的进程信息
或者使用: lsof -i :5000 (看对应的端口号则可)
2.根据pid kill掉进程
@ayuLiao
ayuLiao / getfiletime.py
Last active October 13, 2018 04:50
python读文件的创建修改时间
import time
import datetime
import os
def timestamptotime(timestamp):
'''
时间戳转时间
:param timestamp: 时间戳
:return: 时间
'''