Wednesday, November 26, 2014

Change SQLLite to PyMySQL

Another task is to change the database from SqlLite to MySQL (Hey, we need to use real database here). And since I'm using SQLAlchemy
Changes in development.ini

Change
sqlalchemy.url = sqlite:///%(here)s/xyzdb.db
To
sqlalchemy.url = mysql+pymysql://root:system@localhost/xyz?charset=utf8
The charset=utf8 is important for unicode

And for sure, you need to install pymysql before

% easy_install pymysql
Why I use PyMySql? Long story make short, since it's pure Python MySQL client. For another DB client library (with SQLAlchemy), check http://docs.sqlalchemy.org/en/latest/dialects/mysql.html

test-connection.py :
import sqlalchemy
from sqlalchemy.sql import select
from sqlalchemy import Table, MetaData


def init():
    try:
        server = 'localhost'
        db = 'pyramid'
        login = 'root'
        passwd = 'toordrowssap'
        engine_str = 'mysql+pymysql://{}:{}@{}/{}'.format(login, passwd, server, db)
        engine = sqlalchemy.create_engine(engine_str, echo=False, encoding='utf-8')
        connection = engine.connect()
        metadata = MetaData()
        t_servers = Table('auth_permission', metadata, autoload=True, autoload_with=engine)
        s = select([t_servers])
        result = connection.execute(s)
        for row in result:
            print(row['name'])
    except Exception:
        raise
    finally:
        connection.close()
if __name__ == '__main__':
    init()

Tuesday, November 25, 2014

Upgrading Pyramid Application

Environment:

  • Python 3.4 (using virtualenv)
  • Pyramid 1.5.2
During an upgrade of a project developed in previous release of Pyramid (and Python 2.x), In encountered error(s) caused by Paste. Luckily, there is another person who encounter the same issue and "document" it very well here. My steps for troubleshoot this issue more or less is the same:

Changes in development.ini

Remove
[pipeline:main]
pipeline =
    egg:WebError#evalerror
    {{myproject}}
Change
[app:{{project}}]
To
[app:main]

Changed server from Paste
[server:main]
use = egg:Paste#http
host = 0.0.0.0
port = 6543
 To pyramid
[server:main]
use = egg:pyramid#wsgiref
host = 0.0.0.0
port = 6543
Changes in setup.py changed requires from
requires = [
    'pyramid',
    'pyramid_beaker',
    'SQLAlchemy',
    'transaction',
    'zope.sqlalchemy',
    'WebError',
]
to
requires = [
    'pyramid',
    'pyramid_beaker',
    'SQLAlchemy',
    'transaction',
    'zope.sqlalchemy',
]
It's important to remove WebError

[Update 4/3/2015]
Important:
It's important to remove "paste" from your system, since somehow pyramid will encounter error when this package exists in your system (virtualenv). "pastedeploy" is acceptable

Friday, November 21, 2014

Nosetests Output to a File

If you familiar with python, I'm sure you also familiar with nosetests. Running nosetests is a piece of cake (although writing the test cases is sadly... not). But how to output the test result(s) to a file? I use the normal ">" method:
nosetests > result.txt
Surprisingly, it's not working :(
Looking on the internet (yes, google), I found 2 ways to do it:

1. Indeed by using ">", but the "complete" command is:
nosetests > result.txt 2>&1
2. And by using nosetests option:
nosetests --with-xunit --xunit-file=result.xml
Will produce a xml format file

I prefer the 1st option, txt file, cleaner.

Sunday, November 16, 2014

lxml and Installing a Python Package with a .whl Extension

So, story short: I learn about Ringo as I try to get some example of Pyramid Framework. After download the project, when running the setup, one of the library needed is lxml - and it's fail to install with easy_install.
On installation guide, I found out that on Windows (too bad right, I'm using Windows), the installer expecting a build-tools (which normally is VC++), which indicated in:
For MS Windows, recent lxml releases feature community donated binary distributions, although you might still want to take a look at the related FAQ entry. If you fail to build lxml on your MS Windows system from the signed and tested sources that we release, consider using the binary builds from PyPI or the unofficial Windows binaries that Christoph Gohlke generously provides.
Since no-way to install those crap in my machine, my only choice is "unofficial Windows binaries" that mentioned in above, go to the link, and download-it. But wait, a .whl file?
OK, it's a wheel file, but how I install it?

First use easy_install (or pip) to install wheel
easy_install wheel

Then install the package using pip, somehow easy_install cannot work (correct me if I wrong)
pip install package-name.whl

And ta-da, the package installed.
And after that, I found out that Ringo is "too-huge-and-not-easily-customize", remind me about Django. Seems like I love to make my hands dirty :(

Saturday, November 8, 2014

Preparing Python Environment

Yes, I also using Python....
And since I use it in windows, preparing the environment it's a bit different with if its on Linux
The good thing is on python version is 3.x, the virtualenv is already part of the python basic installer.

To create new virtualenv in d:\python\venv\pylons:
C:\Users\Desson>python -m venv d:\python\venv\pylons
To activate virtualenv:
C:\Users\Desson>d:\python\venv\pylons\Scripts\activate.bat
And now, the virtuanenv is active:
(pylons) C:\Users\Desson>
Now you can work with this 'environment' and install 'anything you need' via easy_install (or pip)
Add virtualenv to Pycharm
I'm using Pycharm as my main IDE (Beside Notepad+, for small tasks), since we have new virtualenv, we need to bind this virtualenv. Jetbrains already documented the process in https://www.jetbrains.com/pycharm/help/adding-existing-virtual-environment.html, I only copied here (with my own words):
  1. Go to Project Settings, find Project Interpreter page, click the 'gear-wheel' button on the right.
  2. In the drop-down list, choose Add local 
  3. In the Select Python Interpreter dialog box that opens, choose the desired Python executable (in our virtualenv), and click OK.
The result should be like this:
And after that? Happy coding...