Skip to content

Instantly share code, notes, and snippets.

View piyonishi's full-sized avatar
On target

Yusuke Nakanishi piyonishi

On target
View GitHub Profile
def escape_html(self, var):
meta_chars = {
'"': '"',
'\'': ''',
'&': '&',
'<': '&lt;',
'>': '&gt;',
}
escaped_var = ""
for i in var:
import pymongo, re
regx = re.compile("^foo",re.IGNORECASE)
db.users.find({"name": regx})
form datetime import datetime
date = '2013-05-20 16:31:59.199579'
strptime = datetime.strptime(date, '%Y-%m-%d %H:%M:%S.%f')
strftime = strptime.strftime('%B %d, %Y')
# jinjaでenumerate()
<ul>
{%- for item in items %}
# loop.indexだと1から始まる
<li>{{ item }} - {{ loop.index0 }}</li>
{%- endfor %}
</ul>
# インデックスつきループ
# listの場合enumerateを使う
sample_list = ['i', 'ro', 'ha', 'ni', 'ho', 'he', 'to']
for i, v in enumerate(sample_list):
print(i, v)
# dictionaryで3.xの場合itemsを使う
# iteritemsは廃止
sample_dictionary = {0:'i', 1:'ro', 2:'ha', 3:'ni', 4:'ho', 5:'he', 6:'to'}
@piyonishi
piyonishi / regexp.$n.js
Created June 27, 2013 09:30
知らんかったわ…/(^o^)\
var str = 'iPhone';
str.replace(/(iPhone)/g); // 括弧でくくった部分が格納される
console.log(RegExp.$1);
import markdown
md = markdown.Markdown(extensions=['codehilite'])
sample = """
sample text
:::python
import sys
def sample():
print 'sample'
"""
# Pythonっぽい書き方
a = '1,2,3,4,5,'
# その1
p1 = [item for item in a.split(',') if not item == '']
print(p1)
#['1', '2', '3', '4', '5']
# その2
Array.prototype.distinct = function() {
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
import sys
argvs = sys.argv
print(argvs)
# ターミナルで以下のように打つと
#python comand-line.py あ a 1
# 以下の内容が返ってくる
#['comand-line.py', 'あ', 'a', '1']