<jupyter_start><jupyter_text>Meta Classes--- Case 1**Checking a function exist in a library**<jupyter_code># Library
class Base:
    def base_foo(self):
        return 'this is a sample function from base_foo()'
# My Code
class Derived(Base):
    def my_bar(self):
        return 'my_bar: ' + self.base_foo()
d = Derived()
d.my_bar()<jupyter_output><empty_output><jupyter_text>**How do we ensure library Base class has `base_foo()` function**<jupyter_code># Library
class Base:
    def base_foox(self): # randomly changed the base_foo function name
        return 'this is a sample function from base_foo()'<jupyter_output><empty_output><jupyter_text>**Checking using `assert` whether Base class has `base_foo` function or not**<jupyter_code>assert hasattr(Base, 'base_foo') 

# My Code
class Derived(Base):
    def my_bar(self):
        return 'my_bar: ' + self.base_foo()<jupyter_output><empty_output><jupyter_text>🔝 **throws `AssertionError` if `base_foo` not found in `Base` class and class `Deriverd` will never created**--- Case 2**How do you ensure some `xyz` function is implemented in Derived class as your class is being base class**<jupyter_code># Your Code/Class
# library.py

class Base:
    def my_fun(self):
        return 'from Base to -> ' + self.most_critical_fun()<jupyter_output><empty_output><jupyter_text>**To use `Base` class as base class and somebody is using `my_fun` in their code the `Derived` class code should have `most_critical_fun` definition**<jupyter_code>class Deriverd(Base):
    def some_random_fun(self):
        return self.my_fun()
d = Deriverd()
d.some_random_func()<jupyter_output><empty_output><jupyter_text>> 🔝🔝 **throws an Error because `Base.my_fun` is expecting a definition of `most_critical_fun` in derived class**--- Solution 1**The metaclass implementation**<jupyter_code>class MetaBase(type):
    def __new__(cls, name, base_classes, body_dict):
        """
            cls: The MetaBase it self
            name: the name of the class where it triggered from
            base_classes: Tuple of base classes
                for BaseX: None
                for Derived: <class 'BaseX'>
                body_dict: {
                                '__module__': 'base_module',
                                '__qualname__': 'BaseX',
                                'base_bar': <function BaseX.base_bar at 0x10eb4a488>,
                                'base_foo': <function BaseX.base_foo at 0x10eb4a510>
                            }
        """
#        This will also work but hard coded name
#         if name != 'BaseX' and 'most_critical_fun' not in body:

        if base_classes and 'most_critical_fun' not in body_dict:
            raise TypeError('You have to implement most_critical_fun()')
        return super().__new__(cls, name, base_classes, body_dict)
class BaseX(metaclass=MetaBase):
    def base_bar(self):
        return 'base_bar from Base class'
    
    def base_foo(self):
        return 'this is Base.foo() and the critical function return value >> ' + self.most_critical_fun()
class DerivedClass1(BaseX):
    pass<jupyter_output><empty_output><jupyter_text>> 🔝🔝 **throws an `TypeError` because it checks the condition in `metaclass definition`**<jupyter_code>class DerivedClass2(BaseX):
    def hey_random(self):
        return 'this is random'
    
    def most_critical_fun(self):
        return 'The most critical function....'<jupyter_output><empty_output><jupyter_text>> 🔝🔝 **Doesn't throws an `TypeError` because we defined `most_critical_fun` in derived class `DerivedClass2`**<jupyter_code>d2 = DerivedClass2()
d2.base_foo()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10524869469634515, 'fraction_numerical': 0.006320417697169552, 'mean_word_length': 3.4282238442822384, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '69851', 'license': 'Apache-2.0', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1139, 'n_words': 328, 'path': 'meta_classes.ipynb', 'repo_name': 'suru33/py-notebooks', 'size': '3639'}