Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Print to standard output in different scripting languages

Answer:

To print a string of "Hello World" to the standard output in different scripting languages

1. Perl

print "Hello, World!";

2. Python

print('Hello, World!')

3. PHP

echo "Hello, World!";

4. Ruby

puts "Hello, World!"

Add Personal Package Archives (PPA) to the current repository in Ubuntu

Answer:

add-apt-repository is a helper script which adds an external APT repository to either /etc/apt/sources.list or a file in /etc/apt/sources.list.d/. It can also import the keys needed.

To get this tool, just a single command:

# sudo apt-get install python-software-properties

That is it.

Render web page using PyQt

Answer:

Qt bindings to WebKit have been added since 4.4, so you can use PyQt to script the browser (Webkit based).

Firstly, make sure you have installed the need packages:

# sudo apt-get install libqt4-core libqt4-webkit python-qt4

Then you can write a simple script to test, e.g.

#!/usr/bin/env python

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

app = QApplication(sys.argv)

web = QWebView()
web.load(QUrl("http://www.google.com"))
web.show()

sys.exit(app.exec_())

How do I convert a number to a string in Python?

Answer:

To convert a number to a string in Python, try the str() function.

foo = str( 999 );
print foo + ' is a string';

Result is :

999 is a string

How do I find the current module name in Python?

Answer:

The easiest way is to look at the look at the predefined global variable __main__. If it has the value "__main__", it means the program is run as a script (not as imported module).

E.g.

def main():
    print 'Running test...'

if __name__ == '__main__':
    main()