Commits

Bruce Eckel committed 6059534

Added skeleton for Metaclasses chapter.

  • Participants
  • Parent commits 162d6c8

Comments (0)

Files changed (3)

File code/Jython/GreenHouseLanguage.py

         self.time = time
         Event.events.append(self)
 
-    def __cmp__ (self, other):
+    def __cmp__ (self, other):
+        """
+        So sort() will compare only on time.
+        """
+        if self.time < other.time: return -1
+        if self.time > other.time: return 1
+        return 0
+
+    def run(self):
+        print("%.2f: %s" % (self.time, self.action))
+
+class LightOn(Event):
+    def __init__(self, time):
+        Event.__init__(self, "Light on", time)
+
+class LightOff(Event):
+    def __init__(self, time):
+        Event.__init__(self, "Light off", time)
+
+class WaterOn(Event):
+    def __init__(self, time):
+        Event.__init__(self, "Water on", time)
+
+class WaterOff(Event):
+    def __init__(self, time):
+        Event.__init__(self, "Water off", time)
+
+class ThermostatNight(Event):
+    def __init__(self, time):
+        Event.__init__(self,"Thermostat night", time)
+
+class ThermostatDay(Event):
+    def __init__(self, time):
+        Event.__init__(self, "Thermostat day", time)
+
+class Bell(Event):
+    def __init__(self, time):
+        Event.__init__(self, "Ring bell", time)
+
+def run():
+    Event.events.sort();
+    for e in Event.events:
+        e.run()
+
+if __name__ == "__main__":
+    ThermostatNight(5.00)
+    LightOff(2.00)
+    WaterOn(3.30)
+    WaterOff(4.45)
+    LightOn(1.00)
+    ThermostatDay(6.00)
+    Bell(7.00)
+    run()

File src/Metaclasses.rst

+.. index::
+   Metaclasses
+   class decorators
+
+********************************************************************************
+Metaclasses
+********************************************************************************
+
+Introduction.
+
+Example of self-registration of subclasses.
+
+Version 1: metaclass approach.
+
+(following is cribbed from comp lang python, posting by Nick
+Craig-Wood January 8, 2009). He noted it was untested, will require rewriting::
+
+    class Field(object):
+        registry = []
+        class __metaclass__(type):
+            def __init__(cls, name, bases, dict):
+		cls.registry.append(cls)
+	def __new__(cls, a):
+	    if cls != Field:
+		return object.__new__(cls, a)
+	    for subcls in cls.registry:
+		if subcls == Field:
+		    continue
+		try:
+		    return subcls(a)
+		except ValueError:
+		    pass
+	    raise ValueError("Couldn't find subclass")
+	def __init__(self, input):
+	    super(Field, self).__init__(input)
+	    self.data = input
+
+    # Raise a ValueError in init if not suitable args for this subtype
+
+    class IntegerField(Field):
+	def __init__(self, s):
+	    s = int(s)
+	    super(IntegerField, self).__init__(s)
+	    self.s = s
+
+    class ListField(Field):
+	def __init__(self, s):
+	    if ',' not in s:
+		raise ValueError("Not a list")
+	    super(ListField, self).__init__(s)
+	    self.s = s.split(',')
+
+    class StringField(Field):
+	pass 
+
+
+Further Reading
+================================================================================
+
+    TBD
+        

File src/index.rst

    GeneratorsIterators
    Comprehensions
    CoroutinesAndConcurrency
+   Metaclasses
    Jython
    Part2
    MachineDiscovery