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 RedisStorage(SessionStorage): |
|
|||
9 | |||
10 | 10 | def __init__(self, redis, prefix='wechatpy'): |
|
11 | 10 | for method_name in ('get', 'set', 'delete'): |
|
12 | 10 | assert hasattr(redis, method_name) |
|
13 | 10 | self.redis = redis |
|
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.redis.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.redis.set(key, value, ex=ttl) |
|
32 | |||
33 | 10 | def delete(self, key): |
|
34 | key = self.key_name(key) |
||
35 | self.redis.delete(key) |
||
36 |