Algorithm
Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:1
2
3
4Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Answer:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
length = len(nums)
if length <= 1:
return []
for i in range(length-1):
for j in range(i+1, length):
if nums[i]+nums[j]==target:
return [i,j]
return []
Rewiew
https://medium.freecodecamp.org/code-comments-the-good-the-bad-and-the-ugly-be9cc65fbf83
文章介绍代码中好的、坏的、丑陋的注释。框架或者库类的注释,最好能使用某些工具与线上文档关联,这样就可以保证文档的实时性。在代码中容易让人迷惑的地方添加注释,可以减少后期维护者的时间。不要在代码中添加无用的、宣泄类的注释。
Tip
Flask Flask_Restful自定义错误处理函数
近期在编写server端代码时,准备使用falsk_restful框架,编写过程中发现该框架提供的自定义错误处理方式有限,flask通过注册和装饰器的方式即可实现自定义错误处理。因而对两个框架的自定义错误处理做了研究。
Flask
Flask提供两种方式注册自定义错误处理函数:
- errorhandler 装饰器
register_error_handler 函数直接注册
两种方式都可以针对响应码或特定异常注册对应的处理函数1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
class DefinedException(Exception):
pass
app = Flask(__name__)
# @app.errorhandler(DefinedException)
def handle_ex(e):
return "handle ex"
@app.route("/test_flask", methods=['GET'])
def test_flask():
raise DefinedException()
app.register_error_handler(DefinedException, handle_ex)
app.run()运行上述代码后,访问http://127.0.0.1:5000/test_flask, 响应为: handle ex。通过自定义的异常处理函数,可以针对特定异常定制特殊操作。
Flask_Restful
对于有flask自定义异常的应用,对于flask_restful抛出的异常,并不会由flask框架定义的异常处理,flask_restful会直接处理框架内抛出的异常。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask_restful import Api, Resource
class DefinedException(Exception):
pass
app = Flask(__name__)
api = Api(app)
@app.errorhandler(DefinedException)
def handle_ex(e):
return "handle ex"
@app.route("/test_flask", methods=['GET'])
def test_flask():
raise DefinedException()
class FlaskTest(Resource):
def get(self):
raise DefinedException
api.add_resource(FlaskTest, '/test_flask_restful')
app.run()服务启动后,访问http://localhost:5000/test_flask_restful, 得到响应码为500的响应。
flask_restful支持自定义错误消息。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask
from flask_restful import Api, Resource
from flask_restful import got_request_exception
class DefinedException(Exception):
pass
errors = {
'DefinedException': {
'message': "A user with that username already exists.",
'status': 200,
},
'ResourceDoesNotExist': {
'message': "A resource with that ID no longer exists.",
'status': 410,
'extra': "Any extra information you want.",
},
}
app = Flask(__name__)
api = Api(app, errors=errors)
@app.errorhandler(DefinedException)
def handle_ex(e):
return "handle ex"
@app.route("/test_flask", methods=['GET'])
def test_flask():
raise DefinedException()
class FlaskTest(Resource):
def get(self):
raise DefinedException
api.add_resource(FlaskTest, '/test_flask_restful')
app.run()同样访问http://localhost:5000/test_flask_restful,得到响应码为200的响应 {“message”: “A user with that username already exists.”, “status”: 200}。
目前来看,flask_restful支持的自定义异常处理方式,不如flask框架灵活。
Share
https://mp.weixin.qq.com/s/G-BCXiWJucUzfzvWG2BIQw
CI/CD主要流程及功能大家都比较清晰,如何推进公司的CI/CD进程与建设,自上而下的组织支持对于公司实施与推进CI/CD流程相当重要。