Category Archives: Python
ChatterBot Tutorial
หัวข้อ
- Read only mode
- Setting the storage adapter
- Input and output adapters
- Specifying logic adapters
- Getting a response from your chat bot
ChatterBot
หัวข้อ
- ติดตั้ง ChatterBot จาก PyPi
- ตรวจสอบเวอร์ชันที่ติดตั้ง
- การอัพเกรดให้เป็นเวอร์ชันล่าสุด
- ทดลองสร้าง chat bot
- สร้าง chat bot รองรับภาษาไทย
S_ISDIR
S_ISDIR
S_ISDIR checks the file mode m to see whether the file is a directory. If so it returns True
freepascal.org
#ifndef S_ISDIR
#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
#endif
#ifndef S_ISREG
#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
#endif
#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
#ifndef S_ISREG
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
#endif
ตรวจสอบหารายชื่อ email จาก text
[code lang=”python”]
import re
email = re.findall("\w+\@\w+\.\w+", text)
if m:
print m
# return list
[/code]
Writing MySQL Scripts with Python DB-API
[code lang=”python”]
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 ()
[/code]
ที่มา: 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
If you get the error “mysql_config not found” follow the instructions here,
which are essentially this: Open up site.cfg from the binding distribution and
make sure it has this (the path to mysql_config should match the actual one
on your system):
mysql_config = /usr/local/mysql/bin/mysql_config
$ python -c “import MySQLdb”
Traceback (most recent call last):
File “<string>”, line 1, in <module>
File “build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py”, line 19, in <module>
ImportError: dlopen(./_mysql.so, 2): Library not loaded: libmysqlclient.16.dylib
Referenced from: /Users/phaisarn/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
Reason: image not found
สาเหตุคือ _mysql.so อ้างอิงไปที่ไฟล์ libmysqlclient.16.dylib แต่ไม่สามารถหาไฟล์นี้พบ
เนื่องจาก libmysqlclient.16.dylib เป็น MySQL client ดังนั้น จึงต้องตรวจสอบไปที่ MySQL
แก้ไข หาไฟล์ ibmysqlclient.16.dylib ว่าอยู่ที่ไหน ปรากฏว่าอยู่ที่/opt/local/lib/mysql5/mysql
ทำการตรวจสอบ DYLD_LIBRARY_PATH ด้วยคำสั่ง echo $DYLD_LIBRARY_PATH
ทำการ export DYLD_LIBRARY_PATH=/opt/local/lib/mysql5/mysql
เรียกคำสั่ง $ python -c “import MySQLdb” ก็จะไม่มี error เป็นอันเรียบร้อย
แต่ถ้าหาไฟล์ libmysqlclient.16.dylib ไม่เจอ ให้ใช้ MacPorts
http://www.macports.org/install.php
ทำการติดตั้ง MacPorts จากนั้นเรียกคำสั่ง
/opt/local/bin/port install py26-mysql
ก็จะได้ไฟล์ libmysqlclient.16.dylib มา
การให้ Python เรียก MySQLdb
import MySQLdb
conn = MySQLdb.connect(host=’localhost’,user=’xxx’,passwd=’xxx’,db=’mysql’, unix_socket=’/tmp/mysql.sock’)
ที่มา: birdhouse.org, stackoverflow.com, macports.org, mangoorange.com (1.2.2)
Python & command line
ตัวอย่างคำสั่งการ Import แบบ command line โดยที่ยังไม่ต้องเข้าไปที่ shell ของ python
[code lang=”python”]
python -c "import MySQLdb"
[/code]
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)
ที่มา: stackoverflow.com, docs.python.org, pleac.sourceforge.net