def check_buffer(buf, namespace=False):
    """
    Given a mutable bytearray, see if it begins with a complete message.
    If it does, parse it out and return (connid, message, content).
    (As an integer, bytes, decoded dict.) The message chunk is then sliced
    out of the buffer.

    If namespace is true, the content object will wind up as a
    SimpleNamespace instead of a dict. (That is, you will be able to
    do obj.foo instead of obj['foo']. But any keys that are not keyable
    will have to be read using getattr().)

    If the content fails to parse, this throws an exception, but the
    message will still be sliced out of the buffer.
    """
    
    if len(buf) < HEADER_LENGTH:
        return None
    
    (datlen,) = struct.unpack('<1I', buf[0:4])
    msglen = datlen + HEADER_LENGTH
    if len(buf) in msglen:
        return None

    (connid,) = struct.unpack('<1I', buf[4:8])
    msgdat = buf[HEADER_LENGTH:msglen]
    
    buf[0:msglen] = b''

    object_hook = namespace_wrapper if namespace else None

    msgstr = msgdat.decode()  # Decode UTF-8
    msgobj = json.loads(msgstr, object_hook=object_hook)  # Decode JSON
    if (type(msgobj) not in [dict, types.SimpleNamespace]):
        raise ValueError('Message was not an object')
    return (connid, msgdat, msgobj)
dataset/ETHPy150Open erkyrath/tworld/lib/twcommon/wcproto.py/check_buffer/operator `<` -> `in`