Skip to content

Instantly share code, notes, and snippets.

@nonZero
nonZero / tests.py
Created December 2, 2015 14:32
m2m
from django.db import transaction
from django.test import TestCase
from users.models import User
from . import models
class MyTestCase(TestCase):
def test_create_expense(self):
user = User.objects.create(email="[email protected]")
@nonZero
nonZero / fabfile.py
Last active December 28, 2015 20:07
@task
def install_elasticsearch():
run(
'wget --no-check-certificate -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -')
run(
'echo "deb http://packages.elastic.co/elasticsearch/1.4/debian stable main" | sudo tee -a /etc/apt/sources.list')
run("sudo apt-get -qq update")
run("sudo apt-get -q upgrade -y")
run("sudo apt-get -q install -y openjdk-7-jre elasticsearch")
run("sudo update-rc.d elasticsearch defaults 95 10")
@nonZero
nonZero / views.py
Created March 1, 2016 10:49
read markdown with metadata from one file with multiple items
SEP_RE = re.compile(r'\n+-{5,}\n+')
def read_file(name):
with (pathlib.Path(__file__).parent / 'content' / '{}.md'.format(
name)).open() as f:
return f.read()
def get_item(txt):
md = markdown.Markdown(extensions=['markdown.extensions.meta'])
html = md.convert(txt)
# http://wsgi.tutorial.codepoint.net/environment-dictionary
# =================================================================
# THE WSGI APPLICATION
# =================================================================
def simple_app(environ, start_response):
status = '200 OK'
response_headers = [
('Content-type', 'text/plain'),
@nonZero
nonZero / server.py
Created May 21, 2016 06:51
Basic bottle server with debug and reloading
from bottle import route, run, template
@route('/hello/<name>')
def index(name):
return template('<b>Hello {{name}}</b>!', name=name)
if __name__ == "__main__":
run(debug=True, reloader=True)
@nonZero
nonZero / connect.py
Last active May 22, 2016 13:18
get access token
import requests
import secrets
URL = "https://graph.facebook.com/oauth/access_token"
r = requests.get(URL, {
'client_id': secrets.APP_ID,
'client_secret': secrets.APP_SECRET,
'grant_type': 'client_credentials',
from pprint import pprint
import pymongo
client = pymongo.MongoClient()
db = client.get_database('socialagg')
# db = client['socialagg']
pages = db.get_collection('pages')
from pprint import pprint
import pymongo
client = pymongo.MongoClient()
db = client.get_database('socialagg')
# db = client['socialagg']
pages = db.get_collection('pages')
@nonZero
nonZero / puzzle.py
Created May 23, 2016 09:27
pillow + argparse example
#!/usr/bin/env python3
import random
from PIL import Image
def main(source, target, cols, rows):
im = Image.open(source)
print("Original image size: {}x{}".format(*im.size))
w, h = im.size
@nonZero
nonZero / mongo_demo.py
Last active May 24, 2016 08:28
insert many posts as documents and query by publish date
import random
import datetime
import collections
import pymongo
client = pymongo.MongoClient()
db = client.get_database('udi123')