Posts

Showing posts from 2014

Convert aware datetime to different timezone with pytz

As simple as this: localized_dt = aware_dt.astimezone( timezone ( 'Europe/Amsterdam' ))   See http://pytz.sourceforge.net/ 

Bootstrap accordion with multiple panels expanded

To avoid other panels collapsing if you open a panel on a Bootstrap accordion, just don't use data-parent anywhere. See http://stackoverflow.com/questions/15696365/twitter-bootstrap-collapse-plugin-how-to-enable-multiple-groups-to-be-opened

Useful ffmpeg commands

See http://www.labnol.org/internet/useful-ffmpeg-commands/28490/

Benchmarks, stress tests, load tests with ApacheBench

See http://infoheap.com/ab-apache-bench-load-testing/

Python language: the with statement and context managers

What ? The with statement is used to wrap the execution of a code block, in such a manner that a runtime context is set up, before the code block executes. the runtime context is teared down, after the code block has executed (no matter if an exception is raised inside of the code block). To do this, the with statement depends on a context manager , which handles the set up and tear down operations. Why ? The with statement is a generalization of the try-finally construct. From the python docs: If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception, it is re-raised at the end of the finally clause. If the finally clause raises another exception or executes a return or break statement, the saved exception is discarded: de...

Python Requests module: HTTP for Humans

Requests is an Apache2 Licensed HTTP library, written in Python, for human beings. Forget httplib , urllib , httplib2 and urllib2 : the requests library is clear, concise and has an extensive functionality. Finally we have a pythonic way of doing HTTP !

Obvious Django tip: one query is better than two

To often, I see unnecessary queries in Django code: first get a complete data object, just so it can be used in another query. For example, we have an employee_id and want to get the Company of the Employee (both data models, where employee is the ForeignKey field of Company). So, don't do this: # get Employee object try:     employee = Employee.objects.get(id = employee_id) except ObjectDoesNotExist:     pass # get Company object try:     company = Company.objects.get(employee = employee) except ObjectDoesNotExist:     pass But instead, make a single query, directly using the ForeignKey: # get Company object try:     company = Company.objects.get(employee__id = employee_id) except ObjectDoesNotExist:     pass See https://docs.djangoproject.com/en/dev/topics/db/queries/ for more info