Categories
Python

Building Command Line Interface using Python

Creating a command line interface with Python is pretty easy 🙂 Python has a built in module — “cmd” to assist you in building a command line interface of your application.

In this example, I have created a dummy shell that uses the os.system() call to execute a command in terminal. Here is the code explanations:

First, we import the modules required for the demo. Then, we create a subclass of the cmd.Cmd class. Then we define the commands by defining and naming methods in “do_” in scheme. When the user types something like “ “, the do_ is called and the parameters are passed as a single string. If a method returns True, the shell exits. We can define help docs by naming methods in “help_” scheme. do_EOF handles the “end of file” character. That’s this function is called when we press Ctrl+D on Linux shell. By default, pressing the enter key without any command executes the last command. We override this behaviour by re-defining the emptyline() method.

For a complete reference, please consult the python manual.

Here’s the source code of the program I wrote:
Type “shell date” or any other commands to try it out.
Type “help” to get help 🙂

Categories
PHP

PHP Text Flipper Library

Have you seen some cool webapps where you can write English texts upside down?

I have found out the techniques. They just use some Unicode characters to do the trick. Unicode has a vast collection of characters and you have to find the right one that looks like an upside down english character.

I have written a PHP library to do the job .
Visit the demo at:
https://masnun.com/flipper/ 😀
Download the Library at:
http://masnun.googlecode.com/files/flipper.php 🙂

Here’s the source code of the demo:

PS: PHP and web browsers have some issues regarding Unicode data. So please ensure the “Content-type” is set to “text/html” and the charset is “utf-8” 🙂

Happy flipping 😀

Categories
PHP

PHP DOM and XPATH

After checking out beautifulsoup in Python, I became very curious to know what we have in PHP for accessing the DOM nodes of a html document. After exploring a while, I found out the PHP DOM and XPATH.

Here’s a little example:

masnun.html – The HTML file

Now we use PHP to access the document:

That’s it, pretty simple and easy ! 😀