Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'_sre.SRE_Match' object is not subscriptable

i have below syntax but whenever runs in 3.6 will always threw error: '_sre.SRE_Match' object is not subscriptable Scripts is works fine in python3.7 but error in python3.6

if {host}.issubset(sg_data['hosts'].split(',')):
    saved_sg = [x for x in recorded_state if x['sg'] == sg_data['id'] and x['host'] == host][0]['data']
    dec_saved_data = json.loads(self.encryption.decrypt(saved_sg).decode())
    if sg_data['display_state'].lower() == 'offline':
        if dec_saved_data['display_state'].lower() != 'offline':
            yield from self.set_online(sg_data, host)
    else:
        parsed_host = re.search('^.*\((.*)\).*$', sg_data['display_state'])
        if sg_data['display_type'].lower() == 'parallel':
            if parsed_host:
                if not {host}.issubset(parsed_host.group(1).split(',')):
                    yield from self.set_online(sg_data, host)
        else:
            if dec_saved_data['display_state'].lower() == 'offline':
                yield from self.set_offline(sg_data, host)
            else:
                parsed_saved_host = re.search('^.*\((.*)\).*$', dec_saved_data['display_state'])
                if parsed_saved_host:
                    if not {parsed_host[0]}.issubset(parsed_saved_host.group(1).split(',')):
                        yield from self.set_switch(sg_data, host)

    if dec_saved_data['frozen'] != sg_data['frozen']:
        if dec_saved_data['frozen'] == 0 and sg_data['frozen'] > 0:
            yield from self.set_unfreeze(sg_data)
        elif dec_saved_data['frozen'] > 0 and sg_data['frozen'] == 0:
            yield from self.set_freeze(sg_data)

    current_sg_state = sg_data['display_state']
    lastest_sg_state = dec_saved_data['display_state']

    parsed_current_sg_state = re.search('^.*\((.*)\).*$', sg_data['display_state'])
    parsed_lastest_sg_state = re.search('^.*\((.*)\).*$', dec_saved_data['display_state'])

    if parsed_current_sg_state:
        current_sg_state = parsed_current_sg_state.group(1)

    if parsed_lastest_sg_state:
        lastest_sg_state = parsed_lastest_sg_state.group(1)
like image 902
cahyo Avatar asked Sep 19 '18 11:09

cahyo


1 Answers

Your line here:

if not {parsed_host[0]}.issubset(parsed_saved_host.group(1).split(',')):

attempts to access item 0 of parsed_host, a Match object.

Since the Match.__getitem__() method is implemented since Python 3.6 according to the documentation, your code should work in Python 3.6, and if you are getting a '_sre.SRE_Match' object is not subscriptable error, it means that you are not actually using Python 3.6, but an earlier version.

Change the line to:

if not {parsed_host.group(0)}.issubset(parsed_saved_host.group(1).split(',')):

and the code would work for earlier versions of Python.

like image 133
blhsing Avatar answered Sep 29 '22 01:09

blhsing