Skip to content

Instantly share code, notes, and snippets.

View jaychoo's full-sized avatar
☢️

Jay Choo jaychoo

☢️
  • New York, NY
View GitHub Profile
@jaychoo
jaychoo / gist:1245697
Created September 27, 2011 17:35
Create dictionary with list of keys (and empty value)
def get_dict(keys):
if keys:
return dict([k, ''] for k in keys)
return {}
@jaychoo
jaychoo / gist:1246146
Created September 27, 2011 20:29
Use MongoDB connection in singleton style
from pymongo import Connection
class MongoCon(object):
__db = None
@classmethod
def get_connection(cls):
if cls.__db is None:
cls.__db = Connection()
return cls.__db
@jaychoo
jaychoo / gist:1262045
Created October 4, 2011 16:09
Sqlalchemy session example
engine = ConnectionPool.get_engine()
Base = declarative_base()
Session = scoped_session(sessionmaker(bind=engine))
@jaychoo
jaychoo / gist:1268245
Created October 6, 2011 18:40
Remove html tags from string
from BeautifulSoup import BeautifulSoup
''.join(BeautifulSoup(page).findAll(text=True))
@jaychoo
jaychoo / gist:1271098
Created October 7, 2011 19:04
Scala - read content from the file
scala.io.Source.fromFile("file.txt").getLines.reduceLeft(_+_)
@jaychoo
jaychoo / gist:1278089
Created October 11, 2011 13:34
Django admin 404/500 html, user login to admin page
from django.utils.functional import curry
from django.views.defaults import server_error, page_not_found
handler500 = curry(server_error, template_name='admin/500.html')
handler404 = curry(page_not_found, template_name='admin/404.html')
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}),
@jaychoo
jaychoo / gist:1285257
Created October 13, 2011 19:29
Django settings
import os
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'),
)
@jaychoo
jaychoo / gist:1295421
Created October 18, 2011 13:29
Python dict key - pop/get
d = {1:1, 2:2}
a = d.pop(1, None) # Pop item from d
b = d.get(2, None) # Get item from d, no change in the dict object
@jaychoo
jaychoo / gist:1299256
Created October 19, 2011 18:46
Itertool chain - add all list into one
from itertools import chain
result_list = list(chain(page_list, article_list, post_list))
@jaychoo
jaychoo / gist:1324410
Created October 29, 2011 12:45
Python revese string
s = 'abcde'
print s[::-1]
print ''.join(reversed(s))