Skip to content

Instantly share code, notes, and snippets.

View lexdene's full-sized avatar

Elephant Liu lexdene

  • 中国北京 Beijing China
  • 02:38 (UTC +08:00)
View GitHub Profile
@lexdene
lexdene / test unicode and default coding
Last active December 15, 2015 16:29
Python的unicode函数似乎和sys.getdefaultencoding()有关。附: 《sys.setdefaultencoding is evil》http://ziade.org/2008/01/08/syssetdefaultencoding-is-evil/
# -*- coding: utf-8 -*-
a = '啦啦啦'
# an error will happen here
print '##### 1 #####'
try:
print unicode(a)
except Exception as e:
print 'error:'
@lexdene
lexdene / text_mysqldb.py
Last active December 16, 2015 14:39
I think this is a bug in MySQLdb .
import MySQLdb
conn = MySQLdb.connect(user='xxx',passwd='xxx',db = 'xxx')
cursor = conn.cursor()
n = cursor.execute(
'select * from xxx where `content` in %(content)s',
{ 'content': ('aa','bb','cc') }
)
@lexdene
lexdene / test_return_line.py
Last active June 25, 2018 23:03
Python在同一行输出内容。 原理: 输出\r \r的作用是回到一行的开头
# -*- coding: utf-8 -*-
'''
Python在同一行输出内容。
原理:
输出\r
在Linux中,\r的作用是回到一行的开头。
这里不能使用print,因为print自带换行。而这里我们不能使用换行。
但是如果禁用print的换行也不行。如果没有换行print就不会flush,导致什么也不输出。
@lexdene
lexdene / tucao.coffee
Created June 28, 2013 07:06
吐槽一段coffee代码
$(a).foo()
id = $(a).attr 'id'
$('#' + id).bar()
@lexdene
lexdene / tucao2.coffee
Created June 28, 2013 07:52
吐槽一下coffee的语法
# coffee可以这样写
foo() if 0 == a
# 也可以这样写
if 0 == a then foo() else bar()
# 但是不能可以这样写,编译出来根本不是我想要的效果
foo() if 0 == a else bar()
# 也不能这样写,会报语法错误
@lexdene
lexdene / code.py
Created July 17, 2013 09:46
讨论一下Python的代码风格
# 第一个例子
name = 'elephant'
# 第一种代码风格
if 'John' == name:
isJohn = True
else:
isJohn = False
# 第二种代码风格
-> sudo apt-get install wine
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
将会安装下列额外的软件包:
binfmt-support fonts-droid fonts-horai-umefont fonts-unfonts-core gcc-4.6-base:i386 libasn1-8-heimdal:i386 libasound2:i386 libavahi-client3:i386 libavahi-common-data:i386
libavahi-common3:i386 libc6:i386 libcapi20-3 libcapi20-3:i386 libcomerr2:i386 libcups2:i386 libdb5.1:i386 libdbus-1-3:i386 libdrm-intel1:i386 libdrm-nouveau1a:i386 libdrm-radeon1:i386
libdrm2:i386 libexif12:i386 libexpat1:i386 libffi6:i386 libfontconfig1:i386 libfreetype6:i386 libgcc1:i386 libgcrypt11:i386 libgd2-xpm:i386 libgettextpo0 libgif4:i386 libgl1-mesa-dri:i386
libgl1-mesa-glx:i386 libglapi-mesa:i386 libglib2.0-0:i386 libglu1-mesa:i386 libgnutls26:i386 libgpg-error0:i386 libgphoto2-2:i386 libgphoto2-port0:i386 libgpm2:i386 libgssapi-krb5-2:i386
libgssapi3-heimdal:i386 libgstreamer-plugins-base0.10-0:i386 libgstreamer0.10-0:i386 libhcrypto4-heimdal:i386 libheimbase1-heimdal:i386 libheimntlm0-heimdal:i386 libhx509-5-heimdal:
@lexdene
lexdene / isMale.py
Created August 19, 2013 07:22
代码风格的讨论,关于是否返回False的问题
# 第一种风格
def isMale(user):
if user['sex'] == 'm':
return True
else:
return False
# 第二种风格
def isMale(user):
if user['sex'] == 'm':
@lexdene
lexdene / 10_times.py
Created August 21, 2013 07:28
减少一个变量完成循环
for i in range(10):
print 'hello world'
# 能不能不产生新的变量 (此例中的i)
# 来完成循环10次的任务
在.emacs.d/core/prelude-core.el最后添加函数
(defun prelude-py-taglist (arg)
"简易版taglist"
(interactive "P")
(let ((buffer-other
(if arg
"*py-taglist*"
(format "*py-taglist from %s*" (buffer-name)))))
(occur-1 "class \\|def " nil