Python

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

24 May , 2015  

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.

1
2
value = "0123123123"
int(value)

Will return

1
"123123123"

So becareful when using int() or Decimal() for converting value with leading zero

Python

Python remove dictionary key if value is empty

11 Mar , 2015  

Here is a quick snippet if we want to remove dictionary key if value is empty:

1
new_dict = {k: v for k, v in _data.items() if v if v is not ''}

Python

Mapping Ajax Filter and Search Datatables to Django with Rest Framework

16 Feb , 2015  

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 :

1
http://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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import re
from urlparse import parse_qs

"""Filtering get params from datatables and mapping into django filter"""
def filter_request(self):
    # Filtering input
    input_request = parse_qs(self.request.GET.urlencode())
    query_dict = {}
   
    for key, value in input_request.iteritems():
        if(re.search('sSearch_', key)):
            if value:
                key_index = input_request.get('mDataProp_%s' % key.split('_')[1])

                if key_index is not None:
                    query_dict[key_index[0]] = value[0]

    return query_dict

And we get nice results :

1
{u'some_field': u'some_value', ...}

Python

Using format instead of %s for string replacement

23 Jan , 2015  

Usually we use “%s” for string replacement in Python:

1
"asddas %s" % ('hello',)

There is better way to do it:

1
2
3
"hello {a} {b} {c}".format(a='error', b='hello', c='world')
"hello {0} {1} {2}".format("hello", "world", "fine")
"hello {}".format("hello")

Python

Solve vcvarsall.bat Python Windows installation

30 Nov , 2014  

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

1
Unable 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 installing, setup your global environment variables:

1
VS90COMNTOOLS = J:\Program Files (x86)\Microsoft Visual Studio 9.0\VC

Also, add into PATH

1
J:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools;J:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin

If your OS Path is C:\ then just replace J: with C:.

Then all your problem should gone. Btw, there some modules that required header files that not covered by Windows:
https://pypi.python.org/pypi/python-ldap/

Reference:

http://springflex.blogspot.com/2014/02/how-to-fix-valueerror-when-trying-to.html

Install Python Header (NO NEED)
http://www.microsoft.com/en-us/download/details.aspx?id=44266

Python

Update reference models in Haystack problem solved

22 Nov , 2014  

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

1
2
3
4
5
class 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 say “CHICAGO” into “UTAH”. We search on UserProfile, the city name will be still “CHICAGO”. Whooppss!

The solution, whenever City updated, we need to update UserProfile as well. Create signal to save UserProfile whenever City updated with post_save()

Python

Solve ImportError: cannot import name IncompleteRead

12 Nov , 2014  

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Traceback (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 "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2363, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2088, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 11, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/lib/python2.7/dist-packages/pip/vcs/mercurial.py", line 9, in <module>
    from pip.download import path_to_url
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 25, in <module>
    from requests.compat import IncompleteRead
ImportError: cannot import name IncompleteRead

The solution is :

1
2
3
sudo apt-get remove python-pip
wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
sudo python get-pip.py

Python

Simple Python Task and Test

8 Oct , 2014  

1. How to rename value in list during loop ?
We can use iterate and using index

Python

Setup latest UWSGI + Django in Production Ubuntu 14.04

14 May , 2014  

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. Install dependecies

1
sudo apt-get install uwsgi build-essential python-dev uwsgi-plugin-python nginx

2. Setup wsgi configuration
Here is my example, i create “/etc/uwsgi/apps-available/ecommerce-dev.ini” and symlink to “/etc/uwsgi/apps-enabled/ecommerce-dev.ini”. This is the content :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[uwsgi]
;enable master process manager
master = true

;spawn 2 uWSGI worker processes
workers = 2

;unix socket (referenced in nginx configuration)
socket = /tmp/ecommerce-dev.sock

# set mode of created UNIX socket
chmod-socket = 666

# place timestamps into log
log-date = true

# user identifier of uWSGI processes
uid = www-data
# group identifier of uWSGI processes
gid = www-data

; number of worker processes
;processes = 2
;vacuum = true

; project-level logging to the logs/ folder
;logto = /var/yodi/ecommerce-dev/uwsgi.log

; django >= 1.4 project
chdir = /var/yodi/ecommerce-dev
wsgi-file=/var/yodi/ecommerce-dev/ecommerce/wsgi.py

;enable-threads = true

virtualenv = /home/ubuntu/.virtualenvs/production
vacuum = true
env = DJANGO_SETTINGS_MODULE=ecommerce.settings
pidfile = /tmp/ecommerce-dev.pid
;harakiri = 20 # respawn processes taking more than 20 seconds
;max-requests = 5000 # respawn processes after serving 5000 requests

3. Register WSGI services

1
sudo vim /etc/init/uwsgi.conf

And write this :

1
2
3
4
5
6
7
8
9
description "uWSGI"
start on runlevel [2345]
stop on runlevel [06]
respawn

env UWSGI=/usr/local/bin/uwsgi
env LOGTO=/var/log/uwsgi-emperor.log

exec $UWSGI --master --emperor /etc/uwsgi/apps-enabled --die-on-term --uid www-data --gid www-data --logto $LOGTO

4. Running services

1
sudo service uwsgi start

5. Setup NGINX to listen UWSGI socket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
    listen 82;
    server_name dev.custoom.com;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    root /var/yodi/ecommerce-dev;

    client_body_timeout  460;
    client_header_timeout 460;
    send_timeout 460;
    client_max_body_size 10m;
    keepalive_timeout 300;

    index  index.php index.html index.htm;

    # Check if a file exists at /var/www/domain/ for the incoming request.
    # If it doesn't proxy to Apache/Django.
    try_files $uri @django;
    location / {
        uwsgi_pass unix:/tmp/ecommerce-dev.sock;
        include uwsgi_params;
    }
}

6. Running NGINX

1
sudo service nginx restart

Success! :D

,

Python

error: Unable to find vcvarsall.bat Python

8 Mar , 2014  

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 :

1
SET VS90COMNTOOLS=%VS110COMNTOOLS%

Python

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

3 Mar , 2014  

When we install django and mysql in Windows 8, there is possibility we got this errors :

1
error: 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.

Python

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

24 Jun , 2013  

When I tried to bind Pyglet and twisted TCP Listener using connectTCP, I got this errors:

1
2
3
4
5
6
7
8
9
10
11
Traceback (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 "/usr/lib64/python2.7/site-packages/twisted/internet/base.py", line 1044, in connect
    self.factory.doStart()
TypeError: unbound method doStart() must be called with TCPClientFactory instance as first argument (got nothing instead)
</module>

More…

,

Python

Browsing with minibrowser and proxy settings using PyQt4

14 May , 2013  

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/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
## {{{ 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 import *

__version__ = "0.1 20091002"
__appname__ = "minibrowser"

TITLE = "%s %s" % (__appname__, __version__)
HOMEPAGE = "http://example.com/"
HISTORY = os.path.join(os.path.dirname(sys.argv[0]), "%s.log" % (__appname__))

class BrowserWidget(QWidget):
    def __init__(self, datpath, parent=None):
        super(self.__class__, self).__init__(parent)
        self.browser = QWebView()
        self.lineedit = QLineEdit()
       
        layout = QVBoxLayout()
        layout.setSpacing(0)
        layout.setMargin(0)
        layout.addWidget(self.lineedit)
        layout.addWidget(self.browser)
        self.setLayout(layout)
        self.lineedit.setFocus()
        self.connect(self.lineedit, SIGNAL("returnPressed()"), self.entrytext)

        self.browser.load(QUrl(HOMEPAGE))
        self.browser.show()
       
    def entrytext(self):
        self.browser.load(QUrl(self.lineedit.text()))

class Window(QMainWindow):
    def __init__(self, histlogpath, parent=None):
        super(self.__class__, self).__init__(parent)
        self.browserWindow = BrowserWidget(histlogpath)
        self.setCentralWidget(self.browserWindow)
        self.setWindowTitle(TITLE)

        status = self.statusBar()
        status.setSizeGripEnabled(True)

        self.label = QLabel("")
        status.addWidget(self.label, 1)

        self.connect(self.browserWindow.browser, SIGNAL("loadFinished(bool)"), self.loadFinished)
        self.connect(self.browserWindow.browser, SIGNAL("loadProgress(int)"), self.loading)
        self.histlogpath = histlogpath

    def loadFinished(self, flag):
        """SLOT of load finished.
        """
        self.label.setText("Done")
        open(self.histlogpath, 'a').write(self.browserWindow.browser.url().toString() + "n")

    def loading(self, percent):
        """SLOT of loading progress.
        """
        self.label.setText("Loading %d%%" % percent)
        self.browserWindow.lineedit.setText(self.browserWindow.browser.url().toString())

if __name__ == '__main__':
    QNetworkProxy.setApplicationProxy(QNetworkProxy(QNetworkProxy.HttpProxy, "proxy.example.com", 8080))
    app = QApplication(sys.argv)
    window = Window(HISTORY)
    window.show()
    app.exec_()
## end of http://code.activestate.com/recipes/576921/ }}}

,

Python

Install Spynner on Virtualenv

11 May , 2013  

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 problems :

1
src/keypress.c:10:35: fatal error: X11/extensions/XTest.h: No such file or directory

Solution:

1
sudo yum install libXtst-devel

Png.h problems :

1
src/png_io.c:3:17: fatal error: png.h: No such file or directory

Solution:

1
sudo yum install libpng-devel

3. Install spynner

1
pip install spynner

Python

Add PyQt4 on virtualenv Python

11 May , 2013  

If we want to make PyQt4 accessible from virtualenv, we can put this scripts into “~/.virtualenvs/postmkvirtualenv”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/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")

for LIB in ${LIBS[@]}
do
    ln -s $LIB_SYSTEM_PATH/$LIB $LIB_VIRTUALENV_PATH/$LIB
done

2. Copy sip.so into your virtualenv site-packages

1
cp /usr/lib64/python2.7/site-packages/sip.so ~/.virtualenvs/hn/lib64/python2.7/site-packages/

3. Last thing if still doesnt works
Copy the PyQt4 folder :

1
cp -dR /usr/lib64/python2.7/site-packages/PyQt4 ~/.virtualenvs/django/lib/python2.7/site-packages/

On Ubuntu:

1
2
3
sudo apt-get install python-qt4
cp /usr/lib/python2.7/dist-packages/PyQt4 ~/.virtualenvs/django/lib/python2.7/site-packages/
cp /usr/lib/python2.7/dist-packages/PyQt4 ~/.virtualenvs/django/lib/python2.7/site-packages/

Python

Restart services or delete files that need root privileges using Python

15 Apr , 2013  

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:

1
Python remove file permission denied

How to solve this problem?
This is easy, we can use Fabric with sudo(“command”) to overcome this problem.

1. Example restart services using Fabric

1
2
3
4
def restart_services():
    with cd('%s' % PROJECT_PATH):
        sudo('/etc/init.d/redis_6379 start')
        sudo('/etc/init.d/nodejs restart')

We can use “sudo” with fabric without any hassle.

,

Python

Python MultiProcessing Files using Manager and Consumer

3 Apr , 2013  

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 put items on a queue (and it has not used JoinableQueue.cancel_join_thread()), then that process will not terminate until all buffered items have been flushed to the pipe. This means that if you try joining that process you may get a deadlock unless you are sure that all items which have been put on the queue have been consumed. Similarly, if the child process is non-daemonic then the parent process may hang on exit when it tries to join all its non-daemonic children. Note that a queue created using a manager does not have this issue. See Programming guidelines.

Here is the problem:
I need to read all HTML files inside folder and process them not in sequential way because it too slow.
That mean I should be able open and read HTML files in multi way.

Then, we can use multiprocessing to solve this problem. First thing first that we will build is queue.
It will cater all input and output. Here is implementation of manager Queue() :

1
2
3
manager = multiprocessing.Manager()
task_queue = manager.Queue()
todo_queue = manager.Queue()

Then we use Consumer instead of Process to wrapped the queue:
More…

,

Python

Python remove javascript from html

2 Apr , 2013  

We can clean-up or remove all javascripts from HTML using BeautifulSoup:

1
2
3
4
5
6
7
8
from 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 :)

,

Python

Example multiprocessing in Python

27 Feb , 2013  

Here is example of using multiprocessing and queue in Python.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from 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):
            arbitary_function(number)
            result_queue.put("%s success with: %s" % (number,
                                                      current_process().name))
    except Exception, e:
        result_queue.put("%s failed with: %s" % (current_process().name,
                                                       e.message))


if __name__ == "__main__":
    numbers = [x for x in range(1000)]
    worker_queue = Queue()
    result_queue = Queue()

    for n in numbers:
        worker_queue.put(n)

    core_worker = 2
    workers = [Process(target=worker, args=(worker_queue, result_queue)) for i in range(core_worker)]

    for each in workers:
        each.start()

    for r in iter(results.get, None):
        print r

You can get the results from “result_queue.get()” and keep in mind that queue is generator.

It will give results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Executing number 0 from Process-1
Executing number 1 from Process-1
Executing number 2 from Process-1
Executing number 4 from Process-2
Executing number 3 from Process-1
Executing number 5 from Process-1
Executing number 6 from Process-1
Executing number 7 from Process-1
Executing number 8 from Process-1
Executing number 9 from Process-2
Executing number 10 from Process-1
Executing number 11 from Process-2
Executing number 12 from Process-1
Executing number 13 from Process-2
Executing number 14 from Process-2
Executing number 15 from Process-1
Executing number 16 from Process-2
Executing number 17 from Process-1
Executing number 18 from Process-2
Executing number 19 from Process-1
Executing number 20 from Process-2

Fyi, If we want to run several multipleprocess in asynchronous way, better don’t use join(). Because it will block main process until all process finished.

,

Python

Example using Python Oauth2 to access Twitter

23 Feb , 2013  

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 :

1
pip install oauth2

Then here is a quick example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import oauth2 as oauth
import json

# Create your consumer with the proper key/secret.
consumer = oauth.Consumer(key="xxxxxxxxxxxxxxxx",
                          secret="xxxxxxxxxxxxxxxx")

# Request token URL for Twitter.
request_token_url = "https://api.twitter.com/oauth/request_token"

# Create our client.
client = oauth.Client(consumer)

# The OAuth Client request works just like httplib2 for the most part.
resp, content = client.request(request_token_url, "GET")
print json.dumps(resp, indent=4)
print json.dumps(content, indent=4)

It will showing results :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
    "status": "200",
    "content-length": "144",
    "content-location": "https://api.twitter.com/oauth/request_token?oauth_body_hash=xxxxx&oauth_nonce=48355082&oauth_timestamp=1361601089&oauth_consumer_key=xxxx&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_signature=xxxxLzwwvw1NtMvuD0PKA%3D",
    "x-transaction": "xxxx36f9200946491",
    "set-cookie": "k=xx.37xx0.136xxx509550; path=/; expires=Sat, 02-Mar-13 06:31:30 GMT; domain=.twitter.com, guest_id=v1%3A136xxx052057281; domain=.twitter.com; path=/; expires=Mon, 23-Feb-2015 18:31:30 GMT, _twitter_sess=BAh7CDoPY3JlxxxxxZjg3ZGYzMjhjYTdiNzM2MDQ1--1bxxxxx8ec0a; domain=.twitter.com; path=/; HttpOnly",
    "expires": "Tue, 31 Mar 1981 05:00:00 GMT",
    "x-mid": "b892643948470824023ffee4beed31c48388104a",
    "server": "tfe",
    "last-modified": "Sat, 23 Feb 2013 06:31:30 GMT",
    "x-runtime": "0.02175",
    "etag": ""d657e3e63axxxx83cea7d"",
    "pragma": "no-cache",
    "cache-control": "no-cache, no-store, must-revalidate, pre-check=0, post-check=0",
    "date": "Sat, 23 Feb 2013 06:31:30 GMT",
    "x-frame-options": "SAMEORIGIN",
    "content-type": "text/html; charset=utf-8",
    "-content-encoding": "gzip",
    "vary": "Accept-Encoding"
}
"oauth_token=xxxxxxxxxxxx&oauth_token_secret=xxxxx&oauth_callback_confirmed=true"

Python

Example implementation of custom EventLoop in Pyglet

11 Feb , 2013  

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():
More…

,

Python

Example bouncing ball using PyGame

2 Feb , 2013  

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import 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 = [1, 1]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load(CURRENT + "/ball.png")
ballrect = ball.get_rect()
ballrect.topleft = [0, 0]

screen.blit(ball, ballrect)
   
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    screen.fill(black)
    screen.blit(ball, ballrect)
   
    ballrect = ballrect.move(speed)
   
    if ballrect.left > width or ballrect.right > width:
        speed[0] = -speed[0]
    elif ballrect.left < 0:
        speed[0] = +abs(speed[0])
       
    if ballrect.top > height or ballrect.bottom > height:
        speed[1] = -speed[1]
    elif ballrect.top < 0:
        speed[1] = +abs(speed[1])

    pygame.display.flip()

It will give us results :

Remember to create your “ball.png” and put in the same folder with this scripts.

Update:
Here is the same scripts in decent format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
import os
import pygame

class Game():
    def __init__(self):
        CURRENT = os.path.abspath(os.path.dirname(__file__))
        pygame.init()

        size = self.width, self.height = 640, 400
        self.speed = [1, 1]
        self.black = 0, 0, 0

        self.screen = pygame.display.set_mode(size)
       
        self.ball = pygame.image.load(CURRENT + "/ball.png")
        self.ballrect = self.ball.get_rect()
        self.ballrect.topleft = [0, 0]

        self.screen.blit(self.ball, self.ballrect)


    def start(self):
        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT: sys.exit()

            self.screen.fill(self.black)
            self.screen.blit(self.ball, self.ballrect)

            self.ballrect = self.ballrect.move(self.speed)

            if (self.ballrect.left > self.width or
                self.ballrect.right > self.width):
                self.speed[0] = -self.speed[0]
            elif self.ballrect.left < 0:
                self.speed[0] = +abs(self.speed[0])

            if (self.ballrect.top > self.height or
                self.ballrect.bottom > self.height):
                self.speed[1] = -self.speed[1]
            elif self.ballrect.top < 0:
                self.speed[1] = +abs(self.speed[1])

            pygame.display.flip()

if __name__ == "__main__":
    game = Game()
    game.start()

,

Python

Solve PyGame sh: smpeg-config: command not found in Fedora

31 Jan , 2013  

Installing PyGame via pip will showing this problems. The easy is installing from yum by:

1
sudo yum install pygame

,

NodeJS,Python

Example create socket connection between Node JS and Python

30 Jan , 2013  

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 movement in laptop.

Then, I try to make it faster by change the display from browser into Python GUI. I’m wondering if I can make socket connection between displayer and NodeJS, it will gain performance 100% since there no websocket anymore.

After look at few documentation, I do a small stuff to make NodeJS can communicate with Python using socket.
More…

,

Python

Example encode and decode image using base64 in Python

22 Jan , 2013  

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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import base64

def convert(image):
    f = open(image)
    data = f.read()
    f.close()

    string = base64.b64encode(data)
    convert = base64.b64decode(string)

    t = open("example.png", "w+")
    t.write(convert)
    t.close()

if __name__ == "__main__":
    convert("test.png")

In this example, I put “test.png” images into the same level of this scripts. Happy encoding & decoding! :D

Django,Python

Example XMLRPC communication between Django and Java

18 Jan , 2013  

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 “django-xmlrpc” and the source code can be taken from my Github https://github.com/yodiaditya/python-collection/tree/master/djangoserver. I have method called “test” which can be accessed through simple python script :

1
2
3
import xmlrpclib
s = xmlrpclib.ServerProxy("http://127.0.0.1:8000/xmlrpc/")
s.test('test')

It will give result “test World!”.

Now how about in Java? There are several XMLRPC library, at this example I will use XMLRPC library from Apache.
More…

,

Python

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

18 Jan , 2013  

Python has a unique way when creating and re-using objects. See this example :

1
2
3
4
5
6
7
8
def x(y=[]):
    y.append("1")
    return y

if __name__ == "__main__":
    print x()
    print x()
    print x()

This will showing result:

1
2
3
['1']
['1', '1']
['1', '1', '1']

Why this is happen? See the logic behind. When the objects y which is not exists, Python will create this objects once the first x() executed. Then come to next of x() execution which Python interpreter will looking for the y and check whether it’s exists or not. If the objects exists, then Python will re-use it and we see the “['1', '1']” result on second execution.

,

Python

Converting image with Base64 encode will give bigger results size

18 Jan , 2013  

Base64 usually needed to represent binary data into ASCII format, or in other case to make it more “readable”. Some case study why we need to convert binary into ASCII like this :

1. There is Android App that have function to take picture
2. This picture will send to website
3. In Website (Django eg) it will have method to handle the picture

When we using POST binary data, there will issue like CSRF_TOKEN, filesize, resume feature, etc. It will hard thing to track the percentage of binary data by the size. Using base64 by encode the image will be a good solution to tackle this problem. Instead of using POST data, we can use XML-RPC and send the “string” of the photos. Later on we can decode back into binary data.

On interesting fact here that base64 encode results will be 1.37 bigger than the actual binary data.
Here is to do in python:

1
2
3
4
5
6
7
8
9
import base64

f = open("grimace.jpg")
encoded = base64.b64encode(f.read())
f.close()

a = open("grimace.txt", "w+")
a.write(encoded)
a.close()

And the results by using “ls” :

1
2
26545 grimace.jpg
35396 grimace.txt

Why it’s get bigger? Because we represent the binary (01111) into ASCII which it take 8 bit and plus with the headers of the files.

,

Python

Python try except exception showing the message error

9 Jan , 2013  

When we don’t know about what the kind of error that throwed by try: … except: .., we can just print out the message by:

1
except Exception, e:

And get the error from “message” attribute of “e”. For instance:

1
2
3
4
5
6
a = []

try:
    a[100]
except Exception, e:
    print e.message