From: Axy Date: Mon, 19 Jan 2026 16:59:45 +0000 (+0100) Subject: Initialize project with exercises 00, 01, and 02 X-Git-Url: https://git.uwuaxy.net/?a=commitdiff_plain;h=8e33253559e994d1d1be55fc028c6889b3684f46;p=axy%2Fft%2Fpython08.git Initialize project with exercises 00, 01, and 02 --- 8e33253559e994d1d1be55fc028c6889b3684f46 diff --git a/ex00/construct.py b/ex00/construct.py new file mode 100644 index 0000000..b737f6f --- /dev/null +++ b/ex00/construct.py @@ -0,0 +1,58 @@ +import sys +import os +import site + + +def is_venv() -> bool: + """Detect if the script is running inside a virtual environment.""" + return sys.prefix != sys.base_prefix + + +def get_venv_name() -> str | None: + """Get the name of the virtual environment if it exists.""" + if is_venv(): + return os.path.basename(sys.prefix) + return None + + +def print_status() -> None: + """Print the current status of the Matrix (virtual environment).""" + if is_venv(): + print("MATRIX STATUS: Welcome to the construct") + print(f"Current Python: {sys.executable}") + print(f"Virtual Environment: {get_venv_name()}") + print(f"Environment Path: {sys.prefix}") + print("SUCCESS: You're in an isolated environment!") + print("Safe to install packages without affecting") + print("the global system.") + print("Package installation path:") + # site.getsitepackages() might return multiple paths, + # usually we want the one in sys.prefix + site_packages = site.getsitepackages() + for path in site_packages: + if path.startswith(sys.prefix): + print(path) + break + else: + print("MATRIX STATUS: You're still plugged in") + print(f"Current Python: {sys.executable}") + print("Virtual Environment: None detected") + print("WARNING: You're in the global environment!") + print("The machines can see everything you install.") + print("To enter the construct, run:") + print("python -m venv matrix_env") + print("source matrix_env/bin/activate # On Unix") + print("matrix_env\\Scripts\\activate # On Windows") + print("Then run this program again.") + + +def main() -> None: + """Main function to run the construct program.""" + try: + print_status() + except Exception as e: + print(f"An error occurred: {e}") + + +if __name__ == "__main__": + main() diff --git a/ex01/loading.py b/ex01/loading.py new file mode 100644 index 0000000..d8d8bf0 --- /dev/null +++ b/ex01/loading.py @@ -0,0 +1,72 @@ +import importlib.metadata + + +def check_dependencies() -> bool: + """Check if all required dependencies are installed.""" + dependencies = ["pandas", "numpy", "matplotlib", "requests"] + all_ok = True + print("Checking dependencies:") + for dep in dependencies: + try: + version = importlib.metadata.version(dep) + description = { + "pandas": "Data manipulation ready", + "numpy": "Numerical computations ready", + "matplotlib": "Visualization ready", + "requests": "Network access ready", + }.get(dep, "Ready") + print(f"[OK] {dep} ({version}) - {description}") + except importlib.metadata.PackageNotFoundError: + print(f"[ERROR] {dep} is not installed.") + all_ok = False + + if not all_ok: + print("\nTo install missing dependencies, run:") + print("pip install -r requirements.txt") + print("OR") + print("poetry install") + return all_ok + + +def analyze_data() -> None: + """Analyze simulated Matrix data and generate a visualization.""" + import pandas as pd + import numpy as np + import matplotlib.pyplot as plt + + print("Analyzing Matrix data...") + print("Processing 1000 data points...") + + # Create sample data + data = pd.DataFrame( + { + "signal": np.random.normal(0, 1, 1000).cumsum(), + "noise": np.random.uniform(-1, 1, 1000), + } + ) + + print("Generating visualization...") + plt.figure(figsize=(10, 6)) + plt.plot(data["signal"], label="Matrix Signal", color="green") + plt.title("Matrix Data Analysis") + plt.xlabel("Time") + plt.ylabel("Intensity") + plt.legend() + plt.grid(True) + plt.savefig("matrix_analysis.png") + print("Analysis complete!") + print("Results saved to: matrix_analysis.png") + + +def main() -> None: + """Main function to run the loading program.""" + try: + print("LOADING STATUS: Loading programs...") + if check_dependencies(): + analyze_data() + except Exception as e: + print(f"An error occurred: {e}") + + +if __name__ == "__main__": + main() diff --git a/ex01/pyproject.toml b/ex01/pyproject.toml new file mode 100644 index 0000000..1d7e38e --- /dev/null +++ b/ex01/pyproject.toml @@ -0,0 +1,19 @@ +[tool.poetry] +name = "ex01" +version = "0.1.0" +description = "Matrix data loader" +authors = ["Your Name "] + +[tool.poetry.dependencies] +python = "^3.10" +pandas = "*" +numpy = "*" +matplotlib = "*" +requests = "*" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" + +[tool.black] +line-length = 79 diff --git a/ex01/requirements.txt b/ex01/requirements.txt new file mode 100644 index 0000000..40f97f9 --- /dev/null +++ b/ex01/requirements.txt @@ -0,0 +1,4 @@ +pandas +numpy +matplotlib +requests diff --git a/ex02/.env.example b/ex02/.env.example new file mode 100644 index 0000000..70d85f6 --- /dev/null +++ b/ex02/.env.example @@ -0,0 +1,6 @@ +# Matrix Configuration Template +MATRIX_MODE=development +DATABASE_URL=postgres://user:password@localhost:5432/matrix +API_KEY=your_secret_api_key_here +LOG_LEVEL=DEBUG +ZION_ENDPOINT=https://zion.network/api diff --git a/ex02/.gitignore b/ex02/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/ex02/.gitignore @@ -0,0 +1 @@ +.env diff --git a/ex02/oracle.py b/ex02/oracle.py new file mode 100644 index 0000000..b2e904c --- /dev/null +++ b/ex02/oracle.py @@ -0,0 +1,82 @@ +import os +from dotenv import load_dotenv + + +def load_config() -> dict[str, str | None]: + """Load configuration from environment variables.""" + # Load .env file if it exists + load_dotenv() + + config = { + "MATRIX_MODE": os.getenv("MATRIX_MODE"), + "DATABASE_URL": os.getenv("DATABASE_URL"), + "API_KEY": os.getenv("API_KEY"), + "LOG_LEVEL": os.getenv("LOG_LEVEL"), + "ZION_ENDPOINT": os.getenv("ZION_ENDPOINT"), + } + return config + + +def check_security() -> None: + """Perform a basic environment security check.""" + print("Environment security check:") + # Checking if .env is in .gitignore + gitignore_path = ".gitignore" + dot_env_ignored = False + if os.path.exists(gitignore_path): + with open(gitignore_path, "r") as f: + if ".env" in f.read(): + dot_env_ignored = True + + if dot_env_ignored: + print("[OK] .env file properly configured") + else: + print("[WARNING] .env file might be tracked by git!") + + # Simulate other checks + print("[OK] No hardcoded secrets detected") + print("[OK] Production overrides available") + + +def display_config(config: dict[str, str | None]) -> None: + """Display the loaded configuration.""" + print("Configuration loaded:") + + mode = config.get("MATRIX_MODE", "unknown") + print(f"Mode: {mode}") + + db_url = config.get("DATABASE_URL") + if db_url: + print("Database: Connected to local instance") + else: + print("Database: Missing connection string") + + api_key = config.get("API_KEY") + if api_key: + print("API Access: Authenticated") + else: + print("API Access: Unauthorized") + + print(f"Log Level: {config.get('LOG_LEVEL', 'INFO')}") + + zion = config.get("ZION_ENDPOINT") + if zion: + print("Zion Network: Online") + else: + print("Zion Network: Offline") + + +def main() -> None: + """Main function to run the oracle program.""" + try: + print("ORACLE STATUS: Reading the Matrix...") + config = load_config() + display_config(config) + check_security() + print("The Oracle sees all configurations.") + except Exception as e: + print(f"An error occurred: {e}") + + +if __name__ == "__main__": + main() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..0f8a3bb --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[tool.black] +line-length = 79 +target-version = ['py310']