1:
Python pass是空语句,是为了保持程序结构的完整性。
pass 不做任何事情,一般用做占位语句。
2:
redis的使用
sudo pip install redis
import redis
rdata = redis.StrictRedis(host='localhost', port=6379, db=0)
rdata.hget(key,item)
rdata.hset(key, item, value)
3:
httpclient请求
sudo pip install tornado
import tornado.httpclient import tornado.httpserver import tornado.gen
rpc = tornado.httpclient.AsyncHTTPClient() response = yield rpc.fetch(rpc_url, method='POST', body=body) raise tornado.gen.Return(response)
4:清空字符串中所有空格
# strip whitespace, conform to string output def strclean(instr): if type(instr) is str: return ' '.join(instr.split()) elif type(instr) is bytes: return ' '.join(instr.decode('utf-8').split())
5:json使用
import json
object to jsonobject:
requested = json.loads(request)
jsonobject to jsonstring
reply = json.dumps(newdict)
6:日志使用
import logging from logging.handlers import WatchedFileHandler
handler = WatchedFileHandler(os.environ.get("NANO_LOG_FILE", "nanocast.log")) formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s", "%Y-%m-%d %H:%M:%S %z") handler.setFormatter(formatter) root = logging.getLogger() root.setLevel(os.environ.get("NANO_LOG_LEVEL", "INFO")) root.addHandler(handler)
logging.info('Starting NANOCast Server')
7:获取当前秒数
time.time()
8:生成uuid值
import uuid
str(uuid.uuid4())
9:获取系统变量
import os
os.getenv(key, default_value)
10:定时执行任务
# push latest price data to all subscribers every minute tornado.ioloop.PeriodicCallback(send_prices, 60000).start() tornado.ioloop.IOLoop.instance().start()