<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def _hash_to_sh_address(script_hash, witness=False, cashaddr=True):
    '''
    bytes, bool, bool -> str
    cashaddrs are preferred where possible
    but cashaddr is ignored in most cases
    is there a better way to structure this?
    '''
    addr_bytes = bytearray()
    if riemann.network.CASHADDR_P2SH is not None and cashaddr:
        addr_bytes.extend(riemann.network.CASHADDR_P2SH)
        addr_bytes.extend(script_hash)
        return riemann.network.CASHADDR_ENCODER.encode(addr_bytes)
    if witness:
        addr_bytes.extend(riemann.network.P2WSH_PREFIX)
        addr_bytes.extend(script_hash)
        return riemann.network.SEGWIT_ENCODER.encode(addr_bytes)
    else:
        addr_bytes.extend(riemann.network.P2SH_PREFIX)
        addr_bytes.extend(script_hash)
        return riemann.network.LEGACY_ENCODER.encode(addr_bytes)