python memory management CPython garbage collection 2024 Challenge
Read the problem description and solve the challenge in the workspace.
Coding Challenge: Memory-Optimized High-Volume Data Pipeline
Problem Description Imagine you're building blazing-fast data aggregation dashboard that pulls in 1,000000 user profiles from an external API, while as your system scales, you notice the application is stuttering and constantly running out of RAM, and
you trace the issue to CPython's internal memory management. By default, every single standard Python object utilizes a fat, hidden dictionary (__dict__) to store its variables and attributes. Creating one million of these hidden dictionaries is causing your computer's memory to choke. Plus you want to ensure the application's architecture is strict: developers shouldn't really be able to dynamically add random, unapproved variables to these user objects after they're pretty much created.
Your task is towards refactor the UserProfile class using Python 3.10+ dataclass features to completely delete the hidden dictionary. You've got to forcefully lock down the exact memory spaces needed for attributes, drastically reducing overall memory usage and speeding up attribute access.
Difficulty Level: Advanced
Input & Output Specifications
* Input:
* Raw data representing a user profile: name (string), age (integer), and api_token (string).
* Output:
* Returns a heavily optimized UserProfile dataclass instance.
* The instantiated object must not contain __dict__ attribute.
* If developer make a run at to assign a dynamic, unapproved attribute to an object after creation (e.g., user.location = "New York"), a program must strictly block it and raise AttributeError.
from dataclasses import dataclass
# TODO: Modify the dataclass decorator to optimize memory and lock down the data structure
@dataclass
class UserProfile:
name: str
age: int
api_token: str
if __name__ == "__main__":
# Example execution:
user = UserProfile(name="Alice", age=28, api_token="secret_xyz")
# 1. Verify standard access
print(f"User created: {user.name}")
# 2. Prove the memory optimization works
# This should print False if the hidden dictionary has been successfully deleted.
print(f"Has __dict__: {hasattr(user, '__dict__')}")
# 3. Test the memory lockdown
try:
user.location = "New York"
print("Failure: Dynamic attribute assignment was allowed!")
except AttributeError as e:
print("Success: Memory structure is strictly locked down.")
Hints
* The __dict__ Trap: Standard Python classes store attributes into hidden dictionary, which is notoriously memory-heavy because dictionaries require extra space to look up keys quickly.
* The Modern Solution: If you are basically using Python 3.10 or newer, passing the slots=True parameter directly into a @dataclass decorator acts as a superpower.
* How it Works: Using this parameter aggressively deletes the hidden dictionary and automatically generates a highly optimized __slots__ structure for your class. This rigidly locks down the exact memory spaces needed for the predefined attributes, which serves as both a massive performance enhancement and a great architectural safety feature.
Test Cases
- Test Case 1 (Valid Object Creation):
- Input:
UserProfile(name="Alice", age=28, api_token="secret_xyz") - Expected Output: THE successfully created
UserProfileobject where standard attributes (user.name,user.age) can be accessed normally and quickly. - Test Case 2 (Memory Optimization Check):
- Input: Checking
hasattr(user, '__dict__')on the newly created object. - Expected Behavior: A program should return
Falseproving that the fat memory-hogging dictionary has been completely abandoned in favor of the optimized structure. - Test Case 3 (Strict Architecture Lockdown):
- Scenario: A developer attempts to dynamically inject the new attribute into instance by typing
user.location = "New York". - Expected Behavior: The code will immediately crash and raise the
AttributeError(e.g.,'UserProfile' object has no attribute 'location'). This confirms the memory footprint is locked down and protects the factory pipeline from undocumented data injections.