Skip to main content

Posts

Showing posts with the label python

The ultimate guide to debugging in Python

Even if you write clear and readable code, even if you cover your code with tests, even if you are very experienced developer, weird bugs will inevitably appear and you will need to debug them in some way. Lots of people resort to just using bunch of print statements to see what's happening in their code. This approach is far from ideal and there are much better ways to find out what's wrong with your code, some of which we will explore in this article. Logging is a Must If you write application without some sort of logging setup you will eventually come to regret it. Not having any logs from your application can make it very difficult to troubleshoot any bugs. Luckily - in Python - setting up basic logger is very simple: import logging logging.basicConfig(     filename='application.log',     level=logging.WARNING,     format= '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',     datefmt='%H:%M:%S' ) logging.error("Some seriou...

Before you "judge" a python programmer

If you're coming from a compiled language context, using exceptions for flow control would look odd to you. But here's the thing - in the python world, exceptions are super cheap and using them for flow control is the "idiomatic python" way! In fact, exception-driven flow control is built right into the language itself e.g. the "for" loop in python terminates when the iterable raise a "StopIteration" exception! There is a lot of material on the internet already on the topic, so if there's one thing you take out of this post, it's this - in python, it's " Easier to Ask for Forgiveness than Permission "!

Capture and compare stdout in python unit tests

A recent fan of TDD, I set out to write tests for whatever comes my way. And there was one feature where the code would print messages to the console. Now - I had tests written for the API but I could not get my head around ways to capture these messages in my unittests. After some searching and some stroke of genius, here's how I accomplished capturing stdout.

Which python web framework do I use - Flask or Django?

Let me take you on a beautiful journey. Flask:   You start to ride a bike. Being a noob you mess up a lot of times. And, get hurt over and over. You learn things now. Django: You start to ride a bike. But, a plot twist. You learn riding a bike by observing others. You sit on their back and see how much smoothly they do ride. Now, either situations are possible: You copy them flawlessly and become a pro. You mess things up badly, end up injuring self and dumbstruck about how things are done. Now the choice lies with you. You know your caliber better than anyone.  Coming to the point, Flask and Django are both awesome in their own aspects. Flask can help you learn and get the feel of how things are carried out in backend development and how frontend can be improved exponentially with a correct backend setup. And the learning curve is much gentle as compared to Django. But the problem lies in doing much on your own messes things up at large scale, hence slows down th...