Completed
Push — master ( e6b198...e30f90 )
by Messense
11:09 queued 10:06
created

ShoveStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 69.57%
Metric Value
dl 0
loc 29
ccs 16
cts 23
cp 0.6957
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
A key_name() 0 2 1
A set() 0 6 2
A get() 0 6 2
A delete() 0 6 2
1
# -*- coding: utf-8 -*-
2 9
from __future__ import absolute_import, unicode_literals
3 9
from wechatpy.session import SessionStorage
4
5
6 9
class ShoveStorage(SessionStorage):
7
8 9
    def __init__(self, shove, prefix='wechatpy'):
9 9
        self.shove = shove
10 9
        self.prefix = prefix
11
12 9
    def key_name(self, key):
13 9
        return '{0}:{1}'.format(self.prefix, key)
14
15 9
    def get(self, key, default=None):
16 9
        key = self.key_name(key)
17 9
        try:
18 9
            return self.shove[key]
19
        except KeyError:
20
            return default
21
22 9
    def set(self, key, value, ttl=None):
23 9
        if value is None:
24 9
            return
25
26 9
        key = self.key_name(key)
27 9
        self.shove[key] = value
28
29 9
    def delete(self, key):
30
        key = self.key_name(key)
31
        try:
32
            del self.shove[key]
33
        except KeyError:
34
            pass
35