-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path@lru_cache_github.py
More file actions
26 lines (20 loc) · 863 Bytes
/
@lru_cache_github.py
File metadata and controls
26 lines (20 loc) · 863 Bytes
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
from functools import lru_cache
import time
class DataProcessor:
def __init__(self, source_name):
self.source_name = source_name
@property
@lru_cache(maxsize=1)
def processed_data(self):
# Simulate a time-consuming process (e.g., loading or processing data)
print(f"🔄 Loading and processing data from {self.source_name}...")
time.sleep(3) # Expensive operation simulation
return f"✅ Data from {self.source_name} is processed and ready."
def __str__(self):
return f"DataProcessor for source: {self.source_name}"
# Example usage
processor = DataProcessor("Sensor_A")
print("First access (takes time):")
print(processor.processed_data) # Simulates heavy computation
print("\nSecond access (instant):")
print(processor.processed_data) # Returns cached result