Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
ayuLiao / turntable.py
Last active April 25, 2018 02:22
python抽奖代码
# turntable.py 幸运转盘抽奖
import random
import bisect
class WeightRandom:
def __init__(self, items):
weights = [w for _, w in items]
self.goods = [x for x, _ in items]
self.total = sum(weights)
self.acc = list(self.accumulate(weights))
@ayuLiao
ayuLiao / ajaxpost.html
Last active June 25, 2018 08:19
AJAX代码片段,按钮点击事件触发AJAX,通过POST方式将input中的内容发送给后端
<from>
<div class="form-group">
<label >工会ID</label>
<input class="form-control" id="parentId">
</div>
<div class="form-group">
<label >用户ID</label>
<input class="form-control" id="userId" >
</div>
<button type="submit" class="btn btn-default">提交</button>
@ayuLiao
ayuLiao / retry.py
Last active May 15, 2018 08:14
当调用网络请求方法或其他易报错的方法时,可以尝试多次调用,再每次失败后,再次调用它,为了让其正常运行
def retry(f, n_attempts=3):
# 包装函数,在函数异常时尝试多次调用,这里是尝试3次
def wrapper(*args, **kwargs):
for i in range(n_attempts):
try:
return f(*args, **kwargs)
except Exception:
if i == n_attempts - 1:
raise
return wrapper
@ayuLiao
ayuLiao / kill_process.sh
Created May 17, 2018 02:26
自己启动了后台运行的python脚本,可以通过关键字和端口号找到该脚本进程,再用kill命令将其杀死
ubuntu@iZwz92no3ygwxhh603stndZ:$ netstat -anutp | grep 9798 | grep LISTEN
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:9798 0.0.0.0:* LISTEN 11944/python
ubuntu@iZwz92no3ygwxhh603stndZ:$ kill 11944
@ayuLiao
ayuLiao / py_use_mysql.py
Last active October 19, 2018 08:29
公司旧项目使用python2,其独特的编码问题,给操作mysql时带来了些小问题
def initMySQL():
'''
链接mysql
:return:
'''
mysql_options = dict(
host="127.0.0.1",
db="数据表名称",
port=3306,
@ayuLiao
ayuLiao / dict_slice.py
Last active October 20, 2022 18:10
在python中字典是无法使用切片的,自定义一个方法,用于实现字典切片
#先取出所有 keys,再对 keys 切片,然后用得到的键去字典里找值重新创建一个新的字典
def dict_slice(adict, start, end):
keys = adict.keys()
dict_slice = {}
for k in keys[start:end]:
dict_slice[k] = adict[k]
return dict_slice
@ayuLiao
ayuLiao / jquery_flot.html
Last active June 25, 2018 08:20
jquery+flot实现折线图
<style>
.demo-placeholder {
height: 280px
}
</style>
<!--折线图会出现到这里-->
<div class="col-md-12 col-sm-12 col-xs-12">
@ayuLiao
ayuLiao / jquery_ajax.js
Last active January 19, 2019 11:08
使用 $.ajax 做ajax请求
var onLoginSuccess = function (data) {
location.reload();
}
$("#changemaintain").click(function(){
var p = $('#changemaintain').val();
if(p == '0'){
p = '1';
@ayuLiao
ayuLiao / add_new_element.js
Last active June 8, 2018 08:26
判断div中子元素数量是否超过8,超过,不能再动态添加 没有超过,则在改div中添加新的子元素
<div class="gameroomall">
<div class="gameroomayu">
</div>
</div>
function onAdd(){
var configTable = $('.gameroomall');
var num = parseInt(configTable.find('.gameroomayu').length);
if (num == 8){
//信息框,使用了layer插件,google搜layer.js导入layer.js和layer.css则可使用
@ayuLiao
ayuLiao / vcode_gen.py
Last active March 15, 2024 07:08
python生成验证码,这个漂亮点,代码量也小
from captcha.image import ImageCaptcha # pip install captcha
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import random
# 验证码中的字符, 就不用汉字了
number = ['0','1','2','3','4','5','6','7','8','9']
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
ALPHABET = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']