cosmicpython / code

Example application code for the python architecture book
Other
2.07k stars 926 forks source link

Question: How this works withoug receiving IndexError: pop from empty list #16

Closed madalinpopa closed 4 years ago

madalinpopa commented 4 years ago

Hi Harry & Bob,

I was not able to make this piece of code work without adding a checking for an empty list:

 def collect_new_events(self):
        for product in self.products.seen:
            while product.events:
                yield product.events.pop(0)

With this, I am getting the following exception: IndexError: pop from empty list

Is this working without checking if product.events is empty?

Reference: https://github.com/cosmicpython/code/blob/e3de63c922c2094d63949949b4cdf776fb5462dc/src/allocation/service_layer/unit_of_work.py#L28

hjwp commented 4 years ago

well we're glad to hear that someone is trying to run the code! i guess you figured out the bug?

how are you enjoying the book in general?

madalinpopa commented 4 years ago

Hi @hjwp ,

Thank you for replying. Well, I think it is the second time I read this book. The first time I just got through it very quickly, and I have read it like it was novel. I really like the style of how it was written, and I love that you are constant. I found the same writing manner as in the first book that I read from you, Test-Driven Development with Python.

Now I decided to read it one more time and also to get my hands dirty with those exercises and the code examples you have provided along with this book.

Even they are evident and concise; I have encountered some difficulties in applying and understanding some of them. Like the example, this piece of code for which I have created this issue.

If you could let me know more details about this specific example and where do you catch that IndexError I will be grateful. :)

hjwp commented 4 years ago

well that product.events.pop is inside a while product.events block so in theory it can never be empty, and you should never be able to cause an IndexError

have you got a repo somewhere you can share, maybe with a test case that reproduces the error?

hjwp commented 4 years ago

i'm glad you've been trying the exercises too! how did you find them?

madalinpopa commented 4 years ago

Yes, you are right. I just double-checked my code, and I found the mistake.

They are great, and if you want to understand them, then definitely, you need to run them. Just reading and glancing through this repo is not enough. I am glad that I did this because now I have connected all the dots, and for sure when I will recommend this book, I will strongly suggest following the code examples along with the book chapters.

Thank you, guys for writing this book, and we are waiting for more to come.