python parsing suggestion

Ganesh Pal ganesh1pal at gmail.com
Mon May 30 03:34:15 EDT 2016


Hi ,

Trying to extract the '1,1,114688:8192' pattern form the below output.

pdb>stdout:
'3aae5d0-1: Parent Block for 1,1,19169280:8192 (block 1,1,114688:8192)
--\n3aae5d0-1:
magic         0xdeaff2fe         mark_cookie
0x0000000000000000\ngpal-3aae5d0-1:         super.status
3        super.cookie          390781895\ngpal-3aae5d0-1:
 cg_xth                  0


I am on python 2.7 and Linux  the below code sample is working fine  (
please raise the error if u find it will help me improve this
codebetter)


def check_block(block):
    """
    Trying to extract the '1,1,114688:8192' pattern from the above output.
    """
    logging.info('Determining history block for block %s' % (block))
    parent_block = None
    node_id = block.split(",")[0]
    cmd = ("get_block_info -l" % (node_id, block))
    logging.info(cmd)
    stdout, stderr, exitcode = run(cmd)
    try:
        parent_block = stdout.strip().split('\n')[0].split()[6][:-1]
    except (IndexError, ValueError):
        logging.error('Error determining history block for %s.' % (block))
        return False
    if re.search(r'(\d+),(\d+),(\d+):(\d+)', parent_block):
        logging.info('Found history block %s for data block %s' %
                    (parent_block, block))
        return parent_block
    return False


Need suggestion for the below 3 points:

1.  Is parsing with  stdout.strip().split('\n')[0].split()[6][:-1]
sufficient do I need to add extra check ? it looks fine for me though.

2.  Better ways to achieve the same output  we need to parse is a string

3. Is re.search(r'(\d+),(\d+),(\d+):(\d+)', parent_block) needed ?  I
added as an extra check ,any ideas on the same


Regards,

Ganesh



More information about the Python-list mailing list