Saturday, January 31, 2009

2D acceleration blurs out pages - Adobe Reader

If you find pages in Adobe Reader fading out as shown below, then it means the 2D acceleration API is not functioning as expected.


You may want to turn it off to get rid of the above effect. Now goto the preferences page in the Reader and select Page Display section. Unselect the choice box shown below.

 

That should solve your problem.

Friday, January 30, 2009

Python under Moon light

After reading this post I thought of comparing the performance of python against the lightweight scripting language Lua. So to have a fair comparison I ported the MorseCode encoder/decoder wirtten in python into a Lua script with no change to the logic or the sequence.



--morse code table
codes = {
['!'] = "-.-.--", [' '] = "|", ['"'] = ".-..-.", ['$'] = "...-..-",
['\''] = ".----.", ['&'] = ".-...", [')'] = "-.--.-", ['('] = "-.--.",
['+'] = ".-.-.", ['-'] = "-....-", [','] = "--..--", ['/'] = "-..-.",
['.'] = ".-.-.-", ['1'] = ".----", ['0'] = "-----", ['3'] = "...--",
['2'] = "..---", ['5'] = ".....", ['4'] = "....-", ['7'] = "--...",
['6'] = "-....", ['9'] = "----.", ['8'] = "---..", [';'] = "-.-.-",
[':'] = "---...", ['='] = "-...-", ['?'] = "..--..", ['A'] = ".-",
['@'] = ".--.-.", ['C'] = "-.-.", ['B'] = "-...", ['E'] = ".",
['D'] = "-..", ['G'] = "--.", ['F'] = "..-.", ['I'] = "..",
['H'] = "....", ['K'] = "-.-", ['J'] = ".---", ['M'] = "--",
['L'] = ".-..", ['O'] = "---", ['N'] = "-.", ['Q'] = "--.-",
['P'] = ".--.", ['S'] = "...", ['R'] = ".-.", ['U'] = "..-",
['T'] = "-", ['W'] = ".--", ['V'] = "...-", ['Y'] = "-.--",
['X'] = "-..-", ['Z'] = "--..", ['_'] = "..--.-"
}


binary = {['.']="10",['-']="1110",[',']="000",['|']="0000000"}


function encode(value)
--encodes the value into morse code
value = string.upper(value)
value = string.gsub(value,'%*', 'X')
value = string.gsub(value,'%^', 'XX')
local morse_value=""
local length = string.len(value)
local i = 1
while i <= length do
local chr = string.sub(value,i,i)
if chr then
morse_value = morse_value .. codes[chr] .. ","
end
i = i + 1
end
return _get_binary(morse_value)
end

function _get_binary(value)
local binary_value = ""
local length = string.len(value)
local i = 1
while i <= length do
local chr = string.sub(value,i,i)
if chr then
binary_value = binary_value .. binary[chr]
end
i = i + 1
end
return binary_value
end

function decode(morse_code_value)
-- decodes the morse bytes
decoded_value = _decode_binary(morse_code_value)
ascii_value=""
for w in string.gfind(decoded_value, "[-.|]+") do
ascii_value = ascii_value .. _get_key(w)
end
return ascii_value
end

function _get_key(value)
--returns the key for the given value
for k, v in next, codes do
if v == value then
return k
end
end
return ''
end

function _decode_binary(binary)
dah_replaced = string.gsub(binary,'1110', '-')
dit_replaced = string.gsub(dah_replaced,'10', '.')
comma_replaced = string.gsub(dit_replaced,'000', ',')
zero_replaced = string.gsub(comma_replaced,'0', '|,')
return zero_replaced
end

function print_usage()
print("Usage : "..arg[0].." [d (decode) |e (encode)] [input string]")
end


if arg[1] == nil then
print_usage()
else
if arg[1] == 'd' then
print("Decoded value : "..decode(arg[2]))
elseif arg[1] == 'e' then
if arg[2] == nil then
print_usage()
else
print("Encoded value : "..encode(arg[2]))
end
else
print("Encoded value : "..encode(arg[1]))
end
end



The Lua script was much faster than the python one. May be i have not written the python script in an optimistic way, but logic remains the same.

Aanand Natarajan@AANAND /h/MorseEnDecode/src
$ time python MorseEnDecode.py aanand
encoding
Encoded : 10111000010111000011101000010111000011101000011101010000

real 0m0.140s
user 0m0.015s
sys 0m0.015s

Aanand Natarajan@AANAND /h/MorseEnDecode/src
$ time lua MorseEnDecode.lua aanand
Encoded value : 10111000010111000011101000010111000011101000011101010000

real 0m0.031s
user 0m0.015s
sys 0m0.015s


Look at the real time which is 0.140 seconds for the Python version and 0.31 seconds for the Lua version.
This doesn't mean Python is not fast. I like Python as a language, for its features and its highly performant comparing against many other scripting languages.

Monday, January 26, 2009

Morse code encoder/decoder

This is a Morse code encoder/decoder program written in python. Simply pass a string that contains Morse code supported characters and you'll see a binary format of the string, encoded as Morse code as specified @ http://en.wikipedia.org/wiki/Morse_code.

  1. short mark, dot or 'dit' (·) — 1
  2. longer mark, dash or 'dah' (–) — 111
  3. intra-character gap (between the dots and dashes within a character) — 0
  4. short gap (between letters) — 000
  5. medium gap (between words) — 0000000


#!/usr/bin/python
import sys
__author__="Aanand Natarajan"

#morse code dictionary
codes = {'1':".----",'2':"..---",'3':"...--",'4':"....-",'5':".....",'6':"-....",'7':"--...",'8':"---..",
'9':"----.",'0':"-----",'A':".-",'B':"-...",'C':"-.-.",'D':"-..",'E':".",'F':"..-.",'G':"--.",
'H':"....",'I':"..",'J':".---",'K':"-.-",'L':".-..",'M':"--",'N':"-.",'O':"---",'P':".--.",
'Q':"--.-",'R':".-.",'S':"...",'T':"-",'U':"..-",'V':"...-",'W':".--",'X':"-..-",'Y':"-.--",
'Z':"--..",
#punctuations
',':"--..--",'.':".-.-.-",'?':"..--..",';':"-.-.-",':':"---...",'/':"-..-.",
'-':"-....-","'":".----.",'(':"-.--.",')':"-.--.-",'!':"-.-.--",'&':".-...",
'=':"-...-",'+':".-.-.",'_':"..--.-",'"':".-..-.",'$':"...-..-",'@':".--.-.",
#space
' ':"|"}

binary = {'.':'10','-':'1110',',':'000','|':'0000000'}


def encode(value):
""" encodes the value into morse code """
morse_value=""
value.replace('*', 'X')
value.replace('^', 'XX')
for c in value:
try :
morse_value += codes[c.upper()]+','
except :
print "Unintended character " + c + " omitted"
return _get_binary(morse_value)

def decode(morse_code_value):
""" decodes the morse bytes """
decoded_value = _decode_binary(morse_code_value)
ascii_value=""
for v in decoded_value.split(","):
ascii_value += _get_key(v)
return ascii_value

def _get_binary(value):
binary_value = ""
for c in value:
binary_value += binary[c]
return binary_value

def _get_key(value):
""" returns the key for the given value """
for k,v in codes.items():
if v == value:
return k
return ''

def _decode_binary(binary):
dah_replaced = binary.replace('1110', '-')
dit_replaced = dah_replaced.replace('10', '.')
comma_replaced = dit_replaced.replace('000', ',')
zero_replaced = comma_replaced.replace('0', '|,')
return zero_replaced

def _do_decode(value):
print "Decoded : "+decode(value)

def _do_encode(value):
print "Encoded : "+encode(value)

if __name__ == "__main__":
if len(sys.argv) > 2:
if sys.argv[1] == 'd' :
print "decoding"
_do_decode(sys.argv[2])
else:
print "encoding"
_do_encode(sys.argv[2])
elif len(sys.argv) > 1:
print "encoding"
_do_encode(sys.argv[1])
else:
print "Usage : "+sys.argv[0]+" [d (decode) |e (encode)] [input string]"


Usage:
aanand@AanandDL:~$ ./MorseEnDecode.py "aanand aka tuxaanand"
encoding
Encoded : 101110000101110000111010000101110000111010000111010
1000000000000001011100001110101110000101110000000000000011100
0010101110000111010101110000101110000101110000111010000101110
00011101000011101010000
aanand@AanandDL:~$ ./MorseEnDecode.py d "1011100001011100001110
100001011100001110100001110101000000000000001011100001110101110
000101110000000000000011100001010111000011101010111000010111000
010111000011101000010111000011101000011101010000"
decoding
Decoded : AANAND AKA TUXAANAND
aanand@AanandDL:~$

Wednesday, December 24, 2008

Numerology in Erlang

After a long time, again with the same problem but using a different language. I have been watching the talks on Erlang - about its strength and stability. I thought why not give it a try.

This is the same numerology problem written in Erlang.


-module(numero).
-export([value/1,start/1]).

start(Args) -> value(lists:nth(1,Args)).

value(NAME) -> io:write(sum(0,lists:sum([val(A) || A <- NAME]))),io:nl(),halt().

sum(SUM,NUM) when NUM > 0 -> Q = NUM,
R = Q rem 10,
NEWQ = Q div 10,
sum(SUM + R,NEWQ);
sum(SUM,0) ->
if
SUM > 9 -> sum(0,SUM);
true -> SUM
end.


val(C) ->
if
(($A =:= C) or ($J =:= C) or ($I =:= C) or ($Y =:= C) or ($Q =:= C)) -> 1;
(($B =:= C) or ($K =:= C) or ($R =:= C)) -> 2;
(($S =:= C) or ($C =:= C) or ($L =:= C) or ($G =:= C)) -> 3;
(($T =:= C) or ($D =:= C) or ($M =:= C)) -> 4;
(($E =:= C) or ($N =:= C) or ($X =:= C) or ($H =:= C)) -> 5;
(($U =:= C) or ($V =:= C) or ($W =:= C)) -> 6;
(($O =:= C) or ($Z =:= C)) -> 7;
(($F =:= C) or ($P =:= C)) -> 8;
true -> 0
end.

Saturday, September 27, 2008

Netbeans setting jvmargs for SHIFT-F6

In Netbeans there is an option to run the current file. It works fine until we need the option to set a JVM argument.
I hit the same problem and was searching the build xml for target that runs the file and customize it. But accidentally found a property runmain.jvmargs in project.properties file and set the JVM args that i wanted and it worked.

--> {project}/nbproject/project.properties (runmain.jvmargs)

This also works for web projects.

Sunday, July 13, 2008

Numerology with Ruby

This is a simple Numerology calculator written in ruby. Just a pass time program while learning ruby.
This will give you the value of the given name and date (date of birth)
Usage:
./numerology.rb <your_name> <date> <month> <year>


#!/usr/bin/env ruby
class Numerology
@name
@date
@month
@year
$map = {'A' => 1, 'J' =>1, 'I' =>1, 'Y' => 1, 'Q' => 1,
'B' => 2, 'K' => 2, 'R' => 2,
'S' =>3, 'C' =>3, 'L' =>3, 'G' => 3,
'T' => 4, 'D' => 4, 'M' => 4,
'E' =>5, 'N' => 5, 'X' =>5, 'H' => 5,
'U' => 6, 'V' => 6, 'W'=>6,
'O' =>7, 'Z' => 7,
'F' =>8, 'P' => 8 ,
' ' => '' #for removing spaces
}

def initialize(name,date,month,year)
if name.nil? or name.empty?
raise "Please provide a name"
end
@name = name.upcase
@date = date.to_i
@month = month.to_i
@year = year.to_i
if @date == 0 or @month == 0 or @year == 0
raise "Please provide a valid date"
end
end

def calculate
puts "Name : #{@name} \n"
puts "Date : #{@date}/#{@month}/#{@year} \n"
sum = 0

#old version
#~ Range.new(0,@name.length).each do|n|
#~ #puts "char at #{n} = #{@name[n]}"
#~ #print @name[n]
#~ case @name[n]
#~ when ?A , ?J , ?I , ?Y , ?Q then sum = sum + 1
#~ when ?B , ?K , ?R then sum = sum + 2
#~ when ?S , ?C, ?L, ?G then sum = sum +3
#~ when ?T, ?D, ?M then sum = sum +4
#~ when ?E, ?N, ?X, ?H then sum = sum + 5
#~ when ?U, ?V, ?W then sum = sum + 6
#~ when ?O, ?Z then sum = sum + 7
#~ when ?F, ?P then sum = sum +8
#~ end
#~ while sum > 9
#~ sum = sum_digits(sum)
#~ end
#~ end

#replace the chararcters with thier numberic equivalent
#map contains key value pairs - A : 1, B : 2....
#replace each char (key in map) in the string with the value from the map
$map.each do |k,v|
@name.gsub!(k, v.to_s)
end

#find the sum of digits until the sum is single digit
sum = @name.to_i
while sum > 9
sum = sum_digits(sum)
end

#calculate sum value for date of birth
dateSum = @date + @month + @year
while dateSum > 9
dateSum = sum_digits(dateSum)
end

print "Name value :#{sum}\n"
print "Date value :#{dateSum}\n"

end

private
#sum the digits of the given number
def sum_digits(num)
sum = 0
quotient = num
while quotient > 0
#puts "sum : #{sum}, reminder: #{reminder}, quotient: #{quotient}"
reminder = quotient % 10
quotient = quotient / 10
sum = sum + reminder
end
return sum
end
end


if __FILE__ == $0
begin
num = Numerology.new(ARGV[0],ARGV[1],ARGV[2],ARGV[3])
num.calculate
rescue => ex
puts "Error : #{ex.message}"
end
end

Friday, March 28, 2008

Komodo may be the choice now....

I have been looking for an open source feature rich IDE for dynamic languages such as Python, ruby and Javascript. Now that ActiveState has released an opensource version of its Komodo, that may be become a primary choice for dynamic languages.

http://www.activestate.com/Products/komodo_ide/komodo_edit.mhtml

No more pydev - a huge junk of java for editing python files.

Thanks a lot ActiveState....