Total Complexity | 9 |
Total Lines | 28 |
Duplicated Lines | 100 % |
Coverage | 86.96% |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | # -*- coding: utf-8 -*- |
||
8 | 10 | View Code Duplication | class MemcachedStorage(SessionStorage): |
|
|||
9 | |||
10 | 10 | def __init__(self, mc, prefix='wechatpy'): |
|
11 | 10 | for method_name in ('get', 'set', 'delete'): |
|
12 | 10 | assert hasattr(mc, method_name) |
|
13 | 10 | self.mc = mc |
|
14 | 10 | self.prefix = prefix |
|
15 | |||
16 | 10 | def key_name(self, key): |
|
17 | 10 | return '{0}:{1}'.format(self.prefix, key) |
|
18 | |||
19 | 10 | def get(self, key, default=None): |
|
20 | 10 | key = self.key_name(key) |
|
21 | 10 | value = self.mc.get(key) |
|
22 | 10 | if value is None: |
|
23 | return default |
||
24 | 10 | return json.loads(to_text(value)) |
|
25 | |||
26 | 10 | def set(self, key, value, ttl=None): |
|
27 | 10 | if value is None: |
|
28 | 10 | return |
|
29 | 10 | key = self.key_name(key) |
|
30 | 10 | value = json.dumps(value) |
|
31 | 10 | self.mc.set(key, value) |
|
32 | |||
33 | 10 | def delete(self, key): |
|
34 | key = self.key_name(key) |
||
35 | self.mc.delete(key) |
||
36 |