Category: Python

Becareful using int() or Decimal() for validation storing in Charfield Django

Becareful using int() or Decimal() for validation storing in Charfield Django

So, we have CharField() as model field in Django. Then we want to validate input into number only. Usually, we using obj.is_digit() to validate. Common mistake happen when we using int() to convert string input. 12value = "0123123123" int(value) Will return 1"123123123" So becareful when using int() or Decimal() for converting value with leading zero

Mapping Ajax Filter and Search Datatables to Django with Rest Framework

Mapping Ajax Filter and Search Datatables to Django with Rest Framework

Datatables sending ajax parameter for filtering and search in very complicated way. Which we need to mapping and parse based on each column. Some example request : 1http://localhost:8000/api/somemodel/?format=json&sEcho=4&iColumns=7&sColumns=%2C%2C%2C%2C%2C%2C&iDisplayStart=0&iDisplayLength=10&mDataProp_0=&sSearch_0=&bRegex_0=false&bSearchable_0=true&mDataProp_1=cif&sSearch_1=22222&bRegex_1=false&bSearchable_1=true&mDataProp_2=name&sSearch_2=&bRegex_2=false&bSearchable_2=true&mDataProp_3=gender&sSearch_3=&bRegex_3=false&bSearchable_3=true&mDataProp_4=ktp&sSearch_4=&bRegex_4=false&bSearchable_4=true&mDataProp_5=location&sSearch_5=&bRegex_5=false&bSearchable_5=true&mDataProp_6=&sSearch_6=&bRegex_6=false&bSearchable_6=true&sSearch=&bRegex=false I have no idea about where the filtering and search query come from in this request. Therefore, we need to auto-mapping it by : 123456789101112131415161718import re from …

+ Read More

Using format instead of %s for string replacement

Using format instead of %s for string replacement

Usually we use “%s” for string replacement in Python: 1"asddas %s" % (‘hello’,) There is better way to do it: 123"hello {a} {b} {c}".format(a=’error’, b=’hello’, c=’world’) "hello {0} {1} {2}".format("hello", "world", "fine") "hello {}".format("hello")

Solve vcvarsall.bat Python Windows installation

Solve vcvarsall.bat Python Windows installation

When installing python modules in Windows 8, this errors is very common and very hard to find solution. I spent 2 hours only to solve this issues 1Unable to find vcvarsall.bat The solution is quite straight-forward: 1. Install Visual Studio 2008 VC Setup http://go.microsoft.com/?linkid=7729279 VS 2008 : http://download.microsoft.com/download/E/8/E/E8EEB394-7F42-4963-A2D8-29559B738298/VS2008ExpressWithSP1ENUX1504728.iso 2. Install Microsoft SDK http://www.microsoft.com/downloads/details.aspx?FamilyId=F26B1AA4-741A-433A-9BE5-FA919850BDBF&displaylang=en 3. After …

+ Read More

Update reference models in Haystack problem solved

Update reference models in Haystack problem solved

When we put reference model in part of index and get updated, it will NOT changed in main model. Here is some example: models.py 12345class UserProfile(CommonInfo):     city = models.ForeignKey(City)     def __unicode__(self):         return unicode(self.code) With search indexes : userprofile.txt 1{{ object.city.name }} When “CITY” models is updated, let …

+ Read More

Solve ImportError: cannot import name IncompleteRead

Solve ImportError: cannot import name IncompleteRead

When doing “pip install -r requirement.txt”, suddenly i got this erros out of nowhere. Fyi, I’m suspecting upgrade “requests” module may cause this issue. 12345678910111213141516Traceback (most recent call last):   File "/usr/bin/pip", line 9, in <module>     load_entry_point(‘pip==1.5.4’, ‘console_scripts’, ‘pip’)()   File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 351, in load_entry_point     return get_distribution(dist).load_entry_point(group, name)   File …

+ Read More

Setup latest UWSGI + Django in Production Ubuntu 14.04

Setup latest UWSGI + Django in Production Ubuntu 14.04

It’s very hard to find article to help setup UWSGI and Django 1.7 in production. I use EC2 with Ubuntu 14.04. The only reason why I need to use uwsgi because Apache mod-wsgi can’t run together with gevent. It’s always throw LoopExit exception. Here is solution. First, we need to install required softwares : 1. …

+ Read More

error: Unable to find vcvarsall.bat Python

error: Unable to find vcvarsall.bat Python

Here is a quickfix how to solve error: Unable to find vcvarsall.bat in Python Windows 8. First, please download Visual Studio 8 Then go to Global Enviroment Variable and add new variable : VS90COMNTOOLS (key) and VS110COMNTOOLS (value) Or running this on command prompt : 1SET VS90COMNTOOLS=%VS110COMNTOOLS%

Solve error: Unable to find vcvarsall.bat for MySQLDB python

Solve error: Unable to find vcvarsall.bat for MySQLDB python

When we install django and mysql in Windows 8, there is possibility we got this errors : 1error: Unable to find vcvarsall.bat To solve this issue, just download MySQL-Python in : http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python If you’re using virtualenv, you need to copy MySQLDB packages into virtualenv Lib/site-packages.

Solve Twisted TypeError: unbound method doStart() must be called Factory

Solve Twisted TypeError: unbound method doStart() must be called Factory

When I tried to bind Pyglet and twisted TCP Listener using connectTCP, I got this errors: 1234567891011Traceback (most recent call last):   File "pong.py", line 484, in <module>     main()   File "pong.py", line 477, in main     reactor.connectTCP("122.248.232.186", 4005, TCPClientFactory)   File "/usr/lib64/python2.7/site-packages/twisted/internet/posixbase.py", line 496, in connectTCP     c.connect()   File …

+ Read More

Browsing with minibrowser and proxy settings using PyQt4

Browsing with minibrowser and proxy settings using PyQt4

When it comes need to have browser that we will using with specific task and run behind proxy, we can rely on PyQT4. Here is the the code that taken from : http://code.activestate.com/recipes/576921-using-proxy-connection-for-qwebview/ 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273## {{{ http://code.activestate.com/recipes/576921/ (r4) import os import sys from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import * from PyQt4.QtNetwork …

+ Read More

Install Spynner on Virtualenv

Install Spynner on Virtualenv

Installing Spynner which using Qt4 and autopy module as it 3rd party is can’t run smooth under virtualenvironment. Thigs to do here : 1. Install Qt4 and make it works with Virtualenv Follow this links Install pyqt4 on virtualenvironment 2. Install autopy under Virtualenv We may facing several errors while installing autopy under Virtualenv. Xtest.h …

+ Read More

Add PyQt4 on virtualenv Python

Add PyQt4 on virtualenv Python

If we want to make PyQt4 accessible from virtualenv, we can put this scripts into “~/.virtualenvs/postmkvirtualenv”: 123456789101112131415#!/bin/bash # This hook is run after a new virtualenv is activated. LIBS=( PyQt4 sip.so ) PYTHON_VERSION=python$(python -c "import sys; print (str(sys.version_info[0])+’.’+str(sys.version_info[1]))") VAR=( $(which -a $PYTHON_VERSION) ) GET_PYTHON_LIB_CMD="from distutils.sysconfig import get_python_lib; print (get_python_lib())" LIB_VIRTUALENV_PATH=$(python -c "$GET_PYTHON_LIB_CMD") LIB_SYSTEM_PATH=$(${VAR[-1]} -c "$GET_PYTHON_LIB_CMD") …

+ Read More

Restart services or delete files that need root privileges using Python

Restart services or delete files that need root privileges using Python

We cannot remove files like PID that need root privileges in Python. We also can’t restart services on “/etc/init.d” using Python. At least, we can’t do it directly using “os.unlink” or “subprocess”. Because, we will caught by this errors that said: 1Python remove file permission denied How to solve this problem? This is easy, we …

+ Read More

Python MultiProcessing Files using Manager and Consumer

Python MultiProcessing Files using Manager and Consumer

Using Multiprocessing in Python is a bit tricky. Sometimes when we are using simple Queue() and join() it’s just hang there. To make it more stable, we can use Manager and Consumer in python multiprocessing. Remember, using Queue on multiprocessing manager is better than Queue(). Why? Warning As mentioned above, if a child process has …

+ Read More

Python remove javascript from html

Python remove javascript from html

We can clean-up or remove all javascripts from HTML using BeautifulSoup: 12345678from bs4 import BeautifulSoup soup = BeautifulSoup("your.html") for javascript in soup("script"):     javascript.extract() print soup.prettify().encode(‘UTF-8’) This will return HTML without javascripts 🙂

Example multiprocessing in Python

Example multiprocessing in Python

Here is example of using multiprocessing and queue in Python. 1234567891011121314151617181920212223242526272829303132333435from multiprocessing import Process, Queue from multiprocessing.process import current_process def arbitary_function(number):     print "Executing number %s from %s" % (number, current_process().name)     return True def worker(worker_queue, result_queue):     try:         for number in iter(worker_queue.get, None):         …

+ Read More

Example using Python Oauth2 to access Twitter

Example using Python Oauth2 to access Twitter

Here is a quick example how to use Python Oauth2 to gain access into Twitter account. We need to install “oauth2” python package first by : 1pip install oauth2 Then here is a quick example: 1234567891011121314151617import oauth2 as oauth import json # Create your consumer with the proper key/secret. consumer = oauth.Consumer(key="xxxxxxxxxxxxxxxx",       …

+ Read More

Example implementation of custom EventLoop in Pyglet

Example implementation of custom EventLoop in Pyglet

Eventloop in Pyglet is a great feature when we want to control or integrate run() into another loop (eg: reactor.run() on twisted). In their documentation, http://www.pyglet.org/doc/programming_guide/customising_the_event_loop.html, the example of using custom eventloop in pyglet isn’t clear. Here is example how to use EventLoop() in Pyglet to control the loop without pyglet.app.run():

Example bouncing ball using PyGame

Example bouncing ball using PyGame

We will create a simple example of bouncing ball using Pygame. I use Fedora 18 and for installation and using Pygame “1.9.1release” from Fedora repository. Here is small code to make bouncing ball using Pygame: 1234567891011121314151617181920212223242526272829303132333435363738394041import sys import os import pygame CURRENT = os.path.abspath(os.path.dirname(__file__)) sys.path.insert(0, CURRENT) pygame.init() size = width, height = 640, 400 speed …

+ Read More

Example create socket connection between Node JS and Python

Example create socket connection between Node JS and Python

I build an experiment which is a multiplayer Pong game that using NodeJS and Django. This game need to build communication between browser in mobile phone as GamePad and browser in my laptop as display. Then, I facing this performance issue which there are delay between button get pressed in mobile browser and pong bar …

+ Read More

Example encode and decode image using base64 in Python

Example encode and decode image using base64 in Python

This is the simple way to encode images using base64 library in Python and decoded it back into the images based on the string encoded results. Here is the code : 12345678910111213141516import base64 def convert(image):     f = open(image)     data = f.read()     f.close()     string = base64.b64encode(data)     …

+ Read More

Example XMLRPC communication between Django and Java

Example XMLRPC communication between Django and Java

XMLRPC is useful when we need to send / receive information from one platform to others. In this case, I will use Django as the server which provide services that can be accessed as XMLRPC method from Java. First, we need to setup Django projects and set the XMLRPC server. In quick way, I use …

+ Read More

Aware with the way Python treat and re-use existing objects

Aware with the way Python treat and re-use existing objects

Python has a unique way when creating and re-using objects. See this example : 12345678def x(y=[]):     y.append("1")     return y if __name__ == "__main__":     print x()     print x()     print x() This will showing result: 123[‘1’] [‘1’, ‘1’] [‘1’, ‘1’, ‘1’] Why this is happen? See the …

+ Read More

Python try except exception showing the message error

Python try except exception showing the message error

When we don’t know about what the kind of error that throwed by try: … except: .., we can just print out the message by: 1except Exception, e: And get the error from “message” attribute of “e”. For instance: 123456a = [] try:     a[100] except Exception, e:     print e.message