Skip to content

Instantly share code, notes, and snippets.

@RussellLuo
RussellLuo / post_pv
Created July 17, 2014 03:53
An example showing how to use @hybrid_property in SQLAlchemy.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
from sqlalchemy.ext.hybrid import hybrid_property
Base = declarative_base()
@RussellLuo
RussellLuo / oneline_print
Created June 16, 2014 03:48
Print messages just on one line. That is, old messages will be replaced by new ones one the same line.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def oneline_print(message):
sys.stdout.write('\r%s' % message)
sys.stdout.flush()
@RussellLuo
RussellLuo / dummy_request_in_django
Last active August 29, 2015 14:01
An example showing how to use the dummy request in Django unit testing.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.test.client import RequestFactory
class RequestTest(TestCase):
def setUp(self):
@RussellLuo
RussellLuo / dotted_dict
Last active August 29, 2015 13:58
A simple function for converting dict to dotted-dict.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class DottedDict(object):
pass
def dotted_dict(src_dict):
assert isinstance(src_dict, dict)
@RussellLuo
RussellLuo / dot_chained_attrs
Created April 5, 2014 03:51
A simple class for generating dot chained attributes
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class DotChainedAttrs(object):
def __getattr__(self, name):
__dict__ = object.__getattribute__(self, '__dict__')
if name not in __dict__:
__dict__[name] = DotChainedAttrs()
return __dict__[name]