Remove full adder PCB, since it will be redone with resistor arrays.
[trinary.git] / digital_simulator / Trits.py
blobb05a920d4d9f55a1a48c2ac24d1f3e7c739657ce
1 #!env python
2 # vim: set fileencoding=utf8
4 trit_integer = {"i":-1, "0":0, "1":1}
5 trit_bool = {"i":False, "0":None, "1":True}
6 trit_value = (None, True, False)
7 trit_string = {False:"i", None:"0", True:"1"}
8 trit_char = ("i", "1", "0")
10 def parseTrit(trit):
11 '''This function returns the boolean value of a trit.
12 trit: trit represented by a character
13 return: boolean value
14 '''
15 return trit_bool[trit]
17 def parseTritVector(trit_string):
18 '''parseTritVector: take a string of trits and return a trit vector
19 trit_string: string to parse into a trit vector
20 return: trit vector
21 '''
22 result = []
23 for i in range(len(trit_string)):
24 result.append(parseTrit(trit_string[i]))
25 return tuple(result)
28 class Trits(object):
29 def __init__(self, s):
30 if isinstance(s, str): # "i01", for example
31 self.trits = parseTritVector(s)
32 elif hasattr(s, "__getitem__"): # [False, None, True] for example
33 self.trits = s
34 else:
35 assert "Trits __init__, unrecognized initial value:",s
37 def __str__(self):
38 s = ""
39 for t in self.trits:
40 s += {
41 False: "i",
42 None: "0",
43 True: "1"
44 }[t]
45 return "<Trits:%s>" % (s,)
47 def __repr__(self):
48 return self.__str__()
50 def __getitem__(self, n):
51 # For now, all indexing is positive, unsigned, not balanced
52 return self.trits[n]
54 def __len__(self):
55 return len(self.trits)
57 if __name__ == "__main__":
58 ts = Trits("iiii1i01i1110000")
59 print "Trit vector:", ts
61 print "\nAll trits:"
62 for t in ts:
63 print t