Completed
Push — master ( aeecc3...4c7ea9 )
by Jace
03:20
created

Cache.add()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
c 2
b 0
f 0
dl 0
loc 11
ccs 6
cts 8
cp 0.75
crap 3.1406
rs 9.4285
1 1
import logging
2
3 1
import yorm
0 ignored issues
show
Configuration introduced by
The import yorm could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4 1
from yorm.types import List, Object
0 ignored issues
show
Configuration introduced by
The import yorm.types could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5
6
7 1
log = logging.getLogger(__name__)
8
9
10 1
@yorm.attr(items=List.of_type(Object))
11 1
@yorm.sync("data/images/cache.yml")
12
class Cache:
13
14 1
    SIZE = 9
15
16 1
    def __init__(self):
17 1
        self.items = []
18
19 1
    def add(self, **kwargs):
20 1
        if kwargs['key'] == 'custom':
21
            return
22
23 1
        log.info("Caching: %s", kwargs)
24
25 1
        self.items.insert(0, kwargs)
26 1
        while len(self.items) > self.SIZE:
27
            self.items.pop()
28
29 1
        yorm.save(self)
30
31 1
    def get(self, index):
32 1
        log.info("Getting cache index: %s", index)
33
34 1
        try:
35 1
            data = self.items[index]
36 1
        except IndexError:
37 1
            data = {}
38
39 1
        log.info("Retrieved cache: %s", data)
40
41
        return data
42