Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

new post

  • Loading branch information...
commit 8944a0b9ae0960ced0d7ad86ee6d34df16bd0e00 1 parent edfca30
lepture authored
2  .config
@@ -15,7 +15,7 @@ year_archive_template = year_archive.html
author = lepture
-;disqus = lepture
+disqus = lepture
sitename = Just lepture
siteurl =
feed =
3  _static/src/screen.less
@@ -246,6 +246,9 @@ h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover {
border-left: 3px solid lighten(@basecolor, 20%);
background: #fff;
+.hentry p {
+ line-height: 1.8;
.hentry .section {
padding: 0.8em 0;
margin: 0.4em 0;
2  content/index.rst
@@ -42,7 +42,7 @@ It's now powered by `Felix Felicis </project/liquidluck>`_ which is a static blo
by me, written in python. It's not perfect, but I wrote it, I can improve it. The source code
of this site can be found at `Github Blog Source <>`_ .
-If you are interested in this project, you should check this article `Mechanism of Felix Felicis <>`_
+If you are interested in this project, you should check this article `Mechanism of Felix Felicis <>`_
6 content/links.rst
@@ -10,12 +10,16 @@ Links / 交集
-+ `Ping <>`_
++ `Ping Blog <>`_
++ `Ping Python China <>`_
+ `Webmaster <>`_
+ `Analytics <>`_
++ `Python China <>`_
Wuhan University
21 content/project/june.rst
@@ -0,0 +1,21 @@
+:template: page.html
+:date: 2012-03-04
+:folder: project
+:public: false
+:nav: home
+:github: lepture/june
+June is a forum
+June for Tornado
+June for WSGI
+June for SAE
82 content/work/2012/mysql-sqlalchemy.rst
@@ -1,5 +1,5 @@
-Fix mysql has gone away with SQLAlchemy in tornado
+MySQL and SQLAlchemy in tornado
:folder: work
:date: 2012-02-09 21:15
@@ -10,8 +10,10 @@ Fix mysql has gone away with SQLAlchemy in tornado
- tornado
-I just started a project at , which is built on tornado with database
-of mysql.
+Fix MySQL has gone away
+I just started a new project, which is built on tornado with database of mysql.
It's a new project, and there is no user. Therefore, the database can be inactive for a long
time, it then occurred to me that mysql has gone away!
@@ -23,51 +25,65 @@ Then I figured it out, I am using tornado! I can set a PeriodicCallback to ping
pool recycle time, so that mysql can not go away.
I have written a `wrap for SQLAlchemy <>`_ to make it a
-little Django like. Then I add this feature to it as:
+little Django like. Then I add this feature to it.
.. sourcecode:: python
- class SQLAlchemy(object):
- """
- Example::
+ if 'pool_recycle' in kwargs:
+ # ping db, so that mysql won't goaway
+ PeriodicCallback(self._ping_db, kwargs['pool_recycle'] * 1000).start()
- db = SQLAlchemy("mysql://user:pass@host:port/db", pool_recycle=3600)
+.. sourcecode:: python
- from sqlalchemy import Column, String
+ def _ping_bd(self):
+ self.session.execute('show variables')
- class User(db.Model):
- username = Column(String(16), unique=True, nullable=False)
- password = Column(String(30), nullable=False)
- >>> db.create_db()
- >>> User.query.filter_by(username='yourname')
+Master Slave in SQLAlchemy
- """
- def __init__(self, database, **kwargs):
- self.engine = create_engine(database, **kwargs)
- self.session = self.create_session()
- self.Model = self.create_model()
- if 'pool_recycle' in kwargs:
- # ping db, so that mysql won't goaway
- PeriodicCallback(self._ping_db, kwargs['pool_recycle'] * 1000).start()
+I hate SQL! I know that orm sucks, but SQL is killing me. I am using SQLAlchemy as the
+orm engine, but it's not that easy to implement in a project. I wrote some snippets to
+make it easy to use. And I introduced a new feature in the snippet today! (2012-02-28)
- def create_session(self):
- session = sessionmaker(bind=self.engine, query_cls=DjangoQuery)
- return scoped_session(session)
+Master and Slave support in SQLAlchemy! What a tremendous feature. There are some answers on
+Stack Overflow, but I thought mine is more elegant (maybe I am wrong).
- def create_model(self):
+.. sourcecode:: python
+ @property
+ def Model(self):
+ if hasattr(self, '_base'):
+ base = self._base
+ else:
base = declarative_base(cls=Model, name='Model')
+ self._base = base
+ if self.slaves:
+ slave = random.choice(self.slaves)
+ base.query = slave.query_property()
+ else:
base.query = self.session.query_property()
- return base
+ return base
- def create_db(self):
- return self.Model.metadata.create_all(self.engine)
- def _ping_db(self):
- self.session.execute('show variables')
+You may have a little confuse, have a better understanding with the source code at Github_ .
+**You must read the whole code before continuing!**
+After it is integrated in your project! You can read data from slave database with:
+For writing data into master database, using ``db.session.add(model)`` .
+For updating data, you should query the model with:
+Always remember, ``db.session`` is master, ``Model.query`` is slave,
+``Model.query`` is read-only!
-You may have a little confuse, have a better understanding with the source code at Github_ .
.. _document:
.. _Github:
271 content/work/2012/tornado-project.rst
@@ -0,0 +1,271 @@
+How I develop a tornado project
+:date: 2012-03-13 17:13
+:folder: work
+ - python
+ - tornado
+ - vim
+For the past year, I have been working on all my web projects in tornado_ ,
+in order to make it easier to manage a project, I have build some useful tools,
+and I would like to share some with you which, I hope, will sort of inspire you.
+I did not invent it myself, thanks to the great project of poweredsites_ by felinx_.
+I learned a lot from it, and I think it's a great site you should know.
+Of course there are better solutions or tricks to do something that I mention.
+If you know any of them, leave a comment below, or `send a tweet`_ to me.
+It is very important that you organize a project folders properly,
+and this is the very part which I learned from poweredsites.
+A glance at my tornado project folder structure:
+ License
+ Makefile <------- or
+ <------- hi, it's a python package
+ assets/ <------- why not static?
+ img/
+ js/
+ less/ <------- hi, I write less
+ {project}/ <------- your project name
+ templates/
+ handlers/
+ locale/
+ lib/
+A live demo can be found at june_ , which is the forum engine that powered `Python China`_.
+And welcome Chinese Pythoners, we need you.
+Why assets
+A compatible WSGI application's normal static directory names are **static** and **media**,
+why should I name it **assets** ?
+Actually it will turn into **static**.
+All images will be copied to static from assets,
+all javascript will be compressed and output to static.
+Assets contains the very pure source code,
+as you can see above that I write less_ rather than css.
+It is very important to keep your source repository clean,
+then I add ``static/`` to .gitignore.
+Every time when I need static, I produce it from assets.
+Makefile and fabric
+How could I produce static from assets? I just ``make static``.
+Makefile is an ancient tool which specify how to derive the target program.
+It is a dependency-tracking build utilities, usually used in building C program.
+But it's more than that, because it describes the dependencies among commands,
+and supports all shell programs, I used it as a set of alias.
+A example of ``make static`` in Makefile:
+ static: copyimg less compilejs
+ less:
+ lessc -nc assets/less/site.less > static/css/site.css
+ compilejs:
+ uglifyjs -nc assets/js/jquery.js > static/js/app.js
+ uglifyjs -nc assets/js/site.js >> static/js/app.js
+ copyimg:
+ copy -r assets/img/* static/img/
+If you need a more powerful tools, you should try fabric_.
+Fabric is a Python library and command-line tool for streamlining the use of
+SSH for application deployment or systems administration tasks.
+You can learn more about fabric by reading `fabric docs`_.
+Python package
+Even if it's a web project, I would make it a python package that you can install.
+By ``python install``, it will create some command-line tools for easier deployment.
+Take june_ as an example, (actually you will be able to ``pip install june`` in the future)
+it will create ``june.server`` and ```` for you. With ```` you can easily
+create config file, database, and super admin user. With ``june.server`` you can easily set
+up a server.
+If you have no idea on how to create a python package, check setuptools_.
+The official distribution tools is Distutils_ , but setuptools is easier.
+The very heart of a distribution is ````, an example of june:
+.. sourcecode:: python
+ #!/usr/bin/env python
+ # -*- coding: utf-8 -*-
+ from setuptools import setup, find_packages
+ setup(
+ name='june',
+ version='1.0',
+ author='Hsiaoming Young',
+ author_email='',
+ url='',
+ packages=find_packages(),
+ description='June: a forum',
+ entry_points={
+ 'console_scripts': [
+ 'june.server=',
+ '',
+ ],
+ },
+ install_requires=[
+ 'python-memcached',
+ 'markdown',
+ 'pygments',
+ 'tornado',
+ 'SQLAlchemy',
+ ],
+ include_package_data=True,
+ license='BSD License',
+ )
+If you are not distributing your web project, try to. By the way, you really should develop
+with virtualenv_. You will find it very useful.
+It's a pity to waste your time in creating a lot of folders and touching lots of files,
+and then copying some code to them.
+I wrote a snippet code called tornadmin.py_ to do the job.
+The name ``tornadmin = tornado + admin``. It's not powerful enough, but I will improve it.
+Database and Forms
+Tornado has a default mysql database, but no form frameworks.
+I cannot stand the stupid Structured Query Language (aka SQL).
+I am using SQLAlchemy instead of raw sql. I know it's slower than raw sql,
+but most web sites are just small enough, they are not and they will not be facebook.
+But SQLAlchemy is not that easy to use, I have written a wrapper snippet code that makes
+it a little django-like. And the snippet code also provides a master-slave database solution,
+`MySQL and SQLAlchemy`_ tells more on this topic.
+Thanks to `Armin Ronacher`_ for the sweet piece of DjangQuery,
+though the code has some bugs (I fixed it in my code).
+Detail of the code can be found at database.py_ .
+wtForms and FormEncode are the only form frameworks that I have tried.
+FormEncode is faster than wtForms, but wtForms is more django-like that it is easier to use.
+Solution on how to implement wtForms to tornado can be found at forms.py_ .
+Actually I don't use wtForms now, I have written another form framework called tforms_ which
+is a clone of wtForms.
+And why should I create tforms? It solved many unicode bugs that wtForms has, and provides
+a locale support solution for tornado.
+Oh, I have not implement it in June, because June's form is not that complex.
+And take your own risk to import it.
+I always think it's significant to have a good code style,
+the pretty layout of the code makes me happy. PEP8 has described a style guide for python,
+and I am always checking my python code style with it.
+You can install pep8: ``pip install pep8`` , and valide your code with the command ``pep8``.
+But pylint is more than that, if you are an IDE user, your IDE may tell you that you have
+imported an unused module, but vim will not. Another fabulous lint tools is pyflakes, it
+will tell you more than code style.
+I have introduced pep8 and pyflakes, and now I will present you flake8 which is a combination
+of pep8 and pyflakes. Install it with ``pip install flake8``.
+flake8 for Vim
+I am an addictive vim user, and if you are too, the good news is that you can implement
+flake8 in vim.
+With the very fantastic syntax checking plugin syntastic_ , you can easily fix your stupid
+syntax errors. Just install it, and edit your .vimrc:
+ let g:syntastic_python_checker="path/to/bin/flake8"
+By the way, manage your vim plugin with Pathogen_ or Vundle_ . For Chinese visitors,
+`this post`_ will tell how I manage my vim plugins.
+I haven't written such long a post yet, and I hope it will at least help you a little.
+I learned many things from others' code, and they really inspire me.
+My poor English makes me hard to express the very thought inside me, however, a coder
+can find the value in the code. Follow me on Github_ , code tells more.
+.. _tornado:
+.. _poweredsites:
+.. _felinx:
+.. _`send a tweet`:
+.. _june:
+.. _`Python China`:
+.. _less:
+.. _fabric:
+.. _`fabric docs`:
+.. _syntastic:
+.. _setuptools:
+.. _Distutils:
+.. _`MySQL and SQLAlchemy`:
+.. _`Armin Ronacher`:
+.. _tforms:
+.. _Pathogen:
+.. _Vundle:
+.. _`this post`:
+.. _Github:
+.. _virtualenv:
Please sign in to comment.
Something went wrong with that request. Please try again.