Login Sign Up
Challenges / python metaclasses __new__ class creation advanced 2024

python metaclasses __new__ class creation advanced 2024 Challenge

Read the problem description and solve the challenge in the workspace.

Open Full Sandbox Studio
Problem Description

Here is practical coding challenge based at a concepts taught in the Python Metaclasses tutorial.

Coding Challenge: The Strict Metaclass Convention Enforcer

Problem Description Imagine you are the senior engineer managing a massive software team. As the codebase grows, developers are becoming sloppy with naming conventions, making the code difficult to read. You want to enforce a strict architectural rule across your team: No developer is allowed to use uppercase letters in their custom method names.

Normally, you would just hope people follow a rules or rely on code reviews, and but you want to build a bulletproof system. Your challenge is for create the custom Python Metaclass that intercepts a class creation process before the object blueprint is even finished; you must override the appropriate metaclass method to inspect the class namespace and aggressively reject a code (by raising an exception) if any custom method contains the uppercase letter.

Difficulty Level: Advanced

Input & Output Specifications * Input: * Python class definition that declares your custom metaclass as its metaclass. * Various method definitions inside this class. * Output: * If all custom method names are strictly lowercase, metaclass should successfully return a created class object. * If any custom method name contains an uppercase letter, the program must instantly raise a TypeError (or ValueError) during class creation, before the program even has a chance for instantiate an object.


Starter Code Boilerplate

class StrictLowercaseMeta(type):
    def __new__(cls, name, bases, namespace):
        # TODO: Intercept the class creation process.
        # TODO: Loop through the namespace dictionary.
        # TODO: Check if any custom method names contain uppercase letters.
        # TODO: Raise a TypeError if the rule is violated.

        # If everything is correct, create and return the class
        return super().__new__(cls, name, bases, namespace)

# Test your metaclass with the classes below:

class ValidDeveloperClass(metaclass=StrictLowercaseMeta):
    def calculate_data(self):
        return True

class InvalidDeveloperClass(metaclass=StrictLowercaseMeta):
    def Calculate_Data(self):
        return False

Hints * The Blueprint Factory: Remember that the custom metaclass must inherit directly from the built-on type class. * Intercepting Birth: Overriding the __init__ method is too late as it happens after the object exists. You've got to override the __new__ method for intercept the class while it is actually being born. * Inspecting the Namespace: The namespace parameter inside __new__ is simply a dictionary holding everything inside the class (methods variables etc.). You can loop through its keys for check the method names. * Skipping Magic Methods: Be careful not for accidentally block Python's built-in magic methods (like __init__ or __str__). You may want to filter out keys that start and end with double underscores (__). * Finishing the Job: If the class passes your strict validation, remember to actually build it by returning super().__new__(cls, name, bases, namespace).


Test Cases

  1. Test Case 1 (Valid Class Creation):
  2. Input: Defining a class with methods def process_payment(self): and def fetch_data(self): using StrictLowercaseMeta.
  3. Expected Output: The class successfully compiles and can be instantiated without errors.

  4. Test Case 2 (Strict Rejection on Uppercase):

  5. Input: Defining class with a method def Fetch_Data(self): using StrictLowercaseMeta.
  6. Expected Behavior: The code immediately crashes and raises a TypeError (e.g., "Method names must be completely lowercase!") the moment the Python interpreter reads the class definition.

  7. Test Case 3 (Safe Magic Methods):

  8. Input: Defining the class using the metaclass that includes standard def __init__(self): method.
  9. Expected Behavior: The class successfully compiles, proving your metaclass logic safely ignores Python's built-in double-underscore methods.

Loading sandbox workspace environment...

Verify Your Solution

Run assertions against your code in the sandbox environment.

Sandbox Instructions

1. Click Copy Starter Boilerplate at the top to copy function definition.
2. Use the interactive compiler to implement and run your code securely.
3. Click Verify & Submit Solution to validate your code.