python-mastery icon indicating copy to clipboard operation
python-mastery copied to clipboard

Add to Completed solution for 2.5(e)

Open minocrafft opened this issue 2 years ago • 0 comments

Thank you for the awesome lecture.

While solving (e) of Exercise 2.5, I thought it would be nice to add a solution.

This pull request is similar to #37, and I think this solution is more elegant, so I request a merge.

There seems to be a lot of changes in the code, but most of them are typo modifications due to the formatter. The key is the following code.

class RideData(collections.abc.Sequence):
...

    def __getitem__(self, index):
        if isinstance(index, slice):
            _item = RideData()
            for i in range(*index.indices(len(self))):
                _item.append(self[i])
        elif isinstance(index, int):
            _item = {
                "route": self.routes[index],
                "date": self.dates[index],
                "daytype": self.daytypes[index],
                "rides": self.numrides[index],
            }
        else:
            return NotImplemented

        return _item

minocrafft avatar Oct 19 '23 15:10 minocrafft