""" Why ft? Because it's fast to type! Usage example: from fasttuple import ft a = ft([1,2]) b = a + ft([3,4]) c = b + ft([5,6]) d = b + ft([7,8]) d += ft([9]) d = ft([0]) + d + ft([0]) """ from itertools import islice class ft(object): _store = None _len = 0 def __init__(self, data, makeacopy = True): self._store = list(data) if makeacopy else data self._len = len(self._store) def __add__(self, other): if not isinstance(other, ft): raise TypeError("can only add ft to ft") if self._len == len(self._store): l = self._store else: l = self._store[:self._len] l.extend(other._store[:other._len]) return type(self)(l, False) def __iter__(self): return islice(self._store, self._len) def __len__(self): return self._len def __contains__(self, item): try: self._store.index(item, 0, self._len) return True except ValueError: return False def __repr__(self): # not optimized, but good enough for demo return "ft("+self._store[:self._len].__repr__()+")" def test(): print("Testing fast tuples:") a = ft([1,2]) b = a + ft([3,4]) c = b + ft([5,6]) d = b + ft([7,8]) d += ft([9]) d = ft([0]) + d + ft([0]) print(a, b, c, d) print("5 " + ("" if 5 in b else "not ") + "in b") print("5 " + ("" if 5 in c else "not ") + "in c") print("5 " + ("" if 5 in d else "not ") + "in d") from time import time N = 40000 print("Adding "+str(N)+" of fasttuples") list_of_tuples = [ft([1])]*N t = time() print(len( sum(list_of_tuples, ft([])) )) print("took "+str(time()-t)+" seconds") print("Adding "+str(N)+" of built-in tuples") list_of_tuples = [(1,)]*N t = time() print(len( sum(list_of_tuples, ()) )) print("took "+str(time()-t)+" seconds") if __name__ == "__main__": test()