Skip to content

Commit 295a095

Browse files
committed
Add NetworkFormat Enum instaed of the strings with the extensions
1 parent 434894f commit 295a095

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

report/report.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from abc import ABC, abstractmethod
33
from dataclasses import dataclass, field
4-
from enum import StrEnum, auto
4+
from enum import StrEnum, Enum, auto
55
from typing import List, Optional, NamedTuple
66
import networkx as nx
77
import pandas as pd
@@ -28,6 +28,18 @@ class IntVisualizationTool(StrEnum):
2828
ALTAIR = auto()
2929
PYVIS = auto()
3030

31+
class NetworkFormat(StrEnum):
32+
GML = auto()
33+
GRAPHML = auto()
34+
GEXF = auto()
35+
CSV = auto()
36+
TXT = auto()
37+
38+
@property
39+
def value_with_dot(self):
40+
"""Return the file extension with the dot."""
41+
return f".{self.name.lower()}"
42+
3143
class CSVNetworkFormat(StrEnum):
3244
EDGELIST = auto()
3345
ADJLIST = auto()
@@ -105,11 +117,9 @@ def read_network(self) -> nx.Graph:
105117
"""
106118
# Mapping of file extensions to NetworkX loading functions
107119
file_extension_map = {
108-
'.gml': nx.read_gml,
109-
'.graphml': nx.read_graphml,
110-
'.gexf': nx.read_gexf,
111-
'.csv': nx.read_edgelist,
112-
'.txt': nx.read_edgelist,
120+
NetworkFormat.GML.value_with_dot: nx.read_gml,
121+
NetworkFormat.GRAPHML.value_with_dot: nx.read_graphml,
122+
NetworkFormat.GEXF.value_with_dot: nx.read_gexf
113123
}
114124

115125
# Check if the file exists
@@ -119,13 +129,16 @@ def read_network(self) -> nx.Graph:
119129
# Determine the file extension and check if it is supported
120130
file_extension = os.path.splitext(self.file_path)[-1].lower()
121131

122-
# Check if the file extension is supported
123-
if file_extension not in file_extension_map:
124-
raise ValueError(f"Unsupported file extension: {file_extension}. Supported extensions are: .gml, .graphml, .gexf, .csv, .txt.")
132+
# Check if the file extension matches any Enum value
133+
if not any(file_extension == fmt.value_with_dot for fmt in NetworkFormat):
134+
raise ValueError(
135+
f"Unsupported file extension: {file_extension}. Supported extensions are: "
136+
f"{', '.join(fmt.value for fmt in NetworkFormat)}."
137+
)
125138

126-
# Handle .csv and .txt files with custom delimiters based on format (edgelist or adjlist)
139+
# Handle .csv and .txt files with custom delimiters based on the text format (edgelist or adjlist)
127140
try:
128-
if file_extension in ['.csv', '.txt'] and self.csv_network_format:
141+
if file_extension in [NetworkFormat.CSV.value_with_dot, NetworkFormat.TXT.value_with_dot] and self.csv_network_format:
129142
delimiter = ',' if file_extension == '.csv' else '\\t'
130143
try:
131144
df_net = pd.read_csv(self.file_path, delimiter=delimiter)

0 commit comments

Comments
 (0)