Created
September 12, 2016 03:14
-
-
Save arischow/f6700025f05e3663fb2a7702b0d14c8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Py 文件所在目录 | |
import os | |
print(__file__) # 包括文件名 | |
print(os.path.dirname(os.path.abspath(__file__))) | |
# REDIS SET ADD | |
import redis | |
r = redis.Redis(host='localhost', port=6379, db=0) | |
r.sadd('token', '12345') # Key, Value respectively | |
print(r.sismember('token', '12345')) # True | |
print(r.sismember('token', '23232')) # False | |
print(r.sismember('javascript', '12345')) # False | |
# Token 唯一值算法,高性能 | |
# http://blog.useasp.net/archive/2015/11/08/performance-compare-of-python-unique-token-generation-algorithms.aspx | |
import os | |
import binascii | |
token=binascii.b2a_base64(os.urandom(24))[:-1] | |
print(token.decode('utf-8')) | |
# 深入理解 None | |
list1 = [] | |
if list1: | |
print('not empty') | |
else: | |
print('empty') | |
# PARSE JSON | |
import json | |
from pprint import pprint | |
a = { | |
'name': 'ArisX', | |
'age': 12, | |
'height': 178, | |
} | |
print(type(json.dumps(a))) # str type | |
print(json.dumps(a)) # a dict-like str | |
a_json = json.dumps(a) | |
print(type(json.loads(a_json))) | |
print(json.loads(a_json)) | |
pprint(a_json) | |
pprint(json.loads(a_json)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment