Size: 4266
Comment:
|
Size: 6602
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 17: | Line 17: |
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 }}} |
|
Line 27: | Line 54: |
nrSeconds = time.time() #usually is UTC on all OS | |
Line 33: | Line 61: |
# timedelta import datetime x=datetime.datetime.fromtimestamp(1284101485) nowx=datetime.datetime.now() ts=(nowx-x).total_seconds() print int(ts) |
|
Line 156: | Line 190: |
== 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') }}} == 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 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) ) subprocess.Popen(['/usr/bin/python','dummyService.py'],cwd='/home/userx/svc') }}} |
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:
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)
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
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
Simple process monitor
1 #service monitor.py
2 import os
3 import syslog
4 import subprocess
5
6 if __name__=="__main__":
7 files = os.listdir('/proc')
8 script="dummyService.py"
9 prefix="dummyService:"
10
11 svcRunning=False
12
13 for file in files:
14
15 if file.isdigit():
16 cmdline = open('/proc/%s/cmdline'%(file),'r').read()
17 proc = "%s %s "%(file,cmdline)
18
19 if script in proc :
20 svcRunning=True
21
22 if svcRunning==False:
23 syslog.syslog(syslog.LOG_INFO,'%s process created '%(prefix) )
24 subprocess.Popen(['/usr/bin/python','dummyService.py'],cwd='/home/userx/svc')