Categories
PHP Python

Calling a function using string

PHP:
In php, you can assign a function name as a string to a variable and call the function by adding “()” after the variable name and appending a semicolon. Like this:

Python:
In python we call the function using the str as a key to the dictionary recieved by calling the globals() function. Like this:

Cool, eh?

Categories
PHP Python

Why PHP is going to be language of the future!

PHP is now the 3rd most popular programming language. And I believe, PHP is going to be the language of the future. I won’t go talking about the strength, flexibility or any other characteristics of programming languages, rather, I would like to highlight why PHP is going to be the most popular and most used language very soon:

# PHP is mainly a web automation tool. Web applications are most portable and cross-platform in nature. The web applications developed in PHP is expected to run on hundreds of thousands of machines without any further modification or system extension. The app I developed on my Linux machine runs smoothly on my Windows as well. Web applications can be accessed by anyone from anywhere. The age of client side applications is going to be over very soon. Undoubtedly PHP has the widest coverage in the web application arena.

# You can easily build mobile web applications using PHP which will run on all mobile devices which have internet connectivity. You don’t really need to learn J2ME to serve your mobile users.

# With jQuery and all those fantastic UI tools, web apps can have cool GUI like desktop apps as well.

# The things I mentioned above could be achieved by some other languages too. But again, almost all web hosting providers have PHP support where as you have to look for Python or Ruby support, let alone Java or Tomcat. So, PHP has a huge advancement in this case.

# People find PHP easy to learn. I am quite surprised how fast beginners learn PHP today. I don’t know why but it’s a clear indication that the popularity of PHP is on the rise for sure.

# PHP has a huge community. You get help and support from them very quickly. Again PHP having a large community and numerous mailing lists, forums etc, you even have the option to switch between them 😀

# Have you ever wondered how many free shared hosting providers support PHP ? Ask google 😀

# PHP has so many tutorials, HOWTOs and docs spread over the internet. I doubt if any other language has as much free resources as PHP have today.

# PHP programs being open source, you always have the chance to modify a program by yourself.

# PHP-GTK and some other tools are being developed for building desktop tools as well. I do believe these tools would do good in the future.

# I always found the PHP Manual more user friendly and very informative. There are examples in almost everywhere. That makes learning and using PHP easy and comfortable.

# PHP has a very large collection of extensions developed by the PHP dev community.

I don’t really doubt that PHP is going to be the language of the future. If they call PHP a “bad” language, I am afraid, the future is going to be “bad” 😉

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 🙂