geostocker
geostocker

Reputation: 1200

Loop unexpectantly stops after one iteration

I'm iterating over a list of lists with length 4, but it stops after one iteration.

        print((data))
        formatted_exec_dictionary_list = []
        for entry in data:
            print(entry)
            try:
                formatted_exec = {
                    'minute': self.validate_type_and_range(entry[0], 0, 59),
                    'hour': self.validate_type_and_range(entry[1], 0, 23),
                    'path': entry[2]
                }
                formatted_exec_dictionary_list.append(formatted_exec)
            except IndexError as exception:
                print(exception)
                if self.logger:
                    self.logger.error("""
                    Could not successfully parse entry...
                    Exception: %s""", exception)

In the topmost print in the snippet this is the output:

[[u'30', u'1', u'/bin/run_me_daily'], [u'45', u'*', u'/bin/run_me_hourly'], 
 [u'*', u'*', u'/bin/run_me_every_minute'], 
 [u'*', u'19', u'/bin/run_me_sixty_times']]

But the for-loop wrapped print only returns the first entry, then it stops.

No exception seems to be thrown as the print isn't firing and the rest of the program functions fine. It's as if all the rest of the entries in the list are just ignored...

Is this something common? Any clue why this would be happening?

For clarification NOTHING in particular happens in the loop that seems to be throwing an exception. The function validate_type_and_range prints the expected data, but only for the one iteration.

Upvotes: 0

Views: 125

Answers (1)

criskross
criskross

Reputation: 84

When I replace your 'validate_type_and_range(...)' with some strings, I get the following output:

[[u'30', u'1', u'/bin/run_me_daily'], [u'45', u'*',  u'/bin/run_me_hourly'], [u'*', u'*', u'/bin/run_me_every_minute'], 
[u'*', u'19', u'/bin/run_me_sixty_times']]
[u'30', u'1', u'/bin/run_me_daily']
[u'45', u'*', u'/bin/run_me_hourly']
[u'*', u'*', u'/bin/run_me_every_minute']
[u'*', u'19', u'/bin/run_me_sixty_times']

I think there is something going on in your 'validate_type_and_range'

print((data))
    formatted_exec_dictionary_list = []
    for entry in data:
        print(entry)
        try:
            formatted_exec = {
                'minute': "foo",
                'hour': "bar",
                'path': entry[2]
            }
            formatted_exec_dictionary_list.append(formatted_exec)
        except IndexError as exception:
            print(exception)
            if self.logger:
                self.logger.error("""
                Could not successfully parse entry...
                Exception: %s""", exception)

Upvotes: 1

Related Questions