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 รองรับภาษาไทย
การติดตั้ง numpy บน Python 2.7
อัพเกรด pip
ให้เป็นเวอร์ชันล่าสุด
> cd c:\Python27 > python -m pip install --upgrade pip Collecting pip Downloading pip-9.0.2-py2.py3-none-any.whl (1.4MB) 100% |################################| 1.4MB 565kB/s Installing collected packages: pip Found existing installation: pip 9.0.1 Uninstalling pip-9.0.1: Successfully uninstalled pip-9.0.1 Successfully installed pip-9.0.2
ดาว์นโหลด numpy จะได้ไฟล์นามสกุล .whl
คัดลอกไฟล์ที่ดาว์นโหลดมา ไปไว้ที่ C:\Python27\Scripts
ติดตั้ง numpy
> cd c:\Python27\Scripts > pip2.7.exe install numpy-1.14.2-cp27-none-win32.whl Processing c:\python27\scripts\numpy-1.14.2-cp27-none-win32.whl Installing collected packages: numpy Successfully installed numpy-1.14.2
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
import re email = re.findall("\w+\@\w+\.\w+", text) if m: print m # return list
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
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
python -c "import MySQLdb"