|
| 1 | +""" |
| 2 | +Demo Scripts - Examples of how to use the utility functions. |
| 3 | +""" |
| 4 | + |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +# Add parent directory to path for imports |
| 9 | +sys.path.insert(0, str(Path(__file__).parent.parent)) |
| 10 | + |
| 11 | +from datetime import datetime, timedelta |
| 12 | +from utils.file_utils import find_files, get_file_info, get_directory_size |
| 13 | +from utils.text_utils import extract_emails, extract_urls, slugify, word_frequency |
| 14 | +from utils.date_utils import time_ago, format_duration, business_days_between |
| 15 | +from utils.web_utils import is_url_valid, parse_url, build_url |
| 16 | +from utils.system_utils import get_system_info, get_disk_usage |
| 17 | + |
| 18 | + |
| 19 | +def demo_file_utils(): |
| 20 | + """Demonstrate file utility functions.""" |
| 21 | + print("\n" + "=" * 50) |
| 22 | + print("FILE UTILITIES DEMO") |
| 23 | + print("=" * 50) |
| 24 | + |
| 25 | + # Find Python files in current directory |
| 26 | + print("\n1. Finding Python files:") |
| 27 | + py_files = find_files(".", "*.py") |
| 28 | + for f in py_files[:5]: |
| 29 | + print(f" - {f}") |
| 30 | + |
| 31 | + # Get file info |
| 32 | + print("\n2. Getting file info:") |
| 33 | + try: |
| 34 | + info = get_file_info(__file__) |
| 35 | + print(f" Name: {info['name']}") |
| 36 | + print(f" Size: {info['size_human']}") |
| 37 | + print(f" Modified: {info['modified']}") |
| 38 | + except FileNotFoundError: |
| 39 | + print(" File not found") |
| 40 | + |
| 41 | + # Get directory size |
| 42 | + print("\n3. Directory size:") |
| 43 | + size_info = get_directory_size(".") |
| 44 | + print(f" Total: {size_info['total_human']}") |
| 45 | + print(f" Files: {size_info['file_count']}") |
| 46 | + |
| 47 | + |
| 48 | +def demo_text_utils(): |
| 49 | + """Demonstrate text utility functions.""" |
| 50 | + print("\n" + "=" * 50) |
| 51 | + print("TEXT UTILITIES DEMO") |
| 52 | + print("=" * 50) |
| 53 | + |
| 54 | + sample_text = """ |
| 55 | + Contact us at support@example.com or sales@company.org. |
| 56 | + Visit our website: https://www.example.com/products |
| 57 | + Check out our blog at http://blog.example.com |
| 58 | + """ |
| 59 | + |
| 60 | + # Extract emails |
| 61 | + print("\n1. Extracting emails:") |
| 62 | + emails = extract_emails(sample_text) |
| 63 | + for email in emails: |
| 64 | + print(f" - {email}") |
| 65 | + |
| 66 | + # Extract URLs |
| 67 | + print("\n2. Extracting URLs:") |
| 68 | + urls = extract_urls(sample_text) |
| 69 | + for url in urls: |
| 70 | + print(f" - {url}") |
| 71 | + |
| 72 | + # Slugify |
| 73 | + print("\n3. Creating URL slugs:") |
| 74 | + titles = ["Hello World!", "Python 3.12 Release Notes", "What's New in 2024?"] |
| 75 | + for title in titles: |
| 76 | + print(f" '{title}' -> '{slugify(title)}'") |
| 77 | + |
| 78 | + # Word frequency |
| 79 | + print("\n4. Word frequency:") |
| 80 | + text = "Python is great. Python is easy. Python is powerful." |
| 81 | + freq = word_frequency(text, top_n=3) |
| 82 | + for word, count in freq.items(): |
| 83 | + print(f" '{word}': {count}") |
| 84 | + |
| 85 | + |
| 86 | +def demo_date_utils(): |
| 87 | + """Demonstrate date utility functions.""" |
| 88 | + print("\n" + "=" * 50) |
| 89 | + print("DATE UTILITIES DEMO") |
| 90 | + print("=" * 50) |
| 91 | + |
| 92 | + # Time ago |
| 93 | + print("\n1. Time ago formatting:") |
| 94 | + times = [ |
| 95 | + datetime.now() - timedelta(minutes=5), |
| 96 | + datetime.now() - timedelta(hours=3), |
| 97 | + datetime.now() - timedelta(days=7), |
| 98 | + datetime.now() - timedelta(days=60), |
| 99 | + ] |
| 100 | + for t in times: |
| 101 | + print(f" {t.strftime('%Y-%m-%d %H:%M')} -> {time_ago(t)}") |
| 102 | + |
| 103 | + # Format duration |
| 104 | + print("\n2. Duration formatting:") |
| 105 | + durations = [45, 3600, 86400, 90061] |
| 106 | + for d in durations: |
| 107 | + print(f" {d} seconds -> {format_duration(d)}") |
| 108 | + |
| 109 | + # Business days |
| 110 | + print("\n3. Business days calculation:") |
| 111 | + from datetime import date |
| 112 | + start = date(2024, 1, 1) |
| 113 | + end = date(2024, 1, 15) |
| 114 | + days = business_days_between(start, end) |
| 115 | + print(f" {start} to {end}: {days} business days") |
| 116 | + |
| 117 | + |
| 118 | +def demo_web_utils(): |
| 119 | + """Demonstrate web utility functions.""" |
| 120 | + print("\n" + "=" * 50) |
| 121 | + print("WEB UTILITIES DEMO") |
| 122 | + print("=" * 50) |
| 123 | + |
| 124 | + # Parse URL |
| 125 | + print("\n1. Parsing URLs:") |
| 126 | + test_url = "https://api.example.com:8080/users/search?q=john&limit=10#results" |
| 127 | + parsed = parse_url(test_url) |
| 128 | + print(f" URL: {test_url}") |
| 129 | + print(f" Host: {parsed['host']}") |
| 130 | + print(f" Port: {parsed['port']}") |
| 131 | + print(f" Path: {parsed['path']}") |
| 132 | + print(f" Params: {parsed['query_params']}") |
| 133 | + |
| 134 | + # Build URL |
| 135 | + print("\n2. Building URLs:") |
| 136 | + url = build_url("https://api.example.com", "/users", {"page": 1, "limit": 20}) |
| 137 | + print(f" Built URL: {url}") |
| 138 | + |
| 139 | + # Check URL validity (commented out to avoid network calls in demo) |
| 140 | + print("\n3. URL validation (checking format):") |
| 141 | + urls_to_check = [ |
| 142 | + "https://google.com", |
| 143 | + "not-a-url", |
| 144 | + "http://localhost:8080", |
| 145 | + ] |
| 146 | + for url in urls_to_check: |
| 147 | + from utils.web_utils import is_valid_url |
| 148 | + valid = is_valid_url(url) |
| 149 | + print(f" '{url}' -> {'Valid' if valid else 'Invalid'}") |
| 150 | + |
| 151 | + |
| 152 | +def demo_system_utils(): |
| 153 | + """Demonstrate system utility functions.""" |
| 154 | + print("\n" + "=" * 50) |
| 155 | + print("SYSTEM UTILITIES DEMO") |
| 156 | + print("=" * 50) |
| 157 | + |
| 158 | + # System info |
| 159 | + print("\n1. System information:") |
| 160 | + info = get_system_info() |
| 161 | + print(f" OS: {info['os']} {info['os_release']}") |
| 162 | + print(f" Machine: {info['machine']}") |
| 163 | + print(f" Python: {info['python_version']}") |
| 164 | + print(f" Hostname: {info['hostname']}") |
| 165 | + |
| 166 | + # Disk usage |
| 167 | + print("\n2. Disk usage:") |
| 168 | + if sys.platform == 'win32': |
| 169 | + disk = get_disk_usage("C:\\") |
| 170 | + else: |
| 171 | + disk = get_disk_usage("/") |
| 172 | + |
| 173 | + if "error" not in disk: |
| 174 | + print(f" Total: {disk['total']}") |
| 175 | + print(f" Used: {disk['used']} ({disk['percent']}%)") |
| 176 | + print(f" Free: {disk['free']}") |
| 177 | + |
| 178 | + |
| 179 | +def main(): |
| 180 | + """Run all demos.""" |
| 181 | + print("\n" + "#" * 60) |
| 182 | + print("#" + " " * 58 + "#") |
| 183 | + print("#" + " PYTHON UTILS TOOLKIT - DEMONSTRATION".center(58) + "#") |
| 184 | + print("#" + " " * 58 + "#") |
| 185 | + print("#" * 60) |
| 186 | + |
| 187 | + demo_file_utils() |
| 188 | + demo_text_utils() |
| 189 | + demo_date_utils() |
| 190 | + demo_web_utils() |
| 191 | + demo_system_utils() |
| 192 | + |
| 193 | + print("\n" + "=" * 50) |
| 194 | + print("Demo complete! Explore the utils/ folder for more functions.") |
| 195 | + print("=" * 50 + "\n") |
| 196 | + |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + main() |
0 commit comments