Size: 2001
Comment:
|
Size: 47237
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 7: | Line 7: |
* [[https://learnxinyminutes.com/docs/python/]] * https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-videos/ == 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") }}} |
|
Line 75: | Line 167: |
== 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 * pip install --user --upgrade 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 == unit tests - mock == {{{#!highlight python from unittest.mock import MagicMock class X(object): def getX(self): return 111 if __name__=='__main__': x = X() print(x.getX()) x.getX = MagicMock(return_value=999) print( x.getX() ) }}} == 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.') # using default logger '' #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') dtx = datetime.datetime(2002, 12, 27, 12, 0, 0,tzinfo=utc ) print 'UTC ' , dtx print 'London ',dtx.astimezone(london) print 'Lisbon ',dtx.astimezone(lisbon) print 'Paris ',dtx.astimezone(paris) print 'Berlin ',dtx.astimezone(berlin) 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 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 ''' }}} {{{#!highlight python # replace using regex (sub) import re a="adasd@gg@g:00-0:0.xml.txt" print re.sub('[@,:]','',a) # adasdggg00-00.xml.txt }}} == 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 }}} == XML tree/graph == {{{#!highlight python from lxml import objectify from lxml import etree from StringIO import StringIO import sys def show_children(parent,node): if node.getchildren(): print parent+"/"+node.tag index=1 for c in node.getchildren(): show_children(parent+"/"+node.tag, c ) index=index+1 else: if node.text: print parent + "/"+node.tag + "=" + node.text else: print parent + "/"+node.tag if node.attrib: for key in node.attrib: print parent + "/"+node.tag +"@"+key+"="+node.attrib[key] if __name__=='__main__': f = open(sys.argv[1]) data='' for line in f: if line.startswith("<?xml ")==False: data = data + line f.close() # print data doc = etree.fromstring( data ) f.close() show_children("",doc) }}} == 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 }}} == Remote debugger == {{{#!highlight python # pip install --user rpdb # cat rpdb_test.py print("aaa") # listens in 0.0.0.0 4444 import rpdb; rpdb.set_trace(addr="0.0.0.0") print("bbb") print("ccc") # python3 rpdb_test.py # docker run (...) -p 4444:4444 # nc localhost 4444 }}} == 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 install --user --upgrade numpy==1.15.1 pip freeze }}} == with statement == * https://www.python.org/dev/peps/pep-0343/ {{{ with EXPR as VAR: BLOCK which roughly translates into this: VAR = EXPR VAR.__enter__() try: BLOCK finally: VAR.__exit__() }}} The open() method returns a file object. The file object has the following methods that fit in the with statement: * __enter__(...) * __exit__(...) Closes the file. {{{#!highlight python with open('filex') as fx: pass }}} == reST (ReStructuredText) docstring format == * https://pythonhosted.org/an_example_pypi_project/sphinx.html#restructured-text-rest-resources * http://www.sphinx-doc.org/en/1.6/domains.html#the-python-domain {{{ .. py:function:: send_message(sender, recipient, message_body, [priority=1]) Send a message to a recipient :param str sender: The person sending the message :param str recipient: The recipient of the message :param str message_body: The body of the message :param priority: The priority of the message, can be a number 1-5 :type priority: integer or None :return: the message id :rtype: int :raises ValueError: if the message_body exceeds 160 characters :raises TypeError: if the message_body is not a basestring }}} == Static method == Adapted from https://stackoverflow.com/questions/735975/static-methods-in-python {{{#!highlight python class ClassX(object): @staticmethod def static(x): print(x) ClassX.static(2) # outputs 2 }}} == Singleton == Adapted from https://www.tutorialspoint.com/python_design_patterns/python_design_patterns_singleton.htm {{{#!highlight python class Singleton: __instance = None @staticmethod def getInstance(): """ Static access method. """ if Singleton.__instance == None: Singleton() return Singleton.__instance def __init__(self): """ Virtually private constructor. """ if Singleton.__instance != None: raise Exception("This class is a singleton!") else: Singleton.__instance = self s = Singleton() }}} == Iterate files from a root folder == {{{#!highlight python #!/usr/bin/python import os for root, dirs, files in os.walk(".", topdown=False): for name in files: if name.endswith(".txt"): print(os.path.join(root, name)) }}} == xml pretty print == {{{#!highlight python import lxml f = open('out.xml') from lxml import etree doc = etree.fromstring( f.read() ) out = etree.tostring(doc, pretty_print=True) f.close() f = open('out2.xml','wb') f.write( out ) f.flush() f.close() quit() }}} == docstring sphynx reST == * https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html * https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html?highlight=param#the-python-domain * https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html?highlight=param#info-field-lists {{{ Info field lists New in version 0.4. Inside Python object description directives, reST field lists with these fields are recognized and formatted nicely: param, parameter, arg, argument, key, keyword: Description of a parameter. type: Type of a parameter. Creates a link if possible. raises, raise, except, exception: That (and when) a specific exception is raised. var, ivar, cvar: Description of a variable. vartype: Type of a variable. Creates a link if possible. returns, return: Description of the return value. rtype: Return type. Creates a link if possible. }}} == __future__ == * https://docs.python.org/2/library/__future__.html {{{#!highlight python from __future__ import absolute_import # 3.0 from __future__ import division # 3.0 from __future__ import generators #2.3 from __future__ import nested_scopes # 2.2 from __future__ import print_function # 3.0 from __future__ import unicode_literals # 3.0 from __future__ import with_statement # 2.6 }}} == Command line argument parser == * https://docs.python.org/2/library/argparse.html Parser for command-line options, arguments and sub-commands == Type checking == {{{#!highlight python assert isinstance(obj,Class) raise NotImplementedError }}} == Private and protected class members == {{{#!highlight python _member # protected member __member # private member, name mangling member # public member }}} == Nested class inside a function == {{{#!highlight python def a(): class X(object): CONST=1 def __init__(self): self._c = X.CONST print(self._c) x=X() if __name__=='__main__': a() }}} == List comprehension == {{{#!highlight python (Pdb) [ val for val in range(1,10) if val % 2 == 0] [2, 4, 6, 8] }}} == Change XML values with xpath == {{{#!highlight python from lxml import etree, objectify xml_data='<a><b><c x="as">def</c></b></a>' doc = etree.fromstring(xml_data) print "Original" print etree.tostring(doc) elem = doc.xpath('//c', namespaces=doc.nsmap) elem[0].text = "hhhhh" node_attr = doc.xpath('/a/b/c', namespaces=doc.nsmap) node_attr[0].attrib['x']='other_xyz' print etree.tostring(doc) }}} == *args and **kwargs == {{{#!highlight python def x(*args,**kwargs): print type(args) # tuple print type(kwargs) # dict print args[0] # prints 1 print args[1] # prints 2 print kwargs['a'] # prints 1234 print kwargs['b'] # prints 4567 x(1 ,2, a = 1234, b = 4567) }}} == json indentation, pretty print == {{{#!highlight python import json def json_to_string(obj): return json.dumps(obj,indent=4,sort_keys=True) if __name__=='__main__': print json_to_string({'a':11,'b':'fffgg'}) }}} == multiple inheritance - python 2 == {{{#!highlight python #!/usr/local/bin/python2 class A(object): def init_A(self): print("Called init_A") self.a = "aaaa" def hello(self): return "hello" def get_c(self): return self.c class C(object): def __init__(self): self.c = "cccc" print("Called init C") def world(self): return "world" class B(C, A): """Multiple inheritance from C and A """ def __init__(self): super(B,self).__init__() # calls first class from which is inherited super(B,self).init_A() print(dir(self)) def hello(self): return super(B,self).hello() + " " + super(B,self).world() + " " + self.c + " " + self.a if __name__=="__main__": b = B() print( b.hello() ) }}} Output {{{#!highlight bash openbsd$ ./inheritance.py Called init C Called init_A ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'c', 'get_c', 'hello', 'init_A', 'world'] hello world cccc aaaa }}} == globals == * https://docs.python.org/3/library/functions.html#globals Return a dictionary representing the current global symbol table. == Profiler - cProfile == * profiler: identify files to review using a profiler * https://docs.python.org/2/library/profile.html ( cProfile, profile, hotshot) * python -m cProfile [-o output_file] [-s sort_order] myscript.py * python -m cProfile -o /tmp/profile.bin myscript.py {{{#!highlight python import pstats import sys filename=sys.argv[1] p = pstats.Stats(filename) collection = [] for stat in p.stats.values(): try: mod=stat[4].keys()[0][0] item = stat[4].keys()[0][2] collection.append({"module":mod,"item":item}) except: print str(stat) for stat in p.stats.keys(): try: mod=stat[0] item = stat[2] collection.append({"module":mod,"item":item}) except: print str(stat) for element in collection: print "%s:%s"%(element['module'],element['item']) }}} == Get current process PID == {{{#!highlight python import os print( os.getpid() ) }}} == AES encryption example == * https://pypi.org/project/pycrypto/ {{{#!highlight python #crypto.py from Crypto.Cipher import AES iv = '0123456789012345' # IV must be 16 key = 'Key in the house' # AES key must be either 16, 24, or 32 bytes long obj = AES.new(key, AES.MODE_CBC, iv) message = 'If u can see1234' # multiple len of 16 ciphertext = obj.encrypt(message) print(ciphertext) obj2 = AES.new(key, AES.MODE_CBC, iv) decoded= obj2.decrypt(ciphertext) print(decoded) assert message == decoded }}} == Map, filter, reduce == {{{#!highlight python3 map_filter.py from functools import reduce nrs = range(1,100+1) even = list( filter( lambda item: item % 2 == 0, nrs ) ) print(even) nrs10 = range(1,10+1) total = reduce( (lambda a,b: a+b) , list(nrs10)) print(total) multiplicationTable2 = list( map( lambda item: item*2 , nrs10 ) ) print(multiplicationTable2) }}} == Interface and Abstract classes == {{{#!highlight python interface_test.py from abc import ABCMeta, abstractmethod from math import pi class Shape(metaclass=ABCMeta): @abstractmethod def calculate_area(self): raise NotImplementedError class Square(Shape): def __init__(self,side:float): self.side = side def calculate_area(self): return self.side*self.side class Circle(Shape): def __init__(self,radius:float): self.radius = radius def calculate_area(self): return pi*self.radius*self.radius class AbsX(metaclass=ABCMeta): @abstractmethod def helloinst(self): raise NotImplementedError class X(AbsX): WORLD="world" def hello(): return "hello " + X.WORLD @staticmethod def hellostatic(): return "hello static " + X.WORLD def helloinst(self): return "Hello inst " + X.WORLD if __name__=='__main__': print( X.hello() ) print( X.hellostatic() ) print( X().helloinst() ) objs = [] objs.append(Square(2)) objs.append(Circle(3)) for o in objs: #if isinstance(o,Shape): # print(o.calculate_area()) assert( isinstance(o,Shape) ) print(o.calculate_area()) }}} |
Python
Python is a programming language that lets you work more quickly and integrate your systems more effectively.
Links:
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:
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.
Utf-8 and Unicode
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.
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)
- pip install coverage
- pip install --user --upgrade coverage
tests.py
impl.py
Steps:
- coverage erase
- coverage run tests.py
- coverage report -m impl.py
- coverage html impl.py #in htmlcov folder
unit tests - mock
cython
Installation:
- su
- cd /tmp
- 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:
Get file modification time
Kill process with PID
Log message to /var/log/messages or /var/log/syslog
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 #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
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.') # using default logger ''
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
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
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 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: args %s, kwargs %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 new_kwargs={}
22 for kwarg in kwargs:
23 new_kwargs[kwarg] = kwargs[kwarg]+1
24
25 decoratedFuncReturn = decoratedFunction(*args, **new_kwargs) # step 2
26 # change output
27 decoratedFuncReturn = decoratedFuncReturn * 2
28 return decoratedFuncReturn
29 return interceptor
30
31 @add_one_args
32 def foo1(x, y=1):
33 print('Got arguments in foo1 %d %d'%(x,y))
34 mul = x * y
35 print('Multiplication result inside function %d'%(mul))
36 return mul
37
38 def foo2(**kwargs):
39 print('Got arguments in foo1 %d %d'%( kwargs['x'] , kwargs['y'] ))
40 mul = kwargs['x'] * kwargs['y']
41 print('Multiplication result inside function %d'%(mul))
42 return mul
43
44 if __name__=='__main__':
45 print( foo1(2,y=2) )
46 print( foo2( x=3,y=3 ) )
47 print( sys.path )
48 print( sys.prefix )
49 print( sys.exec_prefix )
50 here = path.abspath(path.dirname(__file__))
51 print(here)
1 def add_one_args(decoratedFunction,xyz=1):
2 def interceptor(*args, **kwargs):
3 print "Arguments were: args %s, kwargs %s" % (args, kwargs)
4 new_args=[]
5
6 for arg in args:
7 if isinstance(arg,int): new_args.append(arg+1)
8 else: new_args.append(arg)
9
10 args=tuple(new_args)
11 decoratedFuncReturn = decoratedFunction(*args, **kwargs)
12 return decoratedFuncReturn
13 return interceptor
14
15 def subtract_one_args(decoratedFunction,xyz=1):
16 def interceptor(*args, **kwargs):
17 print "Arguments were: args %s, kwargs %s" % (args, kwargs)
18 new_args=[]
19
20 for arg in args:
21 if isinstance(arg,int): new_args.append(arg-1)
22 else: new_args.append(arg)
23
24 args=tuple(new_args)
25 decoratedFuncReturn = decoratedFunction(*args, **kwargs)
26 return decoratedFuncReturn
27 return interceptor
28
29 class Acme(object):
30 def __init__(self):
31 self.a = 0
32 self.b = 0
33
34 @add_one_args
35 @subtract_one_args
36 def set(self,a,b):
37 self.a=a
38 self.b=b
39 print self.a, self.b
40
41 if __name__=='__main__':
42 x = Acme()
43 x.set(0,1)
Dictionaries
1 a={} #create dictionary
2 if 'keyx' in a: print 'ok' #check if key keyx exists
3 a['keyx']=1 # create key keyx with value 1
4 if 'keyx' in a: print 'ok' #checks keyx existence
5 a['key2']={} # create key key2
6 if 'keyx' in a['key2']: print 'ok' # check if keyx exists in key2
7 a['key2']['keyx']=3 # create keyx inside key2
8 if 'keyx' in a['key2']: print 'ok'
Exceptions
Example 1
Install pip
- easy_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
- import sys
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
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.
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
Python 2 virtual env
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:
XML parse with minidom
Environment variable
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
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
acme.xsd
1 <xs:schema
2 xmlns:xs="http://www.w3.org/2001/XMLSchema"
3 xmlns:bit="http://bitarus.allowed.org/types"
4 xmlns:ot="http://bitarus.allowed.org/othertypes"
5 targetNamespace="http://bitarus.allowed.org/othertypes"
6 elementFormDefault="qualified"
7 >
8 <xs:import schemaLocation="test.xsd" />
9 <xs:complexType name="AcmeType">
10 <xs:complexContent>
11 <xs:extension base="bit:AType">
12 <xs:sequence>
13 <xs:element name="other" type="xs:int"/>
14 </xs:sequence>
15 </xs:extension>
16 </xs:complexContent>
17 </xs:complexType>
18 <xs:element name="at" type="ot:AcmeType"/>
19 </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 elementFormDefault="qualified"
6 >
7 <xsd:complexType name="AType">
8 <xsd:sequence>
9 <xsd:element name="b" type="xsd:string" />
10 <xsd:element name="xx" type="bit:BType" />
11 </xsd:sequence>
12 </xsd:complexType>
13 <xsd:complexType name="BType">
14 <xsd:sequence>
15 <xsd:element name="c" type="xsd:string" />
16 </xsd:sequence>
17 </xsd:complexType>
18 <xsd:element name="a" type="bit:AType"/>
19 </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 xmlns:bit="http://bitarus.allowed.org/types" >
18 <bit:b>cccc</bit:b>
19 <bit:xx><bit:c>hhjjkk</bit:c></bit:xx>
20 <ot:other>1234</ot:other>
21 </ot:at>
22 """
23
24 doc = etree.fromstring(xml, etree.XMLParser(schema=xmlschema) )
25 # https://lxml.de/api/lxml.etree._Element-class.html
26 bNode = doc.getchildren()[0]
27 otherNode = doc.getchildren()[2]
28 print bNode.text
29 print otherNode.text
XML tree/graph
1 from lxml import objectify
2 from lxml import etree
3 from StringIO import StringIO
4 import sys
5
6 def show_children(parent,node):
7 if node.getchildren():
8 print parent+"/"+node.tag
9 index=1
10 for c in node.getchildren():
11 show_children(parent+"/"+node.tag, c )
12 index=index+1
13 else:
14 if node.text:
15 print parent + "/"+node.tag + "=" + node.text
16 else:
17 print parent + "/"+node.tag
18
19 if node.attrib:
20 for key in node.attrib:
21 print parent + "/"+node.tag +"@"+key+"="+node.attrib[key]
22
23 if __name__=='__main__':
24 f = open(sys.argv[1])
25 data=''
26 for line in f:
27 if line.startswith("<?xml ")==False: data = data + line
28 f.close()
29 # print data
30 doc = etree.fromstring( data )
31 f.close()
32 show_children("",doc)
Debugger
Remote debugger
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://www.iana.org/assignments/xml-registry/schema/vcard-4.0.xsd
https://www.iana.org/assignments/xml-registry/xml-registry.xhtml
Identify current function
Operative system folder path separator
Install package for user with a more recent version
with statement
with EXPR as VAR: BLOCK which roughly translates into this: VAR = EXPR VAR.__enter__() try: BLOCK finally: VAR.__exit__()
The open() method returns a file object. The file object has the following methods that fit in the with statement:
enter(...)
exit(...) Closes the file.
reST (ReStructuredText) docstring format
https://pythonhosted.org/an_example_pypi_project/sphinx.html#restructured-text-rest-resources
http://www.sphinx-doc.org/en/1.6/domains.html#the-python-domain
.. py:function:: send_message(sender, recipient, message_body, [priority=1]) Send a message to a recipient :param str sender: The person sending the message :param str recipient: The recipient of the message :param str message_body: The body of the message :param priority: The priority of the message, can be a number 1-5 :type priority: integer or None :return: the message id :rtype: int :raises ValueError: if the message_body exceeds 160 characters :raises TypeError: if the message_body is not a basestring
Static method
Adapted from https://stackoverflow.com/questions/735975/static-methods-in-python
Singleton
Adapted from https://www.tutorialspoint.com/python_design_patterns/python_design_patterns_singleton.htm
1 class Singleton:
2 __instance = None
3
4 @staticmethod
5 def getInstance():
6 """ Static access method. """
7 if Singleton.__instance == None:
8 Singleton()
9 return Singleton.__instance
10
11 def __init__(self):
12 """ Virtually private constructor. """
13 if Singleton.__instance != None:
14 raise Exception("This class is a singleton!")
15 else:
16 Singleton.__instance = self
17
18 s = Singleton()
Iterate files from a root folder
xml pretty print
docstring sphynx reST
Info field lists New in version 0.4. Inside Python object description directives, reST field lists with these fields are recognized and formatted nicely: param, parameter, arg, argument, key, keyword: Description of a parameter. type: Type of a parameter. Creates a link if possible. raises, raise, except, exception: That (and when) a specific exception is raised. var, ivar, cvar: Description of a variable. vartype: Type of a variable. Creates a link if possible. returns, return: Description of the return value. rtype: Return type. Creates a link if possible.
__future__
1 from __future__ import absolute_import # 3.0
2 from __future__ import division # 3.0
3 from __future__ import generators #2.3
4 from __future__ import nested_scopes # 2.2
5 from __future__ import print_function # 3.0
6 from __future__ import unicode_literals # 3.0
7 from __future__ import with_statement # 2.6
Command line argument parser
Parser for command-line options, arguments and sub-commands
Type checking
Private and protected class members
Nested class inside a function
List comprehension
Change XML values with xpath
1 from lxml import etree, objectify
2
3 xml_data='<a><b><c x="as">def</c></b></a>'
4
5 doc = etree.fromstring(xml_data)
6 print "Original"
7 print etree.tostring(doc)
8
9 elem = doc.xpath('//c', namespaces=doc.nsmap)
10 elem[0].text = "hhhhh"
11
12 node_attr = doc.xpath('/a/b/c', namespaces=doc.nsmap)
13 node_attr[0].attrib['x']='other_xyz'
14
15 print etree.tostring(doc)
*args and **kwargs
json indentation, pretty print
multiple inheritance - python 2
1 #!/usr/local/bin/python2
2
3 class A(object):
4 def init_A(self):
5 print("Called init_A")
6 self.a = "aaaa"
7
8 def hello(self):
9 return "hello"
10
11 def get_c(self):
12 return self.c
13
14 class C(object):
15 def __init__(self):
16 self.c = "cccc"
17 print("Called init C")
18
19 def world(self):
20 return "world"
21
22 class B(C, A):
23 """Multiple inheritance from C and A """
24 def __init__(self):
25 super(B,self).__init__() # calls first class from which is inherited
26 super(B,self).init_A()
27 print(dir(self))
28
29 def hello(self):
30 return super(B,self).hello() + " " + super(B,self).world() + " " + self.c + " " + self.a
31
32 if __name__=="__main__":
33 b = B()
34 print( b.hello() )
Output
1 openbsd$ ./inheritance.py
2 Called init C
3 Called init_A
4 ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'a', 'c', 'get_c', 'hello', 'init_A', 'world']
5 hello world cccc aaaa
globals
Return a dictionary representing the current global symbol table.
Profiler - cProfile
- profiler: identify files to review using a profiler
https://docs.python.org/2/library/profile.html ( cProfile, profile, hotshot)
- python -m cProfile [-o output_file] [-s sort_order] myscript.py
- python -m cProfile -o /tmp/profile.bin myscript.py
1 import pstats
2 import sys
3
4 filename=sys.argv[1]
5 p = pstats.Stats(filename)
6
7 collection = []
8
9 for stat in p.stats.values():
10 try:
11 mod=stat[4].keys()[0][0]
12 item = stat[4].keys()[0][2]
13 collection.append({"module":mod,"item":item})
14 except:
15 print str(stat)
16
17 for stat in p.stats.keys():
18 try:
19 mod=stat[0]
20 item = stat[2]
21 collection.append({"module":mod,"item":item})
22 except:
23 print str(stat)
24
25 for element in collection:
26 print "%s:%s"%(element['module'],element['item'])
Get current process PID
AES encryption example
1 #crypto.py
2 from Crypto.Cipher import AES
3 iv = '0123456789012345' # IV must be 16
4 key = 'Key in the house' # AES key must be either 16, 24, or 32 bytes long
5 obj = AES.new(key, AES.MODE_CBC, iv)
6 message = 'If u can see1234' # multiple len of 16
7 ciphertext = obj.encrypt(message)
8 print(ciphertext)
9 obj2 = AES.new(key, AES.MODE_CBC, iv)
10 decoded= obj2.decrypt(ciphertext)
11 print(decoded)
12 assert message == decoded
Map, filter, reduce
1 map_filter.py
2 from functools import reduce
3
4 nrs = range(1,100+1)
5 even = list( filter( lambda item: item % 2 == 0, nrs ) )
6 print(even)
7
8 nrs10 = range(1,10+1)
9 total = reduce( (lambda a,b: a+b) , list(nrs10))
10 print(total)
11
12 multiplicationTable2 = list( map( lambda item: item*2 , nrs10 ) )
13 print(multiplicationTable2)
Interface and Abstract classes
1 interface_test.py
2 from abc import ABCMeta, abstractmethod
3 from math import pi
4
5 class Shape(metaclass=ABCMeta):
6 @abstractmethod
7 def calculate_area(self):
8 raise NotImplementedError
9
10 class Square(Shape):
11 def __init__(self,side:float):
12 self.side = side
13
14 def calculate_area(self):
15 return self.side*self.side
16
17 class Circle(Shape):
18 def __init__(self,radius:float):
19 self.radius = radius
20
21 def calculate_area(self):
22 return pi*self.radius*self.radius
23
24 class AbsX(metaclass=ABCMeta):
25 @abstractmethod
26 def helloinst(self):
27 raise NotImplementedError
28
29 class X(AbsX):
30 WORLD="world"
31
32 def hello():
33 return "hello " + X.WORLD
34
35 @staticmethod
36 def hellostatic():
37 return "hello static " + X.WORLD
38
39 def helloinst(self):
40 return "Hello inst " + X.WORLD
41
42 if __name__=='__main__':
43 print( X.hello() )
44 print( X.hellostatic() )
45 print( X().helloinst() )
46
47 objs = []
48 objs.append(Square(2))
49 objs.append(Circle(3))
50
51 for o in objs:
52 #if isinstance(o,Shape):
53 # print(o.calculate_area())
54 assert( isinstance(o,Shape) )
55 print(o.calculate_area())