Passed
Push — main ( ebde94...acc836 )
by Eran
01:54
created

graphinate.enums.GraphType.of()   A

Complexity

Conditions 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
rs 9.3333
c 0
b 0
f 0
cc 5
nop 2
1
from enum import Enum
2
3
import networkx as nx
4
from typing_extensions import Self
5
6
7
class GraphType(Enum):
8
    """Graph Types
9
10
    The choice of graph class depends on the structure of the graph you want to represent.
11
12
    | **Graph Type** | **Type**   | **Self-loops allowed** | **Parallel edges allowed** |
13
    |----------------|------------|:----------------------:|:--------------------------:|
14
    | Graph          | Undirected | Yes                    | No                         |
15
    | DiGraph        | Directed   | Yes                    | No                         |
16
    | MultiGraph     | Undirected | Yes                    | Yes                        |
17
    | MultiDiGraph   | Directed   | Yes                    | Yes                        |
18
19
    See more here: [NetworkX Reference](https://networkx.org/documentation/stable/reference/classes)
20
    """
21
22
    Graph = nx.Graph
23
    DiGraph = nx.DiGraph
24
    MultiDiGraph = nx.MultiDiGraph
25
    MultiGraph = nx.MultiGraph
26
27
    @classmethod
28
    def of(cls, graph: nx.Graph) -> Self:
29
        """Determine the graph type based on structure and properties.
30
31
        Args:
32
            graph (nx.Graph): A NetworkX graph object.
33
34
        Returns:
35
            GraphType: An instance of this Enum matching the input graph.
36
        """
37
        if graph.is_directed() and graph.is_multigraph():
38
            return cls.MultiDiGraph
39
        elif graph.is_directed():
40
            return cls.DiGraph
41
        elif graph.is_multigraph():
42
            return cls.MultiGraph
43
        else:
44
            return cls.Graph
45