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


Python RestClient.get_block方法代码示例

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


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

示例1: do_block

# 需要导入模块: from sawtooth_cli.rest_client import RestClient [as 别名]
# 或者: from sawtooth_cli.rest_client.RestClient import get_block [as 别名]
def do_block(args):
    """Runs the block list or block show command, printing output to the
    console

        Args:
            args: The parsed arguments sent to the command at runtime
    """
    rest_client = RestClient(args.url, args.user)

    if args.subcommand == 'list':
        block_generator = rest_client.list_blocks()
        blocks = []
        left = args.count
        for block in block_generator:
            blocks.append(block)
            left -= 1
            if left <= 0:
                break

        keys = ('num', 'block_id', 'batches', 'txns', 'signer')
        headers = tuple(k.upper() if k != 'batches' else 'BATS' for k in keys)

        def parse_block_row(block):
            batches = block.get('batches', [])
            txns = [t for b in batches for t in b['transactions']]
            return (
                block['header'].get('block_num', 0),
                block['header_signature'],
                len(batches),
                len(txns),
                block['header']['signer_public_key'])

        if args.format == 'default':
            fmt.print_terminal_table(headers, blocks, parse_block_row)

        elif args.format == 'csv':
            fmt.print_csv(headers, blocks, parse_block_row)

        elif args.format == 'json' or args.format == 'yaml':
            data = [{k: d for k, d in zip(keys, parse_block_row(b))}
                    for b in blocks]

            if args.format == 'yaml':
                fmt.print_yaml(data)
            elif args.format == 'json':
                fmt.print_json(data)
            else:
                raise AssertionError('Missing handler: {}'.format(args.format))

        else:
            raise AssertionError('Missing handler: {}'.format(args.format))

    if args.subcommand == 'show':
        output = rest_client.get_block(args.block_id)

        if args.key:
            if args.key in output:
                output = output[args.key]
            elif args.key in output['header']:
                output = output['header'][args.key]
            else:
                raise CliException(
                    'key "{}" not found in block or header'.format(args.key))

        if args.format == 'yaml':
            fmt.print_yaml(output)
        elif args.format == 'json':
            fmt.print_json(output)
        else:
            raise AssertionError('Missing handler: {}'.format(args.format))
开发者ID:Whiteblock,项目名称:sawtooth-core,代码行数:72,代码来源:block.py

示例2: do_block

# 需要导入模块: from sawtooth_cli.rest_client import RestClient [as 别名]
# 或者: from sawtooth_cli.rest_client.RestClient import get_block [as 别名]
def do_block(args):
    subcommands = ['list', 'show']
    if args.subcommand not in subcommands:
        print('Unknown sub-command, expecting one of {0}'.format(
            subcommands))
        return

    rest_client = RestClient(args.url)

    def print_json(data):
        print(json.dumps(
            data,
            indent=2,
            separators=(',', ': '),
            sort_keys=True))

    def print_yaml(data):
        print(yaml.dump(data, default_flow_style=False)[0:-1])

    if args.subcommand == 'list':
        blocks = rest_client.list_blocks()
        keys = ('num', 'block_id', 'batches', 'txns', 'signer')
        headers = (k.upper() for k in keys)

        def get_block_data(block):
            batches = block.get('batches', [])
            txn_count = reduce(
                lambda t, b: t + len(b.get('transactions', [])),
                batches,
                0)

            return (
                block['header'].get('block_num', 0),
                block['header_signature'],
                len(batches),
                txn_count,
                block['header']['signer_pubkey']
            )

        if args.format == 'default':
            print('{:<3}  {:88.88}  {:<7}  {:<4}  {:20.20}'.format(*headers))

            for block in blocks:
                print('{:<3}  {:88.88}  {:<7}  {:<4}  {:17.17}...'.format(
                    *get_block_data(block)))

        elif args.format == 'csv':
            try:
                writer = csv.writer(sys.stdout)
                writer.writerow(headers)
                for block in blocks:
                    writer.writerow(get_block_data(block))
            except csv.Error:
                raise CliException('Error writing CSV.')

        elif args.format == 'json' or args.format == 'yaml':
            block_data = list(map(
                lambda b: dict(zip(keys, get_block_data(b))),
                blocks
            ))

            if args.format == 'json':
                print_json(block_data)
            else:
                print_yaml(block_data)

        else:
            raise CliException('unknown format: {}'.format(args.format))

    elif args.subcommand == 'show':
        block = rest_client.get_block(args.block_id)

        if args.key:
            if args.key in block:
                print(block[args.key])
            elif args.key in block['header']:
                print(block['header'][args.key])
            else:
                raise CliException(
                    'key "{}" not found in block or header'.format(args.key))
        else:
            if args.format == 'yaml':
                print_yaml(block)
            elif args.format == 'json':
                print_json(block)
            else:
                raise CliException('unknown format: {}'.format(args.format))
开发者ID:jsmitchell,项目名称:sawtooth-core,代码行数:89,代码来源:block.py


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