0

I cannot figure how to return all the items using this code:

@staticmethod
    def create_dataset():
        cols = Colleagues.get_all_colleagues()
        cols_abs = ((col['Firstname'] + " " + col['Surname'], col['Absences']) for col in cols)
        for col in cols_abs:
            dataset = list()
            sum_days = list()
            for d in col[1]:
                start_date = d[0]
                end_date = d[1]
                s = datetime.strptime(start_date, "%Y-%m-%d")
                e = datetime.strptime(end_date, "%Y-%m-%d")
                startdate = s.strftime("%b-%y")
                days = numpy.busday_count(s, e) + 1
                sum_days.append(days)
                days_per_month = startdate, days
                dataset.append(days_per_month)
            dict_gen1 = dict(dataset)
            comb_days = sum(sum_days)
            dict_gen2 = {'Name': col[0], 'Spells': len(col[1]), 'Total(Days)': comb_days}
            dict_comb = [{**dict_gen1, **dict_gen2}]
            return dict_comb

It only returns the first "col". If I move the return statement outside of the loop it returns only the last item in my set of data. This is the output that is returned from col_abs:

('Jonny Briggs', [['2015-08-01', '2015-08-05'], ['2015-11-02', '2015-11-06'], ['2016-01-06', '2016-01-08'], ['2016-03-07', '2016-03-11']])
('Matt Monroe[['2015-12-08', '2015-12-11'], ['2016-05-23', '2016-05-26']])
('Marcia Jones', [['2016-02-02', '2016-02-04']])
('Pat Collins', [])
('Sofia Marowzich', [['2015-10-21', '2015-10-30'], ['2016-03-09', '2016-03-24']])
('Mickey Quinn', [['2016-06-06', '2016-06-08'], ['2016-01-18', '2016-01-21'], ['2016-07-21', '2016-07-22']])
('Jenifer Andersson', [])
('Jon Fletcher', [])
('James Gray', [['2016-04-01', '2016-04-06'], ['2016-07-04', '2016-07-07']])
('Matt Chambers', [['2016-05-02', '2016-05-04']])

Can anyone help me understand this better as I want to return a "dict_comb" for each entry in col_abs ?

1
  • If I yield instead of using a return statement that gives me exactly the output I need when printing after looping over the generator object but I need to somehow output that result so I can use it as data in another method? Commented Aug 6, 2016 at 20:19

1 Answer 1

2

Replace your return statement with a yield statement. This will allow your method to continue to loop while "yielding" or returning values after each iteration.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, this gives me a generator object... How do I return all the items in the generator object so that I can use that new data structure in another function? When I try 'for x in create_dataset(): return x' then it again only gives me the first item in the generator?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.