MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 13 as of 2013-10-09 11:41:52
  • Python

Python

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

Links:

  • http://docs.python.org/2/library/

  • http://www.tutorialspoint.com/python/

UTF-8

At the start of source code files:

Toggle line numbers
   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

Time and date

Toggle line numbers
   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 # from timestamp to string date
   9 import datetime
  10 print(datetime.datetime.fromtimestamp(1284101485).strftime('%Y-%m-%d %H:%M:%S'))
  11 #
  12 def toUTCDateStr(timestamp):
  13     return datetime.datetime.utcfromtimestamp( timestamp ).strftime('%Y-%m-%d %H:%M:%S ')

Write and reading data for a plist file

A plist file stores data in XML format.

Toggle line numbers
   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

Toggle line numbers
   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

Toggle line numbers
   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

Toggle line numbers
   1 import unittest
   2 
   3 class SimpleWidgetTestCase(unittest.TestCase):
   4     def setUp(self):
   5         self.widget = Widget('The widget')
   6 
   7     def tearDown(self):
   8         self.widget.dispose()
   9         self.widget = None

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:

Toggle line numbers
   1 syntax on
   2 filetype on
   3 au BufNewFile,BufRead *.wsgi set filetype=python
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01