1. Minimal Dependencies & Pure Python
Use Python’s built-in modules to ensure compatibility across systems. Avoid third-party libraries.
Example: Portable Data Generator
Generate data without external packages:
import os import sys def portable_data_generator(start, end): """Generate numbers and detect OS for logging.""" current = start while current <= end: # Include OS info for cross-platform logging yield { "value": current, "os": sys.platform, # e.g., 'win32', 'linux', 'darwin' (macOS) "path_separator": os.path.sep } current += 1 # Usage for data in portable_data_generator(1, 3): print(data) # Output: # {'value': 1, 'os': 'linux', 'path_separator': '/'} # {'value': 2, 'os': 'linux', 'path_separator': '/'} # {'value': 3, 'os': 'linux', 'path_separator': '/'}
2. Cross-Platform File Handling
Read files safely on any OS using os and sys modules.
Example: Portable File Reader
Handle file paths and line endings consistently:
import os def portable_file_reader(file_path): """Read files with OS-agnostic paths and line endings.""" with open(file_path, 'r', newline='') as file: for line in file: # Strip line endings and handle cross-platform newlines yield line.strip() # Usage (works on Windows, Linux, macOS) for line in portable_file_reader(os.path.join('data', 'example.txt')): print(line)
3. Self-Contained CSV Generator
Parse CSV files without external libraries (using Python’s csv module).
Example: CSV Data Generator
import csv def portable_csv_parser(file_path): with open(file_path, 'r') as file: reader = csv.reader(file) for row in reader: yield row # Usage for row in portable_csv_parser('data.csv'): print(row) # Outputs each CSV row as a list
4. Environment-Aware Configuration
Use environment variables to make generators adaptable to different setups.
Example: Configurable Generator
import os def configurable_generator(): max_items = int(os.environ.get("MAX_ITEMS", 10)) # Read from env or default for i in range(max_items): yield i # Set env variable before running: # export MAX_ITEMS=5 (Linux/macOS) or set MAX_ITEMS=5 (Windows) for num in configurable_generator(): print(num) # Outputs 0-4 if MAX_ITEMS=5
5. Serialization for Data Transfer
Make generated data portable across systems using JSON or bytes.
Example: JSON-Serializable Generator
import json def json_serializable_generator(data_list): for item in data_list: yield json.dumps(item) # Convert to JSON string # Usage data = [{'name': 'Alice'}, {'name': 'Bob'}] for json_str in json_serializable_generator(data): parsed = json.loads(json_str) # Rebuild data on another system print(parsed['name'])
Key Features of Portable Generators
-
No External Dependencies: Only use Python’s standard library.
-
OS Agnostic: Handle paths, line endings, and OS-specific logic safely.
-
Configurable: Use environment variables or input parameters.
-
Serializable Output: Generate data in formats like JSON, CSV, or bytes.
-
Lightweight: Avoid large data structures or complex setups.
Use Case: Portable Data Pipeline
A generator that reads data, processes it, and writes results—designed to run anywhere:
import os import csv def portable_pipeline(input_path, output_path): # Read with open(input_path, 'r') as infile: reader = csv.reader(infile) for row in reader: # Process (e.g., add a new column) processed_row = row + ["processed"] yield processed_row # Write (example: save to a new file) with open(output_path, 'w') as outfile: writer = csv.writer(outfile) for row in portable_pipeline('input.csv', 'output.csv'): writer.writerow(row)
Tips for Maximizing Portability
-
Use Relative Paths: Avoid hardcoding absolute paths.
import os current_dir = os.path.dirname(os.path.abspath(__file__)) # Script's directory file_path = os.path.join(current_dir, 'data.csv')
-
Handle Encoding: Specify encodings (e.g.,
utf-8) for file operations. -
Test Across Platforms: Validate on Windows, Linux, and macOS.
-
Package as a Module: Wrap generators in a
.pyfile for easy reuse.
Example: Standalone Module
Save this as portable_gen.py and reuse across projects:
# portable_gen.py def fibonacci_sequence(max_items): a, b = 0, 1 count = 0 while count < max_items: yield a a, b = b, a + b count += 1 if __name__ == "__main__": # Demo when run directly for num in fibonacci_sequence(10): print(num)













