Coverage for src/kwai/core/domain/value_objects/identifier.py: 95%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module that defines identifiers.""" 

2 

3from abc import ABC, abstractmethod 

4 

5 

6class Identifier[T](ABC): 

7 """Abstract and generic class for an identifier.""" 

8 

9 def __init__(self, id_: T): 

10 self._id = id_ 

11 

12 @property 

13 def value(self) -> T: 

14 """Return the id.""" 

15 return self._id 

16 

17 def __eq__(self, other: "Identifier"): 

18 """Check the equality of identifiers.""" 

19 return self._id == other._id 

20 

21 def __hash__(self): 

22 """Create a hash for an identifier.""" 

23 return hash(self._id) 

24 

25 @abstractmethod 

26 def is_empty(self) -> bool: 

27 """Return true when the identifier is not set.""" 

28 raise NotImplementedError 

29 

30 def __str__(self) -> str: 

31 """Return the string representation of the id.""" 

32 return str(self._id) 

33 

34 

35class IntIdentifier(Identifier[int]): 

36 """Class that implements an identifier with an integer.""" 

37 

38 def __init__(self, id_: int = 0): 

39 super().__init__(id_) 

40 

41 def is_empty(self) -> bool: 

42 """Return True when the id equals 0.""" 

43 return self.value == 0 

44 

45 def __repr__(self): 

46 """Return a string representation of the identifier.""" 

47 return f"IntIdentifier={self.value}"