Wednesday, February 23, 2011

PHP faster than Python?

I know it's been a while since the last time i posted in this blog. Out of technicality little bit, just to express my shocked, and disbelieved with OSCC benchmark report between PHP and Python.

OSCC Benchmark PHP vs Python.

Try to download it and read. My first remark while going through: "they are comparing non object oriented language with a fully object oriented language"

Take look at this:

Language Shoot Out
This OO Benchmark PHP/Python/Perl/Ruby
and This

and quote from one of the link above

Even Rasmus Lerdorf, the creator of PHP, admits this. In this interview from SitePoint, Rasmus is asked about how to make PHP fast. He answers "Well, you can't".

I leave the rest "à vous".

Edited: The puzzle some how answered, thanks EAFaisal for clearing this out. (under comments)

Thursday, May 27, 2010

Extending django model with custom search method


This is really fun. Been requested to developed a prototype for a simple system, my immediate choice is django, this time rather than using sqlalchemy, i'm using anything that comes with django umbrella. In this prototype, I add an ajax based auto completion/suggestion search input form, just to give a small 'wow' effect to my customer. Using jquery ajax call, and returning json list of dictionaries.


The best part actually the way how you can extend django model to add you own specific method, for example i add a search method in profile model.

once extended you can simply call it :

searchObj = Profile.objects.search(request.GET['q']).order_by('prof_firstname')

and in the same time, the search box actually allowed user to search for profile firstname or lastname and aslo ic number using the same input box.

and here how you can do it in your models (this is my django profile model file):
make sure you add the ProfileManager class inside your model (highlighted in yellow).


# Load modules needed
import operator


from django.db import models
from django.db.models import Q


class ProfileManager(models.Manager):
    def search(self, search_terms):
        terms = [term.strip() for term in search_terms.split()]
        q_objects = []


        for term in terms:
            q_objects.append(Q(prof_firstname__icontains=term))
            q_objects.append(Q(prof_lastname__icontains=term))
            q_objects.append(Q(prof_ic__icontains=term))


        # Start with a bare QuerySet
        qs = self.get_query_set()


        # Use operator's or_ to string together all of your Q objects.
        return qs.filter(reduce(operator.or_, q_objects))


class Profile(models.Model):
    """ table profile """
    prof_ic = models.CharField(max_length = 12, primary_key = True) # mungkin ic ada abjad (tentera)
    prof_firstname = models.CharField(max_length = 32)
    prof_lastname = models.CharField(max_length = 32)
    prof_dob = models.DateTimeField()
    prof_gender = models.CharField(max_length = 1)
    prof_race = models.CharField(max_length = 32)
    prof_address_1 = models.CharField(max_length = 128)
    prof_address_2 = models.CharField(max_length = 128)
    prof_city = models.CharField(max_length = 32)
    prof_state = models.CharField(max_length = 32)
    prof_country = models.CharField(max_length = 32)
    prof_postcode = models.CharField(max_length = 16)
    prof_telno = models.CharField(max_length = 16)
    prof_mobileno = models.CharField(max_length = 16)
    prof_email = models.EmailField(max_length = 128)
    prof_img = models.ImageField(max_length = 256, upload_to = 'profile')
    prof_sysdate = models.DateTimeField()
    objects = ProfileManager()


    class Meta:
        app_label = "pingat"





Getting IP Address/Interfaces with Python

# easy_install netifaces

in python shell:

// list all interfaces

>>> import netifaces
>>> netifaces.interfaces()

['lo0', 'gif0', 'stf0', 'en0', 'fw0', 'en1', 'vboxnet0']
>>>

// now get the addresses only for AF_INET

>>> for iface in netifaces.interfaces():
...     if netifaces.AF_INET in netifaces.ifaddresses(iface) and\
...     'addr' in netifaces.ifaddresses(iface)[netifaces.AF_INET][0]:
...             print iface, netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr']
... 
lo0 127.0.0.1
en0 192.168.2.1
en1 192.168.1.2
>>>