I got errno=0 which means Flask is working fine with SSL. :D
PEP8 standards - Style Guide
Logging
Good logging practice
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info('Start reading database')
# read database here
records = {'john': 55, 'tom': 66}
logger.debug('Records: %s', records)
logger.info('Updating records ...')
# update records here
logger.info('Finish updating records')
Output:
INFO:__main__:Start reading database
INFO:__main__:Updating records ...
INFO:__main__:Finish updating records
change the logger level to DEBUG and see the output again
logging.basicConfig(level=logging.DEBUG)
Output:
INFO:__main__:Start reading database
DEBUG:__main__:Records: {'john': 55, 'tom': 66}
INFO:__main__:Updating records ...
INFO:__main__:Finish updating records
logging 教學
Numpy
Tips
Pandas
Tips
Tips
Determine if variable is defined
'a' in vars() or 'a' in globals()
if you want to be pedantic, you can check the builtins too
'a' in vars(__builtins__)
Code Example
list of dictionaries search
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
people = [
{'name': "Tom", 'age': 10},
{'name': "Mark", 'age': 5},
{'name': "Pam", 'age': 7}
]
def searchListDict(dataList, key='name', value=None):
'''
Searching a list of Dictionaries
2018-05-16
Param: List of Dictionaries
Return: Dictionary
'''
try:
for l in dataList:
if l[key] == value:
return l
logger.info('No Matching')
return None # return None if finised looping & no matching
except:
logger.error('ERROR:', exc_info=True)
person = searchListDict(people, key='name', value='Pamx')
print(person)
min(None, x)
min(filter(lambda x: x is not None, lst)) if any(lst) else None
In [3]: lst = [None, 1, None]
In [4]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
Out[4]: 1
In [5]: lst = [-4, None, 11]
In [6]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
Out[6]: -4
In [7]: lst = [0, 7, -79]
In [8]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
Out[8]: -79
In [9]: lst = [None, None, None]
In [10]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
In [11]: print(min(filter(lambda x: x is not None, lst)) if any(lst) else None)
None
def keywithmaxval(d):
""" a) create a list of the dict's keys and values;
b) return the key with the max value"""
v=list(d.values())
k=list(d.keys())
return k[v.index(max(v))]
Tools
Visual Studio Code
Top 10 Visual Studio Code extensions for Python development
8 VS Code Extensions You Might Love
Setting Sync
Kite
Automated RESTful API testing
Tavern
Protect code
Troubleshoot
Mac OS X: ValueError: unknown locale: UTF-8
add these lines to your ~/.bash_profile
~/.bash_profile
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
ERROR: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is
FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type. from ._conv import register_converters as _register_converters
Problem: h5py issue (2.7.1
Solution: upgrade h5py (2.9.0)
pip install --upgrade h5py
17個新手常見的Python運行時錯誤
ERROR: ImportError: cannot import name 'Flask'
Problem: error while import 'flask'
>>> from flask import Flask
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/ryan/Python/github/flask/flask.py", line 9, in <module>
from flask import Flask, jsonify, request, session
ImportError: cannot import name 'Flask'
Solution: The reason is your python file name is 'flask' in same directory.
Python的中文編碼處理
Library
Python library 集合
Reference:
Deploying a Machine Learning Model as a REST API
Flask設置返回json格式數據
Multi-processing
Web Scrapping
Parse Youtube link to iframe embeded code
Html2text - Convert HTML to Markdown-formatted text