Apr 17, 2015

QNAP/Linux - Python Programming 01 (Setup and the First Program)

Python is programming language that lets you work quickly and integrate systems more effectively. It's really true!
https://www.python.org/
Are simple and easy languages not powerful to do big jobs? Job trends from Indeed.com show that Python is the hottest programming language in programming markets in recent years.
http://www.indeed.com/jobtrends?q=Java,+PHP,+Perl,+.Net,+Python&relative=1
As an open source project, Python is a booming community with numerous useful open sourced packages. The Python Package Index is a repository of software for the Python programming language. There are currently 58,114 packages here (April, 2015).

Python is supported in major OS platforms: Windows, Mac and Linux. QNAP NAS (QTS) is Linux/Ubuntu based OS, QNAP App Center also supports Python 2.7. However, developing QNAP Apps must consider the platform-support, workable packages on Linux/Ubuntu do not guarantee the same feature on QTS (a light-weight version of Linux for NAS performance and security issues).
http://download.qnap.com/QPKG/Python_2.7_arm-x19.zip
Many Python packages may not work on QTS. QNAP Optware (IPKG) ports many useful Linux packages and libraries into QTS, let's check its supports on Python 2.7. Unfortunately, the upgrade to 2.7 does not mean upgrades of all packages.
Optware: search "py27" to get packages for Python 2.7
There are more packages supported in Python 2.6. Therefore, Python 2.6 seems a better tool than 2.7 for developing QNAP Apps.
Optware: search "py26" and get many packages for Python 2.6
Let's install python26 and some useful packages with IPKG. I wrote following commands in the script file "install_py26" and execute in the folder "/share/homes/Apps/py26".
ipkg install python26
# some packages for 2.6 version
ipkg install py26-setuptools # install packages with .py source
ipkg install py26-lxml       # parse HTML, XML
ipkg install py26-cjson      
# parse JSON
ipkg install py26-psycopg2   # connect PostgreSQL

To guarantee the current version of Python is 2.6, I wrote following commands in the file "setpy26". Finally, the version is "Python 2.6.8".
unlink /opt/bin/python # unlink other Python version installed by Optware
unlink /urs/bin/python # unlink other Python version installed from App Center
ln -s /opt/bin/python2.6 /usr/bin/python # link python to 2.6 version
ln -s /opt/bin/python2.6 /opt/bin/python
python --version # check current version
Python packages are developed based on open source projects, that is source codes can be manually downloaded and installed into Python 2.6 (but it may not work for all packages since module dependencies are different between Linux and QTS). Following commands download pyquery package with wget, uncompress (tar) sources into a directory and install through its setup.py.
wget https://pypi.python.org/packages/source/p/pyquery/pyquery-1.2.4.tar.gz --no-check-certificate
tar xf pyquery-1.2.4.tar.gz
cd pyquery-1.2.4
python setup.py install
cd ..
Manually install Python package
 Then, test the installed package in Python command mode with "import".
"import pyquery" tested ok
The following codes stored in the file "geturl.py". Execute the .py file will download 2 HTML files from my blogs and store them as 'url/0' and 'url/1'.
import urllib
urls=('http://ilearnblogger.blogspot.tw/2015/04/qnaplinux-tool-postgresql-from-9210.html','http://ilearnblogger.blogspot.tw/2015/03/qnaplinux-tool-postgresql-database.html')
i = 0
for url in urls:
   file_path = './urls/' + str(i)
   urllib.urlretrieve (url, file_path)
   i = i+1
Execute .py programs
Source code: https://github.com/ilearnblogger/pyp/blob/master/geturl.py

Learning Python is easy from the online website: Tutorialspoint - Python Programming. Enjoy!


No comments :

Post a Comment