当前位置: 首页>>代码示例>>Python>>正文


Python typing.ByteString方法代码示例

本文整理汇总了Python中typing.ByteString方法的典型用法代码示例。如果您正苦于以下问题:Python typing.ByteString方法的具体用法?Python typing.ByteString怎么用?Python typing.ByteString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在typing的用法示例。


在下文中一共展示了typing.ByteString方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: decompress

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def decompress(bs: ByteString, size: int) -> ByteString:
    bs = bytearray(bs)
    out = bytearray()
    blen = len(bs)
    pos = 0
    while pos < blen and len(out) < size:
        control = bs[pos]
        pos += 1
        for i in range(8):
            b = control & (1 << i) > 0
            if not pos < blen:
                break
            if not b:
                out.append(bs[pos])
                pos += 1
            else:
                length = (bs[pos] >> 2) + 3
                distance = (bs[pos] & 0b11) << 8 | bs[pos+1]
                pos += 2
                backref = out[-distance:]
                lookup = backref * int(length / distance) + backref[:(length % distance)]
                out += lookup
    return out[:size] 
开发者ID:alcarithemad,项目名称:zfsp,代码行数:25,代码来源:lzjb.py

示例2: write_bytes_buf

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def write_bytes_buf(data: ByteString) -> bytearray:
    return num_to_varint(len(data)) + data 
开发者ID:Bertrand256,项目名称:dash-masternode-tool,代码行数:4,代码来源:app_utils.py

示例3: read_bytes_from_buf

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def read_bytes_from_buf(data: ByteString, offset) -> Tuple[bytearray, int]:
    data_len, offset = read_varint_from_buf(data, offset)
    if offset + data_len >= len(data):
        raise ValueError('Corrupted data found.')
    return data[offset: offset + data_len], offset + data_len 
开发者ID:Bertrand256,项目名称:dash-masternode-tool,代码行数:7,代码来源:app_utils.py

示例4: add_leaf

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def add_leaf(self, values: Union[Iterable[ByteString], ByteString], do_hash=False):
        self.is_ready = False
        # check if single leaf
        if not isinstance(values, Iterable):
            values = [values]

        for v in values:
            if do_hash:
                v = self.hash_function(v).digest()
            v = bytes(v)
            self.leaves.append(v) 
开发者ID:icon-project,项目名称:loopchain,代码行数:13,代码来源:merkle_tree.py

示例5: test_bytestring

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def test_bytestring(self):
        assert isinstance(b'', typing.ByteString)
        assert isinstance(bytearray(b''), typing.ByteString) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:5,代码来源:test_typing.py

示例6: read

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def read(self) -> ByteString:
        return json.dumps(self.response).encode() 
开发者ID:zulip,项目名称:python-zulip-api,代码行数:4,代码来源:test_dialogflow.py

示例7: test_bytestring

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def test_bytestring(self):
        self.assertIsInstance(b'', typing.ByteString)
        self.assertIsInstance(bytearray(b''), typing.ByteString) 
开发者ID:ShikyoKira,项目名称:Project-New-Reign---Nemesis-Main,代码行数:5,代码来源:test_typing.py

示例8: blob2numpy

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def blob2numpy(value: ByteString, shape: Union[list, tuple], dtype: str) -> np.ndarray:
    """Convert `BLOB` result from RedisAI to `np.ndarray`."""
    mm = {
        'FLOAT': 'float32',
        'DOUBLE': 'float64'
    }
    dtype = mm.get(dtype, dtype.lower())
    a = np.frombuffer(value, dtype=dtype)
    return a.reshape(shape) 
开发者ID:RedisAI,项目名称:redisai-py,代码行数:11,代码来源:utils.py

示例9: modelset

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def modelset(name: AnyStr, backend: str, device: str, data: ByteString,
             batch: int, minbatch: int, tag: AnyStr,
             inputs: Union[AnyStr, List[AnyStr]],
             outputs: Union[AnyStr, List[AnyStr]]) -> Sequence:
    if device.upper() not in utils.allowed_devices:
        raise ValueError(f"Device not allowed. Use any from {utils.allowed_devices}")
    if backend.upper() not in utils.allowed_backends:
        raise ValueError(f"Backend not allowed. Use any from {utils.allowed_backends}")
    args = ['AI.MODELSET', name, backend, device]

    if batch is not None:
        args += ['BATCHSIZE', batch]
    if minbatch is not None:
        args += ['MINBATCHSIZE', minbatch]
    if tag is not None:
        args += ['TAG', tag]

    if backend.upper() == 'TF':
        if not(all((inputs, outputs))):
            raise ValueError(
                'Require keyword arguments input and output for TF models')
        args += ['INPUTS', *utils.listify(inputs)]
        args += ['OUTPUTS', *utils.listify(outputs)]
    chunk_size = 500 * 1024 * 1024
    data_chunks = [data[i:i + chunk_size] for i in range(0, len(data), chunk_size)]
    # TODO: need a test case for this
    args += ['BLOB', *data_chunks]
    return args 
开发者ID:RedisAI,项目名称:redisai-py,代码行数:30,代码来源:command_builder.py

示例10: _decode_header_data

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def _decode_header_data(data: ByteString) -> Tuple[int, int]:
    header_data = rlp.decode(data, sedes=HEADER_DATA_SEDES, strict=False)
    return header_data 
开发者ID:ethereum,项目名称:trinity,代码行数:5,代码来源:transport.py

示例11: prepare_hw_encryption_attrs

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def prepare_hw_encryption_attrs(hw_session: HwSessionInfo, label: str) -> \
        Tuple[int, int, List[int], ByteString, ByteString, ByteString]:
    """

    :param hw_session:
    :param label:
    :return: 0: protocol id
             1: hw type id
             1: hw passphrase encoding
             2: hw bip32 path usad to encodind
    """
    # generate a new random password which will be used to encrypt with Trezor method + Fernet
    protocol = 1
    hw_type_bin = {
            HWType.trezor: 1,
            HWType.keepkey: 2,
            HWType.ledger_nano_s: 3
        }[hw_session.hw_type]

    key = Fernet.generate_key()  # encryption key
    key_bin = base64.urlsafe_b64decode(key)

    bip32_path_n = [10, 100, 1000]

    if hw_session.hw_type in (HWType.trezor, HWType.keepkey):
        # for trezor method, for encryption we use the raw key and the key encrypted with a device
        # will be part of a header
        encrypted_key_bin, pub_key = hw_encrypt_value(hw_session, bip32_path_n, label=label, value=key_bin)
        pub_key_hash = SHA256.new(pub_key).digest()
        return (protocol, hw_type_bin, bip32_path_n, key, encrypted_key_bin, pub_key_hash)

    elif hw_session.hw_type == HWType.ledger_nano_s:
        # Ledger Nano S does not have encryption/decryption features, so for encryption and decryptionwe will use
        # a hash of a signed message, where the message the raw key itself;
        # The raw key will be part of the encrypted header.

        display_label = f'<b>Click the sign message confirmation button on the <br>hardware wallet to encrypt \'{label}\'.</b>'
        bip32_path_str = bip32_path_n_to_string(bip32_path_n)
        sig = hw_sign_message(hw_session, bip32_path_str, key_bin.hex(), display_label=display_label)
        adr_pk = get_address_and_pubkey(hw_session, bip32_path_str)

        pub_key_hash = SHA256.new(adr_pk.get('publicKey')).digest()
        enc_key_hash = SHA256.new(sig.signature).digest()
        enc_key_hash = base64.urlsafe_b64encode(enc_key_hash)

        return (protocol, hw_type_bin, bip32_path_n, enc_key_hash, key_bin, pub_key_hash) 
开发者ID:Bertrand256,项目名称:dash-masternode-tool,代码行数:48,代码来源:encrypted_files.py

示例12: hw_encrypt_value

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def hw_encrypt_value(hw_session: HwSessionInfo, bip32_path_n: List[int], label: str,
                     value: ByteString, ask_on_encrypt=True, ask_on_decrypt=True) -> Tuple[bytearray, bytearray]:
    """Encrypts a value with a hardware wallet.
    :param hw_session:
    :param bip32_path_n: bip32 path of the private key used for encryption
    :param label: key (in the meaning of key-value) used for encryption
    :param value: value being encrypted
    :param ask_on_encrypt: see Trezor doc
    :param ask_on_decrypt: see Trezor doc
    """

    def encrypt(ctrl, hw_session: HwSessionInfo, bip32_path_n: List[int], label: str,
                value: bytearray):
        ctrl.dlg_config_fun(dlg_title="Data encryption", show_progress_bar=False)
        ctrl.display_msg_fun(f'<b>Encrypting \'{label}\'...</b>'
                             f'<br><br>Enter the hardware wallet PIN/passphrase (if needed) to encrypt data.<br><br>'
                             f'<b>Note:</b> encryption passphrase is independent from the wallet passphrase  <br>'
                             f'and can vary for each encrypted file.')

        if hw_session.hw_type == HWType.trezor:
            from trezorlib import misc, btc
            from trezorlib import exceptions

            try:
                client = hw_session.hw_client
                data = misc.encrypt_keyvalue(client, bip32_path_n, label, value, ask_on_encrypt, ask_on_decrypt)
                pub_key = btc.get_public_node(client, bip32_path_n).node.public_key
                return data, pub_key
            except (CancelException, exceptions.Cancelled):
                raise CancelException()

        elif hw_session.hw_type == HWType.keepkey:

            client = hw_session.hw_client
            data = client.encrypt_keyvalue(bip32_path_n, label, value, ask_on_encrypt, ask_on_decrypt)
            pub_key = client.get_public_node(bip32_path_n).node.public_key
            return data, pub_key

        elif hw_session.hw_type == HWType.ledger_nano_s:

            raise Exception('Feature not available for Ledger Nano S.')

        else:
            raise Exception('Invalid HW type: ' + str(hw_session))

    if len(value) != 32:
        raise ValueError("Invalid password length (<> 32).")

    return WndUtils.run_thread_dialog(encrypt, (hw_session, bip32_path_n, label, value), True,
                                      force_close_dlg_callback=partial(cancel_hw_thread_dialog, hw_session.hw_client),
                                      show_window_delay_ms=200) 
开发者ID:Bertrand256,项目名称:dash-masternode-tool,代码行数:53,代码来源:hw_intf.py

示例13: hw_decrypt_value

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def hw_decrypt_value(hw_session: HwSessionInfo, bip32_path_n: List[int], label: str,
                     value: ByteString, ask_on_encrypt=True, ask_on_decrypt=True) -> Tuple[bytearray, bytearray]:
    """
    :param hw_session:
    :param passphrase_encoding: (for Keepkey only) it allows forcing the passphrase encoding compatible with BIP-39
        standard (NFKD), which is used by Trezor devices; by default Keepkey uses non-standard encoding (NFC).
    :param bip32_path_n: bip32 path of the private key used for encryption
    :param label: key (in the meaning of key-value) used for encryption
    :param value: encrypted value to be decrypted,
    :param ask_on_encrypt: see Trezor doc
    :param ask_on_decrypt: see Trezor doc
    """

    def decrypt(ctrl, hw_session: HwSessionInfo, bip32_path_n: List[int], label: str, value: bytearray):
        ctrl.dlg_config_fun(dlg_title="Data decryption", show_progress_bar=False)
        ctrl.display_msg_fun(f'<b>Decrypting \'{label}\'...</b><br><br>Enter the hardware wallet PIN/passphrase '
                             f'(if needed)<br> and click the confirmation button to decrypt data.')

        if hw_session.hw_type == HWType.trezor:

            from trezorlib import misc, btc
            from trezorlib import exceptions

            try:
                client = hw_session.hw_client
                data = misc.decrypt_keyvalue(client, bip32_path_n, label, value, ask_on_encrypt, ask_on_decrypt)
                pub_key = btc.get_public_node(client, bip32_path_n).node.public_key
                return data, pub_key
            except (CancelException, exceptions.Cancelled):
                raise CancelException()

        elif hw_session.hw_type == HWType.keepkey:

            client = hw_session.hw_client
            data = client.decrypt_keyvalue(bip32_path_n, label, value, ask_on_encrypt, ask_on_decrypt)
            pub_key = client.get_public_node(bip32_path_n).node.public_key
            return data, pub_key

        elif hw_session.hw_type == HWType.ledger_nano_s:

            raise Exception('Feature not available for Ledger Nano S.')

        else:
            raise Exception('Invalid HW type: ' + str(hw_session))

    if len(value) != 32:
        raise ValueError("Invalid password length (<> 32).")

    return WndUtils.run_thread_dialog(decrypt, (hw_session, bip32_path_n, label, value), True,
                                      force_close_dlg_callback=partial(cancel_hw_thread_dialog, hw_session.hw_client)) 
开发者ID:Bertrand256,项目名称:dash-masternode-tool,代码行数:52,代码来源:hw_intf.py

示例14: modelset

# 需要导入模块: import typing [as 别名]
# 或者: from typing import ByteString [as 别名]
def modelset(self,
                 key: AnyStr,
                 backend: str,
                 device: str,
                 data: ByteString,
                 batch: int = None,
                 minbatch: int = None,
                 tag: AnyStr = None,
                 inputs: Union[AnyStr, List[AnyStr]] = None,
                 outputs: Union[AnyStr, List[AnyStr]] = None) -> str:
        """
        Set the model on provided key.

        Parameters
        ----------
        key : AnyStr
            Key name
        backend : str
            Backend name. Allowed backends are TF, TORCH, TFLITE, ONNX
        device : str
            Device name. Allowed devices are CPU and GPU. If multiple GPUs are available,
            it can be specified using the format GPU:<gpu number>. For example: GPU:0
        data : bytes
            Model graph read as bytes string
        batch : int
            Number of batches for doing auto-batching
        minbatch : int
            Minimum number of samples required in a batch for model execution
        tag : AnyStr
            Any string that will be saved in RedisAI as tag for the model
        inputs : Union[AnyStr, List[AnyStr]]
            Input node(s) in the graph. Required only Tensorflow graphs
        outputs : Union[AnyStr, List[AnyStr]]
            Output node(s) in the graph Required only for Tensorflow graphs

        Returns
        -------
        str
            'OK' if success, raise an exception otherwise

        Example
        -------
        >>> # Torch model
        >>> model_path = os.path.join('path/to/TorchScriptModel.pt')
        >>> model = open(model_path, 'rb').read()
        >>> con.modelset("model", 'torch', 'cpu', model, tag='v1.0')
        'OK'
        >>> # Tensorflow model
        >>> model_path = os.path.join('/path/to/tf_frozen_graph.pb')
        >>> model = open(model_path, 'rb').read()
        >>> con.modelset('m', 'tf', 'cpu', model,
        ...              inputs=['a', 'b'], outputs=['mul'], tag='v1.0')
        'OK'
        """
        args = builder.modelset(key, backend, device, data,
                                batch, minbatch, tag, inputs, outputs)
        res = self.execute_command(*args)
        return res if not self.enable_postprocess else processor.modelset(res) 
开发者ID:RedisAI,项目名称:redisai-py,代码行数:60,代码来源:client.py


注:本文中的typing.ByteString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。