Conditions | 8 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
23 | |||
24 | |||
25 | def remove_session(session): |
||
26 | try: |
||
27 | del session[to_binary("fromUser")] |
||
28 | except: |
||
29 | pass |
||
30 | |||
31 | |||
32 | def test_session(): |
||
33 | robot = werobot.WeRoBot(token=werobot.utils.generate_token(), |
||
34 | enable_session=True) |
||
35 | |||
36 | @robot.text |
||
37 | def first(message, session): |
||
38 | if 'last' in session: |
||
39 | return |
||
40 | session['last'] = message.content |
||
41 | return message.content |
||
42 | |||
43 | @robot.text |
||
44 | def second(_, session): |
||
45 | return session['last'] |
||
46 | |||
47 | tester = werobot.testing.WeTest(robot) |
||
48 | xml_1 = """ |
||
49 | <xml> |
||
50 | <ToUserName><![CDATA[toUser]]></ToUserName> |
||
51 | <FromUserName><![CDATA[fromUser]]></FromUserName> |
||
52 | <CreateTime>1348831860</CreateTime> |
||
53 | <MsgType><![CDATA[text]]></MsgType> |
||
54 | <Content><![CDATA[ss]]></Content> |
||
55 | <MsgId>1234567890123456</MsgId> |
||
56 | </xml> |
||
57 | """ |
||
58 | xml_2 = """ |
||
59 | <xml> |
||
60 | <ToUserName><![CDATA[toUser]]></ToUserName> |
||
61 | <FromUserName><![CDATA[fromUser]]></FromUserName> |
||
62 | <CreateTime>1348831860</CreateTime> |
||
63 | <MsgType><![CDATA[text]]></MsgType> |
||
64 | <Content><![CDATA[dd]]></Content> |
||
65 | <MsgId>1234567890123456</MsgId> |
||
66 | </xml> |
||
67 | """ |
||
68 | |||
69 | try: |
||
70 | os.remove(os.path.abspath("werobot_session")) |
||
71 | except OSError: |
||
72 | pass |
||
73 | session_storages = [ |
||
74 | filestorage.FileStorage(), |
||
75 | mongodbstorage.MongoDBStorage(pymongo.MongoClient().t.t), |
||
76 | redisstorage.RedisStorage(redis.Redis()), |
||
77 | sqlitestorage.SQLiteStorage(), |
||
78 | ] |
||
116 |