# python相关参考文献_[原] Python 开发者面向文档编程的正确姿势

Python

def factorial(n):

"""Return the factorial of n, an exact integer >= 0.

>>> [factorial(n) for n in range(6)]

[1, 1, 2, 6, 24, 120]

>>> factorial(30)

265252859812191058636308480000000

>>> factorial(-1)

Traceback (most recent call last):

...

ValueError: n must be >= 0

Factorials of floats are OK, but the float must be an exact integer:

>>> factorial(30.1)

Traceback (most recent call last):

...

ValueError: n must be exact integer

>>> factorial(30.0)

265252859812191058636308480000000

It must also not be ridiculously large:

>>> factorial(1e100)

Traceback (most recent call last):

...

OverflowError: n too large

"""

import math

if not n >= 0:

raise ValueError("n must be >= 0")

if math.floor(n) != n:

raise ValueError("n must be exact integer")

if n+1 == n: # catch a value like 1e300

raise OverflowError("n too large")

result = 1

factor = 2

while factor <= n:

result *= factor

factor += 1

return result

if __name__ == "__main__":

import doctest

doctest.testmod()

apidoc为Python提供了一种类似于 docstring 的方式来写API文档，从语法上看比较类似于 R中的roxygen，都需要用户以 @xxx 符号作为一个开头，随后书写相关的定义和功能。

GET/POST方法

（这年头没有用例的代码都是耍流氓）

"""

@api {get} /user/:id Request User information

@apiName GetUser

@apiGroup User

@apiParam {Number} id Users unique ID.

@apiSuccess {String} firstname Firstname of the User.

@apiSuccess {String} lastname Lastname of the User.

"""

git clone https://github.com/apidoc/apidoc

cd apidoc

sudo npm install apidoc -g

apidoc -i example/ -o output/ -t template/

open output/index.html

-i:input，表示输入的文件夹

-o:output，表示输出文件夹

-t:template，表示模板文件，通过替换模板我们可以修改文档皮肤

apidoc 还为我们提供了接口调试的功能，在实际使用的时候要注意：

"""Usage:

fiannceR.py tcp [--timeout=]

fiannceR.py serial [--baud=9600] [--timeout=]

fiannceR.py -h | --help | --version

"""

from docopt import docopt

if __name__ == '__main__':

arguments = docopt(__doc__, version='0.1.1rc')

print(arguments)

fiannceR.py tcp 0.0.0.0 3838

{'--baud': None,

'--help': False,

'--timeout': None,

'--version': False,

'-h': False,

'': '0.0.0.0',

'': '3838',

'serial': False,

'tcp': True}

"""unitest

>>> FinanceR('20161001')

21.01

"""

def FinanceR(date):

price = get_price(date)

return(price)

class(BaseHandler):

def get(self):

"""apidoc

@api {get} /price/:date 获取当前价格

@apiName GetPrice

@apiGroup Quota

@apiParam {Number} date 交易日期

@apiSuccess {String} price

"""

date = self.get_argument('date',None)

try:

price = FinanceR(date)

self.write({'data':{'price':price},'response':{'message':'success','code':200}})

except Exception as e:

self.write({'data':None,'response':{'message':str(e),'code':404}})

"""Usage:

fiannceR.py tcp [--timeout=]

fiannceR.py serial [--baud=9600] [--timeout=]

fiannceR.py -h | --help | --version

"""

from docopt import docopt

if __name__ == '__main__':

arguments = docopt(__doc__, version='0.1.1rc')

print(arguments)

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

07-27
12-10 7708

02-20 2万+
11-11 3933
03-20 4824
07-21 7677
01-28
02-21 7715