Python

warning: Creating default object from empty value in /home/www/virtual/phaisarn.com/sun/htdocs/modules/taxonomy/taxonomy.pages.inc on line 33.

ตรวจสอบหารายชื่อ email จาก text

import re
email = re.findall("\w+\@\w+\.\w+", text)
if m:
    print m
# return list
ที่มา: http://docs.python.org/library/re.html

Writing MySQL Scripts with Python DB-API

 
 

import MySQLdb

conn = MySQLdb.connect (host = "localhost",
user = "testuser",
passwd = "testpass",
db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()
ที่มา: kitebird.com/

 

Python: sys.path

path to module
import sys
sys.path

Install MySQLdb for Python

Download and install MySQL package installer
Download MySQLdb mysql-python (1.2.3)
Install MySQLdb 
    ARCHFLAGS='-arch x86_64' python setup.py build
    ARCHFLAGS='-arch x86_64' python setup.py install

Python & command line

ตัวอย่างคำสั่งการ Import แบบ command line โดยที่ยังไม่ต้องเข้าไปที่ shell ของ python
python -c "import MySQLdb"

Python: converting datetime to POSIX time

Python: converting datetime to POSIX time

import time, datetime

d = datetime.datetime.now()
print time.mktime(d.timetuple())

 

Python: converting POSIX time to datetime

import datetime

datetime.datetime.fromtimestamp(1299030400)

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
 

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

การจับเวลาด้วย 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
  >>> 

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

แสดงเวลาว่าเครื่องเปิดมานานเท่าไหร่แล้ว (เฉพาะ Linux) ด้วย Python
การดูว่าเครื่องเปิดมานานเท่าไหร่แล้วบน Linux ปกติใช้คำสั่ง uptime แต่ถ้าต้องการเขียน script เพื่อรายงานเวลาว่าเครื่องเปิดมานานเท่าไหร่แล้ว สามารถใช้ Python เขียน Code ได้ดังนี้

Syndicate content