Skip to content

Line

Bases: BaseModel

A Line (爻) of a trigram or a hexagram in the I Ching

Source code in src/ichingpy/model/line.py
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
class Line(BaseModel):
    """A Line (爻) of a trigram or a hexagram in the I Ching"""

    status: LineStatus
    interpretation: LineInterpretationBase | None = None

    def __repr__(self) -> str:
        if self.interpretation is not None:
            return repr(self.interpretation)

        representation = f"-----" if self.is_yang else f"-- --"
        if self.is_transform:
            if self.is_yin:
                representation += f" X -> -----"
            else:
                representation += f" O -> -- --"
        return representation

    @property
    def value(self) -> int:
        """int: The integer value of the Line."""
        return self.status.value

    @property
    def is_yang(self) -> bool:
        """bool: Whether the Yao is a solid line (阳爻)"""
        return True if self.status in [LineStatus.STATIC_YANG, LineStatus.CHANGING_YANG] else False

    @property
    def is_yin(self) -> bool:
        """bool: Whether the Yao is a broken line (阴爻)"""
        return True if self.status in [LineStatus.STATIC_YIN, LineStatus.CHANGING_YIN] else False

    @property
    def is_transform(self) -> bool:
        """bool: Whether the Yao needs to be transformed (变爻)"""
        return True if self.status in [LineStatus.CHANGING_YIN, LineStatus.CHANGING_YANG] else False

    def get_transformed(self) -> "Line":
        """Get the transformed Line, which is always a static line
        只作用于动爻,返回变爻
        """
        match self.status:
            case LineStatus.STATIC_YANG | LineStatus.STATIC_YIN:
                raise LineTransformationError("Line is already static")
            case LineStatus.CHANGING_YANG:
                return Line(status=LineStatus.STATIC_YIN)
            case LineStatus.CHANGING_YIN:
                return Line(status=LineStatus.STATIC_YANG)

    def transform(self) -> "Line":
        """Create a transform line from a static line.
        只作用与静爻,返回阴阳与自身相同之动爻。
        """
        match self.status:
            case LineStatus.CHANGING_YANG | LineStatus.CHANGING_YIN:
                raise LineTransformationError("Line is already static")
            case LineStatus.STATIC_YANG:
                return Line(status=LineStatus.CHANGING_YANG)
            case LineStatus.STATIC_YIN:
                return Line(status=LineStatus.CHANGING_YIN)

    @classmethod
    def random(cls) -> Self:
        """Create a random Line instance."""
        return cls(status=LineStatus(random.getrandbits(2)))

is_transform property

bool: Whether the Yao needs to be transformed (变爻)

is_yang property

bool: Whether the Yao is a solid line (阳爻)

is_yin property

bool: Whether the Yao is a broken line (阴爻)

value property

int: The integer value of the Line.

get_transformed()

Get the transformed Line, which is always a static line 只作用于动爻,返回变爻

Source code in src/ichingpy/model/line.py
52
53
54
55
56
57
58
59
60
61
62
def get_transformed(self) -> "Line":
    """Get the transformed Line, which is always a static line
    只作用于动爻,返回变爻
    """
    match self.status:
        case LineStatus.STATIC_YANG | LineStatus.STATIC_YIN:
            raise LineTransformationError("Line is already static")
        case LineStatus.CHANGING_YANG:
            return Line(status=LineStatus.STATIC_YIN)
        case LineStatus.CHANGING_YIN:
            return Line(status=LineStatus.STATIC_YANG)

random() classmethod

Create a random Line instance.

Source code in src/ichingpy/model/line.py
76
77
78
79
@classmethod
def random(cls) -> Self:
    """Create a random Line instance."""
    return cls(status=LineStatus(random.getrandbits(2)))

transform()

Create a transform line from a static line. 只作用与静爻,返回阴阳与自身相同之动爻。

Source code in src/ichingpy/model/line.py
64
65
66
67
68
69
70
71
72
73
74
def transform(self) -> "Line":
    """Create a transform line from a static line.
    只作用与静爻,返回阴阳与自身相同之动爻。
    """
    match self.status:
        case LineStatus.CHANGING_YANG | LineStatus.CHANGING_YIN:
            raise LineTransformationError("Line is already static")
        case LineStatus.STATIC_YANG:
            return Line(status=LineStatus.CHANGING_YANG)
        case LineStatus.STATIC_YIN:
            return Line(status=LineStatus.CHANGING_YIN)