su -c ‘rm -rf /mnt/proprietary’

November 12, 2009

Swapping two variables without using third variable – old trick

Filed under: Programming — krish @ 6:21 pm
Tags: , ,

Pretty old trick, used today in my php code :


// Swap the months if from-month selected is later than to-month
//if($frommon > $tomon){
// $tempmon = $frommon;
// $frommon = $tomon;
// $tomon = $tempmon;
//}
// Smarter way using X-OR
if($frommon > $tomon){
$frommon = $frommon ^ $tomon ^ ( $tomon = $frommon );
}

December 4, 2008

Document your python functions

Filed under: Programming — krish @ 12:19 am
Tags: , ,

Many people tend to ignore commenting and documenting the code they write. The small amounts of invested time can make life ease for others; especially if you are a crappy coder like me! :)

In python, to document your functions, give it a doc string. Doc strings are the first triple quoted string you’d want in a function.

test.py

def mySmallFunction(myParams):
      """ Does nothing. Just a time consuming run. """
      return ";)"

if __name__ == "__main__":
     myParams = {"test.py":"dummy"}
     print mySmallFunction(myParams)

In the python interpreter, doc strings can be accessed by calling attribute __doc___

>>> import test
>>> test.mySmallFunction.__doc__
' Does nothing. Just a time consuming run. '

July 13, 2008

KDragons screensaver

Filed under: Gaming, Programming — krish @ 1:12 am
Tags: , ,

Just wrote a simple scrolling marquee screensaver with pygame. Bad thing, it runs in 640 x 480 display mode.

Image:
KDragons Screensaver
Konqi taken from Wormux.

Source:

#!/usr/bin/env python

bkg_image_filename = 'images/wormuxkonqi.png'

# Running in windowed mode
SCREEN_SIZE = (640, 480)
message = "    KDragons coming soon...   "

# Get functions needed
from random import randrange
import pygame
from pygame.locals import *
from sys import exit

pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)

font = pygame.font.SysFont("freemono", 30);
text_surface = font.render(message, True, (0,255,0))

x = 0
y = ( SCREEN_SIZE[1] - text_surface.get_height() ) / 2

bkg = pygame.image.load(bkg_image_filename).convert()

while True:
	for event in pygame.event.get():
		if event.type == QUIT:
			exit()

	# set speed for scroll and change of y axis position
	x-=5
	if x < -text_surface.get_width():
		x = SCREEN_SIZE[0]
		y = randrange(100,450,20)

	# konqi scrolls opposite direction to text
	screen.blit(bkg, (-x,y/2))
	screen.blit(text_surface, (x,y))
	pygame.display.update()
	screen.fill((0,0,0))

December 30, 2007

ah python!

Filed under: Programming — krish @ 11:26 am
Tags: ,

I’ve been writing few newbie programs in python since yesterday. ;)
My ISP’s DNS was not looking up for python.org and I had to spend few extra minutes getting the IP and accessing the ftp docs. (thanks to friends at #python @ freenode)

Few good things for me with this snake, er.. language/interpreter
1. No delimiter ( I have a bad habit of forgetting ; in my C, C++, perl, php code )
2. No angular brackets for condition statements
3. Free form language ( first encountered this in perl )

Most of the times, I am finding myself tempted to compare python code with perl; hopefully I shall get over it with time.

Talking about time, I just remembered – its time for LUG meet and I need to hurry! :P

Blog at WordPress.com.