Conditions | 2 |
Total Lines | 49 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Tests | 1 |
CRAP Score | 5.1463 |
Changes | 0 |
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # -*- coding: utf-8 -*- |
||
10 | 10 | def search(self, |
|
11 | query, |
||
12 | category, |
||
13 | uid=None, |
||
14 | latitude=None, |
||
15 | longitude=None, |
||
16 | city=None, |
||
17 | region=None): |
||
18 | """ |
||
19 | 发送语义理解请求 |
||
20 | 详情请参考 |
||
21 | http://mp.weixin.qq.com/wiki/0/0ce78b3c9524811fee34aba3e33f3448.html |
||
22 | |||
23 | :param query: 输入文本串 |
||
24 | :param category: 需要使用的服务类型,多个可传入列表 |
||
25 | :param uid: 可选,用户唯一id(非开发者id),用户区分公众号下的不同用户(建议填入用户openid) |
||
26 | :param latitude: 可选,纬度坐标,与经度同时传入;与城市二选一传入 |
||
27 | :param longitude: 可选,经度坐标,与纬度同时传入;与城市二选一传入 |
||
28 | :param city: 可选,城市名称,与经纬度二选一传入 |
||
29 | :param region: 可选,区域名称,在城市存在的情况下可省;与经纬度二选一传入 |
||
30 | :return: 返回的 JSON 数据包 |
||
31 | |||
32 | 使用示例:: |
||
33 | |||
34 | from wechatpy import WeChatClient |
||
35 | |||
36 | client = WeChatClient('appid', 'secret') |
||
37 | res = client.semantic.search( |
||
38 | '查一下明天从北京到上海的南航机票', |
||
39 | 'flight,hotel', |
||
40 | city='北京' |
||
41 | ) |
||
42 | |||
43 | """ |
||
44 | if isinstance(category, (tuple, list)): |
||
45 | category = ','.join(category) |
||
46 | |||
47 | data = optionaldict() |
||
48 | data['query'] = query |
||
49 | data['category'] = category |
||
50 | data['uid'] = uid |
||
51 | data['latitude'] = latitude |
||
52 | data['longitude'] = longitude |
||
53 | data['city'] = city |
||
54 | data['region'] = region |
||
55 | data['appid'] = self._client.appid |
||
56 | return self._post( |
||
57 | url='https://api.weixin.qq.com/semantic/semproxy/search', |
||
58 | data=data |
||
59 | ) |
||
60 |