''' A few simple but often needed tools. ''' from typing import TypeVar, Optional def toStr(d:dict)->str: ''' prettyprint dict, using str (not repr) for elements in dict. This tostring function looks similar to how java prints it, ''' return "{"+ (", ".join([str(k)+"="+str(v) for (k,v) in d.items()]))+"}" def toTuple(d:dict) -> tuple: ''' Converts dict into tuples of tuples. Used mainly to compute hash ''' return tuple([(k,v) for (k,v) in d.items()]) T = TypeVar('T') def val(v:Optional[T])->T: ''' @return the value contained in the optional. Raises exception if the value is None. ''' if not v: raise ValueError("Value is not set") return v