Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

"""LICENSE 

Copyright 2018 Hermann Krumrey <hermann@krumreyh.com> 

 

This file is part of anime-list-apis. 

 

anime-list-apis is free software: you can redistribute it and/or modify 

it under the terms of the GNU General Public License as published by 

the Free Software Foundation, either version 3 of the License, or 

(at your option) any later version. 

 

anime-list-apis is distributed in the hope that it will be useful, 

but WITHOUT ANY WARRANTY; without even the implied warranty of 

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

GNU General Public License for more details. 

 

You should have received a copy of the GNU General Public License 

along with anime-list-apis. If not, see <http://www.gnu.org/licenses/>. 

LICENSE""" 

 

from enum import Enum 

from typing import Dict, List, Tuple, Set, Optional 

from anime_list_apis.models.Serializable import Serializable 

from anime_list_apis.models.attributes.Id import Id 

from anime_list_apis.models.attributes.MediaType import MediaType 

 

 

class RelationType(Enum): 

""" 

An enumeration modelling the different kinds of relations between 

entries. 

""" 

PREQUEL = 100 

SEQUEL = 101 

PARENT = 102 

SIDE_STORY = 103 

SUMMARY = 104 

CHARACTER = 200 

SPIN_OFF = 201 

OTHER = 202 

ADAPTATION = 203 

ALTERNATIVE = 204 

 

 

class Relation(Serializable): 

""" 

Class that models a relation edge between two anime entries 

""" 

 

def __init__( 

self, 

source: Id, 

source_type: MediaType, 

dest: Id, 

dest_type: MediaType, 

relation_type: RelationType 

): 

""" 

Initializes the Relation object. 

:param source: The source node in the relation edge 

:param source_type: The media type of the source node 

:param dest: The destination node in the relation edge 

:param dest_type: The destination node's media type 

:param relation_type: The type of the relation 

:raises TypeError: If invalid ID types are provided 

or other types mismatch 

:raises ValueError: If both IDs are the same, i.e. an invalid relation 

""" 

list(map(lambda x: self.ensure_type(x, Id), [source, dest])) 

list(map( 

lambda x: self.ensure_type(x, MediaType), [source_type, dest_type] 

)) 

self.ensure_type(relation_type, RelationType) 

 

if source == dest and source_type == dest_type: 

raise ValueError("Same ID") 

 

self.source, self.dest, self.type = source, dest, relation_type 

self.source_type, self.dest_type = source_type, dest_type 

 

# For easier access 

self.id, self.media_type = self.dest, self.dest_type 

 

def is_important(self) -> bool: 

""" 

Checks if this relation is "important", meaning if it's a direct 

connection (Sequel, Prequel, Parent, Side Story or a summary) or not 

:return: True if the relation is important, False otherwise 

""" 

# noinspection PyTypeChecker 

return 200 > self.type.value and self.source_type == self.dest_type 

 

def _serialize(self) -> Dict[str, Optional[str or int or float or bool 

or Dict or List or Tuple or Set]]: 

""" 

Serializes the object into a dictionary 

:return: The serialized form of this object 

""" 

return { 

"source": self.source.serialize(), 

"source_type": self.source_type.name, 

"dest": self.dest.serialize(), 

"dest_type": self.dest_type.name, 

"type": self.type.name 

} 

 

@classmethod 

def _deserialize(cls, data: Dict[str, Optional[str or int or float or bool 

or Dict or List or Tuple or Set]]): 

""" 

Deserializes a dictionary into an object of this type 

:param data: The data to deserialize 

:return: The deserialized object 

:raises TypeError: If a type error occurred 

:raises ValueError: If the data could not be deserialized 

""" 

source = Id.deserialize(data["source"]) 

source_type = MediaType[data["source_type"]] 

dest = Id.deserialize(data["dest"]) 

dest_type = MediaType[data["dest_type"]] 

relation_type = RelationType[data["type"]] 

generated = cls( 

source, source_type, dest, dest_type, relation_type 

) # type: Relation 

return generated