Category Archives: Programming

Programming

Make sure you are closing the DB connections after accessing Django ORM in your threads

I was writing a service which import various feeds and then post to another RESTful API, simple enough as below: from django.db import connections class Receiver(PolymorphicModel): … class Database(Receiver): name = models.CharField(max_length=255) def get_data_source(self): return connections[name] Then the feed model: class Feed(models.Model): receiver = models.ForeignKey(‘Receiver’) def get_data_source(self): return self.receiver.get_real_instance().get_data_source() Then run this feed in trivial… Read More »

EJB stateful vs stateless

I have been studying Java EE and I posted a question on Stackoverflow to verify my understanding of “SessionBean”, here is the summary: Stateless bean in client: public void work(){ bean.work1(); // this uses instance 1 in App Server … bean.work2(); // this can be instance 2 in App Server }   Stateful bean in… Read More »

PHP developers should try Python.

PHP and Python both are dynamic typed language, so there is no point the discuss anything related in this aspect. Exception is first class citizen try: a = 1 / 0 except: print ‘Caught’ Prints ‘Caught’ Let’s look at PHP: try{ $a = 1 / 0; }catch (Exception $e){ print “Caught”; } I cannot believe… Read More »

Python OpenCV Facial Detection On OSX

Just a code snippet to demo Python script using OpenCV import cv2, time cap = cv2.VideoCapture(0) time.sleep(1) cascade = cv2.CascadeClassifier(“haarcascade_frontalface_alt.xml”) def detect(image): #faces = cascade.detectMultiScale(img, 1.1, 2, cv2.cv.CV_HAAR_SCALE_IMAGE, (20,20), (200,200)) faces = cascade.detectMultiScale(image) for _face in faces: cv2.rectangle(image, (_face[0], _face[1]), (_face[0]+_face[2], _face[1]+_face[3]), (255,255,255)) def repeat(): ret, image = cap.read() detect(image) cv2.imshow(“w1”, image) cv2.waitKey(1) while True:… Read More »

RequireJS + Zurb Foundation

“use strict”; require.config({ baseUrl: ‘static’, paths: { jquery: ‘bower_components/jquery/dist/jquery’, foundation: ‘bower_components/foundation/js/foundation’, }, shim: { ‘foundation’: { deps: [‘jquery’] }, } }); require([‘foundation’], function(f){ $(function(){ alert(1); }); });

django-rest-framework: api versioning

Django Rest Framework is a very solid api framework, but it doesn’t provide out-of-box versioning mechanism, here is my attempt to implement version specific APIs. The goal is to achieve something like http://localhost:8000/api/(resource)/ http://localhost:8000/api/v1/(resource)/ plus allowing clients to specify the version in request header (X-Version), here is how we did it. Structure in side the… Read More »