Skip to main content

Posts

Django grouping logs belonging to one request

Python has a very robust and easy to use logging framework. Django being a Python based web framework uses same logging as Python. To see how to implement logging in Django applications please follow this link of Django's official documentation site. In case you want me to explain how to implement logger in Django, feel free to put up a message in comments section and I will write a blog on it. Assuming you already have basic knowledge of implementing logger in Django, I will move forward with tutotrial. One problem I faced with logger of Django is tracing logs of a request through different apps of django. Request Response Cycle Django A typical request response cycle in Django would look like the image above. Now when a request hits middleware you may want to log something after that flow goes to application and in application there could be many modules through which our code flows. Now assume for a particular request we want to check what has happened through logs,
Recent posts

Python Generators use cases

Hi Friends recently I have been studying generators in python. During my course of study I found few really cool use cases for use of generators. In this post I will be describing few use cases where generators could be useful. Eager Parsing of List Suppose we have a function which does some computation and returns a set of data. In real world scenario we could relate it to a function which does some database operation and returns a list of entry from database. Below is the sample code. # Scenario 1 without generators from time import sleep def load_data: data = list() for i in range(10): data.append(i) sleep(0.5) return data data = load_data() for _ in data: if _ > 5: break print _ Above code calls load_data function, the function above iterates 10 times and in each iteration it sleeps for 0.5 seconds. So total time taken by function is 5 seconds. Now if see our code after the call to load_data we are looping throug