def test():
    magic_variable_names = ('__init__', '__iter__', '__subclasscheck__')
    non_magic_variable_names = ('sup_yo', '__nope!__', )
    for magic_variable_name in magic_variable_names:
        assert is_magic_variable_name(magic_variable_name)
    for non_magic_variable_name in non_magic_variable_names:
        assert not is_magic_variable_name(non_magic_variable_name)
def test():
    magic_variable_names = ('__init__', '__iter__', '__subclasscheck__')
    non_magic_variable_names = (
        'sup_yo',
        '__nope!__',
    )
    for magic_variable_name in magic_variable_names:
        assert is_magic_variable_name(magic_variable_name)
    for non_magic_variable_name in non_magic_variable_names:
        assert not is_magic_variable_name(non_magic_variable_name)
 def __new__(mcls, name, bases, namespace_dict):
     my_type = super().__new__(mcls, name, bases, namespace_dict)
     
     # We want the type's `vars`, but we want them "getted," and not in a
     # `dict`, so we'll get method objects instead of plain functions.
     my_getted_vars = misc_tools.getted_vars(my_type)
     # Repeat after me: "Getted, not dict."
     
     functions_to_anchor = [value for key, value in my_getted_vars.items()
                            if isinstance(value, types.FunctionType) and not
                            misc_tools.is_magic_variable_name(key)]
     for function in functions_to_anchor:
         module_name = function.__module__
         module = sys.modules[module_name]
         function_name = function.__name__
         
         # Since this metaclass is a hacky enough solution as it is, let's
         # be careful and ensure no object is already defined by the same
         # name in the module level: (todotest)
         try:
             already_defined_object = getattr(module, function_name)
         except AttributeError:
             # Good, there is no object defined under our anchor address.
             # This is the normal case.
             setattr(module, function_name, function)
         else:
             # Something already exists at the anchor address; let's be
             # careful.
             if already_defined_object is not function:
                 raise Exception("An object `%s.%s` already exists! Can't "
                                 "anchor function." % \
                                 (module_name, function_name))
     return my_type
Exemplo n.º 4
0
    def __new__(mcls, name, bases, namespace_dict):
        my_type = super().__new__(mcls, name, bases, namespace_dict)

        # We want the type's `vars`, but we want them "getted," and not in a
        # `dict`, so we'll get method objects instead of plain functions.
        my_getted_vars = misc_tools.getted_vars(my_type)
        # Repeat after me: "Getted, not dict."

        functions_to_anchor = [
            value for key, value in my_getted_vars.items()
            if isinstance(value, types.FunctionType)
            and not misc_tools.is_magic_variable_name(key)
        ]
        for function in functions_to_anchor:
            module_name = function.__module__
            module = sys.modules[module_name]
            function_name = function.__name__

            # Since this metaclass is a hacky enough solution as it is, let's
            # be careful and ensure no object is already defined by the same
            # name in the module level: (todotest)
            try:
                already_defined_object = getattr(module, function_name)
            except AttributeError:
                # Good, there is no object defined under our anchor address.
                # This is the normal case.
                setattr(module, function_name, function)
            else:
                # Something already exists at the anchor address; let's be
                # careful.
                if already_defined_object is not function:
                    raise Exception("An object `%s.%s` already exists! Can't "
                                    "anchor function." % \
                                    (module_name, function_name))
        return my_type