File size: 1,267 Bytes
485f76b
60ec487
 
 
ae7097b
 
d48129a
 
60ec487
e04a33d
60ec487
 
 
 
 
 
4b890a6
 
485f76b
 
 
60ec487
485f76b
 
 
 
 
 
 
 
 
 
 
60ec487
485f76b
 
60ec487
 
 
485f76b
 
 
 
60ec487
485f76b
 
60ec487
485f76b
 
60ec487
485f76b
 
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
#!/usr/bin/env python3
import csv
from typing import NamedTuple

from common import defaults

def read_entities(fn = defaults.MAIN_CSV_PATH):
    with open(fn, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        bcos = { d['bco']:Entity.from_dict(d) for d in reader}
    return bcos

class Entity(NamedTuple):
    name: str
    id: int = 0
    bco: str = "debug"
    url: str = ''
    logo: str = ''

    def __repr__(self):
        return f"""
Entity {self.id}:
        name: {self.name}
        bco:  {self.bco}
        url:  {self.url}
        logo: {self.logo}
        """

    @classmethod
    def from_list(cls, l):
        self = apply(cls, l)
        return self

    # this now looks horrible…
    @classmethod
    def from_dict(cls, d):
        o = {'name': None, 'id': 0, 'bco': None, 'url': None, 'logo': None}
        o.update(d)
        self = cls(o['name'], o['id'], o['bco'], o['url'], o['logo'])
        return self

    @classmethod
    def row_names(cls):
        return ['id', 'name', 'bco', 'url', 'logo']

    def to_row(self):
        return [self.id, self.name, self.bco, self.url, self.logo]

if __name__ == '__main__':
    e = Entity.from_dict({'name': 'test', 'url': 'blah'})
    assert(e.url == 'blah')
    print(e)