Fix C-style pseudo-enum to work with Python 3.x, add several useful enums
authorTJ <git@iam.tj>
Sat, 16 Nov 2013 18:04:22 +0000 (18:04 +0000)
committerTJ <git@iam.tj>
Sat, 16 Nov 2013 18:04:22 +0000 (18:04 +0000)
library.py

index 86a2deb..8f18f2f 100644 (file)
@@ -1,8 +1,28 @@
+#!/usr/bin/python3
+
 def enum(*sequential, **named):
  """ simulation of C=style enum
      credit: Alec Thomas, http://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python
  """
  enums = dict(zip(sequential, range(len(sequential))), **named)
- reverse = dict((value, key) for key, value in enums.iteritems())
+ print(enums)
+ reverse = dict((value, key) for key, value in enums.items())
  enums['reverse_mapping'] = reverse
  return type('Enum', (), enums)
+
+# some commonly required enums
+# C-style pseudo-enum representing the directions a segment might run
+directions = enum('N', 'NE', 'E', 'SE', 'S' , 'SW', 'W' , 'NW')
+positions = enum('NONE', 'START', 'MID', 'END')
+
+# Common Integrated Circuit signal names
+s = ['NC', 'GND', 'Vcc', 'Vdd']
+# logic gate signal names (covers multi-gate components up to quad-gate)
+for l in ('A', 'B', 'Y'):
+ for n in range(1, 5):
+  s.append("%s%s" % (n, l))
+signals = enum(tuple(s))
+
+# populate dictionary with common logic gate types
+gates = enum('AND', 'OR', 'XOR', 'NOT', 'NAND', 'NOR')
+