Tag Archive: python


How to quickly start a django project in rails way

Situation:

You want to create a scaffold,
Project’s name is “another”, with a application ‘blogs’ in it,
‘blogs’ application has a model named Post, which has
a string field named ‘title’,
a text field named ‘body’,
a boolean field named ‘is_public’.
You also want a api binded with the Post model.
The application should have full CRUD function(Create, Remove, Update, Detail)

Preparing:

requirement.txt:

Django==1.5.1
Markdown==2.3.1
PIL==1.1.7
PyYAML==3.10
South==0.8.1
defusedxml==0.4.1
django-filter==0.6
-e git://github.com/fortable1999/django-generate-scaffold.git#egg=django_generate_scaffold
django-oauth-plus==2.1.2
django-oauth2-provider==0.2.6
djangorestframework==2.3.6
gunicorn==17.5
httplib2==0.8
oauth2==1.5.211
shortuuid==0.3
wsgiref==0.1.2

save above lines into a requirement.txt file,
then:

pip install -r requirement.txt

Howto:

1. Start a Django project from zero:

django-admin.py startproject another
cd another/

2. Edit setting.py:

vim another/settings.py

Fix here:

'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'dev.db',

uncomment this:

'django.contrib.admin',

add these lines into APPS_INSTALLED:

'generate_scaffold',
'rest_framework',
'south',
'blogs',

add there lines to setting.py:

REST_FRAMEWORK = {
    # Use hyperlinked styles by default.
    # Only used if the `serializer_class` attribute is not set on a view.
    'DEFAULT_MODEL_SERIALIZER_CLASS':
    'rest_framework.serializers.HyperlinkedModelSerializer',
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
        'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ]
}

3. Create a empty application:

django-admin.py startapp blogs

6. open a url pattern to application:

vim another/urls.py

uncomment these lines:

from django.contrib import admin
admin.autodiscover()
url(r'^admin/', include(admin.site.urls)),

add there new url pattern:

url(r'^', include('blogs.urls')),

4. create scaffold:

python manage.py generatescaffold blogs Post title:string body:text is_public:bool

5. edit application’s sub urls.py:

vim blogs/urls.py

add there lines after import part:

from django.conf.urls import include
from rest_framework import viewsets, routers
from models import Post
class PostViewSet(viewsets.ModelViewSet): 
    model = Post 
router = routers.DefaultRouter() 
router.register(r'posts', PostViewSet)

then add this into urlpatters:

url(r'api/', include(router.urls)),

6. prepare development envionment database:

python manage.py syncdb
python manage.py schemamigration --initial blogs
python manage.py migrate blogs

7. testing project:

python manage.py runserver

Project done!

References:

Django Rest Framework: http://django-rest-framework.org/
Django generate scaffold: https://github.com/fortable1999/django-generate-scaffold

warning: this article is under build

todo list:

comparing minibufexpl, FuzzyFinder. which is better?

—————–

1. How to install vundle plugin

first, You need vundle. find it here: git://github.com/gmarik/vundle.git

read the install instruction on the git page, download the vundle source, copy it into ~/.vim/bundle/. directory,

then copy the configuration on the git page to your .vimrc(copy it to TOP!).

then run

:BundleInstall

to install all the gears.

2.You need the The-NERD-Tree plugin

find it in github here: https://github.com/scrooloose/nerdtree

here is a interesting video introduction

to install:

Bundle 'git://github.com/scrooloose/nerdtree.git'

then i highly recommend you add this into your .vimrc

nmap n :NERDTreeToggle

if you have a problem on the NERD TREE will not access a subdirectory, add this to .vimrc:

let g:NERDTreeDirArrows=0

3. Install easy-motion plugin.

add this into .vimrc:

Bundle 'Lokaltog/vim-easymotion'

4. Install minibufexpl/fuzzyfinder.

You can find fuzzyfinder in the github here:

fuzzyfinder: https://github.com/vim-scripts/FuzzyFinder

minibufexpl: https://github.com/fholgado/minibufexpl.vim

5. Install surround

You can find the plugin in the github: https://github.com/tpope/vim-surround

6. Install TComment

You can find it here: https://github.com/tomtom/tcomment_vim

7. Install MRU

You can find the MRU plugin here: https://github.com/vim-scripts/mru.vim/

8.Install snipmate

You can find the snipmate here: https://github.com/msanders/snipmate.vim

9. Install delimitMate:

You can find it here: https://github.com/Raimondi/delimitMate/

10. (for HTML code) Install Sparkup:

you can find it here: https://github.com/kogakure/vim-sparkup

11. install neocompleche:

you can find it here: https://github.com/Shougo/neocomplcache

after installed it, Execute

:NeoComplCacheEnable

command or add this into your .vimrc

let g:neocomplcache_enable_at_startup = 1

12. install taglist

you can find it here: https://github.com/vim-scripts/taglist.vim

——————————–

the below plugins is specialized for Python programming language.

some need you compile vim with +python option like:

./configure --enable-pythoninterp=yes

13.(Specialization for Python programming) install pythoncomplete:

you can find it here: https://github.com/vim-scripts/pythoncomplete

14.(for pythoners) install pysmell

you can find here: https://github.com/orestis/pysmell 

but this plugin is a littile different from other plugins as it is wrote in python.

so make sure you have installed python, east_install, and use this before install it to vim:

easy_install pysmell

after that, use bundle to install but remember to make a “plugin” folder in bundle/pysmell, and copy pysmell.vim into it like this:

~/.vim/bundle/pysmell/plugin/pysmell.vim

but this is will work until you make the tag file using:

$ pysmell . #this will make a tag for your project

$pysmell /usr/local/lib/python2.7/site-packages -x <packages you dont want> -o PYSMELLTAGS.django #make a django tag file

15. (specialization for Django) install vim-django-support

you can find it here: https://github.com/lambdalisue/vim-django-support

16. (specialized for Django) install snipmate_for_django:

find the project here: https://github.com/robhudson/snipmate_for_django/

download the .vim file and put it into your snipmate folder.

References:

http://hashnote.net/2011/12/7/12/

Top Vim Plugins http://www.youtube.com/watch?v=_galFWwSDt0

Vim Essential Plugin: SnipMate http://www.youtube.com/watch?v=xV2IsE5OHd4

Vim Essential Plugin: Sparkup:  http://www.youtube.com/watch?v=dB2Q9EN37eY&feature=relmfu

 

————-

I will read these later.

some new references:

http://nvie.com/posts/how-i-boosted-my-vim/

https://github.com/tpope/vim-repeat

除了java等少数语言,大部分现代语言都支持运算符重载,

比如说在C++中,我们既可以这样写

a = 1 + 1; //a =2

也可以这样把一个运算符改变意义重载成例如:

a = “abc” + “def”; // a = “abcdef”

在python中,传统的可重载运算符包括,

+, -, *,**, /, %, []等等,分别对应的__add__等成员方法

今天我想到了个问题,能不呢自己定义一些比较酷的方法,实用python原来没有的运算符呢?

例如,我想求一个10的阶乘,直接用 10$ = 1*2*3*4*5*6*7*8*9*10

在例如,我想求3,8两个数的最大公约束gcd,直接用 3 @g@ 8 = 1

---------------

google了一下,发现很遗憾的是python并不提供这么BT的运算符自定义功能,但是仅仅是python本身不提供,并不代表hack们做不到,于是这个hack就被提出了了:

我们假设某一个运算,称它为X,有一个对象支持这种运算,我们称为y。

首先,这个hack最重要的一个思想是,python的运算符重载和其他语言不一样的地方在于,不仅提供重载 ? X y这种运算符在对象左边的重载,还同时支持 y X ?这种在右边的重载。

于是开始了!

---------------

我们的任务:重载一个求gcd的运算。达到这样的各式: x gcd y = gcd(x,y)

上代码:

class Infix:

def __init__(self,function):

self.function = function

def __ror__(self,other):

return Infix(lambda x, self=self, other=other: self.function(other,x))

def __or__(self,other):

return self.function(other)

def __call__(self,value1,value2):

return self.function(value1,value2)

———————–

在这里我们定义了一个特殊的类Infix来进行运算符的处理,接下来我们看看怎么用:

首先,写一个gcd函数:

>>>gcd = lambda x,y: gcd(y,x) if x>>g = Infix(gcd)

>>>print 104 |g| 78

26

得到了输出!

——————

于是瞬间我就觉得这太酷了简直碉堡了啊有木有啊!

当然,著名的pipe包也实用了类似的原理,实现了流式程序,

但是这么活用这个原理的这个hack恐怕是第一个了吧。。。

同样道理,也可以自己定制其他的各式的运算符,例如

比较萌的*_*, ^_^, (T_T),

传统一点的++, --神马的。。。

--------------

追加一句,其实ruby也是有类似的功能的