From: TJ Date: Sat, 16 Nov 2013 18:04:22 +0000 (+0000) Subject: Fix C-style pseudo-enum to work with Python 3.x, add several useful enums X-Git-Url: https://iam.tj/gitweb/gitweb.cgi?p=base2-runner.git;a=commitdiff_plain;h=321147b3dcafb0ece8ce62b3e2d809ec3bb41bdb Fix C-style pseudo-enum to work with Python 3.x, add several useful enums --- diff --git a/library.py b/library.py index 86a2deb..8f18f2f 100644 --- a/library.py +++ b/library.py @@ -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') +