-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory.py
More file actions
77 lines (65 loc) · 2.33 KB
/
memory.py
File metadata and controls
77 lines (65 loc) · 2.33 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# SPDX-License-Identifier: MIT
# Copyright (c) 2019 Intel Corporation
"""
Fake data sources used for testing
"""
from typing import Dict, List, AsyncIterator
from ..base import config, field
from ..record import Record
from .source import BaseSourceContext, BaseSource
from ..util.entrypoint import entrypoint
class MemorySourceContext(BaseSourceContext):
async def update(self, record):
self.parent.mem[record.key] = record
async def records(self) -> AsyncIterator[Record]:
for record in self.parent.mem.values():
yield record
async def record(self, key: str) -> Record:
return self.parent.mem.get(key, Record(key))
@config
class MemorySourceConfig:
records: List[Record]
display: int = field(
"When repr() is called, how many records to display", default=10
)
@entrypoint("memory")
class MemorySource(BaseSource):
"""
Stores records in a dict in memory
"""
CONFIG = MemorySourceConfig
CONTEXT = MemorySourceContext
def __init__(self, config: MemorySourceConfig) -> None:
super().__init__(config)
self.mem: Dict[str, Record] = {}
if isinstance(self.config, MemorySourceConfig):
self.mem = {record.key: record for record in self.config.records}
def __repr__(self):
if isinstance(self.config, MemorySourceConfig):
if not self.config.display:
return "%s(%d records)" % (
self.__class__.__qualname__,
len(self.mem),
)
elif self.config.display == -1:
return "%s(records=%r)" % (
self.__class__.__qualname__,
self.mem.values(),
)
elif len(self.mem) > self.config.display:
first_n = [
record
for _, record in zip(
range(0, self.config.display), self.mem.values()
)
]
return (
"%s(records=%r ... (only displaying %d records, %d total) ... )"
% (
self.__class__.__qualname__,
first_n,
self.config.display,
len(self.mem),
)
)
return super().__repr__()