fb403d0module-08: add almost complete ex1
3 files changed, +138, -0
module-08/ex1/loading.py
+115, -0
module-08/ex1/pyproject.toml
+20, -0
+115, -0
@@ -0,0 +1,115 @@
1+import sys
2+import importlib
3+from types import ModuleType
4+from typing import Any
5+
6+
7+REQUIRED_PKGS: dict[str, str] = {
8+ "pandas": "Data manipulation ready",
9+ "numpy": "Numerical computation ready",
10+ "matplotlib": "Visualization ready",
11+}
12+
13+OPTIONAL_PKGS: dict[str, str] = {
14+ "requests": "Network access ready"
15+}
16+
17+
18+def check_pkg(
19+ pkg_name: str
20+) -> tuple[bool, ModuleType | None, str | None]:
21+ try:
22+ module = importlib.import_module(pkg_name)
23+ version = getattr(module, "__version__", "unknown")
24+ return True, module, version
25+ except ImportError:
26+ return False, None, None
27+
28+
29+def show_missing_instructions(
30+ missing_pkgs: list[str]
31+) -> None:
32+ pkgs = " ".join(missing_pkgs)
33+
34+ print("\nInstall using pip:")
35+ print(f"pip install {pkgs}\nor")
36+ print("pip install -r requirements.txt\n")
37+
38+ print("Install using Poetry:")
39+ print(f"poetry add {pkgs}\nor")
40+ print("poetry install")
41+
42+
43+def generate_matrix_data(
44+ numpy: ModuleType
45+) -> dict[str, Any]:
46+ numpy.random.seed(42)
47+ data_size = 1000
48+ return {
49+ "cool_data": numpy.random.normal(50, 15, data_size),
50+ "mid_data": numpy.random.randint(0, 100, data_size),
51+ }
52+
53+
54+def analyze_data(
55+ pandas: ModuleType,
56+ numpy: ModuleType,
57+ plt: ModuleType
58+) -> None:
59+ try:
60+ print("\nAnalyzing Matrix data...")
61+ matrix_data = generate_matrix_data(numpy)
62+ df = pandas.DataFrame(matrix_data)
63+ print(f"Processing {len(df)} data points...")
64+ print("\nGenerating Visualization...")
65+ _, axes = plt.subplots(2, 1, figsize=(10, 8))
66+ axes[0].plot(df["cool_data"][:100])
67+ axes[0].set_title("uhmm")
68+ axes[1].hist(df["mid_data"], bins=20)
69+ axes[1].set_title("hmmmm")
70+ plt.tight_layout()
71+ output_file = "matrix_analysis.png"
72+ plt.savefig(output_file)
73+ print("\nAnalysis complete!")
74+ print(f"Results saved to: {output_file}")
75+
76+ except Exception as error:
77+ print(f"Error during analysis: {error}")
78+ sys.exit(1)
79+
80+
81+def main() -> None:
82+ print("LOADING STATUS: Loading programs...")
83+ print("\nChecking dependencies:")
84+
85+ missing_pkgs = []
86+ loaded_modules = {}
87+
88+ for pkg, msg in REQUIRED_PKGS.items():
89+ installed, module, version = check_pkg(pkg)
90+ if installed and module is not None:
91+ print(f"[OK] {pkg} ({version}) - {msg}")
92+ loaded_modules[pkg] = module
93+ else:
94+ print(f"[MISSING] {pkg} - Not installed")
95+ missing_pkgs.append(pkg)
96+
97+ for pkg, msg in OPTIONAL_PKGS.items():
98+ installed, module, version = check_pkg(pkg)
99+ if installed and module is not None:
100+ print(f"[OK] {pkg} ({version}) - {msg}")
101+ loaded_modules[pkg] = module
102+
103+ if missing_pkgs:
104+ show_missing_instructions(missing_pkgs)
105+ sys.exit(1)
106+
107+ pandas = loaded_modules["pandas"]
108+ numpy = loaded_modules["numpy"]
109+ plt = importlib.import_module("matplotlib.pyplot")
110+
111+ analyze_data(pandas, numpy, plt)
112+
113+
114+if __name__ == "__main__":
115+ main()
+20, -0
@@ -0,0 +1,20 @@
1+[tool.poetry]
2+package-mode = false
3+
4+[project]
5+name = "matrix-data"
6+version = "0.1.0"
7+requires-python = ">=3.10"
8+dependencies = [
9+ "pandas",
10+ "numpy",
11+ "matplotlib",
12+ "requests"
13+]
14+authors = [
15+ {name = "mleitao", email = "mleitao@student.42lisboa.com"}
16+]
17+
18+[build-system]
19+requires = ["poetry-core"]
20+build-backend = "poetry.core.masonry.api"