= Python =
Python is a programming language that lets you work more quickly and integrate your systems more effectively. 

Links:
 * [[http://docs.python.org/2/library/]]
 * [[http://www.tutorialspoint.com/python/]]

== Python3 ==
=== Install from SlackBuild ===
 * cd /tmp
 * wget http://slackbuilds.org/slackbuilds/13.1/development/python3.tar.gz
 * tar xvzf python3.tar.gz
 * cd python3
 * wget http://python.org/ftp/python/3.1.3/Python-3.1.3.tar.bz2
 * ./python3.SlackBuild
 * installpkg /tmp/python3-3.1.1-i486-1_SBo.tgz

== UTF-8 ==
At the start of source code files:
{{{#!highlight python
# -*- coding: utf-8 -*-
print u"Olá mundo" # hello world in portuguese
}}}

Letter '''á''' is encoded as hexadecimal '''0xC3A1''' in UTF-8 and as '''0x00E1''' in UTF-16.
[[http://www.fileformat.info/info/unicode/char/e1/index.htm]]

Letter '''ú''' is encoded  as hexadecimal '''0xC3BA''' in UTF-8, as '''0x00FA''' in UTF-16 and as '''0xFA''' in latin-1/ISO8859-1/CP-1252.

{{{#!highlight python
#utf-8 data
utf81 ='\xc3\xba' 
# convert from utf-8 to unicode
unic = utf81.decode('utf-8') 

for c in unic:
    print '%04X'%(ord(c))
# convert from unicode to latin1/ISO8859-1 CP-1252
lat1=unic.encode('latin1') 
for c in lat1:
    print '%02X'%(ord(c))
}}}

== Utf-8 and Unicode ==
{{{#!highlight python
utf81='\xe1\xb7\x97' #utf-8 data

unic = utf81.decode('utf-8') #converts from utf-8 to unicode (utf-16)
for c in unic:
    print '%04X'%(ord(c))
type(unic) # unicode, 2 bytes per char
type(utf81) # str, 1 bytes per char
}}}

== Time and date ==
{{{#!highlight python
import time
# get seconds since epoch until now in UTC to a string year-month-dayThour:minute:second
strutc = time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime() ) 
# get seconds since epoch until now  in localtime to a string  year-month-dayThour:minute:second
strlocal = time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime() ) 
# number seconds since epoch
nrSeconds = time.mktime(time.gmtime())
nrSeconds = time.time() #usually is UTC on all OS
# from timestamp to string date
import datetime
print(datetime.datetime.fromtimestamp(1284101485).strftime('%Y-%m-%d %H:%M:%S'))
#
def toUTCDateStr(timestamp):
    return datetime.datetime.utcfromtimestamp( timestamp ).strftime('%Y-%m-%d %H:%M:%S ')
# timedelta
import datetime
x=datetime.datetime.fromtimestamp(1284101485)
nowx=datetime.datetime.now()                    
ts=(nowx-x).total_seconds()
print int(ts)
### add , subtract days
import datetime
x = datetime.datetime(2014,1,2)
print x
y = x - datetime.timedelta(days=1)
print y
z = y - datetime.timedelta(days=1)
print z

### convert string with date to time tuple
import time
timeTuple = time.strptime('2014-01-01 10:11:12','%Y-%m-%d %H:%M:%S')
nrSecondsSince1970=time.mktime(timeTuple)
### get seconds since epoch
start = int(datetime.datetime(2014,3,9,0,0,0,0).strftime("%s"))*1000
end = int(datetime.datetime(2014,3,14,23,59,59,999).strftime("%s"))*1000

### string from utc current date
currutc=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
}}}

== Write and reading data for a plist file ==
A plist file stores data in XML format.

{{{#!highlight python
import plistlib
value = [{'key1':123,'key2':'asdf'},{'keyx1':'testing','keyz1':'yup'}]
# save value in plist file
plistlib.writePlist(value,'/tmp/plist1.plist')
o=plistlib.readPlist('/tmp/plist1.plist')
print o
}}}

Content of the file '''/tmp/plist1.plist'''
{{{#!highlight xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
        <dict>
                <key>key1</key>
                <integer>123</integer>
                <key>key2</key>
                <string>asdf</string>
        </dict>
        <dict>
                <key>keyx1</key>
                <string>testing</string>
                <key>keyz1</key>
                <string>yup</string>
        </dict>
</array>
</plist>
}}}

== Threading ==

{{{#!highlight python
#!/usr/bin/python
# timestable.py
# calculates the times table in concurrency
import threading
import time

class TimesTable (threading.Thread):
    def __init__(self, timesTable):
        threading.Thread.__init__(self) #required
        self.timeTable = timesTable
        self.count = 1
    def run(self):
        loop=True
        while loop:
            time.sleep(1) #sleep for 1 second
            result=self.timeTable * self.count
            print "%d*%d=%d"%(self.timeTable,self.count,result)
            if self.count<10:
	        self.count = self.count+1
	    else:
	        self.count=1

# create threads
timesTable2 = TimesTable(2)
timesTable5 = TimesTable(7)

# start the threads
timesTable2.start()
timesTable5.start()
}}}

== unit tests (pyunit) ==
{{{#!highlight python
#filename: unitTest.py
#Run tests: python unitTest.py
#Should fail on testAdd2
import unittest

class SimpleTestCase(unittest.TestCase):
    def setUp(self):
        self.res=4

    def tearDown(self):
        self.res=0

    def testAdd1(self):
        res=2+2
        self.assertEqual(self.res,res,'')       

    def testAdd2(self):
        res=2+3
        self.assertEqual(self.res,res,'')       

    def testAdd3(self):
        res=2+3
        self.assertNotEqual(self.res,res,'')       

if __name__ == '__main__':
    unittest.main()
}}}

== unit tests coverage (pyunit) ==
 * http://coverage.readthedocs.io/en/latest/cmd.html
 * pip install coverage
'''tests.py'''
{{{#!highlight python
import unittest
from impl import add 
from impl import sub

class TestCase(unittest.TestCase):
    def test_add(self):
        self.assertEqual(5,add(2,3))
#    def test_sub(self):
#        self.assertEqual(1,add(3,2))
if __name__ == '__main__':
    unittest.main()
}}}

'''impl.py'''
{{{#!highlight python
def add(arg1,arg2):
    return arg1+arg2

def sub(arg1,arg2):
    return arg-+arg2
}}}

Steps:
 * coverage erase
 * coverage run tests.py
 * coverage report -m impl.py
 * coverage html impl.py  #in htmlcov folder

== cython ==
Installation:
 * su
 * cd /tmp
 * wget http://cython.org/release/Cython-0.19.1.tar.gz
 * tar xvzf Cython-0.19.1.tar.gz 
 * cd Cython-0.19.1
 * python setup.py build
 * python setup.py install

== pymssql ==
Requires cython.
Installation:
 * su
 * cd /tmp
 * wget [[http://pymssql.googlecode.com/files/pymssql-2.0.0b1-dev-20111019.tar.gz]]
 * tar xvvzf pymssql-2.0.0b1-dev-20111019.tar.gz
 * cd pymssql-2.0.0b1-dev-20111019
 * python setup.py build
 * python setup.py install
 * python
 * import pymssql

== pywhois ==
Python module for retrieving WHOIS information of domains [[http://code.google.com/p/pywhois/]].

Fetch code with mercurial:
 * cd /tmp
 * hg clone https://code.google.com/p/pywhois/
 * cd pywhois
 * python setup.py build
 * python setup.py install

== Syntax highlighting on Vim for wsgi ==
Edit ~./.vimrc:
{{{#!highlight bash 
syntax on
filetype on
au BufNewFile,BufRead *.wsgi set filetype=python
}}}

== Get file modification time ==
{{{#!highlight python
import os
nrSeconds=os.path.getmtime('/folder/file') # unix epoch, nr secs since 1970-01-01 00:00:00
}}}

== Kill process with PID ==
{{{#!highlight python
import os
import signal
os.kill(pid,signal.SIGTERM) 
}}}

== Log message to /var/log/messages or /var/log/syslog ==
{{{#!highlight python
import syslog
syslog.syslog(syslog.LOG_INFO,'message to syslog')
}}}

{{{#!highlight python
import syslog

syslog.syslog(syslog.LOG_DEBUG,'DEBUG')
syslog.syslog(syslog.LOG_INFO,'INFO')
syslog.syslog(syslog.LOG_WARNING,'WARNING')
syslog.syslog(syslog.LOG_ERR,'ERR')
syslog.syslog(syslog.LOG_CRIT,'CRIT')
}}}

||OS||File||Logged levels||
||Slack64 14||/var/log/messages|| INFO ||
||Slack64 14||/var/log/syslog|| WARNING ERR CRIT ||
||CentOS 6.4||/var/log/messages|| INFO WARNING ERR CRIT ||
||Ubuntu 12.04 Precise||/var/log/syslog||DEBUG INFO WARNING ERR CRIT ||
||Debian 7.0 Wheezy||/var/log/syslog||DEBUG INFO WARNING ERR CRIT ||

== Simple process monitor ==
{{{#!highlight python
# example service, dummyService.py
import time
while True:
    try:
        f=open('filex.log','wb')
        f.write('%f'%(time.time()))
        f.close() 
        time.sleep(5)
    except KeyboardInterrupt:
        quit()
}}}

{{{#!highlight python
#service monitor.py
# * * * * * /usr/bin/python /home/vitor/svc/monitor.py
import os
import syslog
import subprocess

if __name__=="__main__":
    files = os.listdir('/proc')
    script="dummyService.py"
    prefix="dummyService:"

    svcRunning=False

    for file in files:

        if file.isdigit():
            cmdline = open('/proc/%s/cmdline'%(file),'r').read()
            proc = "%s %s "%(file,cmdline)

            if script  in proc :
                svcRunning=True

    if svcRunning==False:
        syslog.syslog(syslog.LOG_INFO,'%s process created '%(prefix) )
        # run exec process on python Popen
        subprocess.Popen(['/usr/bin/python','dummyService.py'],cwd='/home/userx/svc')
}}}

== Read URL content ==
{{{#!highlight python
import urllib2
r=urllib2.urlopen('http://www.sapo.pt')
resp=r.read()
print resp
}}}

== Windows service ==
Install http://sourceforge.net/projects/pywin32/files/pywin32/Build%20218/pywin32-218.win32-py2.5.exe/download for Python 2.5 for Windows.

Adapted from http://ryrobes.com/python/running-python-scripts-as-a-windows-service/
{{{#!highlight python
# C:\cmt\test>python testWinSvc.py install
# C:\cmt\test>python testWinSvc.py start
# C:\cmt\test>python testWinSvc.py stop
# C:\cmt\test>python testWinSvc.py remove
# C:\cmt\test>service.msc
import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os, sys, string, time

class aservice(win32serviceutil.ServiceFramework):
   _svc_name_ = "MyServiceShortName"
   _svc_display_name_ = "My Service Long Fancy Name!"
   _svc_description_ = "THis is what my crazy little service does - aka a DESCRIPTION! WHoa!"
         
   def __init__(self, args):
           win32serviceutil.ServiceFramework.__init__(self, args)
           self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)           

   def SvcStop(self):
           self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
           win32event.SetEvent(self.hWaitStop)                    
         
   def SvcDoRun(self):
      import servicemanager      
      servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))       
      self.timeout = 1000     #1 second
      # This is how long the service will wait to run / refresh itself (see script below)

      while 1:
         # Wait for service stop signal, if I timeout, loop again
         rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
         # Check to see if self.hWaitStop happened
         if rc == win32event.WAIT_OBJECT_0:
            # Stop signal encountered
            servicemanager.LogInfoMsg("SomeShortNameVersion - STOPPED!")  #For Event Log
            break
         else:
                 try:
                     handle = open('c:/windows/temp/outx1.txt', "a")
                     handle.write('%s \n' % ('tst svc'))
                     handle.close()
                 except:
                     pass



def ctrlHandler(ctrlType):
   return True
                  
if __name__ == '__main__':   
   win32api.SetConsoleCtrlHandler(ctrlHandler, True)   
   win32serviceutil.HandleCommandLine(aservice)
}}}

== Logging ==
Adapted from http://docs.python.org/2/howto/logging-cookbook.html#logging-to-multiple-destinations

{{{#!highlight python
import logging

logging.basicConfig(level=logging.DEBUG, \
    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', \
    datefmt='%Y-%m-%d %H:%M:%S %z', \
    filename='/tmp/myapp.log', \
    filemode='a')
#log to console
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
logging.info('Jackdaws love my big sphinx of quartz.')
#other loggers
logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger(__name__)
logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')
}}}


Copied from http://docs.python.org/2/howto/logging-cookbook.html#configuration-server-example
Logging configurable on runtime, receiving new parameters by socket

Server:
{{{#!highlight python
import logging
import logging.config
import time
import os

# read initial config file
logging.config.fileConfig('logging.conf')

# create and start listener on port 9999
t = logging.config.listen(9999)
t.start()

logger = logging.getLogger('simpleExample')

try:
    # loop through logging calls to see the difference
    # new configurations make, until Ctrl+C is pressed
    while True:
        logger.debug('debug message')
        logger.info('info message')
        logger.warn('warn message')
        logger.error('error message')
        logger.critical('critical message')
        time.sleep(5)
except KeyboardInterrupt:
    # cleanup
    logging.config.stopListening()
    t.join()
}}}

Client:
{{{#!highlight python
#!/usr/bin/env python
import socket, sys, struct

with open(sys.argv[1], 'rb') as f:
    data_to_send = f.read()

HOST = 'localhost'
PORT = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('connecting...')
s.connect((HOST, PORT))
print('sending config...')
s.send(struct.pack('>L', len(data_to_send)))
s.send(data_to_send)
s.close()
print('complete')
}}}

== Logging with date rotation and email ==
{{{#!highlight python
import logging
from logging.handlers import TimedRotatingFileHandler
from logging.handlers import SMTPHandler

class Logger(object):
    def __init__(self):
        loggerName='appx'
        logFormatter = logging.Formatter('[%(asctime)s %(name)-12s %(levelname)-8s] %(message)s',datefmt='%Y-%m-%d %H:%M:%S %z')
        
        rotatingLogHandler = TimedRotatingFileHandler('%s.log'%(loggerName),when='midnight',utc=True)
        rotatingLogHandler.setFormatter(logFormatter)
        rotatingLogHandler.setLevel(logging.DEBUG)

        smtpLogHandler = SMTPHandler(mailhost='hostx.com', fromaddr='mail@hostx.com', toaddrs='dest@hostx.com', subject=loggerName, \
            credentials=('accountx','12345678'), secure=None)
        smtpLogHandler.setFormatter(logFormatter)
        smtpLogHandler.setLevel(logging.WARNING)
                        
        self.logger = logging.getLogger(loggerName)
        self.logger.addHandler(rotatingLogHandler)
        self.logger.addHandler(smtpLogHandler)
        self.logger.setLevel(logging.DEBUG)
        
    def debug(self,m):
        self.logger.debug(m)

    def info(self,m):
        self.logger.info(m)

    def warning(self,m):
        self.logger.warning(m)
        
    def error(self,m):
        self.logger.error(m)
        
    def critical(self,m):
        self.logger.critical(m)

}}}

== pytz - World Timezone Definitions for Python ==
{{{#!highlight python

import pytz
import datetime

#fmt = '%Y-%m-%d %H:%M:%S %Z%z'
london=pytz.timezone('Europe/London')
lisbon=pytz.timezone('Europe/Lisbon')
paris  =pytz.timezone('Europe/Paris')
utc = pytz.timezone('UTC')
berlin = pytz.timezone('Europe/Berlin')

print
dtx = datetime.datetime(2002, 12, 27, 12, 0, 0,tzinfo=utc ) 
print 
print 'UTC    ' , dtx
print 'London ',dtx.astimezone(london)
print 'Lisbon ',dtx.astimezone(lisbon)
print 'Paris  ',dtx.astimezone(paris)
print 'Berlin ',dtx.astimezone(berlin)


print
dty = datetime.datetime(2002, 8, 27, 13, 0, 0,tzinfo=utc ) 
print 'UTC    ',dty
print 'London ',dty.astimezone(london)
print 'Lisbon ',dty.astimezone(lisbon)
print 'Paris  ',dty.astimezone(paris)
print 'Berlin ',dty.astimezone(berlin)
}}}

== Sort example ==
{{{#!highlight python
class Acme(object):
    def __init__(self,valuex):
        self.valuex=valuex

    def __cmp__(self, other):
        if self.valuex==other.valuex:
            return 0
        if self.valuex>other.valuex:
            return 1
        if self.valuex<other.valuex:
            return -1

    def __repr__(self):
        return self.valuex

if __name__=='__main__':
    listx=[]
    listx.append( Acme('C') )
    listx.append( Acme('A') )
    listx.append( Acme('Z') )
    print 'original:',listx
    print 'sorted asc:',sorted(listx,reverse=False)
    print 'sorted desc:',sorted(listx,reverse=True)
    listx.sort()
    print 'sort:',listx
    listx.reverse()
    print 'reverse:',listx
}}}

== sys.path ==
With sys.path we are able to add new folders which serve as base path to modules. The idea is similar to classpath in Java.

Create file /tmp/testex.py
{{{#!highlight python
def test1()
    print("Alright !!!!")
}}}

Run python interactively:
{{{#!highlight bash
vitor@darkstar[~]$python
Python 2.7.3 (default, Jul  3 2012, 21:16:07) 
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import testex
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named testex
>>> import sys
>>> sys.path.append('/tmp') # add /tmp to modules paths
>>> testex.test1()
Allright !!!!
}}}

By default on Linux systems, modules are also searched in '''~/.local/pythonXX.YY/site-packages'''. If the file testex.py would exist, it would also be used without being necessary to add the folder /tmp to the '''sys.path''' . For the user vitor and python 2.7 the path would be '''/home/vitor/.local/lib/python2.7/site-packages''' .

== Math ==
{{{#!highlight python
# round float to 6 decimal places
nr=1.23456789
print round(nr,6)
# round float to 4 decimal places
nr=1.23456789
print round(nr,4)
}}}

== SMTP ==
{{{#!highlight python
import smtplib

class Smtp(object):
    def __init__(self,server,port,user,passx,appName):
        self.server=server
        self.port=port
        self.user=user
        self.passx=passx
        self.appName=appName

    def send(self,fromx,tox,subjectx,contentType,body):
        s=smtplib.SMTP()
        s.connect(self.server,self.port)
        s.ehlo(self.appName)
        s.login(self.user,self.passx)         
        msg='From: %s\r\nTo: %s\r\nSubject: %s\r\nContent-Type: %s\r\n\r\n%s'%(fromx,tox,subjectx,contentType,body)
        s.sendmail(fromx,tox,msg)
        s.quit()

s=Smtp('mail.example.org',25,'norep@example.org','12345678','TestApp')
# content types: text/plain text/html
s.send('norep@example.org',['user1@example.org','user2@example.org'],'Subject','text/plain','Body mail')
}}}

== JSON serializable ==
{{{#!highlight python
from json import JSONEncoder
import json

class EncoderJSON(JSONEncoder):
    def default(self, o):
        return o.getJSONDict()    
    
class Acme(object):
    def __init__(self,paramx,valuex):
        self.paramx=paramx
        self.valuex=valuex
        
    def getJSONDict(self):
        return {'paramx':self.paramx ,'valuex':self.valuex}
                
    def __repr__(self):
        return '%s %s %s'%(self.paramx ,self.valuex)

if __name__=='__main__':
    listx=[]
    listx.append(Acme('p1','v1'))
    listx.append(Acme('p2','v2'))
    # json.dumps likes lists and dictionaries
    print( json.dumps(listx,cls=EncoderJSON) ) 
    # output [{"valuex": "v1", "paramx": "p1"}, {"valuex": "v2", "paramx": "p2"}]

}}}

== Decorators ==
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/

Wraps functions with functionality, before, after , changes parameters of the decorated function.

{{{#!highlight python
#decorator logger
def logger(func):
        def inner(*args, **kwargs): # step 1
            print "Arguments were: %s, %s" % (args, kwargs)
            return func(*args, **kwargs) # step 2
        return inner

@logger
def foo1(x, y=1):
    return x * y
if __name__=='__main__':
    foo1(2,2)
}}}

{{{#!highlight python
import sys
from os import path 
#decorator adder
def add_one_args(decoratedFunction):
    def interceptor(*args, **kwargs): # step 1
        print "Arguments were: args %s, kwargs %s" % (args, kwargs)
        index=0
        new_args=[]
        # change input 
        isnotint=False
        for arg in args:
            if isinstance(arg,int):                    
                new_args.append(arg+1)
            else:
                isnotint=True
            index=index+1
                
        if isnotint==False: 
            args=tuple(new_args)

        new_kwargs={}
        for kwarg in kwargs:
            new_kwargs[kwarg] = kwargs[kwarg]+1

        decoratedFuncReturn =  decoratedFunction(*args, **new_kwargs) # step 2
        # change output
        decoratedFuncReturn = decoratedFuncReturn * 2
        return decoratedFuncReturn
    return interceptor

@add_one_args
def foo1(x, y=1):
    print('Got arguments in foo1 %d %d'%(x,y))
    mul = x * y
    print('Multiplication result inside function %d'%(mul))
    return mul

def foo2(**kwargs):
    print('Got arguments in foo1 %d %d'%( kwargs['x'] , kwargs['y']  ))
    mul = kwargs['x'] * kwargs['y']
    print('Multiplication result inside function %d'%(mul))
    return mul

if __name__=='__main__':
    print( foo1(2,y=2) )
    print( foo2( x=3,y=3  ) )
    print( sys.path )
    print( sys.prefix )
    print( sys.exec_prefix )
    here = path.abspath(path.dirname(__file__))
    print(here)
}}}

{{{#!highlight python
def add_one_args(decoratedFunction,xyz=1):
    def interceptor(*args, **kwargs):
        print "Arguments were: args %s, kwargs %s" % (args, kwargs)
        new_args=[]

        for arg in args:
            if isinstance(arg,int): new_args.append(arg+1)
            else: new_args.append(arg)

        args=tuple(new_args)
        decoratedFuncReturn = decoratedFunction(*args, **kwargs) 
        return decoratedFuncReturn
    return interceptor

def subtract_one_args(decoratedFunction,xyz=1):
    def interceptor(*args, **kwargs):
        print "Arguments were: args %s, kwargs %s" % (args, kwargs)
        new_args=[]

        for arg in args:
            if isinstance(arg,int): new_args.append(arg-1)
            else: new_args.append(arg)

        args=tuple(new_args)
        decoratedFuncReturn = decoratedFunction(*args, **kwargs) 
        return decoratedFuncReturn
    return interceptor

class Acme(object):
    def __init__(self):
        self.a = 0
        self.b = 0

    @add_one_args
    @subtract_one_args
    def set(self,a,b):
        self.a=a
        self.b=b
        print self.a, self.b

if __name__=='__main__':
    x = Acme()
    x.set(0,1)
}}}


== Dictionaries ==
{{{#!highlight python
Type "help", "copyright", "credits" or "license" for more information.
a={} #create dictionary
if 'keyx' in a: print 'ok' #check if key keyx exists
a['keyx']=1 # create key keyx with value 1
if 'keyx' in a: print 'ok' #checks  keyx existence
a['key2']={} # create key key2
if 'keyx' in a['key2']: print 'ok' # check if keyx exists in key2
a['key2']['keyx']=3 # create keyx inside key2
if 'keyx' in a['key2']: print 'ok'
}}}

== Exceptions ==
Example 1
{{{#!highlight python
def dummyMethod(datax):
    try:
        if 'key1' in datax:
            key1 = datax['key1']

            if 'key2' in key1:
                key2 = dvi['key2']
                print('key2 is %s '%(key2))                    

    except Exception,ex:
        print('Error in dummyMethod %s'%( str(ex) ) )
}}}

== Install pip ==
 * easy_install pip


== Regular expressions ==
{{{#!python
import re
listx=['aa12.txt','a123sss.txt','bs11bb.txt','123aaaa.sql','a12.txt','aaaa12','20ghj','6657']

for item in listx:
    res = re.match('^(\D+)(\d+)(\D+)$',item)
    if res!=None:
        decimal =  int(res.group(2)) + 1
        print('A: mv %s %s%d%s'%(item , res.group(1),decimal , res.group(3) ))
    res = re.match('^(\d+)(\D+)$',item)
    if res!=None:
        decimal =  int(res.group(1)) + 1
        print('B: mv %s %d%s'%(item , decimal,res.group(2)))
    res = re.match('^(\D+)(\d+)$',item)
    if res!=None:
        decimal =  int(res.group(2)) + 1
        print('C: mv %s  %s%d'%(item , res.group(1),decimal))
    res = re.match('^(\d+)$',item)
    if res!=None:
        decimal =  int(res.group(1)) + 1
        print('D: mv %s  %d'%(item , decimal))
'''
#ouput
 python addNumber.py 
A: mv aa12.txt aa13.txt
A: mv a123sss.txt a124sss.txt
A: mv bs11bb.txt bs12bb.txt
B: mv 123aaaa.sql 124aaaa.sql
A: mv a12.txt a13.txt
C: mv aaaa12  aaaa13
B: mv 20ghj 21ghj
D: mv 6657  6658
'''
}}}

== Command line arguments ==
 * import sys
{{{#!highlight python
import sys
# ...
if __name__=='__main__':
    intArgA = int(sys.argv[1])
    intArgB = int(sys.argv[2])
    intArgC = int(sys.argv[3])

}}}

== Run python code in shell without source code file .py ==
{{{#!highlight sh
echo "<log type="typex" other="bbbbb" msgId="bbbbbb" >" | xargs -i echo -e "strx='{}'\nsplitted = strx.split(' ')\nfor spl in splitted:\n\tif 'msgId' in spl:\n\t\tprint spl.split('=')[1]"  | python                         
}}}

{{{#!highlight python
strx='{}' # where xargs value is used
splitted = strx.split(' ')
for spl in splitted:
    if 'msgId' in spl:
        print spl.split('=')[1]
}}}

== Alias identifier inside module ==
{{{
>>> from json import dumps as jsonDumps
>>> jsonDumps({'key':'value'})
'{"key": "value"}'
>>> print jsonDumps({'key':'value' , 'key2':1234})
{"key2": 1234, "key": "value"}
>>> 
}}}

== Execute process using subprocess ==
{{{#!highlight python
import subprocess 

if __name__=='__main__':
  try:
    args=[]
    args.append('ls')
    args.append('/tmpx')
    out = subprocess.check_output(args, stderr=subprocess.STDOUT, shell=False)
    print( out )
  except subprocess.CalledProcessError as ex1:
    print("Exception command: %s"%(ex1.cmd)  )    
    print("Exception code: %s"%(ex1.returncode)  )    
    print("Exception output: %s"%(ex1.output)  )
  except Exception as ex2:
    print("General exception %s"%(ex2.message) )    
}}}

== Recursion ==
{{{#!highlight python   
def factorial(n):
  if n > 0: return n * factorial(n-1)
  else: return 1

def fibo(n):
  if n==0: return 0
  if n==1: return 1
  if n>1: return fibo(n-1) + fibo(n-2)

def sum(idx,lista):
  if idx<0: return 0
  if idx>=0: return lista[idx] + sum(idx-1,lista)

if __name__=='__main__':
  print( factorial(4) )
  print( fibo(3) )
  lst=[1,2,3,4]
  print( sum(len(lst)-1,lst) )


}}}

== Python3 virtual environment ==
 * https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments

Python “Virtual Environments” allow Python packages to be installed in an isolated location for a particular application, rather than being installed globally.

You can deactivate a virtual environment by typing “deactivate” in your shell.

{{{#!highlight bash
sudo apt-get install python3-venv
python3 -m venv testVenv
source testVenv/bin/activate
pip install --upgrade pip
pip install -i https://testpypi.python.org/pypi vbhelloworld
pip uninstall vbhelloworld
}}}

 * https://docs.python.org/3/library/venv.html#module-venv

When a virtual environment is active (i.e., the virtual environment’s Python interpreter is running), the attributes sys.prefix and sys.exec_prefix point to the base directory of the virtual environment, whereas sys.base_prefix and sys.base_exec_prefix point to the non-virtual environment Python installation which was used to create the virtual environment. 

If a virtual environment is not active, then sys.prefix is the same as sys.base_prefix and sys.exec_prefix is the same as sys.base_exec_prefix (they all point to a non-virtual environment Python installation).

== List Comprehensions ==
{{{#!highlight python
squares = [x**2 for x in range(10)]
# squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
sequence = [x for x in range(1,9)]
# sequence [1, 2, 3, 4, 5, 6, 7, 8]
range(10)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
squares = [x**2 for x in range(10) if x<= 4]
# squares [0, 1, 4, 9, 16]
}}}

== Python 2 virtual env ==
{{{#!highlight sh
# in windows with powershell
cd ~
pip install virtualenv --user
# add the scripts folder to the user path c:\users\userx\AppData\Roaming\Python\Scripts 
# using cmd
cd %HOME_PATH%
mkdir tmp
cd tmp
virtualenv.exe test_env
cd test_env
Scripts\activate.bat
}}}

{{{#!highlight python
import sys
sys.path
}}}

== Generator functions yield ==
{{{#!highlight python
#test yield
def factorial(val):
    v=val
    res=1
    while v>1:
        res=res*v
        v=v-1
        yield None # v
    yield res

if __name__ == "__main__":
    for i in factorial(3):
        print(i)

    f = factorial(4)
    iterate=True

    while iterate:
        try:
            print( f.next() )
            #print( next(f) )
        except StopIteration as ex:
            iterate=False
}}}

'''Output:'''
{{{#!highlight bash
python test_yeld.py 
None
None
6
None
None
None
24
}}}

== XML parse with minidom ==
 * https://docs.python.org/2/library/xml.dom.minidom.html
{{{#!highlight python
from xml.dom import minidom
print("Hello world")
a="<a><b>aaaa</b><b>bbcc</b></a>"
doc = minidom.parseString(a) # return document 
tags = doc.getElementsByTagName("b")
for t in tags:
  print(t.firstChild.nodeValue)

}}}

== Environment variable ==
{{{#!highlight python
import os
print(os.environ)
}}}


== XSD and objectify XML ==
'''test.py'''
{{{#!highlight python
from lxml import objectify
from StringIO import StringIO
from lxml import etree

f = open('test.xsd')
xsddata=''
for line in f:
    xsddata = xsddata +  line
f.close()

xmlschema_doc = etree.parse(StringIO(xsddata))
xmlschema = etree.XMLSchema(xmlschema_doc)
print xmlschema 

parser = objectify.makeparser(schema=xmlschema)
xml = "<a><b>test</b></a>"
a = objectify.fromstring(xml, parser)
print(a.b)
}}}

'''test.xsd'''
{{{#!highlight xml
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="a" type="AType"/>
  <xsd:complexType name="AType">
       <xsd:sequence>
         <xsd:element name="b" type="xsd:string" />
       </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
}}}

'''Other example ....'''
{{{
@startuml
class AType
class BType
class AcmeType

/' 
<|--  extends 
-- association
..> depends 
<|.. implements / realizes
'/ 

AType : b : String
AType : xx : BType
AcmeType : other : int
BType : c : String

AType ..> BType
AcmeType --|> AType
@enduml
}}}

{{attachment:classes.png}}

'''acme.xsd'''
{{{#!highlight xml
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:bit="http://bitarus.allowed.org/types" 
    xmlns:ot="http://bitarus.allowed.org/othertypes" 
    targetNamespace="http://bitarus.allowed.org/othertypes" 
    elementFormDefault="qualified"
  >
  <xs:import schemaLocation="test.xsd" />  
  <xs:complexType name="AcmeType">  
    <xs:complexContent>
        <xs:extension base="bit:AType">
            <xs:sequence>
                <xs:element name="other" type="xs:int"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>      
  </xs:complexType>
  <xs:element name="at" type="ot:AcmeType"/>
</xs:schema>
}}}

'''test.xsd'''
{{{#!highlight xml
<xsd:schema 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:bit="http://bitarus.allowed.org/types" 
    targetNamespace="http://bitarus.allowed.org/types" 
    elementFormDefault="qualified" 
>
  <xsd:complexType name="AType">
       <xsd:sequence>
         <xsd:element name="b" type="xsd:string" />         
         <xsd:element name="xx" type="bit:BType" />
       </xsd:sequence>
  </xsd:complexType>  
  <xsd:complexType name="BType">
      <xsd:sequence>
        <xsd:element name="c" type="xsd:string" />
      </xsd:sequence>
  </xsd:complexType>  
  <xsd:element name="a" type="bit:AType"/>
</xsd:schema>
}}}

'''acme.py'''
{{{#!highlight python
from lxml import objectify
from StringIO import StringIO
from lxml import etree
import os
f = open('acme.xsd')
xsddata=''
for line in f:
    xsddata = xsddata +  line
f.close()

xmlschema = etree.XMLSchema(etree.parse(StringIO(xsddata)))

xml = """
<ot:at xmlns:ot="http://bitarus.allowed.org/othertypes" 
  xsi:schemaLocation="http://bitarus.allowed.org/othertypes other.xsd" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:bit="http://bitarus.allowed.org/types" >
  <bit:b>cccc</bit:b>
  <bit:xx><bit:c>hhjjkk</bit:c></bit:xx>
  <ot:other>1234</ot:other>  
</ot:at>
"""

doc = etree.fromstring(xml, etree.XMLParser(schema=xmlschema) )
# https://lxml.de/api/lxml.etree._Element-class.html
bNode = doc.getchildren()[0]
otherNode = doc.getchildren()[2]
print bNode.text
print otherNode.text
}}}

== Debugger ==
{{{#!highlight python
# python test.py
# DEBUG=on python test.py  
import os
# ....
if 'DEBUG' in os.environ:
  # breakpoint 
  import pdb; pdb.set_trace()

# commands
# c continue
# q quit
# n next
# l list
# s step
}}}

== Install package in user home folder pointing to other libs ==
 * rpm -ql rpmpackage # check installed RPM package contents
 * pip install  --user --global-option=build_ext --global-option="-L/usr/lib64" package_to_install

== Atom syndication ==
 * https://tools.ietf.org/html/rfc4287
 * https://www.iana.org/assignments/xml-registry/schema/vcard-4.0.xsd 
 * https://www.iana.org/assignments/xml-registry/xml-registry.xhtml

== Identify current function ==
{{{#!highlight python
import inspect
print 'In %s'%( inspect.getframeinfo(inspect.currentframe()).function )
}}}

== Operative system folder path separator ==
{{{#!highlight python
import os
print( os.sep )
help(os)
dir(os)
}}}

== Install package for user with a more recent version ==
{{{#!highlight bash
pip freeze
pip install --user  numpy --upgrade
pip freeze
}}}