File: virtual_array_caching.py

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (51 lines) | stat: -rw-r--r-- 1,505 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from collections.abc import MutableMapping
import collections

counter = [0]
def generate():
    counter[0] += 1
    dict_generate = {1: 'Ferrari', 2: 'Mercedes', 3: 'Red Bull', 4: 'McLaren'}
    return dict_generate[counter[0]]

class Cache():
    def __init__(self, )

class VirtualArray():
    def __init__(self, generator, cache=None, cache_key=None):
        assert callable(generator)
        if cache is not None:
            assert isinstance(cache, collections.abc.MutableMapping)
        if cache_key is not None:
            assert isinstance(cache_key, str)
        self.generator = generator
        self.cache = cache
        self.cache_key = cache_key

    @staticmethod
    def random(minlen, choices):
        raise NotImplementedError("FIXME!")

    def __len__(self):
        return len(self.cache)

    def __getitem__(self, where):
        return self.cache[where]

    def __repr__(self):
        return '{}, D({})'.format(super(VirtualArray, self).__repr__(), 
                                  self.__dict__)

    def xml(self, indent="", pre="", post=""):
        raise NotImplementedError("FIXME!")

    def array(self):
        if(self.cache != None):
            array = self.cache[self.cache_key]
        else:
            array = self.generator()
        if(self.cache != None):
            self.cache_key = str(counter[0])
        return array

va = VirtualArray(generate, {"ak0": 'Ferrari', "ak1": 'Mercedes', "ak2": 'Red Bull', "ak3": 'McLaren'}, "ak2")
print(va.array())