QR Code: in Python

libpng12-0
libpng12-dev
pkg-config

Install libqrencode
http://fukuchi.org/works/qrencode/index.en.html
Libqrencode is a C library for encoding data in a QR Code symbol, a kind of 2D symbology that can be scanned by handy terminals such as a mobile phone with CCD. The capacity of QR Code is up to 7000 digits or 4000 characters, and is highly robust.
Download: http://fukuchi.org/works/qrencode/qrencode-3.1.1.tar.gz

Compile & install
./configure
make
make install

Usage
qrencode -h

Install pyqrencode
http://github.com/bitly/pyqrencode
Python bindings for libqrencode

Download
http://github.com/bitly/pyqrencode/archives/master

Install
python setup.py install

Usage
python test_qr.py

Problem
$ python test_qr.py
Traceback (most recent call last):
File “test_qr.py”, line 1, in <module>
from qrencode import Encoder
ImportError: libqrencode.so.3: cannot open shared object file: No such file or directory
Solution
$ export LD_LIBRARY_PATH=/usr/local/lib

QR Code: in Python

pyqrcode
Project site at: http://pyqrcode.sourceforge.net/
Download source code: http://downloads.sourceforge.net/pyqrcode/pyqrcode-0.2.1.tar.gz?use_mirror=
Install from sourcecode on Ubuntu 10.04

$ sudo apt-get install jcc sun-openjdk-6-jdk python-imaging python-setuptools python-dev
For Python 2.6
replace “-m jcc” with “-m jcc.__main__” in Makefile
make
sudo make install

code example

[code lang=”python”]
#!/usr/bin/env python
# coding: utf-8
#
# pyqrcode sample encoder

import sys, qrcode

e = qrcode.Encoder()
image = e.encode(‘woah!’, version=15, mode=e.mode.BINARY, eclevel=e.eclevel.H)
image.save(‘out.png’)
[/code]

There is a error like this.
Traceback (most recent call last):
File “sample1.py”, line 6, in <module>
import sys, qrcode
File “/usr/local/lib/python2.6/dist-packages/qrcode-0.2.1-py2.6-linux-i686.egg/qrcode/__init__.py”, line 21, in <module>
_qrcode._setExceptionTypes(JavaError, InvalidArgsError)
AttributeError: ‘module’ object has no attribute ‘_setExceptionTypes’

การจับเวลาด้วย Python

ตัวอย่างการใช้ฟังก์ชั่น clock() and time() บน Windows and Linux
Run on linux
>>> from time import clock, time
>>> print clock(), time()
0.01 1169573460.96
>>> print clock(), time()
0.01 1169573463.76
>>> print clock(), time()
0.01 1169573467.09
>>>
Run on Windows
>>> from time import clock, time
>>> print clock(), time()
7.54285810068e-006 1169574534.84
>>> print clock(), time()
3.32073322168 1169574538.16
>>> print clock(), time()
7.32428004118 1169574542.15
>>> #!/usr/bin/python

จะเห็นได้ว่าฟังก์ชั่น time() เหมาะกับการนำมาใช้จับเวลามากกว่า (แต่ก็ไม่ตรงซะทีเดียว สู้คำสั่ง command line:time  ไม่ได้)
import time

[code lang=”python”]
def procedure():
time.sleep(2.5)

# measure process time
t0 = time.clock()
procedure()
print time.clock() – t0, "seconds process time"

# measure wall time
t0 = time.time()
procedure()
print time.time() – t0, "seconds wall time"

0.0 seconds process time
2.50023603439 seconds wall time
[/code]

ที่มา: stackoverflow.comdocs.python.orgtutorialspoint.com

แสดงเวลาว่าเครื่องเปิดมานานเท่าไหร่แล้ว (เฉพาะ Linux) ด้วย Python

แสดงเวลาว่าเครื่องเปิดมานานเท่าไหร่แล้ว (เฉพาะ Linux) ด้วย Python
การดูว่าเครื่องเปิดมานานเท่าไหร่แล้วบน Linux ปกติใช้คำสั่ง uptime แต่ถ้าต้องการเขียน script เพื่อรายงานเวลาว่าเครื่องเปิดมานานเท่าไหร่แล้ว สามารถใช้ Python เขียน Code ได้ดังนี้
[code lang=”c”]
def uptime():
try:
f = open("/proc/uptime")
contents = f.read().split()
f.close()
except:
return "Cannot open uptime file: /proc/uptime"
total_seconds = float(contents[0])
# Helper vars:
MINUTE = 60
HOUR = MINUTE * 60
DAY = HOUR * 24
# Get the days, hours, etc:
days = int(total_seconds / DAY)
hours = int((total_seconds % DAY) / HOUR)
minutes = int((total_seconds % HOUR) / MINUTE)
seconds = int(total_seconds % MINUTE)
# Build up the pretty string (like this: "N days, N hours, N minutes, N seconds")
string = ""
if days > 0:
string += str(days) + " " + (days == 1 and "day" or "days") + ", "
if len(string) > 0 or hours > 0:
string += str(hours) + " " + (hours == 1 and "hour" or "hours") + ", "
if len(string) > 0 or minutes > 0:
string += str(minutes) + " " + (minutes == 1 and "minute" or "minutes") + ", "
string += str(seconds) + " " + (seconds == 1 and "second" or "seconds")
return string

print "The system uptime is:", uptime()
[/code]

ที่มา: Python: Uptime script

Python เรียกใช้ Command line

การเรียกใช้ command line จาก Python สามารถเรียกได้โดยใช้คำสั่ง popen
import os
ipFile = “ip.txt”
opFile = “op.txt”
cmd = ‘swath <%s> %s’ % (ipFile, opFile)
f = os.popen(cmd)

หรือใช้ Popen
import subprocess
subprocess.Popen([r”gzip”,”-d”, “x.txt.gz”]).wait()

ที่มา: Subprocess managementMaking System Calls in Perl and Python

การติดตั้ง Pydev

เปิด Eclipse ขึ้นมา จากนั้นไปที่เมนู Help > Install New Software

แล้วทำการ Add Site

http://pydev.org/updates

Configure Interpreter

After installing it, the first thing you must do is configure the Python and/or Jython and/or IronPython interpreter. To configure the interpreter:
  1. Go to: window > preferences > pydev > Interpreter – (Python/Jython/IronPython).
  2. Choose the interpreter you have installed in your computer (such as python.exe, jython.jar or ipy.exe, /usr/bin/python).

ที่มา: http://pydev.org/