Skip to content

Instantly share code, notes, and snippets.

View PoplarYang's full-sized avatar
🦖
Focusing

PoplarYang

🦖
Focusing
View GitHub Profile
# encoding: utf-8
""" return 返回的是完整的结果
yield 返回的是一个值"""
def myzip1(*seqs):
"""使用返回值"""
seqs = [list(S) for S in seqs]
res = []
while all(seqs):
@PoplarYang
PoplarYang / modreload.py
Last active July 5, 2018 04:20
python测试代码,用来测试重载
#!/usr/bin/env python
# coding: utf-8
import subprocess
def test(size=1024*64):
""" pip默认为 64k"""
print 'start'
cmd = 'dd if=/dev/urandom bs=1 count=%d 2>/dev/null' % size
p = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
@PoplarYang
PoplarYang / getProgTotalMem.py
Created March 11, 2017 03:20
获取进程所占物理内存
#!/usr/bin/env python
# coding: utf-8
import os
import sys
from subprocess import Popen, PIPE
def getPidList(prog_name):
"""获取进程的所有PID
输入:进程名
@PoplarYang
PoplarYang / django-mysql.py
Created March 20, 2017 13:06
django - mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'cmdb',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
@PoplarYang
PoplarYang / update_one
Created March 25, 2019 10:45
usage for pymongo
update_one(filter, update, upsert=False, bypass_document_validation=False, collation=None, array_filters=None, session=None)
Update a single document matching the filter.
>>> for doc in db.test.find():
... print(doc)
...
{u'x': 1, u'_id': 0}
{u'x': 1, u'_id': 1}
{u'x': 1, u'_id': 2}
>>> result = db.test.update_one({'x': 1}, {'$inc': {'x': 3}})
@PoplarYang
PoplarYang / json
Created March 25, 2019 10:49
usage for json module
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
# 行迭代
# while 循环法
while read line;do
echo $line;
done < file.txt
# 命令行法
cat file.txt | (while read line;do echo $line;done)
# awk法
function check_status() {
local app_name=$1
local app_port=$2
app_port_c=$(ss -antu | grep -c $app_port)
app_name_c=$(ps -ef | grep $app_name | grep -vc grep)
echo
}
@PoplarYang
PoplarYang / awk.sh
Last active November 22, 2019 02:09
awk '{$5~/status/}{now=strftime("%T", $3/10000000);print now}' ky | sort | uniq -c | awk 'BEGIN{sum=0;count=0}{sum+=$1;count++}END{print sum/count}'