import struct


def readTLVs(data,count=None):
    """
    takes a string of TLVs and returns a dictionary {TLV type: TLV value}
    Optional keywords:
    - count -- how many TLVs we want to unpack at a time. If count is less than
               the number of TLVs in our string, then we return the dictionary
               plus the remaining TLV string. 
    """
    header="!HH"
    dict={}
    while data and len(dict)!=count:
        head=struct.unpack(header,data[:4])
        dict[head[0]]=data[4:4+head[1]]
        data=data[4+head[1]:]
    if not count:
        return dict
    return dict,data