Tuesday 20 September, 2011

Solr gem and xml ruby gem How it matters


If you use solr-ruby gem, you should be well aware that Solr attempts to create an XML doc from the provided doc-hash. It first attempts to use 'xml/libxml', which if not available, falls back to REXML. It is recommended to use libxml.

All you need to do is

gem install libxml-ruby
If you are lucky enough, that's all for you. However, many a times we face issues like
Building native extensions.  This could take a while...
ERROR:  Error installing libxml-ruby:
	ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb
checking for socket() in -lsocket... no
checking for gethostbyname() in -lnsl... yes
checking for atan() in -lm... no
checking for atan() in -lm... yes
checking for inflate() in -lz... no
checking for inflate() in -lzlib... no
checking for inflate() in -lzlib1... no
checking for inflate() in -llibz... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
If this is the case, you should install zlib-devel and libxml2-devel packages.
yum -y install zlib-devel
yum -y install libxml2-devel
gem install libxml-ruby

That sorts it all. You are good to go


Monday 19 September, 2011

ruby fetch_hash ArgumentError: NULL pointer given


Recently, One of our servers started to have the following error
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'mysql'
=> true
irb(main):003:0> conn = Mysql.connect('db01', 'xxxxxx', 'xxxxxx', 'employee')
=> #
irb(main):004:0> a = conn.query("SELECT * FROM employees WHERE id >= 8500 AND id < 9000")
=> #
irb(main):005:0> a.fetch_hash
ArgumentError: NULL pointer given
	from (irb):5:in `fetch_hash'
	from (irb):5
irb(main):006:0> exit
The method fetch_hash is a standard method and works. Here is the versions of MySQL and ruby that I used
-bash-3.2$ mysql --version
mysql  Ver 14.12 Distrib 5.0.89, for unknown-linux-gnu (x86_64) using readline 5.1

-bash-3.2$ ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
If you face this issue in your servers or anywhere try the following workaround. They have worked for me, and they should work for you as well.
  1. uninstall the MySQL-shared-compat package
  2. Re Install mysql gem

Wednesday 16 March, 2011

Control activity feeds on facebook



Controlling activity feeds on facebook

Facebook has a setting which lets you chose amongst your friends and pages, whose updates you want to see and whose you want to skip. Here it is how


Next to Most Recent, there is a drop down button. Click on that to gain access to control options. The drop down menu comes only when you are viewing the Most Recent news feeds only. So if you don't see an option, just click on Most Recent. The page will refresh itself and the menu options will come.


Select Edit Options in the drop down menu


Now conveniently you can choose to show news from

  1. Those friends and pages you interact with most or
  2. All friends and pages.
The default setting is "friends and pages" you interact with most. You can also edit friends whose feeds have been blocked in the list.

Tuesday 15 March, 2011

Recursively traversing Python dictionary and removing keys



Code Snippet to recursively traverse a dictionary and remove certain key/value pair


In case of complex dictionaries like
my_blog = { '_created' : datetime.datetime (2007,01,03),
          '_updated' : datetime.datetime (2011,06,11),
            'name'     : 'MyBLive',
            'latest_post'    : { '_created' : datetime.datetime (2007,06,9),
                                 '_updated' : datetime.datetime (2007,069),
                                 'name'     : 'Hello World !' }

The problem with this is that datetime.datetime entities are not JSON Serializable. One approach could be to provide a serializer for datetime.datetime entities as suggested by verte over IRC. If you want to have a datetime.datetime aware JSONSerializer, you should have a look at the django.core.serializers.json module

In my case, we were using a Google App Engine application and the JSON response need not contain these key/value pairs so it makes more sense if these are removed from the dictionary.

def rm (d, l):
  """
  Removed from dictionary "d" all those key-value pairs the keys of which
  are defined as a list in "l"
  """
  if not l: return d
  if reduce ( lambda x,y: x or y, [x in d.keys () for x in l]):
    [d.pop (x, None) for x in l]
  [rm (x, l) for x in d.values () if isinstance (x, dict)]
  return d