/images/avatar.jpeg

Python基础操作

字典

合并两个字典 merge two dictionaries

dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
dict3 = {**dict1, **dict2} # python3.5+ 写法
dict3 = dict(dict1, **dict2) # python3.5- 写法
print(dict3)

字典按值排序 sort a Python dict by value

xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
for k, v in sorted(xs.items(), key=lambda x: x[1]):
    print(k, v)
import operator
xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
for k, v in sorted(xs.items(), key=operator.itemgetter(1)):
    print(k, v)

春语

四月的风,撩乱倒映水中白云的心。

迁徙的鸟,勾起岸旁垂杨柳的思恋。

迷乱的花,怎能敌四季长青的松乔。

暮霭晨雾,是否也是春姑娘的话语。

MongoDB常用操作命令

import pymongo

myclient = pymongo.MongoClient(host="localhost", port=27017)
mydb = myclient.database_name
collection_obj = mydb.collection_name
# upsert 是否插入新数据,如果不存在则插入,存在则更新 
collection_obj.update_one({"id": "1"}, {"$set": {"id": "1", "age": 18}}, upsert=True)

统计 collection信息

db.getCollection('collection_name').stats()

RCA-MongoDB数据写入失败

问题现象

程序崩溃,提示MongoDB写入失败,无法再连起。

分析原因

1.首先想到分析mongoDB日志记录 通过 cat /etc/mongod.conf 找到日志所在目录 /var/log/mongodb/mongod.log

2018-11-07T16:50:44.165+0800  W FTDC     [ftdc] Uncaught exception in 'FileStreamFailed: Failed to write to interim file buffer for full-time diagnostic data capture: /var/lib/mongo/diagnostic.data/metrics.interim.temp' in full-time diagnostic data capture subsystem. Shutting down the full-time diagnostic data capture subsystem.
2018-11-07T16:51:30.913+0800 E STORAGE  [WTCheckpointThread] WiredTiger error (28):handle-write: pwrite: failed to write 4096 bytes at offset 1486848: No space left on device
2018-11-07T16:51:30.914+0800 E STORAGE  [WTCheckpointThread] WiredTiger error (28): fatal checkpoint failure: No space left on device
2018-11-07T16:51:30.914+0800 E STORAGE  [WTCheckpointThread] WiredTiger error (-31804)  WT_SESSION.checkpoint: the process must exit and restart: WT

日志反馈的信息很明确,“磁盘已被写满啦!”, 但是很奇怪,写入量并不大,且只有唯一任务在执行,写满是不可能的。 可能想到的问题是蠕虫病毒,或是由程序递归,死循环等造成的错误数据写入。

使用supervisor监控管理Linux服务进程

Supervisor Docs

使用ssh远程维护VPS时候,要在后台挂起一个程序。挂起程序很简单,在command后加上**&符号就可以,但是当断开ssh时候,挂起的程序也随之killed, 很是恼火。在使用nohup** 的时候意外发现了supervisor, supervisor使用python开发,通过配置文件来配置需要启动监管的程序,作为supervisord的子进程。执行程序supervisord即‘守护进程’(Daemon)[在希腊神话中是守护神的意思!类似中国的护法?daemon这个词和恶魔(demon)只差一个字母,且发音完全一样!多的a这个品质可理解为全神关注的(absorbed)/精确的(accurate)/能干的(able)/乐于助人的(accommodating)/友好的(amicable)]。类似httpd, mysqld最后的d字母意为守护程序。

ubuntu16.04(18.04)下编译安装 postgresql v10.3

编译安装

# downloads postgresql-10.3.tar.gz
wget https://ftp.postgresql.org/pub/source/v10.3/postgresql-10.3.tar.gz
wget https://ftp.postgresql.org/pub/source/v10.3/postgresql-10.3.tar.gz.md5
wget https://ftp.postgresql.org/pub/source/v10.3/postgresql-10.3.tar.gz.sha256

# MD5 check 检查数据是否被篡改
cat postgresql-10.3.tar.gz.md5
md5sum  postgresql-10.3.tar.gz

# extract
tar -xvf postgresql-10.3.tar.gz

cd postgresql-10.3/

# 安装文档介绍的很详细
cat INSTALL

./configure

# 提示缺少readline library, 尝试安装libreadline5-dev 提示已被libreadline-gplv2-dev 替代, 安装之
checking for library containing readline... no
configure: error: readline library not found

sudo apt install libreadline5-dev

However the following packages replace it:
  libreadline-gplv2-dev:i386 libreadline-gplv2-dev

sudo apt install libreadline-gplv2-dev


checking for inflate in -lz... no
configure: error: zlib library not found
# 又提示缺失zlib library, 安装之
sudo apt install zlib1g-dev 

# 后边编译安装就顺利了:)
make
sudo make install
sudo adduser postgres
sudo mkdir /usr/local/pgsql/data
sudo chown postgres /usr/local/pgsql/data
su - postgres
# 初始化数据库
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
or
initdb -D  /usr/local/pgsql/data

# start the database server
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &
or
pg_ctl -D /usr/local/pgsql/data -l logfile start
# 测试成功
/usr/local/pgsql/bin/createdb test
/usr/local/pgsql/bin/psql test

# set postgresql env to .bashrc
export PGHOME=/usr/local/pgsql
export PGDATA=/usr/local/pgsql/data
export PATH=$PGHOME/bin:$PATH:$HOME/bin

创建用户

# CREATEDB是权限,还有其它权限,比如SUPERUSER、CREATEUSER等。一般情况下CREATEDB是最安全的做法。因为这个test用户的权限被限制在很小范围。
CREATE USER username CREATEDB; 
# 修改用户密码
ALTER USER username PASSWORD 'password';

设置远程访问权限

# edit postgresql.conf '*' replace 'localhost'
vim /usr/local/pgsql/data/postgresql.conf
#listen_addresses = 'localhost'
listen_addresses = '*'

# edit pg_hba.conf
vim /usr/local/pgsql/data/pg_hba.conf

# IPv4 local connections: add username authentication
host    all             all             127.0.0.1/32            trust
host    all             username        0.0.0.0/0               md5

# 重启使之生效
su - postgres
pg_ctl restart

我为什么喜爱尊巴

  • 不全身心投入地跳一次, 永无法理解Zumba的魅力.

第一次来到健身房的我,对桑巴,尊巴,POP,普拉提,阿斯汤加等课程的新鲜名词一丝概念也没有,仿佛发现了新大陆。按我以往对新事物的态度,必然会在网上检索一番。然后针对自己的特长喜好来做出选择。但我这次想要把所有的课程都亲身体验一遍。“实践是检验真理的唯一标准”,亲自体验才能真正了解自己想要的。也考虑到了,即使自己没有任何基础,跟不上节奏,我也要坚持完一节课。我很庆幸自己做出了这样的选择,假使我只是隔窗相望, 看到的必然是一副"群魔乱舞"的景象而心存芥蒂。

阿斯汤加瑜伽学习笔记-唱诵经文

  • 祈祷式: for the beginning of Ashtanga Yoga practice
  • Sanskrit Mantra/梵语唱诵: CHANT-PATANJALI
OM 
vande gurunam caranaravinde 
sandarsita svatma sukhava bodhe 
nih sreyase jangalikayamane 
samsara halahala mohasantyai
abahu purusakaram 
sankhacakrasi dharinam 
sahasra sirasam svetam 
pranamami patanjalim 
OM
OM 
I bow to the two lotus feet of the Gurus which awaken insight into the happiness of pure Being, 
which are the refuge, the jungle physician, which eliminate the delusion caused by the 
poisonous herb of Samara (conditioned existence)
I prostrate before the 
sage Patanjali who has thousands of radiant, white heads (as the divine 
serpent, Ananta) and who has, as far as his arms, assumed the form of a man holding a conch 
shell (divine sound), a wheel (discus of light or infinite time) and a sword (discrimination
) 
OM
OM
Vande = bow 
Gurunam = to the plurality of gurus 
Caranaravinde = two lotus feet
sandarsita = to bring to sight 
Svatma = pure Being 
Sukhava = happiness 
Bodhe = awakened
nishreyase = refuge 
jangalikayamane = 
jungle hut (meaning shaman, jungle physician)
samsara = conditioned existence 
halahala = poison 
moha = delusion 
santyai = peace
abahu = down to the shoulders 
purusakaram = assumes the form of a man
sankha = conch shell 
cakra = a wheel (discus of light
or infinite time) 
asi = sword (of discrimination) 
dharinam = holding
sahasra = thousands 
sirasam = heads 
svetam = radiant, white
pranamami = prostrate 
patanjalim = the sage Patanjali

我虔诚地向我最尊敬的导师俯身祈祷,他传授我知识,唤起我内心的巨大的快乐。他像一名丛林医生,能祛除在某种情况下由于存在毒素而生的幻念。帕坦加力(Patanjali),阿迪塞萨(Adisesa)的化身,他有白色的肌肤,长着一千个向四方延展的头(外形如蛇神),人身;手举一把歧视之剑,一个象征无穷时间的火轮和一个象征给他神的声音的海螺。我俯身致敬。