Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

new post

  • Loading branch information...
commit 8944a0b9ae0960ced0d7ad86ee6d34df16bd0e00 1 parent edfca30
lepture authored
View
2  .config
@@ -15,7 +15,7 @@ year_archive_template = year_archive.html
[context]
author = lepture
-;disqus = lepture
+disqus = lepture
sitename = Just lepture
siteurl = http://lepture.com
feed = http://feeds.lepture.com/lepture
View
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;
View
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 <https://github.com/lepture/lepture.com>`_ .
-If you are interested in this project, you should check this article `Mechanism of Felix Felicis <http://dev.lepture.com/work/mechanism-of-liquidluck/>`_
+If you are interested in this project, you should check this article `Mechanism of Felix Felicis <http://lepture.com/work/mechanism-of-liquidluck/>`_
Elsewhere
View
6 content/links.rst
@@ -10,12 +10,16 @@ Links / 交集
Tools
--------
-+ `Ping <http://blogsearch.google.com/ping?url=http://lepture.com/archive/>`_
++ `Ping Blog <http://blogsearch.google.com/ping?url=http://lepture.com/archive/>`_
+
++ `Ping Python China <http://blogsearch.google.com/ping?url=http://python-china.org/feed>`_
+ `Webmaster <https://www.google.com/webmasters/tools/home>`_
+ `Analytics <https://www.google.com/analytics/settings/home>`_
++ `Python China <http://python-china.org>`_
+
Wuhan University
-----------------
View
21 content/project/june.rst
@@ -0,0 +1,21 @@
+June
+=============
+
+: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
+------------
View
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 http://start.lepture.com , 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 <http://lepture.com/work/tornado-ext/>`_ 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:
+
+``Member.query.filter_by(username='lepture')``
+
+For writing data into master database, using ``db.session.add(model)`` .
+
+For updating data, you should query the model with:
+
+``db.session.query(Member).filter_by(username='lepture')``
+
+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: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html
.. _Github: https://github.com/lepture/tornado.ext/blob/master/database.py
View
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
+:tags:
+ - 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.
+
+
+Structure
+----------
+
+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:
+
+::
+
+ README
+ License
+ Makefile <------- or fabfile.py
+ setup.py <------- hi, it's a python package
+ assets/ <------- why not static?
+ img/
+ js/
+ less/ <------- hi, I write less
+ {project}/ <------- your project name
+ __init__.py
+ app.py
+ tools.py
+ urls.py
+ models.py
+ 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 setup.py 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 ``june.tools`` for you. With ``june.tools`` 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 ``setup.py``, an example setup.py 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='lepture@me.com',
+ url='http://lepture.com/project/june',
+ packages=find_packages(),
+ description='June: a forum',
+ entry_points={
+ 'console_scripts': [
+ 'june.server= june.app:run_server',
+ 'june.tools= june.tools:main',
+ ],
+ },
+ 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.
+
+Tools
+~~~~~~
+
+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.
+
+Database
+~~~~~~~~
+
+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_ .
+
+Forms
+~~~~~~
+
+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.
+
+PyLint
+---------
+
+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.
+
+
+Conclusion
+-----------
+
+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: http://www.tornadoweb.org
+.. _poweredsites: http://poweredsites.org
+.. _felinx: http://feilong.me
+.. _`send a tweet`: https://twitter.com/lepture
+.. _june: https://github.com/lepture/june
+.. _`Python China`: http://python-china.org
+.. _less: http://lesscss.org
+.. _fabric: http://fabfile.org
+.. _`fabric docs`: http://docs.fabfile.org
+.. _syntastic: https://github.com/scrooloose/syntastic
+.. _tornadmin.py: https://github.com/lepture/dotfiles/blob/master/dotpy/tornadmin.py
+.. _setuptools: http://packages.python.org/an_example_pypi_project/setuptools.html
+.. _Distutils: http://docs.python.org/distutils/index.html
+.. _`MySQL and SQLAlchemy`: http://lepture.com/work/mysql-sqlalchemy/
+.. _`Armin Ronacher`: http://lucumr.pocoo.org/
+.. _database.py: https://github.com/lepture/tornado.ext/blob/master/database.py
+.. _forms.py: https://github.com/lepture/tornado.ext/blob/master/forms.py
+.. _tforms: https://github.com/lepture/tforms
+.. _Pathogen: http://github.com/tpope/vim-pathogen
+.. _Vundle: https://github.com/gmarik/vundle
+.. _`this post`: http://lepture.com/work/manage-vim/
+.. _Github: https://github.com/lepture
+.. _virtualenv: http://www.virtualenv.org
Please sign in to comment.
Something went wrong with that request. Please try again.