Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, August 11, 2014

Cross-platform way of working with python temporary file and directory

import tempfile
import os

print 'Temp Directory:', tempfile.gettempdir()
f = tempfile.NamedTemporaryFile(delete=False, prefix='_PREFIX_', suffix='_SUFFIX_')
print 'File Name:', f.name;
f.write('Something On Temporary File... 1')
f.write('\nSomething On Temporary File... 2')
f.seek(0)
print '\nFile Contents: '
print f.read()
print '\nFile Contents Line By Line: '
f.seek(0)
for line in f:
    print '--->', line.rstrip()
f.close()
print '\nFile Exists:', os.path.exists(f.name)
os.unlink(f.name)
print '\nFile Exists:', os.path.exists(f.name)
dirc = tempfile.mkdtemp()
print '\nDirectory Name:', dirc
print '\nDirectory Exists:', os.path.exists(dirc)

for num in range(1, 3):
    fname = os.path.join(dirc, str(num) + '.file');
    print '\nCustom File:', fname
    tpfile = open(fname, 'w+b');
    tpfile.write('Writting Something...' + str(num));
    tpfile.seek(0);
    print 'File Contents:'
    print tpfile.read();
    tpfile.close()
    print 'File Exists:', os.path.exists(tpfile.name)
    os.unlink(tpfile.name)
    print 'File Exists:', os.path.exists(tpfile.name)

print '\nDirectory Name:', dirc
print '\nDirectory Exists:', os.path.exists(dirc)
os.removedirs(dirc)
print '\nDirectory Exists:', os.path.exists(dirc)

Output would be like this:

Temp Directory: c:\users\user\appdata\local\temp
File Name: c:\users\user\appdata\local\temp\_PREFIX_mrr8st_SUFFIX_

File Contents: 
Something On Temporary File... 1
Something On Temporary File... 2

File Contents Line By Line: 
---> Something On Temporary File... 1
---> Something On Temporary File... 2

File Exists: True

File Exists: False

Directory Name: c:\users\user\appdata\local\temp\tmpdvd8ej

Directory Exists: True

Custom File: c:\users\user\appdata\local\temp\tmpdvd8ej\1.file
File Contents:
Writting Something...1
File Exists: True
File Exists: False

Custom File: c:\users\user\appdata\local\temp\tmpdvd8ej\2.file
File Contents:
Writting Something...2
File Exists: True
File Exists: False

Directory Name: c:\users\user\appdata\local\temp\tmpdvd8ej

Directory Exists: True

Directory Exists: False

Sunday, August 10, 2014

Parse xml with namespace and store data to data dictionary - Python


import sys
import xml.etree.ElementTree as ET

class XmlParser:
    def fixAttrs(self, attrs, c):
        nattrs = {}
        for attr in attrs:
            nattrs[self.buildXmlnsKey(attr, c)] = attrs[attr]
        return nattrs

    def buildXmlnsKey(self, tagtxt, tcounter):
        done = False
        if not tagtxt.startswith("{"):
            return tagtxt
        #print 'Search for: ', tagtxt, ' in ', tcounter
        if self.nsmap.has_key(str(tcounter)):
            nslmap = self.nsmap[str(tcounter)]
            #print 'nslmap-->', nslmap
            for obj in nslmap:
                if done is False:
                    for key in obj.keys():
                        kstr = '{' + key + '}'
                        #print 'key--->', key, ', --->', kstr
                        if tagtxt.startswith(kstr) and done is False:
                            done = True
                            kval = obj[key]
                            #print 'need to replace to: ', kval
                            if len(kval):
                                tagtxt = tagtxt.replace(kstr, kval+':', 1)
                            else:
                                tagtxt = tagtxt.replace(kstr, '', 1)

        if done is False and tcounter > 0:
            tcounter = tcounter - 1
            return self.buildXmlnsKey(tagtxt, tcounter)
        return tagtxt
                        

    def xmlToDict(self, node, dictclass = None):
        if dictclass is None:
            dictclass = {}
        self.ncounter = self.ncounter + 1
        if len(node):        
            if node.attrib:
                #print node.attrib
                dictclass['<<attr>>'] = self.fixAttrs(node.attrib, self.ncounter);            
            for child in node:
                tagtxt = self.buildXmlnsKey(child.tag, self.ncounter)
                newItem = self.xmlToDict(child)
                #tagtxt = child.tag
                if dictclass.has_key(tagtxt):
                    if type(dictclass[tagtxt]) is type([]):
                        dictclass[tagtxt].append(newItem)
                    else:
                        oldItem = dictclass[tagtxt];
                        dictclass[tagtxt] = [];
                        dictclass[tagtxt].append(oldItem);
                        dictclass[tagtxt].append(newItem);
                else:
                    dictclass[tagtxt] = newItem
        else:
            if node.text is None:
                text = ''
            else:
                text = node.text.strip()

            
            if node.attrib:
                #print fixAttrs(node.attrib, ncounter)
                dictclass['<<attr>>'] = self.fixAttrs(node.attrib, self.ncounter)
                dictclass['<<value>>'] = text;
            else:
                dictclass = text;

        return dictclass

    def printDic(self, dic, pos = None):
        if pos is None:
            pos = 0
        for key in dic.keys():
            #print 'key--->', key
            if dic[key] is None:
                print self.getLenStr(pos), key, ''
            elif type(dic[key]) is type({}):
                npos = pos + 1
                print self.getLenStr(pos) + str(key), '{'
                self.printDic(dic[key], npos)
                print self.getLenStr(pos) + '}'
            elif type(dic[key]) is type([]):
                npos = pos + 1
                print self.getLenStr(pos) + str(key), '['
                self.printList(dic[key], npos)
                print self.getLenStr(pos) + ']'
            else:
                print self.getLenStr(pos) + str(key), ': ', dic[key]

    def printList(self, dic, pos = None):
        if pos is None:
            pos = 0
        lindex = -1
        for obj in dic:
            lindex = lindex + 1            
            if obj is not None:
                if type(obj) is type({}):
                    print self.getLenStr(pos) + str(lindex)+'. {'
                    npos = pos + 1
                    self.printDic(obj, npos)
                    print self.getLenStr(pos) + '}'
                elif type(obj) is type([]):
                    print self.getLenStr(pos) + str(lindex)+'. ['
                    npos = pos + 1
                    self.printList(obj, npos)
                    print self.getLenStr(pos) +']'
                else:
                    print self.getLenStr(pos), str(lindex)+'.', obj

    def getLenStr(self, pos):
        sstr = ''
        while pos > 0:
            sstr = sstr + '   '
            pos = pos - 1
        return sstr

    def __init__(self, fileLocation):
        self.location = fileLocation;

    def parse(self):
        tree = ET.parse(self.location)
        root = tree.getroot()
        self.nsmap = {}
        self.lcounter = 0

        for event, elem in ET.iterparse(self.location, events=('start', 'end', 'start-ns', 'end-ns')):
            if event == 'start-ns':
                #print 'start-ns', lcounter
                a, b = elem
                if b is not None and len(b):
                    #print "A-->", a, ", B-->" , b
                    scounter = self.lcounter + 0
                    if not self.nsmap.has_key(str(scounter)):
                        self.nsmap[str(scounter)] = []
                        self.nsmap[str(scounter)].append({'http://www.w3.org/XML/1998/namespace': 'xml'});
                    self.nsmap[str(scounter)].append({b: str(a)})
            elif event == 'start':
                #print 'start', lcounter
                if not self.nsmap.has_key(str(self.lcounter)) and self.nsmap.has_key(str(self.lcounter - 1)):
                    #print 'exist: ', nsmap[str(lcounter - 1)]
                    self.nsmap[str(self.lcounter)] = self.nsmap[str(self.lcounter - 1)];
                self.lcounter = self.lcounter + 1       

        self.ncounter = 0
        self.dic = self.xmlToDict(root)
        self.dic = {self.buildXmlnsKey(root.tag, 0): self.dic}
        return self.dic;

if __name__ == "__main__":
    xmlObj = XmlParser('xml7.xml');
    theXmlDictionary = xmlObj.parse()
    xmlObj.printDic(theXmlDictionary);

Suppose you have the xml as file:


<?xml version="1.0"?>
<lib:library
    xmlns:lib="http://eric.van-der-vlist.com/ns/library"
    xmlns:hr="http://eric.van-der-vlist.com/ns/person">
    <lib:book id="b0836217462" available="true">
        <lib:isbn>0836217462</lib:isbn>
        <lib:title xml:lang="en">Being a Dog Is a Full-Time Job</lib:title>
        <hr:author id="CMS">
            <hr:name>Charles M Schulz</hr:name>
            <hr:born>1922-11-26</hr:born>
            <hr:dead>2000-02-12</hr:dead>
        </hr:author>
        <lib:character id="PP">
            <hr:name>Peppermint Patty</hr:name>
            <hr:born>1966-08-22</hr:born>
            <lib:qualification>bold, brash and tomboyish</lib:qualification>
        </lib:character>
        <lib:character id="Snoopy">
            <hr:name>Snoopy</hr:name>
            <hr:born>1950-10-04</hr:born>
            <lib:qualification>extroverted beagle</lib:qualification>
        </lib:character>
        <lib:character id="Schroeder">
            <hr:name>Schroeder</hr:name>
            <hr:born>1951-05-30</hr:born>
            <lib:qualification>brought classical music to the Peanuts strip</lib:qualification>
        </lib:character>
        <lib:character id="Lucy">
            <hr:name>Lucy</hr:name>
            <hr:born>1952-03-03</hr:born>
            <lib:qualification>bossy, crabby and selfish</lib:qualification>
        </lib:character>
    </lib:book>
    <Purchase>
        <PurchaseId>AAAAA</PurchaseId>
        <PurchaseType>ONLINE</PurchaseType>
    </Purchase>
    <Purchase>
        <PurchaseId>BBBBB</PurchaseId>
        <PurchaseType>OFFLINE</PurchaseType>
    </Purchase>
    <Purchase paid='True'>
        <Purchase age='30'>
            <Purchase>HMM 1</Purchase>
            <Purchase>HMM 2</Purchase>
        </Purchase>
    </Purchase>
</lib:library>

Output would be like this:

lib:library {
   Purchase [
      0. {
         PurchaseId :  AAAAA
         PurchaseType :  ONLINE
      }
      1. {
         PurchaseId :  BBBBB
         PurchaseType :  OFFLINE
      }
      2. {
         <<attr>> {
            paid :  True
         }
         Purchase {
            <<attr>> {
               age :  30
            }
            Purchase [
                0. HMM 1
                1. HMM 2
            ]
         }
      }
   ]
   lib:book {
      hr:author {
         <<attr>> {
            id :  CMS
         }
         hr:name :  Charles M Schulz
         hr:born :  1922-11-26
         hr:dead :  2000-02-12
      }
      <<attr>> {
         available :  true
         id :  b0836217462
      }
      lib:title {
         <<attr>> {
            xml:lang :  en
         }
         <<value>> :  Being a Dog Is a Full-Time Job
      }
      lib:isbn :  0836217462
      lib:character [
         0. {
            <<attr>> {
               id :  PP
            }
            hr:name :  Peppermint Patty
            hr:born :  1966-08-22
            lib:qualification :  bold, brash and tomboyish
         }
         1. {
            <<attr>> {
               id :  Snoopy
            }
            hr:name :  Snoopy
            hr:born :  1950-10-04
            lib:qualification :  extroverted beagle
         }
         2. {
            <<attr>> {
               id :  Schroeder
            }
            hr:name :  Schroeder
            hr:born :  1951-05-30
            lib:qualification :  brought classical music to the Peanuts strip
         }
         3. {
            <<attr>> {
               id :  Lucy
            }
            hr:name :  Lucy
            hr:born :  1952-03-03
            lib:qualification :  bossy, crabby and selfish
         }
      ]
   }
}

Saturday, July 26, 2014

Quick Guide to Using MySQL in Python

If you are using windows 7 32 bit, then download the python-mysql-connector msi installer and install first.

MySqlPython.py


import MySQLdb
import sys

try :
    db = MySQLdb.connect(host="localhost", port=3306, user="root", passwd="", db="python")
except Exception:
    pass

if db is None:
    print("The database could not connect");
    sys.exit(0)
else:
    print("The database connected");
    cursor = db.cursor();

def executeQuery(query, params = None):
    return cursor.execute(query, params);

def listQuery(query, params = None):
    cursor.execute(query, params);
    return cursor.fetchall();

def readQuery(query, params = None):
    cursor.execute(query, params);
    return cursor.fetchone();

print("Deleting all user from database");
executeQuery("DELETE FROM USER");

print("Inserting user to database...");
query = """Insert into user(name, roll) values(%s, %s)""";
params = ('Pritom', '001');
result = executeQuery(query, params);
print("Insert result: " + str(result));

params = ('Sumon', '002');
result = executeQuery(query, params);
print("Insert result: " + str(result));

params = ('Liton', '003');
result = executeQuery(query, params);
print("Insert result: " + str(result));

print("Reading total user count...");
result = readQuery("SELECT COUNT(*) FROM user");
print("Total user: " + str(result[0]));

print("Reading total user list...");
result = listQuery("SELECT * FROM user");
for row in result:
    print("Id: " + str(row[0]) + ", Name: " + str(row[1]) + ", Roll: " + str(row[2]));

cursor.close();
db.close();   
sys.exit();

Output be as follows:


The database connected
Deleting all user from database
Inserting user to database...
Insert result: 1
Insert result: 1
Insert result: 1
Reading total user count...
Total user: 3
Reading total user list...
Id: 66, Name: Pritom, Roll: 001
Id: 67, Name: Sumon, Roll: 002
Id: 68, Name: Liton, Roll: 003

Check Null Value Using Python

fieldNullable = None
fieldNotNullable = 'Some Value'
fieldBoolean = False

if fieldNullable is None:
    print("fieldNullable is null/None")
else:
    print("fieldNullable is not null/None")

if fieldNotNullable is None:
    print("fieldNotNullable is null/None")
else:
    print("fieldNotNullable is not null/None")

if fieldBoolean is None:
    print("fieldBoolean is null/None")
else:
    print("fieldBoolean is not null/None")


class TheClass1:
    def __main__(self, theObject):
        self.theObject = theObject


theClass1 = TheClass1()
if theClass1 is None:
    print("theClass1 is null/None")
else:
    print("theClass1 is not null/None")
    
if theClass1 == None:
    print("theClass1 is null/None")
else:
    print("theClass1 is not null/None")






class TheClass2:
    def __eq__(self, theObject):
        self.theObject = theObject
        #Always return true when check using ==
        return True


theClass2 = TheClass2()
if theClass2 is None:
    print("theClass2 is null/None")
else:
    print("theClass2 is not null/None")
    
if theClass2 == None:
    print("theClass2 is null/None")
else:
    print("theClass2 is not null/None")   

fieldNullable is null/None
fieldNotNullable is not null/None
fieldBoolean is not null/None
theClass1 is not null/None
theClass1 is not null/None
theClass2 is not null/None
theClass2 is null/None 

Install python on windows 7

1. Download python stable version (2.7.8) msi installer from https://www.python.org/download/.

2. After download run the msi installer.

3. Open the Command Prompt. Start > All Programs > Accessories > Command Prompt and type
set path=%path%;C:\Python27\
and hit enter button.


4. Download simplejson from: http://pypi.python.org/pypi/simplejson or from: https://drive.google.com/file/d/0B5nZNPW48dpFNEJSeGJtT2dxX1U/edit?usp=sharing & then extract to any folder your want. Suppose I extracted into C:\simpleJson\ folder.

5. From Command Prompt go to C:\simpleJson\ folder using type: cd C:\simpleJson and then type:
python setup.py install
and hit enter.

6. Open directory at C:\Python27\Lib\site-packages\. You'll see simplejson-2.1.6-py2.7.egg has been added to the directory.

7. Now open IDLE (Start > All Programs > Python 2.7 > IDLE (Python GUI)) and type the following:
>>> import simplejson

8. See all that nothing that happens? Congratulations. You've just installed and imported your first package for Python on Windows 7. Repeat as needed.

9. Finally your are at the end to execute python program in your computer.