2626logging .basicConfig (
2727 level = logging .INFO ,
2828 format = "%(levelname)s - %(message)s" ,
29- handlers = [RichHandler (rich_tracebacks = True )]
29+ handlers = [RichHandler (rich_tracebacks = True )],
3030)
3131
32+
3233class UCIDataset (IntEnum ):
3334 IRIS = 53
3435
36+
3537class IrisVariable (StrEnum ):
3638 PETAL_LENGTH = "petal length"
3739 PETAL_WIDTH = "petal width"
3840 SEPAL_WIDTH = "sepal width"
3941 SEPAL_LENGTH = "sepal length"
4042
43+
4144class Operation (StrEnum ):
4245 SUMMARY = auto ()
4346 METADATA = auto ()
4447
48+
4549@dataclass
4650class DescriptiveStatistics :
4751 data : pd .Series
@@ -61,6 +65,7 @@ def __post_init__(self):
6165 def __str__ (self ):
6266 return pformat (self )
6367
68+
6469@click .command ()
6570@click .option (
6671 "--operation" ,
@@ -95,20 +100,23 @@ def main(operation, variable):
95100 logging .info ("Metadata summary:" )
96101 logging .info (pformat (iris .metadata ))
97102
103+
98104def fetch_iris ():
99105 """Return the Iris dataset from the UCI ML Repository."""
100106 logging .info ("Fetching Iris dataset..." )
101107 try :
102108 iris_data = fetch_ucirepo (id = UCIDataset .IRIS .value )
103- assert "data" in iris_data .keys (), \
104- "Object does not have expected structure"
109+ assert (
110+ "data" in iris_data .keys ()
111+ ), "Object does not have expected structure"
105112 except Exception as e :
106113 logging .critical (f"Failed to correctly fetch Iris dataset: { e } " )
107114 sys .exit (1 )
108115 else :
109116 logging .info ("Iris dataset fetched successfully" )
110117 return iris_data
111118
119+
112120def generate_table (dataset , variable ):
113121 """Generate a formatted table of descriptive statistics for a variable."""
114122 column = IrisVariable (variable ).value
@@ -121,12 +129,14 @@ def generate_table(dataset, variable):
121129 table .add_row ("Mean-Median Diff" , f"{ stats .mm_diff :.2f} " )
122130 return table
123131
132+
124133def format_rich_for_log (renderable , width = 100 ):
125134 """Render a rich object to a plain text string suitable for logging."""
126135 console = Console (width = width )
127136 with console .capture () as capture :
128137 console .print (renderable )
129138 return Text .from_ansi (capture .get ())
130139
140+
131141if __name__ == "__main__" :
132142 main ()
0 commit comments