-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry_examples.py
More file actions
129 lines (98 loc) · 3.64 KB
/
registry_examples.py
File metadata and controls
129 lines (98 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Registry examples for Load
"""
import sys
from pathlib import Path
# Add parent directory to path to allow importing from src
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
def registry_examples():
"""Registry examples"""
print("🔧 Load Registry Examples")
print("=" * 50)
from load import load, info, test_cache_info as cache_info
print("\n📦 Testing basic loading:")
json_lib = load('json')
print("✅ JSON loaded successfully")
# Test different source detection
print("\n🎯 Testing source detection:")
# Standard module
os_lib = load('os')
print("✅ Standard module (os) loaded")
# Test local file loading (create a temp file)
import tempfile
import importlib.util
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
f.write(
"""
# Temporary test module
TEST_VALUE = "Hello from temp module"
def greet():
return "Hello from temporary module!"
"""
)
temp_path = Path(f.name)
try:
# Load the temporary module using importlib
spec = importlib.util.spec_from_file_location("temp_module", temp_path)
temp_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(temp_module)
print(f"✅ Local file loaded: {temp_module.TEST_VALUE}")
print(f"✅ Function works: {temp_module.greet()}")
except Exception as e:
print(f"⚠️ Local file loading: {e}")
finally:
temp_path.unlink()
# Show cache status
print("\n💾 Cache status:")
cache_stats = info()
print(f" Cached modules: {cache_stats['cache_size']}")
print(f" Module list: {cache_stats['cached_modules']}")
print("\n✅ Registry examples completed")
def test_magic_import():
"""Test module import functionality"""
print("\n📦 Testing module import:")
from load import load
try:
# Test module import using function call
json_module = load('json')
os_module = load('os')
sys_module = load('sys')
# Verify the modules were loaded
assert json_module is not None, "Failed to load json module"
assert os_module is not None, "Failed to load os module"
assert sys_module is not None, "Failed to load sys module"
print("✅ Module import works for stdlib modules")
# Test that they're the same as cached imports
json_cached = load('json', silent=True)
assert json_module is json_cached, "Subsequent loads should return cached module"
print("✅ Module caching works correctly")
except Exception as e:
print(f"❌ Module import error: {e}")
raise
def test_error_handling():
"""Test error handling"""
print("\n🚨 Testing error handling:")
import load
# Test nonexistent module
try:
load.load("definitely_does_not_exist_12345", install=False, silent=True)
print("❌ Should have raised ImportError")
except ImportError:
print("✅ ImportError raised correctly for nonexistent module")
# Test nonexistent file
try:
load.load("./nonexistent_file.py", silent=True)
print("❌ Should have raised ImportError")
except ImportError:
print("✅ ImportError raised correctly for nonexistent file")
if __name__ == "__main__":
try:
registry_examples()
test_magic_import()
test_error_handling()
print("\n🎉 All registry examples completed successfully!")
except Exception as e:
print(f"\n❌ Error in registry examples: {e}")
import traceback
traceback.print_exc()
sys.exit(1)