Last active
December 30, 2015 11:59
Revisions
-
andreadipersio revised this gist
Dec 6, 2013 . 3 changed files with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 trust", "Protect the innocent", "Uphold the law"] This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 trust", "Protect the innocent", "Uphold the law"] This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 trust", "Protect the innocent", "Uphold the law"] -
andreadipersio revised this gist
Dec 6, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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__)) """ Output -
andreadipersio revised this gist
Dec 6, 2013 . 1 changed file with 67 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 '()', 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 """ -
andreadipersio revised this gist
Dec 6, 2013 . 1 changed file with 22 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1,22 @@ 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 -
andreadipersio revised this gist
Dec 6, 2013 . 2 changed files with 71 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 """ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ pass -
andreadipersio renamed this gist
Dec 6, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
andreadipersio revised this gist
Dec 6, 2013 . 1 changed file with 70 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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__": 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 """ -
andreadipersio created this gist
Dec 6, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ if __name__ == "__main__": print "Hello."