Python

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

Links:

Python3

Install from SlackBuild

UTF-8

At the start of source code files:

   1 # -*- coding: utf-8 -*-
   2 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.

   1 #utf-8 data
   2 utf81 ='\xc3\xba' 
   3 # convert from utf-8 to unicode
   4 unic = utf81.decode('utf-8') 
   5 
   6 for c in unic:
   7     print '%04X'%(ord(c))
   8 # convert from unicode to latin1/ISO8859-1 CP-1252
   9 lat1=unic.encode('latin1') 
  10 for c in lat1:
  11     print '%02X'%(ord(c))

Utf-8 and Unicode

   1 utf81='\xe1\xb7\x97' #utf-8 data
   2 
   3 unic = utf81.decode('utf-8') #converts from utf-8 to unicode (utf-16)
   4 for c in unic:
   5     print '%04X'%(ord(c))
   6 type(unic) # unicode, 2 bytes per char
   7 type(utf81) # str, 1 bytes per char

Time and date

   1 import time
   2 # get seconds since epoch until now in UTC to a string year-month-dayThour:minute:second
   3 strutc = time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime() ) 
   4 # get seconds since epoch until now  in localtime to a string  year-month-dayThour:minute:second
   5 strlocal = time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime() ) 
   6 # number seconds since epoch
   7 nrSeconds = time.mktime(time.gmtime())
   8 nrSeconds = time.time() #usually is UTC on all OS
   9 # from timestamp to string date
  10 import datetime
  11 print(datetime.datetime.fromtimestamp(1284101485).strftime('%Y-%m-%d %H:%M:%S'))
  12 #
  13 def toUTCDateStr(timestamp):
  14     return datetime.datetime.utcfromtimestamp( timestamp ).strftime('%Y-%m-%d %H:%M:%S ')
  15 # timedelta
  16 import datetime
  17 x=datetime.datetime.fromtimestamp(1284101485)
  18 nowx=datetime.datetime.now()                    
  19 ts=(nowx-x).total_seconds()
  20 print int(ts)
  21 ### add , subtract days
  22 import datetime
  23 x = datetime.datetime(2014,1,2)
  24 print x
  25 y = x - datetime.timedelta(days=1)
  26 print y
  27 z = y - datetime.timedelta(days=1)
  28 print z
  29 
  30 ### convert string with date to time tuple
  31 import time
  32 timeTuple = time.strptime('2014-01-01 10:11:12','%Y-%m-%d %H:%M:%S')
  33 nrSecondsSince1970=time.mktime(timeTuple)
  34 ### get seconds since epoch
  35 start = int(datetime.datetime(2014,3,9,0,0,0,0).strftime("%s"))*1000
  36 end = int(datetime.datetime(2014,3,14,23,59,59,999).strftime("%s"))*1000
  37 
  38 ### string from utc current date
  39 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.

   1 import plistlib
   2 value = [{'key1':123,'key2':'asdf'},{'keyx1':'testing','keyz1':'yup'}]
   3 # save value in plist file
   4 plistlib.writePlist(value,'/tmp/plist1.plist')
   5 o=plistlib.readPlist('/tmp/plist1.plist')
   6 print o

Content of the file /tmp/plist1.plist

   1 <?xml version="1.0" encoding="UTF-8"?>
   2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
   3 <plist version="1.0">
   4 <array>
   5         <dict>
   6                 <key>key1</key>
   7                 <integer>123</integer>
   8                 <key>key2</key>
   9                 <string>asdf</string>
  10         </dict>
  11         <dict>
  12                 <key>keyx1</key>
  13                 <string>testing</string>
  14                 <key>keyz1</key>
  15                 <string>yup</string>
  16         </dict>
  17 </array>
  18 </plist>

Threading

   1 #!/usr/bin/python
   2 # timestable.py
   3 # calculates the times table in concurrency
   4 import threading
   5 import time
   6 
   7 class TimesTable (threading.Thread):
   8     def __init__(self, timesTable):
   9         threading.Thread.__init__(self) #required
  10         self.timeTable = timesTable
  11         self.count = 1
  12     def run(self):
  13         loop=True
  14         while loop:
  15             time.sleep(1) #sleep for 1 second
  16             result=self.timeTable * self.count
  17             print "%d*%d=%d"%(self.timeTable,self.count,result)
  18             if self.count<10:
  19                 self.count = self.count+1
  20             else:
  21                 self.count=1
  22 
  23 # create threads
  24 timesTable2 = TimesTable(2)
  25 timesTable5 = TimesTable(7)
  26 
  27 # start the threads
  28 timesTable2.start()
  29 timesTable5.start()

unit tests (pyunit)

   1 #filename: unitTest.py
   2 #Run tests: python unitTest.py
   3 #Should fail on testAdd2
   4 import unittest
   5 
   6 class SimpleTestCase(unittest.TestCase):
   7     def setUp(self):
   8         self.res=4
   9 
  10     def tearDown(self):
  11         self.res=0
  12 
  13     def testAdd1(self):
  14         res=2+2
  15         self.assertEqual(self.res,res,'')       
  16 
  17     def testAdd2(self):
  18         res=2+3
  19         self.assertEqual(self.res,res,'')       
  20 
  21     def testAdd3(self):
  22         res=2+3
  23         self.assertNotEqual(self.res,res,'')       
  24 
  25 if __name__ == '__main__':
  26     unittest.main()

unit tests coverage (pyunit)

tests.py

   1 import unittest
   2 from impl import add 
   3 from impl import sub
   4 
   5 class TestCase(unittest.TestCase):
   6     def test_add(self):
   7         self.assertEqual(5,add(2,3))
   8 #    def test_sub(self):
   9 #        self.assertEqual(1,add(3,2))
  10 if __name__ == '__main__':
  11     unittest.main()

impl.py

   1 def add(arg1,arg2):
   2     return arg1+arg2
   3 
   4 def sub(arg1,arg2):
   5     return arg-+arg2

Steps:

cython

Installation:

pymssql

Requires cython. Installation:

pywhois

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

Fetch code with mercurial:

Syntax highlighting on Vim for wsgi

Edit ~./.vimrc:

   1 syntax on
   2 filetype on
   3 au BufNewFile,BufRead *.wsgi set filetype=python

Get file modification time

   1 import os
   2 nrSeconds=os.path.getmtime('/folder/file') # unix epoch, nr secs since 1970-01-01 00:00:00

Kill process with PID

   1 import os
   2 import signal
   3 os.kill(pid,signal.SIGTERM) 

Log message to /var/log/messages or /var/log/syslog

   1 import syslog
   2 syslog.syslog(syslog.LOG_INFO,'message to syslog')

   1 import syslog
   2 
   3 syslog.syslog(syslog.LOG_DEBUG,'DEBUG')
   4 syslog.syslog(syslog.LOG_INFO,'INFO')
   5 syslog.syslog(syslog.LOG_WARNING,'WARNING')
   6 syslog.syslog(syslog.LOG_ERR,'ERR')
   7 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

   1 # example service, dummyService.py
   2 import time
   3 while True:
   4     try:
   5         f=open('filex.log','wb')
   6         f.write('%f'%(time.time()))
   7         f.close() 
   8         time.sleep(5)
   9     except KeyboardInterrupt:
  10         quit()

   1 #service monitor.py
   2 # * * * * * /usr/bin/python /home/vitor/svc/monitor.py
   3 import os
   4 import syslog
   5 import subprocess
   6 
   7 if __name__=="__main__":
   8     files = os.listdir('/proc')
   9     script="dummyService.py"
  10     prefix="dummyService:"
  11 
  12     svcRunning=False
  13 
  14     for file in files:
  15 
  16         if file.isdigit():
  17             cmdline = open('/proc/%s/cmdline'%(file),'r').read()
  18             proc = "%s %s "%(file,cmdline)
  19 
  20             if script  in proc :
  21                 svcRunning=True
  22 
  23     if svcRunning==False:
  24         syslog.syslog(syslog.LOG_INFO,'%s process created '%(prefix) )
  25         # run exec process on python Popen
  26         subprocess.Popen(['/usr/bin/python','dummyService.py'],cwd='/home/userx/svc')

Read URL content

   1 import urllib2
   2 r=urllib2.urlopen('http://www.sapo.pt')
   3 resp=r.read()
   4 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/

   1 # C:\cmt\test>python testWinSvc.py install
   2 # C:\cmt\test>python testWinSvc.py start
   3 # C:\cmt\test>python testWinSvc.py stop
   4 # C:\cmt\test>python testWinSvc.py remove
   5 # C:\cmt\test>service.msc
   6 import win32service
   7 import win32serviceutil
   8 import win32api
   9 import win32con
  10 import win32event
  11 import win32evtlogutil
  12 import os, sys, string, time
  13 
  14 class aservice(win32serviceutil.ServiceFramework):
  15    _svc_name_ = "MyServiceShortName"
  16    _svc_display_name_ = "My Service Long Fancy Name!"
  17    _svc_description_ = "THis is what my crazy little service does - aka a DESCRIPTION! WHoa!"
  18          
  19    def __init__(self, args):
  20            win32serviceutil.ServiceFramework.__init__(self, args)
  21            self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)           
  22 
  23    def SvcStop(self):
  24            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  25            win32event.SetEvent(self.hWaitStop)                    
  26          
  27    def SvcDoRun(self):
  28       import servicemanager      
  29       servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))       
  30       self.timeout = 1000     #1 second
  31       # This is how long the service will wait to run / refresh itself (see script below)
  32 
  33       while 1:
  34          # Wait for service stop signal, if I timeout, loop again
  35          rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
  36          # Check to see if self.hWaitStop happened
  37          if rc == win32event.WAIT_OBJECT_0:
  38             # Stop signal encountered
  39             servicemanager.LogInfoMsg("SomeShortNameVersion - STOPPED!")  #For Event Log
  40             break
  41          else:
  42                  try:
  43                      handle = open('c:/windows/temp/outx1.txt', "a")
  44                      handle.write('%s \n' % ('tst svc'))
  45                      handle.close()
  46                  except:
  47                      pass
  48 
  49 
  50 
  51 def ctrlHandler(ctrlType):
  52    return True
  53                   
  54 if __name__ == '__main__':   
  55    win32api.SetConsoleCtrlHandler(ctrlHandler, True)   
  56    win32serviceutil.HandleCommandLine(aservice)

Logging

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

   1 import logging
   2 
   3 logging.basicConfig(level=logging.DEBUG, \
   4     format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', \
   5     datefmt='%Y-%m-%d %H:%M:%S %z', \
   6     filename='/tmp/myapp.log', \
   7     filemode='a')
   8 #log to console
   9 console = logging.StreamHandler()
  10 console.setLevel(logging.INFO)
  11 formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
  12 console.setFormatter(formatter)
  13 logging.getLogger('').addHandler(console)
  14 logging.info('Jackdaws love my big sphinx of quartz.')
  15 #other loggers
  16 logger1 = logging.getLogger('myapp.area1')
  17 logger2 = logging.getLogger(__name__)
  18 logger1.debug('Quick zephyrs blow, vexing daft Jim.')
  19 logger1.info('How quickly daft jumping zebras vex.')
  20 logger2.warning('Jail zesty vixen who grabbed pay from quack.')
  21 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:

   1 import logging
   2 import logging.config
   3 import time
   4 import os
   5 
   6 # read initial config file
   7 logging.config.fileConfig('logging.conf')
   8 
   9 # create and start listener on port 9999
  10 t = logging.config.listen(9999)
  11 t.start()
  12 
  13 logger = logging.getLogger('simpleExample')
  14 
  15 try:
  16     # loop through logging calls to see the difference
  17     # new configurations make, until Ctrl+C is pressed
  18     while True:
  19         logger.debug('debug message')
  20         logger.info('info message')
  21         logger.warn('warn message')
  22         logger.error('error message')
  23         logger.critical('critical message')
  24         time.sleep(5)
  25 except KeyboardInterrupt:
  26     # cleanup
  27     logging.config.stopListening()
  28     t.join()

Client:

   1 #!/usr/bin/env python
   2 import socket, sys, struct
   3 
   4 with open(sys.argv[1], 'rb') as f:
   5     data_to_send = f.read()
   6 
   7 HOST = 'localhost'
   8 PORT = 9999
   9 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  10 print('connecting...')
  11 s.connect((HOST, PORT))
  12 print('sending config...')
  13 s.send(struct.pack('>L', len(data_to_send)))
  14 s.send(data_to_send)
  15 s.close()
  16 print('complete')

Logging with date rotation and email

   1 import logging
   2 from logging.handlers import TimedRotatingFileHandler
   3 from logging.handlers import SMTPHandler
   4 
   5 class Logger(object):
   6     def __init__(self):
   7         loggerName='appx'
   8         logFormatter = logging.Formatter('[%(asctime)s %(name)-12s %(levelname)-8s] %(message)s',datefmt='%Y-%m-%d %H:%M:%S %z')
   9         
  10         rotatingLogHandler = TimedRotatingFileHandler('%s.log'%(loggerName),when='midnight',utc=True)
  11         rotatingLogHandler.setFormatter(logFormatter)
  12         rotatingLogHandler.setLevel(logging.DEBUG)
  13 
  14         smtpLogHandler = SMTPHandler(mailhost='hostx.com', fromaddr='mail@hostx.com', toaddrs='dest@hostx.com', subject=loggerName, \
  15             credentials=('accountx','12345678'), secure=None)
  16         smtpLogHandler.setFormatter(logFormatter)
  17         smtpLogHandler.setLevel(logging.WARNING)
  18                         
  19         self.logger = logging.getLogger(loggerName)
  20         self.logger.addHandler(rotatingLogHandler)
  21         self.logger.addHandler(smtpLogHandler)
  22         self.logger.setLevel(logging.DEBUG)
  23         
  24     def debug(self,m):
  25         self.logger.debug(m)
  26 
  27     def info(self,m):
  28         self.logger.info(m)
  29 
  30     def warning(self,m):
  31         self.logger.warning(m)
  32         
  33     def error(self,m):
  34         self.logger.error(m)
  35         
  36     def critical(self,m):
  37         self.logger.critical(m)

pytz - World Timezone Definitions for Python

   1 import pytz
   2 import datetime
   3 
   4 #fmt = '%Y-%m-%d %H:%M:%S %Z%z'
   5 london=pytz.timezone('Europe/London')
   6 lisbon=pytz.timezone('Europe/Lisbon')
   7 paris  =pytz.timezone('Europe/Paris')
   8 utc = pytz.timezone('UTC')
   9 berlin = pytz.timezone('Europe/Berlin')
  10 
  11 print
  12 dtx = datetime.datetime(2002, 12, 27, 12, 0, 0,tzinfo=utc ) 
  13 print 
  14 print 'UTC    ' , dtx
  15 print 'London ',dtx.astimezone(london)
  16 print 'Lisbon ',dtx.astimezone(lisbon)
  17 print 'Paris  ',dtx.astimezone(paris)
  18 print 'Berlin ',dtx.astimezone(berlin)
  19 
  20 
  21 print
  22 dty = datetime.datetime(2002, 8, 27, 13, 0, 0,tzinfo=utc ) 
  23 print 'UTC    ',dty
  24 print 'London ',dty.astimezone(london)
  25 print 'Lisbon ',dty.astimezone(lisbon)
  26 print 'Paris  ',dty.astimezone(paris)
  27 print 'Berlin ',dty.astimezone(berlin)

Sort example

   1 class Acme(object):
   2     def __init__(self,valuex):
   3         self.valuex=valuex
   4 
   5     def __cmp__(self, other):
   6         if self.valuex==other.valuex:
   7             return 0
   8         if self.valuex>other.valuex:
   9             return 1
  10         if self.valuex<other.valuex:
  11             return -1
  12 
  13     def __repr__(self):
  14         return self.valuex
  15 
  16 if __name__=='__main__':
  17     listx=[]
  18     listx.append( Acme('C') )
  19     listx.append( Acme('A') )
  20     listx.append( Acme('Z') )
  21     print 'original:',listx
  22     print 'sorted asc:',sorted(listx,reverse=False)
  23     print 'sorted desc:',sorted(listx,reverse=True)
  24     listx.sort()
  25     print 'sort:',listx
  26     listx.reverse()
  27     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

   1 def test1()
   2     print("Alright !!!!")

Run python interactively:

   1 vitor@darkstar[~]$python
   2 Python 2.7.3 (default, Jul  3 2012, 21:16:07) 
   3 [GCC 4.7.1] on linux2
   4 Type "help", "copyright", "credits" or "license" for more information.
   5 >>> import testex
   6 Traceback (most recent call last):
   7   File "<stdin>", line 1, in <module>
   8 ImportError: No module named testex
   9 >>> import sys
  10 >>> sys.path.append('/tmp') # add /tmp to modules paths
  11 >>> testex.test1()
  12 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

   1 # round float to 6 decimal places
   2 nr=1.23456789
   3 print round(nr,6)
   4 # round float to 4 decimal places
   5 nr=1.23456789
   6 print round(nr,4)

SMTP

   1 import smtplib
   2 
   3 class Smtp(object):
   4     def __init__(self,server,port,user,passx,appName):
   5         self.server=server
   6         self.port=port
   7         self.user=user
   8         self.passx=passx
   9         self.appName=appName
  10 
  11     def send(self,fromx,tox,subjectx,contentType,body):
  12         s=smtplib.SMTP()
  13         s.connect(self.server,self.port)
  14         s.ehlo(self.appName)
  15         s.login(self.user,self.passx)         
  16         msg='From: %s\r\nTo: %s\r\nSubject: %s\r\nContent-Type: %s\r\n\r\n%s'%(fromx,tox,subjectx,contentType,body)
  17         s.sendmail(fromx,tox,msg)
  18         s.quit()
  19 
  20 s=Smtp('mail.example.org',25,'norep@example.org','12345678','TestApp')
  21 # content types: text/plain text/html
  22 s.send('norep@example.org',['user1@example.org','user2@example.org'],'Subject','text/plain','Body mail')

JSON serializable

   1 from json import JSONEncoder
   2 import json
   3 
   4 class EncoderJSON(JSONEncoder):
   5     def default(self, o):
   6         return o.getJSONDict()    
   7     
   8 class Acme(object):
   9     def __init__(self,paramx,valuex):
  10         self.paramx=paramx
  11         self.valuex=valuex
  12         
  13     def getJSONDict(self):
  14         return {'paramx':self.paramx ,'valuex':self.valuex}
  15                 
  16     def __repr__(self):
  17         return '%s %s %s'%(self.paramx ,self.valuex)
  18 
  19 if __name__=='__main__':
  20     listx=[]
  21     listx.append(Acme('p1','v1'))
  22     listx.append(Acme('p2','v2'))
  23     # json.dumps likes lists and dictionaries
  24     print( json.dumps(listx,cls=EncoderJSON) ) 
  25     # 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.

   1 #decorator logger
   2 def logger(func):
   3         def inner(*args, **kwargs): # step 1
   4             print "Arguments were: %s, %s" % (args, kwargs)
   5             return func(*args, **kwargs) # step 2
   6         return inner
   7 
   8 @logger
   9 def foo1(x, y=1):
  10     return x * y
  11 if __name__=='__main__':
  12     foo1(2,2)

   1 import sys
   2 from os import path 
   3 #decorator adder
   4 def add_one_args(decoratedFunction):
   5     def interceptor(*args, **kwargs): # step 1
   6         print "Arguments were: %s, %s" % (args, kwargs)
   7         index=0
   8         new_args=[]
   9         # change input 
  10         isnotint=False
  11         for arg in args:
  12             if isinstance(arg,int):                    
  13                 new_args.append(arg+1)
  14             else:
  15                 isnotint=True
  16             index=index+1
  17                 
  18         if isnotint==False: 
  19             args=tuple(new_args)
  20     
  21         decoratedFuncReturn =  decoratedFunction(*args, **kwargs) # step 2
  22         # change output
  23         decoratedFuncReturn = decoratedFuncReturn * 2
  24         return decoratedFuncReturn
  25     return interceptor
  26 
  27 @add_one_args
  28 def foo1(x, y=1):
  29     print('Got arguments in foo1 %d %d'%(x,y))
  30     mul = x * y
  31     print('Multiplication result inside function %d'%(mul))
  32     return mul
  33 
  34 if __name__=='__main__':
  35     print( foo1(2,2) )
  36     print( sys.path )
  37     print( sys.prefix )
  38     print( sys.exec_prefix )
  39     here = path.abspath(path.dirname(__file__))
  40     print(here)

Dictionaries

   1 Type "help", "copyright", "credits" or "license" for more information.
   2 a={} #create dictionary
   3 if 'keyx' in a: print 'ok' #check if key keyx exists
   4 a['keyx']=1 # create key keyx with value 1
   5 if 'keyx' in a: print 'ok' #checks  keyx existence
   6 a['key2']={} # create key key2
   7 if 'keyx' in a['key2']: print 'ok' # check if keyx exists in key2
   8 a['key2']['keyx']=3 # create keyx inside key2
   9 if 'keyx' in a['key2']: print 'ok'

Exceptions

Example 1

   1 def dummyMethod(datax):
   2     try:
   3         if 'key1' in datax:
   4             key1 = datax['key1']
   5 
   6             if 'key2' in key1:
   7                 key2 = dvi['key2']
   8                 print('key2 is %s '%(key2))                    
   9 
  10     except Exception,ex:
  11         print('Error in dummyMethod %s'%( str(ex) ) )

Install pip

Regular expressions

   1 import re
   2 listx=['aa12.txt','a123sss.txt','bs11bb.txt','123aaaa.sql','a12.txt','aaaa12','20ghj','6657']
   3 
   4 for item in listx:
   5     res = re.match('^(\D+)(\d+)(\D+)$',item)
   6     if res!=None:
   7         decimal =  int(res.group(2)) + 1
   8         print('A: mv %s %s%d%s'%(item , res.group(1),decimal , res.group(3) ))
   9     res = re.match('^(\d+)(\D+)$',item)
  10     if res!=None:
  11         decimal =  int(res.group(1)) + 1
  12         print('B: mv %s %d%s'%(item , decimal,res.group(2)))
  13     res = re.match('^(\D+)(\d+)$',item)
  14     if res!=None:
  15         decimal =  int(res.group(2)) + 1
  16         print('C: mv %s  %s%d'%(item , res.group(1),decimal))
  17     res = re.match('^(\d+)$',item)
  18     if res!=None:
  19         decimal =  int(res.group(1)) + 1
  20         print('D: mv %s  %d'%(item , decimal))
  21 '''
  22 #ouput
  23  python addNumber.py 
  24 A: mv aa12.txt aa13.txt
  25 A: mv a123sss.txt a124sss.txt
  26 A: mv bs11bb.txt bs12bb.txt
  27 B: mv 123aaaa.sql 124aaaa.sql
  28 A: mv a12.txt a13.txt
  29 C: mv aaaa12  aaaa13
  30 B: mv 20ghj 21ghj
  31 D: mv 6657  6658
  32 '''

Command line arguments

   1 import sys
   2 # ...
   3 if __name__=='__main__':
   4     intArgA = int(sys.argv[1])
   5     intArgB = int(sys.argv[2])
   6     intArgC = int(sys.argv[3])

Run python code in shell without source code file .py

   1 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                         

   1 strx='{}' # where xargs value is used
   2 splitted = strx.split(' ')
   3 for spl in splitted:
   4     if 'msgId' in spl:
   5         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

   1 import subprocess 
   2 
   3 if __name__=='__main__':
   4   try:
   5     args=[]
   6     args.append('ls')
   7     args.append('/tmpx')
   8     out = subprocess.check_output(args, stderr=subprocess.STDOUT, shell=False)
   9     print( out )
  10   except subprocess.CalledProcessError as ex1:
  11     print("Exception command: %s"%(ex1.cmd)  )    
  12     print("Exception code: %s"%(ex1.returncode)  )    
  13     print("Exception output: %s"%(ex1.output)  )
  14   except Exception as ex2:
  15     print("General exception %s"%(ex2.message) )    

Recursion

   1 def factorial(n):
   2   if n > 0: return n * factorial(n-1)
   3   else: return 1
   4 
   5 def fibo(n):
   6   if n==0: return 0
   7   if n==1: return 1
   8   if n>1: return fibo(n-1) + fibo(n-2)
   9 
  10 def sum(idx,lista):
  11   if idx<0: return 0
  12   if idx>=0: return lista[idx] + sum(idx-1,lista)
  13 
  14 if __name__=='__main__':
  15   print( factorial(4) )
  16   print( fibo(3) )
  17   lst=[1,2,3,4]
  18   print( sum(len(lst)-1,lst) )

Python3 virtual environment

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.

   1 sudo apt-get install python3-venv
   2 python3 -m venv testVenv
   3 source testVenv/bin/activate
   4 pip install --upgrade pip
   5 pip install -i https://testpypi.python.org/pypi vbhelloworld
   6 pip uninstall vbhelloworld

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

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

Python 2 virtual env

   1 # in windows with powershell
   2 cd ~
   3 pip install virtualenv --user
   4 # add the scripts folder to the user path c:\users\userx\AppData\Roaming\Python\Scripts 
   5 # using cmd
   6 cd %HOME_PATH%
   7 mkdir tmp
   8 cd tmp
   9 virtualenv.exe test_env
  10 cd test_env
  11 Scripts\activate.bat

   1 import sys
   2 sys.path

Generator functions yield

   1 #test yield
   2 def factorial(val):
   3     v=val
   4     res=1
   5     while v>1:
   6         res=res*v
   7         v=v-1
   8         yield None # v
   9     yield res
  10 
  11 if __name__ == "__main__":
  12     for i in factorial(3):
  13         print(i)
  14 
  15     f = factorial(4)
  16     iterate=True
  17 
  18     while iterate:
  19         try:
  20             print( f.next() )
  21             #print( next(f) )
  22         except StopIteration as ex:
  23             iterate=False

Output:

   1 python test_yeld.py 
   2 None
   3 None
   4 6
   5 None
   6 None
   7 None
   8 24

XML parse with minidom

   1 from xml.dom import minidom
   2 print("Hello world")
   3 a="<a><b>aaaa</b><b>bbcc</b></a>"
   4 doc = minidom.parseString(a) # return document 
   5 tags = doc.getElementsByTagName("b")
   6 for t in tags:
   7   print(t.firstChild.nodeValue)

Environment variable

   1 import os
   2 print(os.environ)

XSD and objectify XML

test.py

   1 from lxml import objectify
   2 from StringIO import StringIO
   3 from lxml import etree
   4 
   5 f = open('test.xsd')
   6 xsddata=''
   7 for line in f:
   8     xsddata = xsddata +  line
   9 f.close()
  10 
  11 xmlschema_doc = etree.parse(StringIO(xsddata))
  12 xmlschema = etree.XMLSchema(xmlschema_doc)
  13 print xmlschema 
  14 
  15 parser = objectify.makeparser(schema=xmlschema)
  16 xml = "<a><b>test</b></a>"
  17 a = objectify.fromstring(xml, parser)
  18 print(a.b)

test.xsd

   1 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   2   <xsd:element name="a" type="AType"/>
   3   <xsd:complexType name="AType">
   4        <xsd:sequence>
   5          <xsd:element name="b" type="xsd:string" />
   6        </xsd:sequence>
   7   </xsd:complexType>
   8 </xsd:schema>

Other example ....

acme.xsd {{{highlight xml <xs:schema

</xs:schema> }}}

test.xsd

   1 <xsd:schema 
   2     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   3     xmlns:bit="http://bitarus.allowed.org/types" 
   4     targetNamespace="http://bitarus.allowed.org/types" >
   5   <xsd:complexType name="AType">
   6        <xsd:sequence>
   7          <xsd:element name="b" type="xsd:string" />         
   8          <xsd:element name="xx" type="bit:BType" />
   9        </xsd:sequence>
  10   </xsd:complexType>  
  11   <xsd:complexType name="BType">
  12       <xsd:sequence>
  13         <xsd:element name="c" type="xsd:string" />
  14       </xsd:sequence>
  15   </xsd:complexType>  
  16   <xsd:element name="a" type="bit:AType"/>
  17 </xsd:schema>

acme.py

   1 from lxml import objectify
   2 from StringIO import StringIO
   3 from lxml import etree
   4 import os
   5 f = open('acme.xsd')
   6 xsddata=''
   7 for line in f:
   8     xsddata = xsddata +  line
   9 f.close()
  10 
  11 xmlschema = etree.XMLSchema(etree.parse(StringIO(xsddata)))
  12 
  13 xml = """
  14 <ot:at xmlns:ot="http://bitarus.allowed.org/othertypes" 
  15   xsi:schemaLocation="http://bitarus.allowed.org/othertypes other.xsd" 
  16   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  17   <b>cccc</b>
  18   <xx><c>hhjjkk</c></xx>
  19   <other>1234</other>  
  20 </ot:at>
  21 """
  22 
  23 doc = etree.fromstring(xml, etree.XMLParser(schema=xmlschema) )
  24 # https://lxml.de/api/lxml.etree._Element-class.html
  25 bNode = doc.getchildren()[0]
  26 otherNode = doc.getchildren()[2]
  27 print bNode.text
  28 print otherNode.text

Debugger

   1 # python test.py
   2 # DEBUG=on python test.py  
   3 import os
   4 # ....
   5 if 'DEBUG' in os.environ:
   6   # breakpoint 
   7   import pdb; pdb.set_trace()
   8 
   9 # commands
  10 # c continue
  11 # q quit
  12 # n next
  13 # l list
  14 # s step

Python (last edited 2018-07-26 21:16:57 by localhost)