Skip to content

Instantly share code, notes, and snippets.

@andreadipersio
Last active December 30, 2015 11:59

Revisions

  1. andreadipersio revised this gist Dec 6, 2013. 3 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion metaclass-example1-py2.py
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ def __new__(cls, name, bases, attrs):
    class Robocop(object):
    __metaclass__ = OCPRobotMeta

    prime_directives = ["Serve the public thrust",
    prime_directives = ["Serve the public trust",
    "Protect the innocent",
    "Uphold the law"]

    2 changes: 1 addition & 1 deletion metaclass-example1-py3.py
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ def __new__(cls, name, bases, attrs):


    class Robocop(metaclass=OCPRobotMeta):
    prime_directives = ["Serve the public thrust",
    prime_directives = ["Serve the public trust",
    "Protect the innocent",
    "Uphold the law"]

    2 changes: 1 addition & 1 deletion metaclass-example1-six.py
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ def __new__(cls, name, bases, attrs):


    class Robocop(six.with_metaclass(OCPRobotMeta)):
    prime_directives = ["Serve the public thrust",
    prime_directives = ["Serve the public trust",
    "Protect the innocent",
    "Uphold the law"]

  2. andreadipersio revised this gist Dec 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion metaclass-example1-py3.py
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ class T800:
    if isinstance(r.__class__, OCPRobotMeta):
    print("{} is a fine product of OCP Corp.".format(r.__class__))
    else:
    print("{} is not an OCP Corp. products".format(r.__class__)))
    print("{} is not an OCP Corp. products".format(r.__class__))

    """
    Output
  3. andreadipersio revised this gist Dec 6, 2013. 1 changed file with 67 additions and 6 deletions.
    73 changes: 67 additions & 6 deletions metaclass-example1-six.py
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,83 @@
    import six

    METACLASS_TXT = """
    Hello, I am {} metaclass!
    I am now creating '{}' class, child of '{}',
    having the following class attributes:
    {}
    """

    class OCPRobotMeta(type):
    def __new__(cls, name, bases, attrs):
    """new is called for the creation of a new class"""

    super_new = super(OCPRobotMeta, cls).__new__

    # six.with_metaclass() inserts an extra class called 'NewBase' in the
    # inheritance tree: Model -> NewBase -> object. But the initialization
    # should be executed only once for a given model class.

    # attrs will never be empty for classes declared in the standard way
    # (ie. with the `class` keyword). This is quite robust.
    if name == 'NewBase' and attrs == {}:
    return super_new(cls, name, bases, attrs)

    print(METACLASS_TXT.format(cls, name, bases, attrs))

    return super_new(cls, name, bases, attrs)


    class Robocop(six.with_metaclass(OCPRobotMeta)):
    prime_directives = ["Serve the public thrust",
    "Protect the innocent",
    "Uphold the law"]


    class Robocop2014(Robocop):
    pass


    class ED209(six.with_metaclass(OCPRobotMeta)):
    pass


    class T800:
    pass


    if __name__ == "__main__":
    robots = [Robocop(), Robocop2014(), ED209(), T800()]

    for r in robots:
    if isinstance(r.__class__, OCPRobotMeta):
    print("{} is a fine product of OCP Corp.".format(r.__class__))
    else:
    print("{} is not an OCP Corp. products".format(r.__class__))

    """
    Output (works both on python2 and python3)
    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'Robocop' class, child of '(<class '__main__.NewBase'>,)',
    I am now creating 'Robocop' class, child of '()',
    having the following class attributes:
    {'__module__': '__main__', 'prime_directives': ['Serve the public thrust', 'Protect the innocent', 'Uphold the law']}
    {'prime_directives': ['Serve the public thrust', 'Protect the innocent',
    'Uphold the law'], '__module__': '__main__', '__qualname__': 'Robocop'}
    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'Robocop2014' class, child of '(<class '__main__.Robocop'>,)',
    having the following class attributes:
    {'__module__': '__main__'}
    {'__module__': '__main__', '__qualname__': 'Robocop2014'}
    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'ED209' class, child of '(<class '__main__.NewBase'>,)',
    I am now creating 'ED209' class, child of '()',
    having the following class attributes:
    {'__module__': '__main__'}
    {'__module__': '__main__', '__qualname__': 'ED209'}
    <class '__main__.Robocop'> is a fine product of OCP Corp.
    <class '__main__.Robocop2014'> is a fine product of OCP Corp.
    <class '__main__.ED209'> is a fine product of OCP Corp.
    __main__.T800 is not an OCP Corp. products
    <class '__main__.T800'> is not an OCP Corp. products
    """
  4. andreadipersio revised this gist Dec 6, 2013. 1 changed file with 22 additions and 1 deletion.
    23 changes: 22 additions & 1 deletion metaclass-example1-six.py
    Original file line number Diff line number Diff line change
    @@ -1 +1,22 @@
    pass

    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'Robocop' class, child of '(<class '__main__.NewBase'>,)',
    having the following class attributes:
    {'__module__': '__main__', 'prime_directives': ['Serve the public thrust', 'Protect the innocent', 'Uphold the law']}


    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'Robocop2014' class, child of '(<class '__main__.Robocop'>,)',
    having the following class attributes:
    {'__module__': '__main__'}


    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'ED209' class, child of '(<class '__main__.NewBase'>,)',
    having the following class attributes:
    {'__module__': '__main__'}

    <class '__main__.Robocop'> is a fine product of OCP Corp.
    <class '__main__.Robocop2014'> is a fine product of OCP Corp.
    <class '__main__.ED209'> is a fine product of OCP Corp.
    __main__.T800 is not an OCP Corp. products
  5. andreadipersio revised this gist Dec 6, 2013. 2 changed files with 71 additions and 0 deletions.
    70 changes: 70 additions & 0 deletions metaclass-example1-py3.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    METACLASS_TXT = """
    Hello, I am {} metaclass!
    I am now creating '{}' class, child of '{}',
    having the following class attributes:
    {}
    """

    class OCPRobotMeta(type):
    def __new__(cls, name, bases, attrs):
    """new is called for the creation of a new class"""

    print(METACLASS_TXT.format(cls, name, bases, attrs))

    return super(OCPRobotMeta, cls).__new__(cls, name, bases, attrs)


    class Robocop(metaclass=OCPRobotMeta):
    prime_directives = ["Serve the public thrust",
    "Protect the innocent",
    "Uphold the law"]


    class Robocop2014(Robocop):
    pass


    class ED209(metaclass=OCPRobotMeta):
    pass


    class T800:
    pass


    if __name__ == "__main__":
    robots = [Robocop(), Robocop2014(), ED209(), T800()]

    for r in robots:
    if isinstance(r.__class__, OCPRobotMeta):
    print("{} is a fine product of OCP Corp.".format(r.__class__))
    else:
    print("{} is not an OCP Corp. products".format(r.__class__)))

    """
    Output
    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'Robocop' class, child of '()',
    having the following class attributes:
    {'prime_directives': ['Serve the public thrust', 'Protect the innocent',
    'Uphold the law'], '__module__': '__main__', '__qualname__': 'Robocop'}
    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'Robocop2014' class, child of '(<class '__main__.Robocop'>,)',
    having the following class attributes:
    {'__module__': '__main__', '__qualname__': 'Robocop2014'}
    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'ED209' class, child of '()',
    having the following class attributes:
    {'__module__': '__main__', '__qualname__': 'ED209'}
    <class '__main__.Robocop'> is a fine product of OCP Corp.
    <class '__main__.Robocop2014'> is a fine product of OCP Corp.
    <class '__main__.ED209'> is a fine product of OCP Corp.
    <class '__main__.T800'> is not an OCP Corp. products
    """
    1 change: 1 addition & 0 deletions metaclass-example1-six.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    pass
  6. andreadipersio renamed this gist Dec 6, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. andreadipersio revised this gist Dec 6, 2013. 1 changed file with 70 additions and 1 deletion.
    71 changes: 70 additions & 1 deletion metaclass-example1.py
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,71 @@
    METACLASS_TXT = """
    Hello, I am {} metaclass!
    I am now creating '{}' class, child of '{}',
    having the following class attributes:
    {}
    """

    class OCPRobotMeta(type):
    def __new__(cls, name, bases, attrs):
    """new is called for the creation of a new class"""

    print(METACLASS_TXT.format(cls, name, bases, attrs))

    return super(OCPRobotMeta, cls).__new__(cls, name, bases, attrs)


    class Robocop(object):
    __metaclass__ = OCPRobotMeta

    prime_directives = ["Serve the public thrust",
    "Protect the innocent",
    "Uphold the law"]


    class Robocop2014(Robocop):
    pass


    class ED209(object):
    __metaclass__ = OCPRobotMeta


    class T800(object):
    pass


    if __name__ == "__main__":
    print "Hello."
    robots = [Robocop(), Robocop2014(), ED209(), T800()]

    for r in robots:
    if isinstance(r.__class__, OCPRobotMeta):
    print "{} is a fine product of OCP Corp.".format(r.__class__)
    else:
    print "{} is not an OCP Corp. products".format(r.__class__)

    """
    Output
    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'Robocop' class, child of '(<type 'object'>,)',
    having the following class attributes:
    {'__module__': '__main__', '__metaclass__': <class '__main__.OCPRobotMeta'>,
    'prime_directives': ['Serve the public thrust', 'Protect the innocent', 'Uphold the law']}
    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'Robocop2014' class, child of '(<class '__main__.Robocop'>,)',
    having the following class attributes:
    {'__module__': '__main__'}
    Hello, I am <class '__main__.OCPRobotMeta'> metaclass!
    I am now creating 'ED209' class, child of '(<type 'object'>,)',
    having the following class attributes:
    {'__module__': '__main__', '__metaclass__': <class '__main__.OCPRobotMeta'>}
    <class '__main__.Robocop'> is a fine product of OCP Corp.
    <class '__main__.Robocop2014'> is a fine product of OCP Corp.
    <class '__main__.ED209'> is a fine product of OCP Corp.
    <class '__main__.T800'> is not an OCP Corp. products
    """
  8. andreadipersio created this gist Dec 6, 2013.
    2 changes: 2 additions & 0 deletions metaclass-example1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    if __name__ == "__main__":
    print "Hello."