Month: February 2013

  • 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):         […]

  • 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",       […]

  • Javascript URL window location works on Chrome but fail on Firefox

    This is commmon things that happen when we’re using “window.location.origin” to define the path for AJAX call or another purpose. Actually, “window.location.origin” isn’t in browser standard. Firefox try the best to obey the standard which “window.location.origin” is not supported hence it give “undefined” result. Substitution for this, you can use: 1window.location.protocol + "//" + window.location.host

  • Top showing wrong amount of memory usage in server

    “Top” (Go to console and type top) became a handy tools to track what applications that currently running in our server. But, if we’re using it for showing how much actual memory usage in server, that’s WRONG! Why? First, see this screenshot of my Fedora with my total RAM is 3.6 GB: Top results (Free: […]

  • Django queryset distinct foreign key and count

    What we want to do here is: 1. We want distinct foreign key from the records 2. We want this unique foreign key contains number of records that belongs to. Example: Country -> has many -> Insurance 12345Country(models.Model):     name = models.Charfields(..) Insurance(model.Model):     country = models.ForeignKey(Country) We want to distinct Country in […]

  • Add export data into CSV files action in Django Admin

    Having “export data as CSV” action in Django Admin sometimes it useful to exchange data into another platform like Excel. To make this feature, we need to create action in Django admin. 1. Create admin export csv action Create “action.py” in your app. Then, in this files, we create function to do queryset and export […]

  • 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():

  • How to draw cirle / ball objects in PyGame

    There is a lack documentation about how to draw circle or ball in Pygame. Here is an example taken from my game source code: 123456self.ball = pygame.Surface((25, 25)) self.ballrect = pygame.draw.circle(self.ball, (0, 255, 0), (15 / 2, 15 / 2), 15 / 2) self.ball = self.ball.convert() self.ball.set_colorkey((0, 0, 0)) self.ballrect.topleft = [0, 0] self.screen.blit(self.ball, self.ballrect)

  • 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 […]