models.category.Category.from_dict()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 21
rs 9.376
c 0
b 0
f 0
cc 1
nop 1
1
from dataclasses import dataclass
2
from datetime import datetime, timedelta
3
from typing import Any, List, Optional
4
5
from models import (from_bool, from_datetime, from_int, from_list, from_none,
6
                    from_str, from_timedelta, from_union)
7
from models.race import Goal, Status
8
from models.user import User
9
10
11
@dataclass
12
class CurrentRace:
13
    name: Optional[str] = None
14
    status: Optional[Status] = None
15
    url: Optional[str] = None
16
    data_url: Optional[str] = None
17
    goal: Optional[Goal] = None
18
    info: Optional[str] = None
19
    entrants_count: Optional[int] = None
20
    entrants_count_inactive: Optional[int] = None
21
    opened_at: Optional[datetime] = None
22
    started_at: Optional[datetime] = None
23
    time_limit: Optional[timedelta] = None
24
25
    @staticmethod
26
    def from_dict(obj: Any) -> 'CurrentRace':
27
        if not isinstance(obj, dict):
28
            return None
29
        name = from_union([from_str, from_none], obj.get("name"))
30
        status = from_union([Status.from_dict, from_none], obj.get("status"))
31
        url = from_union([from_str, from_none], obj.get("url"))
32
        data_url = from_union([from_str, from_none], obj.get("data_url"))
33
        goal = from_union([Goal.from_dict, from_none], obj.get("goal"))
34
        info = from_union([from_str, from_none], obj.get("info"))
35
        entrants_count = from_union(
36
            [from_int, from_none], obj.get("entrants_count"))
37
        entrants_count_inactive = from_union(
38
            [from_int, from_none], obj.get("entrants_count_inactive"))
39
        opened_at = from_union(
40
            [from_datetime, from_none], obj.get("opened_at"))
41
        started_at = from_union(
42
            [from_datetime, from_none], obj.get("started_at"))
43
        time_limit = from_union(
44
            [from_timedelta, from_none], obj.get("time_limit"))
45
        return CurrentRace(
46
            name=name, status=status, url=url, data_url=data_url, goal=goal,
47
            info=info, entrants_count=entrants_count,
48
            entrants_count_inactive=entrants_count_inactive,
49
            opened_at=opened_at, started_at=started_at, time_limit=time_limit
50
        )
51
52
53
@dataclass
54
class Category:
55
    name: str
56
    short_name: str
57
    slug: str
58
    url: str
59
    data_url: str
60
    image: str
61
    info: str
62
    streaming_required: bool
63
    owners: List[User]
64
    moderators: List[User]
65
    goals: List[str]
66
    current_races: List[CurrentRace]
67
68
    @staticmethod
69
    def from_dict(obj: Any) -> 'Category':
70
        assert isinstance(obj, dict)
71
        name = from_str(obj.get("name"))
72
        short_name = from_str(obj.get("short_name"))
73
        slug = from_str(obj.get("slug"))
74
        url = from_str(obj.get("url"))
75
        data_url = from_str(obj.get("data_url"))
76
        image = from_str(obj.get("image"))
77
        info = from_str(obj.get("info"))
78
        streaming_required = from_bool(obj.get("streaming_required"))
79
        owners = from_list(User.from_dict, obj.get("owners"))
80
        moderators = from_list(User.from_dict, obj.get("moderators"))
81
        goals = from_list(from_str, obj.get("goals"))
82
        current_races = from_list(
83
            CurrentRace.from_dict, obj.get("current_races"))
84
        return Category(
85
            name=name, short_name=short_name, slug=slug, url=url,
86
            data_url=data_url, image=image, info=info,
87
            streaming_required=streaming_required, owners=owners,
88
            moderators=moderators, goals=goals, current_races=current_races
89
        )
90
91
92
def category_from_dict(s: Any) -> Category:
93
    return Category.from_dict(s)
94