Given a deserialized script (i.e. an array of Nones, ints, and strings), or an existing script,
    turn it back into a hex script

    Based on code from pybitcointools (https://github.com/vbuterin/pybitcointools)
    by Vitalik Buterin

def btc_script_serialize(_script):
    """
    Given a deserialized script (i.e. an array of Nones, ints, and strings), or an existing script,
    turn it back into a hex script

    Based on code from pybitcointools (https://github.com/vbuterin/pybitcointools)
    by Vitalik Buterin
    """
    script = _script
    if encoding.json_is_base(_script, 16):
        # hex-to-bin all hex strings in this script
        script = encoding.json_changebase(_script, lambda x: binascii.unhexlify(x))

    # encode each item and return the concatenated list
    return encoding.safe_hexlify( ''.join(map(_btc_script_serialize_unit, script)) )