Skip to content

Instantly share code, notes, and snippets.

@pylemon
pylemon / using_request_post_or_none.py
Created April 7, 2012 16:34
django: using(request.POST or None) hack in views | advanced forms usage
# simple form usage in view
def my_view(request, template_name='home.html'):
# sticks in a POST or renders an empty form
form = MyForm(request.POST or None)
if form.is_valid():
do_something()
return redirect('/')
return render_to_response(template_name, {'form':form})
# form with files
@pylemon
pylemon / shanbay2.py
Created April 7, 2012 16:03
python: using mechanize to login shanbay.com
from mechanize import Browser
import json
LOGIN_URL = 'http://www.shanbay.com/accounts/login/'
USERNAME = ''
PASSWORD = ''
API = 'http://www.shanbay.com/api/word/'
def login(username=USERNAME, password=PASSWORD, login_url=LOGIN_URL):
print '[begin login]'
@pylemon
pylemon / 0_reuse_code.js
Created September 29, 2016 08:08
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@pylemon
pylemon / test
Created August 30, 2016 07:41
Title
/v2/post_check
=============
```json
{
"try_split": false,
"kind": "1000",
"group_order": false,
"line_items": [{
"quote_line_item_id": "147183895782223",
"quantity": 1
@pylemon
pylemon / betterpaginator.py
Created April 7, 2012 15:54
django: better paginator
from django.core.paginator import Paginator, EmptyPage
class BetterPaginator(Paginator):
"""An enhanced version of the Paginator"""
def __init__(self, getvars, *args, **kwargs):
"""default args are:
self, object_list, per_page, orphans=0, allow_empty_first_page=True
@pylemon
pylemon / EmacsKeyBinding.dict
Created December 29, 2015 08:14 — forked from jwreagor/EmacsKeyBinding.dict
Global Emacs Key Bindings for OS X
{
/* Keybindings for emacs emulation. Compiled by Jacob Rus.
*
* This is a pretty good set, especially considering that many emacs bindings
* such as C-o, C-a, C-e, C-k, C-y, C-v, C-f, C-b, C-p, C-n, C-t, and
* perhaps a few more, are already built into the system.
*
* BEWARE:
* This file uses the Option key as a meta key. This has the side-effect
* of overriding Mac OS keybindings for the option key, which generally
# coding=utf8
import subprocess
import itertools
import psycopg2
dc = "a,ai,an,ang,ao,ba,bai,ban,bang,bao,bei,ben,beng,bi,bian,biao,bie,bin,bing,bo,bu,ca,cai,can,cang,cao,ce,cei,cen,ceng,cha,chai,chan,chang,chao,che,chen,cheng,chi,chong,chou,chu,chua,chuai,chuan,chuang,chui,chun,chuo,ci,cong,cou,cu,cuan,cui,cun,cuo,da,dai,dan,dang,dao,de,dei,den,deng,di,dia,dian,diao,die,din,ding,diu,dong,dou,du,duan,dui,dun,duo,e,ei,en,eng,er,fa,fan,fang,fei,fen,feng,fiao,fo,fou,fu,ga,gai,gan,gang,gao,ge,gei,gen,geng,gong,gou,gu,gua,guai,guan,guang,gui,gun,guo,ha,hai,han,hang,hao,he,hei,hen,heng,hm,hng,hong,hou,hu,hua,huai,huan,huang,hui,hun,huo,ji,jia,jian,jiang,jiao,jie,jin,jing,jiong,jiu,ju,juan,jue,jun,ka,kai,kan,kang,kao,ke,kei,ken,keng,kong,kou,ku,kua,kuai,kuan,kuang,kui,kun,kuo,la,lai,lan,lang,lao,le,lei,leng,li,lia,lian,liang,liao,lie,lin,ling,liu,lo,long,lou,lu,luan,lun,luo,ma,mai,man,mang,mao,me,mei,men,meng,mi,mian,miao,mie,min,ming,miu,mo,mou,mu,n,na,nai,nan,nang,nao,ne,nei,nen,neng,ng,ni,nia,nian,niang,niao,nie
@pylemon
pylemon / validate_code.py
Created May 9, 2012 01:13
django: validate code
#coding=utf-8
import random
from PIL import Image, ImageDraw, ImageFont, ImageFilter
_letter_cases = "abcdefghjkmnpqrstuvwxy" # 小写字母,去除可能干扰的i,l,o,z
_upper_cases = _letter_cases.upper() # 大写字母
_numbers = ''.join(map(str, range(3, 10))) # 数字
init_chars = ''.join((_letter_cases, _numbers))
@pylemon
pylemon / testhtml.py
Created December 14, 2012 03:06
django: html filter for clean up html tags
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.template.defaultfilters import stringfilter, striptags, slice_filter
def cut_string(value, limit):
s = striptags(value)
s = s.replace('\n', '').replace(' ', '')
return slice_filter(s, str(limit))
@pylemon
pylemon / gist:3963079
Created October 27, 2012 05:37 — forked from douglasmiranda/gist:2527687
linux: fixing dropbox - Ubuntu 12
#When dropbox is not working on ubuntu 12
#you tried to execute on terminal:
#dropbox start
#and the error is something like this:
#Starting Dropbox.../home/YOUR_USER/.dropbox-dist/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required #by /usr/lib/x86_64-linux-gnu/libproxy.so.1)
#Failed to load module: /usr/lib/x86_64-linux-gnu/gio/modules/libgiolibproxy.so
#Done!
#FIX
cd /home/YOUR_USER/.dropbox-dist/