Skip to main content

Posts

Showing posts with the label Programming

Java : Thread example using thread class

Below code in an example of jav a program implementing thread.Here thread is implemented by extended Thread class. The function to be perform ed is implemented in run() function. public class ThreadMini extends Thread{ private String str; ThreadMini(String st){ this .str = st; } public void run(){ System.out.println( "Thread:" + str); } } public class MainClass { public static void main(String[] args) { ThreadMini t1 = new ThreadMini( "t1" ); ThreadMini t2 = new ThreadMini( "t2" ); ThreadMini t3 = new ThreadMini( "t3" ); t1.start(); t2.start(); t3.start(); System.out.println( "Main Thread!" ); } } Output of above program Thread:t1 Thread:t2 Main Thread! Thread:t3 The above lines can get printed in any order.

Python : Calling unix script from python

We have certain modules like subprocess which can be used to call a shell script from python program. The below code shows how to call the module from python program. #!/usr/bin/python import subprocess as sp sp.call(('hello2.sh') For more information on subprocess module refer https://docs.python.org/2/library/subprocess.html

Python : How to create your own module in python

Python modules are easy to create and imported in to your python program. How to create a module? The python program itself can be called as a python module. All the function which you find that can be grouped together can be put into the file. Here a simple example to print the text on display is used and called as techinfotalks. #!/usr/bin/python # Python Module def func_hi(): print "Welcome to the module" #End How to use the module in program? You just import the module created by calling the import.After that directly call the function which ever you need.The module named techinfotalks is imported as named as ti. #!/usr/bin/python import techinfotalks as ti print "Calling funcion in module" ti.func_hi() #End of program

UNIX : Using IF in bash

The IF statement is used to test the given condition and based on the truth value of that the expression the statement followed is executed.Otherwise the else part is executed. The basic structure of if is as follows. If [[ expression ]];then      expression elif [[expression ]];then      expression else      expression 1) Integer comparison    2) String comparison    3) Using multiple conditions in IF statement    4) Compare regular expression in bash

Python : Prime Number range

#/usr/bin/python def prime_check(n):     bitval = False     for x in range(2,n):         if n % x == 0:             bitval = True             break             if (bitval):         print ("{} is not a prime number".format(n))     else:         print ("{} is a prime number".format(n)) for i in range(1,10):     prime_check(i)

Python : Prime number

#/usr/bin/python bitval = False a = 20 for x in range(2,20):     if a % x == 0:         bitval = True         break         if (bitval):     print ("x is not a prime number") else:     print ("x is a prime number")

Python : Read words from a string

The below example prints all the words starting with letter 's' from the input string. split() - This function separate the string based on space by default. if we need to split we can give the separator inside the function as argument. The result can be directly assigned to a variable and a list is created implicitly. startswith() - this function finds the strings which start with letter specified. Output sample starting s sentence