convert docstring to Google style

This commit is contained in:
debugtalk
2018-11-23 22:52:23 +08:00
parent cfd1d84189
commit 23cf17cb39
4 changed files with 123 additions and 66 deletions
+16 -10
View File
@@ -58,21 +58,27 @@ def load_json_file(json_file):
def load_csv_file(csv_file): def load_csv_file(csv_file):
""" load csv file and check file content format """ load csv file and check file content format
@param
csv_file: csv file path Args:
e.g. csv file content: csv_file (str): csv file path, csv file content is like below:
username,password
test1,111111 Returns:
test2,222222 list: list of parameters, each parameter is in dict format
test3,333333
@return Examples:
list of parameter, each parameter is in dict format >>> cat csv_file
e.g. username,password
test1,111111
test2,222222
test3,333333
>>> load_csv_file(csv_file)
[ [
{'username': 'test1', 'password': '111111'}, {'username': 'test1', 'password': '111111'},
{'username': 'test2', 'password': '222222'}, {'username': 'test2', 'password': '222222'},
{'username': 'test3', 'password': '333333'} {'username': 'test3', 'password': '333333'}
] ]
""" """
csv_content_list = [] csv_content_list = []
+21 -14
View File
@@ -150,20 +150,27 @@ def parse_function(content):
def parse_validator(validator): def parse_validator(validator):
""" parse validator, validator maybe in two format """ parse validator
@param (dict) validator
format1: this is kept for compatiblity with the previous versions. Args:
{"check": "status_code", "comparator": "eq", "expect": 201} validator (dict): validator maybe in two formats:
{"check": "$resp_body_success", "comparator": "eq", "expect": True}
format2: recommended new version format1: this is kept for compatiblity with the previous versions.
{'eq': ['status_code', 201]} {"check": "status_code", "comparator": "eq", "expect": 201}
{'eq': ['$resp_body_success', True]} {"check": "$resp_body_success", "comparator": "eq", "expect": True}
@return (dict) validator info format2: recommended new version
{ {'eq': ['status_code', 201]}
"check": "status_code", {'eq': ['$resp_body_success', True]}
"expect": 201,
"comparator": "eq" Returns
} dict: validator info
{
"check": "status_code",
"expect": 201,
"comparator": "eq"
}
""" """
if not isinstance(validator, dict): if not isinstance(validator, dict):
raise exceptions.ParamsError("invalid validator: {}".format(validator)) raise exceptions.ParamsError("invalid validator: {}".format(validator))
+45 -22
View File
@@ -15,7 +15,10 @@ class ResponseObject(object):
def __init__(self, resp_obj): def __init__(self, resp_obj):
""" initialize with a requests.Response object """ initialize with a requests.Response object
@param (requests.Response instance) resp_obj
Args:
resp_obj (instance): requests.Response instance
""" """
self.resp_obj = resp_obj self.resp_obj = resp_obj
@@ -36,11 +39,22 @@ class ResponseObject(object):
def _extract_field_with_regex(self, field): def _extract_field_with_regex(self, field):
""" extract field from response content with regex. """ extract field from response content with regex.
requests.Response body could be json or html text. requests.Response body could be json or html text.
@param (str) field should only be regex string that matched r".*\(.*\).*"
e.g. Args:
self.text: "LB123abcRB789" field (str): regex string that matched r".*\(.*\).*"
field: "LB[\d]*(.*)RB[\d]*"
return: abc Returns:
str: matched content.
Raises:
exceptions.ExtractFailure: If no content matched with regex.
Examples:
>>> # self.text: "LB123abcRB789"
>>> filed = "LB[\d]*(.*)RB[\d]*"
>>> _extract_field_with_regex(field)
abc
""" """
matched = re.search(field, self.text) matched = re.search(field, self.text)
if not matched: if not matched:
@@ -53,14 +67,17 @@ class ResponseObject(object):
def _extract_field_with_delimiter(self, field): def _extract_field_with_delimiter(self, field):
""" response content could be json or html text. """ response content could be json or html text.
@param (str) field should be string joined by delimiter.
e.g. Args:
"status_code" field (str): string joined by delimiter.
"headers" e.g.
"cookies" "status_code"
"content" "headers"
"headers.content-type" "cookies"
"content.person.name.first_name" "content"
"headers.content-type"
"content.person.name.first_name"
""" """
# string.split(sep=None, maxsplit=-1) -> list of strings # string.split(sep=None, maxsplit=-1) -> list of strings
# e.g. "content.person.name" => ["content", "person.name"] # e.g. "content.person.name" => ["content", "person.name"]
@@ -207,14 +224,20 @@ class ResponseObject(object):
def extract_response(self, extractors): def extract_response(self, extractors):
""" extract value from requests.Response and store in OrderedDict. """ extract value from requests.Response and store in OrderedDict.
@param (list) extractors
[ Args:
{"resp_status_code": "status_code"}, extractors (list):
{"resp_headers_content_type": "headers.content-type"},
{"resp_content": "content"}, [
{"resp_content_person_first_name": "content.person.name.first_name"} {"resp_status_code": "status_code"},
] {"resp_headers_content_type": "headers.content-type"},
@return (OrderDict) variable binds ordered dict {"resp_content": "content"},
{"resp_content_person_first_name": "content.person.name.first_name"}
]
Returns:
OrderDict: variable binds ordered dict
""" """
if not extractors: if not extractors:
return {} return {}
+41 -20
View File
@@ -64,8 +64,17 @@ def build_url(base_url, path):
def query_json(json_content, query, delimiter='.'): def query_json(json_content, query, delimiter='.'):
""" Do an xpath-like query with json_content. """ Do an xpath-like query with json_content.
@param (dict/list/string) json_content
json_content = { Args:
json_content (dict/list/string): content to be queried.
query (str): query string.
delimiter (str): delimiter symbol.
Returns:
str: queried result.
Examples:
>>> json_content = {
"ids": [1, 2, 3, 4], "ids": [1, 2, 3, 4],
"person": { "person": {
"name": { "name": {
@@ -76,11 +85,16 @@ def query_json(json_content, query, delimiter='.'):
"cities": ["Guangzhou", "Shenzhen"] "cities": ["Guangzhou", "Shenzhen"]
} }
} }
@param (str) query >>>
"person.name.first_name" => "Leo" >>> query_json(json_content, "person.name.first_name")
"person.name.first_name.0" => "L" >>> Leo
"person.cities.0" => "Guangzhou" >>>
@return queried result >>> query_json(json_content, "person.name.first_name.0")
>>> L
>>>
>>> query_json(json_content, "person.cities.0")
>>> Guangzhou
""" """
raise_flag = False raise_flag = False
response_body = u"response body: {}\n".format(json_content) response_body = u"response body: {}\n".format(json_content)
@@ -505,21 +519,28 @@ def create_scaffold(project_name):
def gen_cartesian_product(*args): def gen_cartesian_product(*args):
""" generate cartesian product for lists """ generate cartesian product for lists
@param
(list) args Args:
[{"a": 1}, {"a": 2}], args (list of list): lists to be generated with cartesian product
Returns:
list: cartesian product in list
Examples:
>>> arg1 = [{"a": 1}, {"a": 2}]
>>> arg2 = [{"x": 111, "y": 112}, {"x": 121, "y": 122}]
>>> args = [arg1, arg2]
>>> gen_cartesian_product(*args)
>>> # same as below
>>> gen_cartesian_product(arg1, arg2)
[ [
{"x": 111, "y": 112}, {'a': 1, 'x': 111, 'y': 112},
{"x": 121, "y": 122} {'a': 1, 'x': 121, 'y': 122},
{'a': 2, 'x': 111, 'y': 112},
{'a': 2, 'x': 121, 'y': 122}
] ]
@return
cartesian product in list
[
{'a': 1, 'x': 111, 'y': 112},
{'a': 1, 'x': 121, 'y': 122},
{'a': 2, 'x': 111, 'y': 112},
{'a': 2, 'x': 121, 'y': 122}
]
""" """
if not args: if not args:
return [] return []