Custom Search

Sunday, June 26, 2011

python try finally return

def x1():
    try:
        print " in try block"     
            return 'returned value'
    finally:
                 print "in Finally block"

print"-----------test1----------\n",x1()





def x1():
         try:
             a= 1
        print " in try block"     
                 return  a
         finally:
             a = 2   
                 print "in Finally block"

print"-----------test2----------\n",x1()





def x1():
         try:
             a= 1
        print " in try block"     
                 return  a
         finally:
             a = 2   
                 print "in Finally block"
        return a

print"-----------test3----------\n",x1()




def x1():
         try:
             a= 1
        print " in try block"     
                 return  a
         finally:
             a = a+1   
                 print "in Finally block"

print"-----------test4----------\n",x1()



def x1():
    a= 1
         try:
        a = a+1
        print " in try block"     
                 return  a
         finally:
             a = a+2   
                 print "in Finally block"

print"-----------test5----------\n",x1()




def x1():
    try:
        a= 1
    finally:
        pass
         try:
        a = a+1
        print " in try block"     
                 return  a
         finally:
             a = a+2   
                 print "in Finally block"

print"-----------test6----------\n",x1()


OUTPUT
=========
-----------test1----------
 in try block
in Finally block
returned value
-----------test2----------
 in try block
in Finally block
1
-----------test3----------
 in try block
in Finally block
2
-----------test4----------
 in try block
in Finally block
1
-----------test5----------
 in try block
in Finally block
2
-----------test6----------
 in try block
in Finally block
2

Friday, June 17, 2011

How to mount HPFS/NTFS filesystem on linux (SOLVED)

How to mount HPFS/NTFS filesystem on linux


* Run following command to find all partitions and file system type
#sudo fdisk -l

1) Mount using mount command
#sudo mount /dev/sda2 /mnt/ntfs

OR

2) Mount using ntfs-3g command
#sudo ntfs-3g /dev/sda2 /mnt/ntfs



How to install ntfs-3g

a) Fedora/CentOS
#yum install fuse-ntfs-3g

b) Ubuntu
#sudo apt-get install ntfs-3g

Monday, June 13, 2011

How to sqlalchemy print query for debug

How to sqlalchemy print query for debug


qry = DBsession.query(A).filter(A.id=2)
print "-------------", str(qry)

Sunday, June 12, 2011

How to format drive in FAT32 in Linux

How to format drive in FAT32 in Linux
#mkfs.vfat -F32 /dev/sdb1

How to Quick NTFS format in Linux

How to Quick NTFS format in Linux
#mkfs.ntfs -f /dev/sdb1

How to create EXT4 and FAT32 NTFS partitions in USB Hard Disk

How to create EXT4 and FAT32 NTFS partitions in USB Hard Disk

1) Create two partitions in USB Hard Dsik using 'cfdisk' command.

# cfdisk /dev/sdb <----- Here sdb is the USB drive.

*Suppose we created two partitions /dev/sdb2 and /dev/sdb3 in USB drive.
* Make /dev/sdb2 to FAT32 and Primary.
* Make /dev/sdb3 to EXT4.

Note: We should create one partition as FAT32 and Primary, Other wise we can not open this USB Hard Disk in Windows System.
Note: Windows need one Primary partition.

3)
* Format /dev/sdb2 in FAT32.
#mkfs.vfat -F32 /dev/sdb1

* Quick Format /dev/sdb3 in NTFS.
#mkfs.ntfs -f /dev/sdb3


4)
* Now we can see/open both partitions in Linux System.
* We can see/open FAT32 partition in Windows System.

Thursday, June 9, 2011

python howto call parent class method

python howto call parent class method


class A:
def __init__(self):
self.x = 100

def test(self, a):
print "--self----a--1-", self, a
print "--self.x----a--1-", self.x

class B(A):
def __init__(self):
#A.__init__(self)
self.x = 200
#pass

def test(self, a):
A.test(self, 10) #<----------
print "--self----a---2-", self, a
print "--self.x----a--1-", self.x

b = B()
b.test(5)

OUTPUT
========
$ python p.py
--self----a--1- <__main__.B instance at 0xb7f39acc> 10
--self.x----a--1- 200
--self----a---2- <__main__.B instance at 0xb7f39acc> 5
--self.x----a--1- 200

how to install all python packages to virtualenv directory without pointing to global site-packages directory if package already installed there

how to install all python packages to virtualenv directory without pointing to global site-packages directory if package already installed there.

use option --no-site-packages

Example:
#virtualenv myvenv --no-site-packages
#virtualenv test_project --no-site-packages

Can not connect to remote MySQL server Solved

Can not connect to remote MySQL server Solved

------------------------- Error-1
$ mysql -uroot -ppassword --host=192.168.1.108
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '192.168.1.108' (111)")

Solution
===========
In /etc/mysql/my.cnf comment fllowing line.
#bind-address = 127.0.0.1


------------------------- Error-2
$ mysql -uroot -ppassword --host=192.168.1.108
_mysql_exceptions.OperationalError: (1130, "Host 'localhost.local' is not allowed to connect to this MySQL server")

Solution
===========
Run following commands in Mysqlserver.

$ mysql -uroot -ppassword


mysql> CREATE USER 'root'@'localhost.local' IDENTIFIED BY "password";
Query OK, 0 rows affected (0.00 sec)


mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost.local';
Query OK, 0 rows affected (0.00 sec)

ubuntu 11 NFS server and client setup and Configuration

ubuntu 11 NFS server and client setup and Configuration

Assume
-----------

Server Machine
IP 192.168.1.1

Client Machine
IP 192.168.1.3


Server side setup
============

1) Package installation
-----------------------
apt-get install nfs-kernel-server nfs-common portmap


2) Starting Services
--------------------
/etc/init.d/nfs-kernel-server restart
/etc/init.d/portmap restart

Test whether NFS is running correctly with the rpcinfo command.
You should get a listing of running RPC programs that must include mountd,
portmapper, nfs, and nlockmgr.

rpcinfo -p localhost


3) Sharing files / Directories
------------------------------
You have to mention shares & their permission in /etc/exports file. I am sharing my /movies directory with another pc on the network. Giving read only permission to client machine
Edit the file /etc/exports

vi /etc/exports

/movies *(ro,sync)

If you want to share same folder with read & write permission then syntax will be

/movies *(rw,sync)

If you want to share same folder only for particular machine on the network then

/movies 192.168.1.3(ro,sync)

For more info you can refer man pages for NFS

man 5 exports


4) Whenever we modify /etc/exports, you must run
------------------------------------------------
exportfs -av

-a Export or unexport all directories
-v Be verbose. When exporting or unexporting, show what’s going on.
When displaying the current export list, also display the list
of export options.




Client side setup
===========

1) Package installation
-----------------------
apt-get install nfs-common portmap


2) Starting Services
--------------------
/etc/init.d/portmap restart


3) Geting server share information
----------------------------------
showmount -e 192.168.1.1


4) Mounting The NFS Shares On The Client
----------------------------------------
mkdir /tp
mount -t nfs 192.168.1.1:/movies /tp

Confirm the share by mount command. You can also add mount entry in fstab file, so that at the next start of your machine you don’t need to manually mount the share.

echo "192.168.1.1:/movies/ /tp nfs defaults 0 0" >> /etc/fstab

Note : If you can’t access nfs share from client please ensure that
1. TCP/UDP 2049,111 ports should be open on your firewall.
2. Check the output of rpcinfo command
3. Update the share information via exportfs -av command
4. Reload the service on NFS server as well as NFS client

Wednesday, June 1, 2011

How To change permission of file folder so only root can access open it

How To change permission of file folder so only root can access open it

1. Change the ownership of the directory / file to root
chown root:root

2. Change the permissions so that only the owner of the file can read/write/execute it
chmod 700

-----------------------------------

How to change file permission in Linux so that only root user can access/open the file/dir

sudo chown root:root filename
sudo chmod 600 filename


If it's an executable, use 700 for the permissions.

You can also specify the permissions in a symbolic style:

sudo chmod u=rw filename


-------------------------------------

1) chmod 0700
The directory’s owner can read or write files in that directory as well as change to it.
All other users (except root) have no access.


2) chmod 0771
Same as for the owner. All other users can change to the directory, but not view or
change files in the directory. This can be useful for server hardening,
where you prevent someone from listing directory contents,
but allow access to a file in the directory if someone already knows it’s there.


3) chmod 0777
All permissions are wide open.


4) chmod 0000
All permissions are closed. Good to protect a directory from errant changes.
However, backup programs that run as non-root may fail to back up the directory’s contents.


5) chmod 666
Open read/write permissions completely on a file.


6) chmod 644
Only the owner can change or delete the file, but all can view it.


The first 0 in the mode line can usually be dropped (so you can use 777 instead of 0777).
The -R option is a handy feature of the chmod command. With -R, you can
recursively change permissions of all files and directories starting from a
point in the file system. Here are some

examples:
$ sudo chmod -R 700 /tmp/test Open permission only to owner below /tmp/test
$ sudo chmod -R 000 /tmp/test Close all permissions below /tmp/test
$ sudo chmod -R a+rwx /tmp/test Open all permissions to all below /tmp/test