Truth, Computing and Fail

  • Home
  • About

PyCon India or Code Jam?

anomit | September 15, 2009

That is something which has been playing at the back of my mind for the past couple of days. not this shit again

Google Code Jam Round 2 is on 26th Sept, 21:30 IST, right on the day PyCon India starts. In theory I can attend the 1st day of PyCon and compete in GCJ as well at night. But there are a few small things that have been bugging me like the possibility of being dead tired at the end of the day, lack of a decent internet connection etc.

Advancing to round 3 of GCJ would require me to be placed within the top 500 of the 3000 competitors. Effectively I have about 5-6 days in total to prepare for it, excluding the useless exams in between and a ~500 rank isn’t asking for too much. This is one of the reasons that I’m inclined to stay back instead of taking on a 10 hour long overnight bus journey coupled with running around the city for a whole day.

If you are reading this, put in a few words of wisdom (considering you have them at your disposal).

Comments
6 Comments »
Categories
Coding, My Life
Tags
code jam, PyCon, python
Comments rss Comments rss
Trackback Trackback

Moving to Google Code

anomit | June 27, 2009

I have decided to move some of my personal projects to Google Code so that it’d give me some impetus and make me get off my lazy ass and actually put some real effort into the things I like.

Er, not really. It’s more of a selfish decision. Right now I want other people to read and criticize my code so that I’d know where I’m going wrong and correct myself. Nothing’s more dangerous than complacency.

As a starter, I have uploaded the tiny mpd now playing plugin I wrote for xchat about an year ago when I was learning socket programming in python. Showing the current playing track was too easy. Right now I want it to become more like an interface to mpd that provides basic controls sitting in the comfort of xchat.

I named the project mpd-xchat

PS: Just after I had uploaded the code, I found a project by the name of xchat-mpd :P

I hope to add one more project in the next few days :)

Comments
5 Comments »
Categories
Coding, GNU/Linux
Tags
mpd, python, xhcat
Comments rss Comments rss
Trackback Trackback

Pimp my Vim

anomit | June 18, 2009

I’ve spent the past few weeks changing the look and feel of my Vim environment. One fine day I decided to explore some :colorscheme while I was on GVim apart from the classic ’slate’ that I always use.

Now, now. I can already see purists scoffing at me for using GVim. The main reason for me using GVim is just for the occasional visual treat. While you are on the terminal and working on Vim, you don’t really have much choice of beautifying things within the editor itself. You rather have to make some changes to the terminal profile about the background/font color, font to be used and all that stuff.

Coming back to where I started off, I googled and found a few color schemes. This caught my eye because it claimed to be The last vim color scheme you’ll ever need. Apparently this was supposed to give GVim a TextMate kinda look but applying the colorscheme is only the first step. You need to set the proper fonts and background too. I suggest you checkout the vimrc and gvimrc of the author of the linked post.

Important!

You need the Monaco font to get the right look. Follow the easy steps here to install it on Ubuntu. I’m sure similar guides will be available for other distros too.

After all was done, I didn’t find the final outcome to be that impressive. In addition to GVim being buggy on occasions like no input displayed in the command mode, this time it would get stuck after I’d scroll down or up about 20 lines for like a second. Besides, it wasn’t looking too great. See for yourself.

gvim

Now it was time to make some changes to gnome-terminal and see if it suited my tastes. As usual none of set backgorund=dark and colorscheme ir_black showed the necessary changes within Vim. This part is easy. Just make a new terminal profile with the following options:

  • White on black for foreground/background
  • Font:Monaco with size 12

After this just add the following lines to your .vimrc

set background=dark
colorscheme ir_black

Switch to the new profile, open up Vim and see the difference in the richness of the colors and better font rendering.

vim

Bonus Tip

If you are a python coder, you might want to look into this post by Samuel Huckins on making Vim a complete IDE for python development. I’ve been using the NERDTree and code folding plugins mentioned there and they have been of really great help.

Comments
No Comments »
Categories
Coding, GNU/Linux
Tags
Coding, gvim, plugins, python, vim
Comments rss Comments rss
Trackback Trackback

Parsing XML in Python

anomit | November 16, 2008

I started off with BeautifulSoup for a certain project that needs to construct database queries out of XML. I was almost done with this component when I discovered much to my chagrin that BeautifulSoup does not respect the case of the strings in the tag name. For eg some text would yield the name of the tag as ‘foobar’. I had to find some way to replace the code base within a short time.
I quickly went through some of my starred items in Google Reader. One of the interesting finds was using lxml from the IBM developer works pages. One of the examples in that page seemed very similar to using a SAX parser. I wasn’t quite interested at that moment to learn a third party library from scratch, so I decided to go for the xml.sax package already provided by default. A tutorial and reading through a couple of pages from the book ‘Python and XML’ later, I managed to get it right.

Basically, an SAX parser is event driven, and you can define functions that can be described like event handlers in JAVA which act upon those events. These events include encountering the opening or closing of an XML text node, an XML element node etc. The JAVA implementation of SAX defined interfaces for the handlers. SAX for one thing, doesn’t have a formal specification as yet and since Python doesn’t support interfaces, it included four classes in xml.sax.handler. You can read about them in the python docs. From the docs:

Handler implementations should inherit from the base classes provided in the module xml.sax.handler, so that all methods get default implementations.

The one we will be using the most is the class ContentHandler. The handler for our own XML format will inherit it. We override functions implemented in ContentHandler to customize them to our own needs. Some of the important functions are:

startElement(name,attrs): This is called by the parser when it encounters the start of an element. name holds the name of the element and attr holds the attributes.

endElement(name): Called by the parser when it encounters the end of an element.

character(content): Returns chunks of character data when found. The character data may be included in a single chunk or split into multiple chunks. It’s advisable to use flags rather than relying on the former assumption.

Since a SAX parser is a stream parser and doesn’t build an in-memory tree, you can’t backtrack in the tree and nodes don’t have the usual child-parent relationship found in DOM style parsers. Flags are used to track what element the parser is currently in and take appropriate actions. There is a caveat here. If you are working on a tree with a large depth, it can be really frustrating and painful to manage a lot of flags. The one I am working on can have a maximum depth of 5, but it would rarely exceed 4. So it wasn’t a big deal for me to handle that many flags.

The basic outline of a program that makes use of SAX in Python would be like
#####################################################

from xml.sax.handler import ContentHandler
from xml.sax import make_parser

class CustomHandler(ContentHandler):
	def __init__(self):
		#initialize flags and other data structures if needed
	def startElement(self,name,attrs):
		#set flags, get value of attributes etc
	def endElement(self,name):
		#clear flags etc
	def character(self,ch):
		#copy character data to any data structure if needed
ch=CustomHandler()
saxparser=make_parser()
saxparser.setContentHandler(ch)
saxparser.parse(some_file_stream)

########################################################

PS: I am still stuck with Python 2.5.2. I’m trying to keep pace by reading the changes in 2.6. I am counting on Harsh to bail me out when the need arises :p

Comments
4 Comments »
Categories
Coding
Tags
python, xml
Comments rss Comments rss
Trackback Trackback

mpd now playing plugin for XChat

anomit | July 6, 2008

It is 3 in the morning. Don’t expect me to blabber much. Windows users, please fuck off at this point. Just give the file a py extension and move it to ~/.xchat2 so that it loads automatically at startup. Use /show to tell everyone in the channel you are playing [insert an emo band name here] and proceed to cut your wrist or let everyone know you are a real emo with the legwarmers and all. OK ENOUGH.

import socket,re,xchat

__module_name__ = "mpd-np"
__module_version__ = "0.1"
__module_description__ = "mpd now playing"

def playing(word, word_eol, userdata):
	mpd=socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
	host='127.0.0.1'
	port=6600
	mpd.connect((host,port))
        #welcome msg could be used to extract mpd version information
	welcome=mpd.recv(1024)
	mpd.send('currentsong\r\n')
        #get all the info about current track being played
	data=mpd.recv(4096)
	#I wont be explaining any REs
	artist=re.findall(r'Artist[:]\s[\S ]+',data)[0].split(':')[1]
	title=re.findall(r'Title[:]\s[\S ]+',data)[0].split(':')[1]
	msg='Now playing:'+artist+'-'+title
	xchat.command('me '+msg)
	data=""
	mpd.close()
	return xchat.EAT_ALL
#hooks into the show xchat command
xchat.hook_command("show",playing)

Reports of idiotic behavior go into the comments.

Comments
2 Comments »
Categories
Coding, GNU/Linux
Tags
mpd, plugin, python, xchat
Comments rss Comments rss
Trackback Trackback

One time XOR pad with /dev/urandom

anomit | June 27, 2008

I made one :) I think doing a md5 hash of the resulting ciphertext would add an extra layer of security. What do the others think of this idea? Of course, the safe storage and transmission of the XOR key becomes an issue. Check the code and see if you could come up with suggestions to optimize it. I particularly don’t like the exponential order for loop at the very end. The XOR key is stored in the xor-key file in the same directory where the code is run.

import sys

if len(sys.argv)<=1:
	print 'Usage: python basicsalt.py <input file>'
	sys.exit(1)

frandom=open('/dev/urandom','r')  #open the random device
fpickle=open(sys.argv[1],'r')	#open the input file in read-only mode
bytes=1024

key=open('xor-key','w')	#open/create the xor-key file

picklebuf=fpickle.read() #read the input file
fpickle.close()

fpickle=open(sys.argv[1],'w')	#open the input file in write mode
				#the ciphertext is stored in the same file
tempicklebuf=''			#temporary buffer for storing the ciphertext

fileLen=len(picklebuf)
print fileLen

#a function that doesn't exactly 'add' the salt in the classical sense of the term
def addsalt(pb,sb,fileLen):
	global tempicklebuf
	for i in range(fileLen):
		tempicklebuf+=chr(ord(pb[i]) ^ ord(sb[i]))	#just plain XOR

bufsalt=frandom.read(fileLen)
addsalt(picklebuf,bufsalt,fileLen)
key.write(bufsalt)

fpickle.write(tempicklebuf)
frandom.close()
key.close()
Comments
2 Comments »
Categories
Coding, GNU/Linux, Security
Tags
cryptography, python
Comments rss Comments rss
Trackback Trackback

What’s in

  • Symlinks in a libfs virtual file system: The Pains
  • Small rant on the FUSE API reference
  • Kernel module debugging: a simple technique
  • Vim/Cscope quickie
  • PyCon India or Code Jam?

Blogroll

  • Akshay Kothari
  • Ankur Shrivastav (OS)
  • Ankur Sinha
  • Harsh J
  • Hullap
  • LUG manipal
  • Swap

Tags

aircrack airfail airtel assembly blues build c Coding college country cryptography dean faculty file systems fuckery gnuplot hacking India kernel linux mangalore manipal mpd music NASM plugin plugins politicians pub culture python rant rock sam scheduler simulation SSFNet stupidity supernatural suppression syscall syscalls unix vim xchat xml

Archives

  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • January 2009
  • November 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • October 2007
  • September 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

License

Creative Commons License
This work by Anomit Ghosh is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 India License.
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox