Archive for the ‘Technical’ Category

Adding local latex packages to texlive in ubuntu

February 19, 2011

When having custom packages to use for a document, you need to put them next your latex files when compiling. This causes problems when you need these packages for many documents. The solution is to add these packages to the latex installation. This can be done for the current user by creating a latex tree in the $HOME/texmf directory. The structure of this directory needs to follow the texmf-local described here. This directory is searched by default by the texlive installation.

Layar POI service using Google App Engine

April 4, 2010

Layar is a cool handheld augmented reality application that allows to overlay “Layers” on the image seen by a handheld device’s camera. One can think of these layers as content that is seen based on your current location. This allows to overlay digital data over actual live imagery.

The Layar API depends that the source of data (Points Of Interest) is a RESTful web service, that sends an HTTP GET request, and expects back  a JSON object. The details of the GET parameters, and the required JSON object could be found here in the layar API documentation.

To create a layer, one needs to provide such a service that provides the Points Of Interest (POI). Such a service could easily be written and provided by google app engine. This article will discuss how to do so using the google app engine python SDK, and is highly depends on the getting started guide for google app engine through python. Sections 1-4,6 are sufficient for understanding of the coming content.

Handling Requests

Since requests from layar to the POI web service come in the form of GET requests, the parameters can simply be accessed in the request handling method through:

self.request.get(parameter_name)

POI Response

The POI response that layar expects is a json object. The API place many restrictions on the response. The first comes in the content type of the HTTP response. Thus, it has to be set as follows:

self.response.headers['Content-type'] = 'text/javascript; charset=utf-8'

Another restriction is in JSON object returned as a response, where almost all the fields are required by layar. To make things simpler, the object is to be represented as a python object. This could be later converted to a JSON object. The JSON object responded with could be represented as follows (as a minumum):

{'layer':'layer name', 'hotspots':list_of_POI, 'errorCode':0, 'errorString':'ok'}

Where hotspots is a list of POI object. These object could be represented as:

class POI:
    def __init__(self,poi_id,title,lat,lang):
        self.actions = []
        self.id = poi_id
        self.imageURL = None
        self.lat = lat
        self.lon = lang
        self.distance = None
        self.title = title
        self.line2 = None
        self.line3 = None
        self.line4 = None
        self.attribution = ""
        self.type = 0
        self.dimenion = 1
        self.transform = {'rel':True, 'angle':0, 'scale':1.0}
        self.object = {'baseURL': ""}

From python to JSON

As python follows a batteries all included strategy, there are libraries that convert python dictionaries to JSON objects. Even though google app engine uses python, the “json” module is not available. Thankfully, an equivalent one is found through “from django.utils import simplejson”. It could be used to convert a python dictionary (that looks frightfully like a JSON object) to a JSON object string as follows:

simplejson.dumps({'layer':'guc', 'hotspots':poi_list, 'errorCode':0, 'errorString':'ok'})

The problem with simplejson is that it only takes dictionaries or lists. Thus, this poses a problem when using the POI class mentioned earlier. Again, python comes with a rescue, where a dictionary representing the object could be obtained as follows:

poi = POI('C1','C1',29986707,31438864)
poiDictionary = poi1.__dict__

Putting the code together

Now that we have the POI object, and the conversion mechanism, we are ready to have a request handler for layar requests. An example of static POIs is as follows:

from django.utils import simplejson
from poi import POI
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class POIHandler(webapp.RequestHandler):
	def get(self):
		self.response.headers['Content-type'] = 'text/javascript; charset=utf-8'
		#latitude and longitude is an integer will be divided by 10^6
		# so take care of accuracy after the division
		poi1 = POI('C1','C1',29986707,31438864).__dict__
		poi2 = POI('C2','C2',29986744,31439272).__dict__
		poi3 = POI('C3','C3',29986995,31438923).__dict__
		poi4 = POI('C4','C4',29987153,31439245).__dict__
		poi5 = POI('C5','C5',29986326,31438810).__dict__
		poi6 = POI('C6','C6',29986688,31438569).__dict__
		poi7 = POI('C7','C7',29986442,31438370).__dict__
		pois = [poi1,poi2,poi3,poi4,poi5,poi6,poi7]
		# final getPOI response dictionary
		d = {'layer':'guc', 'hotspots':pois, 'errorCode':0, 'errorString':'ok'}
		self.response.out.write(simplejson.dumps(d))

application = webapp.WSGIApplication(
		[('/',POIHandler),
		 ('/getPOI',POIHandler)],
		debug=True)

def main():
	run_wsgi_app(application)

if __name__=='__main__':
	main()

Updates

With the new layar 4 api coming soon, and the new features being in beta2, un update of how to add one of my favorite new features is needed. This is having different actions on the entire layar. It can be easily done by adding an action attribute to the final getPOI response dictionary (see code above). An example of the addition is as follows:

d['actions'] = [{'uri':'http://google.com','label':'action on the entire layar'}]

SVN post-commit hook cronjob

April 4, 2010

Few SVN hosting sites provide ssh access to their svn servers. Such is needed to create hooks, like a post-commit hook that sends an email with every commit. Such hooks are often required by the development team. This article discusses the creation of a cron job that polls the SVN repository to simulate a post-commit hook that sends an email with every commit.

Setting up sendEmail

sendEmail is a nice tool to send emails via command line. This is needed to script in the cronjob sending an email. On ubuntu, install it from apt-get by:

sudo apt-get install sendemail libio-socket-ssl-perl libcrypt-ssleay-perl

The latter 2 packages are needed for connecting to an SMTP server that uses a secure connection (like Gmail, which is used in the example).

The post-commit hook script

The following is a python script, that could be added as an entry in the cron table

#! /usr/bin/env python

import os,re

log_file = LOG_DIR
svn_log_seperator  = '------------------------------------------------------------------------'
svn_dir = SVN_DIR
mail_filter = '[special mail identifier in subject]'
message_file = '/tmp/commit_mail'

mail_from = MAIL_FROM
mail_to = MAIL_TO

smtp_server = "smtp.gmail.com:587"
smtp_user = SMTP_USER
smtp_pass = SMTP_PASS

def send_mail(subject,mail):
    mail_file = open(message_file,'w')
    mail_file.write(mail)
    mail_file.close()
    mail_command = 'sendEmail -f %s -t %s -u "%s" -s %s -xu "%s" -xp "%s" -o message-file="%s"' % (mail_from, mail_to, subject, smtp_server, smtp_user, smtp_pass,message_file)
    print mail_command
    os.system(mail_command)

print "analyzing old log file"

old_log_f = open(log_file)
old_log = old_log_f.read().split(svn_log_seperator)
old_log_f.close()

print "updating svn"
os.system("/usr/local/bin/svn update %s" % svn_dir)
os.system("/usr/local/bin/svn log %s > %s" % (svn_dir,log_file))
print "got new log"


new_log_f = open(log_file)
new_log = new_log_f.read().split(svn_log_seperator)
new_log_f.close()

delta = set(new_log) - set(old_log)

for commit in delta:
    l = [x for x in commit.split('\n') if x]
    details = l[0].split('|')
    subject = "%s %s %s" %(mail_filter,details[0],details[1])
    f_diff=os.popen('/usr/local/bin/svn diff -c %s %s' % (details[0], svn_dir))
    diff = f_diff.read()
    f_diff.close()
    mail = "%s\n%s" % (commit, diff)
    send_mail(subject,mail)

Perquisites before running the script

      Having a working copy of the svn reposity
      Running the following command once before the script:

      svn log > $PATH_TO_LOG_FILE

Two Columned LaTeX Presentation Slides

December 7, 2009

Coming from a Power Point background of creating presentation slides, i am often tempted to used a two column approach when laying out the content of my slides. This is where text is shown on the left column and related pictures are placed on the right.

After i decided to to move all my document creation work to be in LaTeX, I started to also create my presentations that way too. Such a feat is made possible using the beamer document class, whose user guide could be found here.

The environment for a single slide is the “frame” environment. It is used a follows:

\begin{frame}{frame title}

\end{frame}

To be able to split the frame (slide) into two columns, the “columns” environment is places inside the slide content. It is used as follows:

\begin{columns}
\column{column_width} %first column
column 1 content
\column{column_width} %second column
column 2 content
\column{column_width} %third column
column 3 content
...

It is best to have the column_width in terms of the text width. So for example:

\column{0.5\textwidth} %half slide's text width

An example for a two columned slide looks as like:

\documentclass{beamer}
\mode<presentation>
{
  \usetheme[width=2cm]{Hannover}
  \setbeamercovered{transparent}
}
\title[Presentation]{Presentation}
\author[Saher Mohamed El-Neklawy]{Saher Mohamed El-Neklawy}

\begin{document}
  \begin{frame}{Frame Title}
    \begin{columns}
      \column{0.5\textwidth}
	\begin{itemize}
	\item Item 1
	\item Item 2
	\item Item 3
	\end{itemize}
      \column{0.5\textwidth}
	\includegraphics[width=\textwidth]{i1.eps}

	\includegraphics[width=\textwidth]{i2.eps}
    \end{columns}
  \end{frame}
\end{document}

Two Columned Slide

The next step is to sequence the appearance of the images along with the images. In the beamer class, this could be done using the “” operator, following a certain component in the frame. The “n” represents when the component appears in the frame. For example:

\begin{itemize}
  \item<2> Item 1 % seen after 1 transition
  \item<3> Item 2 % seen after 2 transition
  \item<4> Item 3 % seen after 3 transition
\end{itemize}

In a sense, the “” splits a frame into n slides, placing the respective component in the nth slide of that frame. For more advanced uses of “”, check out the beamer class user guide.

To do the same for images, the “\visible” or “\only” macros could be used along with the “” operator.

With using “\visible”, the slides would now look like:

\documentclass{beamer}
\mode<presentation>
{
  \usetheme[width=2cm]{Hannover}
  \setbeamercovered{transparent}
}
\title[Presentation]{Presentation}
\author[Saher Mohamed El-Neklawy]{Saher Mohamed El-Neklawy}

\begin{document}
  \begin{frame}{Frame Title}
    \begin{columns}
      \column{0.5\textwidth}
	\begin{itemize}
	  \item<2> Item 1
	  \item<3> Item 2
	  \item<4> Item 3
	\end{itemize}
      \column{0.5\textwidth}
	\visible<2>
	{
	  \includegraphics[width=\textwidth]{i1.eps}
	}
	\visible<3>{
	  \includegraphics[width=\textwidth]{i2.eps}
	}
    \end{columns}
  \end{frame}
\end{document}
Sequenced Two Columned 1

Sequenced Two Columned 1

Sequenced Two Columned 2

Sequenced Two Columned 2

The problem with “\visible” is that it keeps the place of the component from the previous transition reserved in the next, just not seen. The fix to this issue is the use of “\only”, where the space for components is not reserved across transitions. Such makes the slides look as follows:

\documentclass{beamer}
\mode<presentation>
{
  \usetheme[width=2cm]{Hannover}
  \setbeamercovered{transparent}
}
\title[Presentation]{Presentation}
\author[Saher Mohamed El-Neklawy]{Saher Mohamed El-Neklawy}

\begin{document}
  \begin{frame}{Frame Title}
    \begin{columns}
      \column{0.5\textwidth}
	\begin{itemize}
	  \item<2> Item 1
	  \item<3> Item 2
	  \item<4> Item 3
	\end{itemize}
      \column{0.5\textwidth}
	\only<2>
	{
	  \includegraphics[width=\textwidth]{i1.eps}
	}
	\only<3>{
	  \includegraphics[width=\textwidth]{i2.eps}
	}
    \end{columns}
  \end{frame}
\end{document}

Two column slide using only macro 1

Two column slide using only macro 2

When taking a closer look at the output pdf, it will be observed that there is a slight change in the position of the bulleted list. This is most seen between pages 3 and 4 of the pdf. Such is due to the nature of the \only, as it does not reserver the position of component across transitions of the frame. Thus, the location of the bulleted list differs when there is a transition with a slide having a slide to one without, and vice versa.

This problem could be fixed by going back to the “\visible” macro, as it reserves the spaces for components. The difference this time is is that the location of the image in the slide will be forced. This could be done using the “picture” environment as follows:

\begin{picture}(0,0)(x,y)
    \put(0,0){\includegraphics[width=\textwidth]{i1.eps}}
\end{picture}

Take care to consider the x and y values from the top right corner of the column.

The final code results in this pdf, and looks like follows:

\documentclass{beamer}
\mode<presentation>
{
  \usetheme[width=2cm]{Hannover}
  \setbeamercovered{transparent}
}
\title[Presentation]{Presentation}
\author[Saher Mohamed El-Neklawy]{Saher Mohamed El-Neklawy}

\begin{document}
  \begin{frame}{Frame Title}
    \begin{columns}
    \column{0.5\textwidth}
      \begin{itemize}
	\item<2> Item 1
	\item<3> Item 2
	\item<4> Item 3
      \end{itemize}
    \column{0.5\textwidth}
      \visible<2>
      {
	\begin{picture}(0,0)(40,50)
	  \put(0,0){\includegraphics[width=\textwidth]{i1.eps}}
	\end{picture}
      }
      \visible<3>
      {
	\begin{picture}(0,0)(50,50)
	  \put(0,0){\includegraphics[width=\textwidth]{i2.eps}}
	\end{picture}
      }
    \end{columns}
  \end{frame}
\end{document}

Running launchpad remotely

November 4, 2009

Launchpad is a software collaboration platform developed by Canonical (famous for Ubuntu :D). Recently the the launchpad project went open source. This allowed people to host copies of it locally. The process for the installation process could be found here.

The next natural step is to configure remote computers to be able to access launchpad. After following the how-to by launchpad, and trying the access the local copy of launchpad from an external source, the following error is shown by the running launchpad server:

NotFound: Object: <canonical.launchpad.webapp.servers.ProtocolErrorPublication
object at 0x8157b50>, name: ''

The solution to this lies in the /etc/hosts file on the client accessing the lauchpad copy. The file should contain the following:

{ip for server}      launchpad.dev answers.launchpad.dev api.launchpad.dev
bazaar-internal.launchpad.dev beta.launchpad.dev
blueprints.launchpad.dev bugs.launchpad.dev code.launchpad.dev
feeds.launchpad.dev id.launchpad.dev keyserver.launchpad.dev
lists.launchpad.dev openid.launchpad.dev ppa.launchpad.dev
private-ppa.launchpad.dev shipit.edubuntu.dev shipit.kubuntu.dev
shipit.ubuntu.dev translations.launchpad.dev
xmlrpc-private.launchpad.dev xmlrpc.launchpad.dev

{ip for server}      bazaar.launchpad.dev

{ip of the server} is the ip of the machine containing the running copy of the launchpad server. Also take care the lines starting with {ip of the server} until an empty line should be a single line in the file. For client systems that are not linux, check out the Wikipedia article concerning hosts for the files equivalent to “/etc/hosts”.

To run launchpad, access https://launchpad.dev/.

On a side note, when installing launchpad, it does not add scripts for running automatically at startup, this process has to be done manually.

Searching Source Code: LXR and grep

November 2, 2009

Looking for a certain functionality in someone else’s code could prove to be a tough challenge. This is increased by the size of the given project. Many systems exist today facilitating easy search in a project. For Java, eclipse is best used for that task, as it contains a strong in java class indexing, classification, and searching.

Some projects provide for you a portal to search their code base. Famous examples of these are LXR for the Linux kernel, and it’s customized version MXR used for Mozilla projects.

When all the above options are not available, the alternative exists natively in most linux systems. This is namely “grep”. Grep is a strong command for regular expression matching. On if it’s features is that it can also match to the content within files. Putting that in mind, the following command could be very useful:

grep -R "search query" .

This looks recursively for all mentions of search query in all files under the current directory. This prints output in the console the file name where this query was found, and the line that included it.

To be able to store this search result in a file, the output could be piped as follows:

grep -R "search query" . > log.txt

Converting System::String to char *

October 31, 2009

The official way to convert a visual c++ System::String to a char * could be found here. Such strings are everywhere in windows forms applications.

A simpler way (for people who are used to using standard c\c++) to do the conversion is as follows:

void string2charPtr(String ^orig, char *&out)
{
int length = orig->Length;
out = new char[length+1];
for(int i=0;i<length;i++)
out[i] = (char) orig[i];
out[length] = '';
}

Please feel free to comment on the function above.

Processing.js:Reinventing the wheel

October 26, 2009

HTML5 is the new standard for web pages. One of the most exciting features that it introduces is the canvas tag. This component allows rendering vector graphics on the browser. This was not previously possible with older HTML versions.

With that advancement in HTML, new JavaScript libraries emerge to be able to create vector visualizations at the client side. One of these libraries is Processing.js, which is a port of the java processing library.

For a first look at processing, the examples show very exciting examples. But the week point of the framework shows when try to develop an application that required much interaction week. To reach such an application, the “processing.js” written has to be highly optimized. No room for unnecessary code execution, use of floats or doubles, cache as much as you can, etc….

My other issue with Processing.js is the fact that they ported from the java library everything, even the syntax. This comes to be very frustrating to web developers. The first reason for that is most web developers are used to the syntax of JavaScript, so having things like int x=0; and int [] arr = new int[4]; come as a big surprise. This could be good for the java developer, but not the the web developer.

My other bone to pick with Processing.js is how primitive it is relative to the current state of the web. There are no components, and you can’t define events on specific components. Everything  has to be done from scratch. Further strengthening the point of needing highly optimized code.

Backing to the title of my post, Processing.js tries to provide an alternative for Flash. In the web, the major disadvantage of Flash is it’s isolation from the rest of the webpage, making it an independent entity. Processing.js does not over come this problem, as the visualization code for each canvas is a world of it’s own. It can only access normal JavaScript code in the same page (which is possible with ActionScript 3’s ExternalInterface). This problem even escalates with lack of direct communication between different canvases directly using Processing.js code. Furthermore, Flash is currently cross browser compatible, unlike the canvas tag due to it’s new addition to the HTML standard.

Putting all the above factors in mind, I feel that using a the much mature flash plugin libraries which are closer to web developers is the best option currently in the market until the canvas tag matures more with richer libraries.

Finding your true computer name on the network

October 13, 2009

In many cases, it is essential to find the name of a computer over a local network. Such fits well when on a DHCP network, and need to always reach a certain machine.

An easy way to obtain this data, is via the python socket library. The ip of the desired machine is obtained via ipconfig (windows) or ifconfig (linux). For the sake of the example, assume that the ip from the DHCP network is 10.0.0.5.

In the python interpretor, execute the following code:

import socket
true_name = socket.gethostbyaddr("10.0.0.5")[0]
print true_name

The code above prints the true name of the machine, as seen on the network. This name is not always the machine name. For example the machine name could be “saher-desktop”, while the name on the network is “saher-desktop.local”.


%d bloggers like this: