May 2020 Summaries
4 posts from Pybites
Filter
Month:
Year:
Post Summaries
Back to Blog
The article explores the use of pytest's `raises()` function as a context manager to assert that a block of code raises a specific exception, using an example from the pytest documentation. It discusses the initially counterintuitive nature of using `excinfo` after exiting the `with` block, highlighting the differences between traditional file handling context managers and pytest's approach to handling exceptions. The author explains that while a file object becomes inaccessible after its context is closed, `excinfo` only becomes useful after the `with` block has concluded, once an exception has been raised and its details are filled in. The text delves into the underlying mechanics of `pytest.raises()`, detailing how the `__enter__` and `__exit__` methods work to manage the lifecycle of `excinfo`, transitioning it from an uninitialized state to being filled with exception details, thereby allowing assertions to be made outside the `with` block. This exploration underscores the elegance and clarity of this design pattern within pytest, offering a deeper understanding of how context managers can be leveraged in testing scenarios.
May 18, 2020
776 words in the original blog post.
Proper error handling in Python can enhance code simplicity, readability, and adherence to idiomatic practices. Key approaches include using explicit rather than implicit exceptions and ordering specific exceptions before general ones to avoid catch-all scenarios. Raising exceptions can help prevent unnecessary code indentation, and creating custom exceptions provides clearer communication with function callers. Narrow try/except blocks are recommended to reduce the temptation of handling too many exceptions, and the "easier to ask for forgiveness than permission" (EAFP) principle is preferred over excessive condition checking. Verbose logging, particularly using logger.exception, aids in capturing stack traces, and tools like Sentry can alert developers to unexpected errors, providing full stack traces for efficient debugging.
May 06, 2020
575 words in the original blog post.
Volunteering as a mentor for young children in coding, particularly through initiatives like Code Club Aotearoa, presents unique challenges and learning opportunities that extend beyond teaching technical skills. The experience of guiding children aged 9-13 through their first coding steps, such as programming in Python, not only enhances the mentor's communication and empathy skills but also reflects on the importance of inspiring and supportive mentorship in the technical field. The mentor's narrative illustrates the joys and hurdles of teaching coding concepts, such as variables and data types, to curious young minds, highlighting the importance of patience and adaptability. The article also emphasizes the value of volunteering in fostering a sustainable future by sparking children's interest in technology and encourages others to participate in similar initiatives or share knowledge in community meetups. Through this engagement, mentors gain insights into their own skills and development, contributing positively to the community while reflecting on the foundational support they received in their own learning journeys.
May 04, 2020
1,691 words in the original blog post.
Cal Newport's book "So Good They Can’t Ignore You" challenges the conventional career advice of following one's passion, asserting that developing skills is far more crucial for success. The author emphasizes that while passion can inspire, it is the mastery of skills and strategic focus on building "career capital" that leads to career advancement. This approach requires patience, deliberate practice, and a willingness to embrace discomfort and explore new opportunities. For those in tech, particularly in Python programming, it's not just technical skills that are important, but also soft skills that contribute to a well-rounded career. The journey to becoming proficient in programming involves overcoming challenges and continuously seeking growth and learning in adjacent fields.
May 02, 2020
462 words in the original blog post.