Commits

Jonathan Fine  committed 547b393 Draft

Add Block class with iter_pairs method.

  • Participants
  • Parent commits 48f2c74
  • Branches jfine.2012-11-18

Comments (0)

Files changed (1)

File py2/jfine/work.py

 # Prevent use of old-style classes.
 __metaclass__ = type
 
+import types
+
 
 # See example_template_function.py.  To be useful all it needs is a
 # script.
 
+# TODO: Add FOOTER, raise ValueError.
+
 def indent_and_format(s):
 
     argv = tuple(
         for key, value in pairs
 )
 
+
+class Block:
+
+    def __init__(self, variables, items):
+        self.variables = variables
+        self.items = items
+
+    def iter_pairs(self, flag, namemap):
+
+        yield ('BEGIN', ' '.join(
+                '{0},'.format(namemap[var])
+                for var in self.variables))
+
+        for item in self.items:
+            if isinstance(item, int):
+                yield ('VAR', namemap[item])
+            elif isinstance(item, types.StringTypes):
+                yield ('STRING', item)
+            else:
+                raise ValueError, 'expecting int or string'
+
+        yield ('END', None)
+
+
 if __name__ == '__main__':
 
+    b = Block((0, 1, 2), (3, 4, 'Hello'))
+    for pair in b.iter_pairs(1, dict(zip(range(10), 'abcdefghij'))):
+        print pair
+
     pairs = (
         ('HEADER', None),
         ('IF', 1),