Skip to content

Instantly share code, notes, and snippets.

View lxneng's full-sized avatar
🎯
Focusing

Eric Luo lxneng

🎯
Focusing
View GitHub Profile
@lxneng
lxneng / Makefile
Created November 6, 2012 01:46 — forked from turicas/Makefile
Create slugs and abbreviate names using Python
test:
clear
nosetests --with-coverage --cover-package name_utils test_name_utils.py
clean:
find -regex '.*\.pyc' -exec rm {} \;
find -regex '.*~' -exec rm {} \;
.PHONY: test clean
http://www.google.com/profiles/c/favicons?domain=lxneng.com
@lxneng
lxneng / gist:3616907
Created September 4, 2012 05:21
Responsive Design: Max-Width Images
img, video, audio, embed {
max-width:100%;
height:auto !important;
}
>>> import csv
>>> reader = csv.reader(open('ttt.csv'))
>>> keys = reader.next()
>>> data = [dict(zip(keys, property)) for property in reader]
>>>
@lxneng
lxneng / gist:3346315
Created August 14, 2012 04:45
strapdownjs
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sanitise a (unicode) string so it is suitable as a URL path
component.</title>
</head>
<body>
<xmp theme="United">

DictMixin is pretty cool. You just need to define getitem, setitem, delitem and keys and then you get a full dictionary interface (everything else being defined in terms of those methods). For example:

from UserDict import DictMixin

class Filesystem(DictMixin):
    def __init__(self, dir):
        self.dir = dir
    def __getitem__(self, path):
        new_path = os.path.join(self.dir, path)

Instead of:

if s.startswith('http://') or s.startswith('https://'): ...

You can do:

if s.startswith(('http://', 'https://')): ...
in python3, it'll even work for tuple unpacking:
>>> head, *tail = [1, 2, 3, 4]
>>> head
1
>>> tail
[2, 3, 4]
@lxneng
lxneng / gist:3148701
Created July 20, 2012 04:28
gc.get_objects()

gc.get_objects() is also pretty fun, every object in the system. For example:

def all_instances(cls):
    return [o for o in gc.get_objects() if isinstance(o, cls)]
@lxneng
lxneng / hstore.py
Created May 30, 2012 12:44 — forked from dahlia/hstore.py
PostgreSQL hstore + SQLAlchemy
import collections
import sqlalchemy.types
class Hstore(sqlalchemy.types.UserDefinedType, sqlalchemy.types.MutableType):
"""The ``hstore`` type that stores a dictionary. It can take an any
instance of :class:`collections.Mapping`.
It can be extended to store other types than string e.g.::