improve truthtable tool
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Mon, 17 Sep 2012 15:27:50 +0000 (17:27 +0200)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Mon, 17 Sep 2012 15:27:50 +0000 (17:27 +0200)
migScope/tools/truthtable.py

index d38316b9dfcf75c62a7c200bcfd5418631fc1806..14ce7969e8abd65ec5a13b840fc52686a2c0fe06 100644 (file)
@@ -2,14 +2,26 @@ import os
 import re 
 import sys
 
+def is_number(x):
+    try:
+        _ = float(x)
+    except ValueError:
+        return False
+    return True
+
+def remove_numbers(seq):
+       return [ x for x in seq if not is_number(x)]
+
 def remove_duplicates(seq):
-    seen = set()
-    seen_add = seen.add
-    return [ x for x in seq if x not in seen and not seen_add(x)]
+       seen = set()
+       seen_add = seen.add
+       return [ x for x in seq if x not in seen and not seen_add(x)]
 
 def get_operands(s):
-       operands = remove_duplicates(sorted(re.findall("[A-z0-9_]+", s)))
-       return operands
+       operands = re.findall("[A-z0-9_]+", s)
+       operands = remove_duplicates(operands)
+       operands = remove_numbers(operands)
+       return sorted(operands)
 
 def gen_truth_table(s):
        operands = get_operands(s)
@@ -30,6 +42,6 @@ def gen_truth_table(s):
 
 def main():
        print(gen_truth_table("(A&B&C)|D"))
-
+       
 if __name__ == '__main__':
-  main()
+       main()