Python

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

Links:

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)

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

   1 import unittest
   2 
   3 class SimpleTestCase(unittest.TestCase):
   4     def setUp(self):
   5         self.res=4
   6 
   7     def tearDown(self):
   8         self.res=0
   9 
  10     def testAdd1(self):
  11         res=2+2
  12         self.assertEqual(self.res,res,'')       
  13 
  14     def testAdd2(self):
  15         res=2+3
  16         self.assertEqual(self.res,res,'')       
  17 
  18 if __name__ == '__main__':
  19     unittest.main()

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')

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         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')

Python (last edited 2013-12-10 21:40:54 by 95)