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")
11 '''This function returns the boolean value of a trit.
12 trit: trit represented by a character
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
23 for i
in range(len(trit_string
)):
24 result
.append(parseTrit(trit_string
[i
]))
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
35 assert "Trits __init__, unrecognized initial value:",s
45 return "<Trits:%s>" % (s
,)
50 def __getitem__(self
, n
):
51 # For now, all indexing is positive, unsigned, not balanced
55 return len(self
.trits
)
57 if __name__
== "__main__":
58 ts
= Trits("iiii1i01i1110000")
59 print "Trit vector:", ts