Python

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

Links:

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

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

Python (last edited 2013-07-01 03:12:53 by 188)