Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday, February 3, 2016

Multi-threading in Python

How to create multi-threaded programs in Python?

Before going into programming we will give you small introductions about threads. A thread is a light weight component of a process in operating system which can act just like a process and it can share its resources i.e. memory with another threads. The operating system should have thread schedular in order to make sure which thread will get the CPU at time t.

Threads are very useful for parallel execution of programs.

In the below example we will create some threads and show you how we can execute multiple threads simultaneously.

=====
import threading
import time
class X:
   def myfunction(self,firstName,age=20):
      print threading.currentThread().getName(), 'start'
      time.sleep(3)
      print firstName,age
      print threading.currentThread().getName(), 'end of myfunction method'
threads=[]
x=X()
for i in range(4):
    t = threading.Thread(target=x.myfunction, args=(i,),kwargs={'age':21})
    threads.append(t)
[x.start() for x in threads]
=====
Here we have created 4 threads and all threads are executing myfunction inside class X.

We have passed method arguments as args which is a tuple.
We have passed method optional arguments as kwargs which is a dict. The key of the dict will be the variable name in the method.

Here in the above example, args=(1,) means  firstName=1
and kwargs={'age':21} means we passed age=21 not 20 as default. 

Ouput:
------------------------------------------------
Thread-1 start
Thread-2 start
Thread-3 start
Thread-4 start

0 21
Thread-1 end of myfunction method
1 21
Thread-2 end of myfunction method
2 21
Thread-3 end of myfunction method
3 21
Thread-4 end of myfunction method
---------------------------------------------------

Try it yourself and comment in case you have any questions.

Monday, February 1, 2016

How to configure logger in python?

How to configure logger in python?

In this page we have given how to configure a basic logging using python logging module.

==
import threading
import time
import logging
def configLogger():
   logging.basicConfig(level=logging.DEBUG,
                    format='[%(levelname)s] (%(threadName)-10s) %(message)s',
                    )

def task(name,age=20):
      logging.info(threading.currentThread().getName() + 'Start...')
      time.sleep(2)
      print name,age
      logging.debug(threading.currentThread().getName() + 'End...')
threads=[]
for i in range(5):
    t = threading.Thread(target=task, args=(i,),kwargs={'age':21})
    t.setDaemon(True)
    threads.append(t)
    #t.start()
configLogger()
logging.debug("using logger")
[x.start() for x in threads]
===

Output:
[DEBUG] (MainThread) using logger
[INFO] (Thread-1 ) Thread-1Start...
[INFO] (Thread-2 ) Thread-2Start...
[INFO] (Thread-3 ) Thread-3Start...
[INFO] (Thread-4 ) Thread-4Start...

How to redirect output of logger into a file?

We can redirect the logger output into a file instead of stdout/stderr file. We might need this file for future reference. In below code snippet I have shown how to configure the logger to redirect its output into a file. It is very useful when we try to develop any application.

==========
def Logger(logDir, fileName=None):
   logFormat = logging.Formatter('%(asctime)s [%(levelname)s %(filename)s::'
                                 '%(funcName)s::%(lineno)s] %(message)s')
   handler = logging.StreamHandler(sys.stdout)
   handler.setLevel(logging.DEBUG)
   handler.setFormatter(logFormat)
   log = logging.getLogger("basic")
   log.level = logging.DEBUG
   log.addHandler(handler)

   if not fileName:
      t = time.time()
      timeStr = time.strftime("%d%b%Y-%H%M", time.gmtime(t))
      timeStr = "%.3f-%s" % (t, timeStr)
      fileName='Test-%s.log' % timeStr
   fhandler = logging.FileHandler("%s/%s" % (logDir, fileName))
   fhandler.setLevel(logging.DEBUG)
   fhandler.setFormatter(logFormat)
   log.addHandler(fhandler)
   return log
==========

Sunday, January 31, 2016

class method in details

class method in details

In python class method invocation should be done with care because it is little different from Java.

3 types of method can be defined in a class
1. Instance method: This methods are the method for which class instance is passed implicitly as an argument. 
2. Class method: This methods are the method for which caller class is passed implicitly as an argument. 
We need to use decorator @classmethod to define this inside a class.
3. Static method: This methods are the method for which neither class nor instance is passed implicitly as an argument. 
We need to use decorator @staticmethod to define this inside a class.

In below I have given one example class.

===start of class_example.py==
class A:
   def __init__(self,name,org='init_xyz'):
      print "start constructor"
      self.name=name
      self.org=org
      self.age=10
      print "end constructor"

   def instanceMethod(self):
      print "start of instanceMethod"
      print self.name,self.org
      print "End of instanceMethod"
   @classmethod
   def class_method(ownClass, cls):
      print "start class_method"
      print cls.name,cls.org
      print "End of class_method"

   @staticmethod
   def static_method(name,org='static_xyz'):
      print "start static_method"
      print name,org
      print "End of static_method"


a=A('james')
print "output of instanceMethod: calling using instance..."
a.instanceMethod()
b=A('Thor','ABC')
print "output of class_method: calling using syntax a.methodName(...)"
a.class_method(b)

print "output of class_method: calling using syntax A.methodName(a,...)"
A.class_method(b)
====end of class_example.py==

Output:
[root@xxxx pyws]# python class_example.py
start constructor
end constructor
output of instanceMethod: calling using instance...
start of instanceMethod
james init_xyz
End of instanceMethod
start constructor
end constructor
output of class_method: calling using syntax a.methodName(...)
start class_method
Thor ABC
End of class_method
output of class_method: calling using syntax A.methodName(a,...)
start class_method
Thor ABC
End of class_method



Thursday, January 28, 2016

Packaging in right way

How to make packages in python?

Packaging is very important in case of building any application using programming language. Python has a very unique and easy way to do this.

Say we want to call a function[mod()] written into /work/pyws/pac/dir1/mod1.py
from a caller.py which is in /work/pyws/pac/ 

To do that we need to follow below steps:

  1.  We need to set the python path[i.e. from where modules get loaded] It can be done using command:- export PYTHONPATH=$PYTHONPATH:/work/pyws/pac/
  2.  In each directory we need to create __init__.py [content might be left as blank]
  3.  We need to import the file before calling a function. e.g. from dir1 import mod1


I will demonstrate the above steps using my sample code snipptes in the following section.

Say we are in /work/pyws/pac/ directory. Here we have a caller.py inside /work/pyws/pac/
==caller.py===
from dir1 import mod1

def f():
   mod1.mod()

f()
===end of caller.py===

AND a blank __init__.py file.

Inside /work/pyws/pac/dir1 we have mod1.py.
===mod1.py===
def mod():
   print "inside mod1"
===end of mod1.py====

AND a blank __init__.py file.

Now run caller.py [python caller.py]
You will get output:
[root@XXXXX pac]# python caller.py
inside mod1

Try it yourself and comment below if you have any problem.

Tuesday, January 26, 2016

Python: String

Like Java programming language, str objects in python is also an immutable datatype.

How to create str objects?

We can create a string object using below syntax;
name="james"
OR
name='james'

Please note that use of single quote(') or double quote(") should be consistent. If we start a string with single quote(') then it should be ended with that only. Otherwise compiler error will be thrown. Also say if we use single quote and we want use double quote as str character then that is allowed in python.

Basic Operations on string:

len(string): This method will return the length of the string.

Example:
len('abc') should produce output 4.

split([sep [,maxsplit]])
This method is used to split a string into several strings based on the parameter separator(sep) and max no of split to be done (maxsplit). By default sep is any white space.

Example:

>>> str="I am a     graduate."
>>> str.split()
['I', 'am', 'a', 'graduate.']
>>> str.split(" ",2)
['I', 'am', 'a     graduate.']
>>> str.split(" ",1)
['I', 'am a     graduate.']





Sunday, January 24, 2016

Python: Print in the same line

During my learning days i was getting troubles to print values in the same line.
To overcome this see the below code:
for s in res:
    print s,// This comma is used to print anything in the same line.
Isn't it simple? try it out...