Skip to content

Instantly share code, notes, and snippets.

@yosemitebandit
yosemitebandit / stack.py
Created August 12, 2015 23:02
stacks in python
import re
class Stack(object):
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
@yosemitebandit
yosemitebandit / linked_list.py
Created August 12, 2015 22:32
linked list in python
class Node(object):
def __init__(self, value):
self.value = value
self.next = None
def __repr__(self):
return 'Node %s' % self.value
class LinkedList(object):
@yosemitebandit
yosemitebandit / order_by_ignoring_null.py
Last active August 18, 2021 21:34
an example of ordering a Django queryset while ignoring null values
"""Order a queryset by last_active and make null values sort last."""
import datetime
from django.db.models.functions import Coalesce
from app import models
# Coalesce works by taking the first non-null value. So we give it
@yosemitebandit
yosemitebandit / gprs_lister.py
Created May 6, 2015 00:27
get the output of the "gprs list" command in OpenBTS for analysis purposes
"""Continuously records the output of "gprs list" from OpenBTSCLI.
Run as follows (then you can logout of the session and it'll keep going):
$ nohup python gprs_lister.py &
"""
import time
import envoy
@yosemitebandit
yosemitebandit / test_headers.py
Created April 24, 2015 17:52
header testing script
import requests
good_hosts = ('www.endaga.com', 'endaga.com', 'staff.endaga.com',
'staging.endaga.com')
bad_hosts = ('www.bad-host.com', 'bad-host.com')
url = 'https://staging.endaga.com'
print 'GET %s' % url
@yosemitebandit
yosemitebandit / tables.py
Created April 20, 2015 15:56
django tables 2 table with column header from db
class EventTable(tables.Table):
def __init__(self, *args, **kwargs):
# Pop out the date_header named arg before calling super.
date_header = kwargs.pop('date_header', 'Date')
super(EventTable, self).__init__(*args, **kwargs)
self.base_columns['date'].verbose_name = date_header
class Meta:
model = models.Event
fields = ('date', 'value')
@yosemitebandit
yosemitebandit / xls_to_json.py
Created April 3, 2015 03:28
converting xls files to JSON dicts in python
"""XLS -> json converter
first:
$ pip install xlrd
then:
$ cat in.xls
date, temp, pressure
Jan 1, 73, 455
Jan 3, 72, 344
@yosemitebandit
yosemitebandit / decode.py
Created February 26, 2015 18:04
decode and resave urlencoded files
import sys
import urllib
# Read.
with open(sys.argv[1]) as cdr_file:
cdr_data = cdr_file.read()
# Decode.
decoded_data = urllib.unquote(cdr_data).decode('utf8')
print decoded_data
@yosemitebandit
yosemitebandit / even_points.py
Last active August 29, 2015 14:14
SA with scipy
""" Placing points in a polygon evenly with simulated annealing.
"""
import math
import random
import descartes
from matplotlib import pyplot
import scipy.optimize
import shapely.geometry
@yosemitebandit
yosemitebandit / plot_shp.py
Last active August 29, 2015 14:11
using tiger census data at the block level to find the population of a circular area
"""plot_shp.py
Plots a shapefile with fiona, decartes and matplotlib.
"""
import descartes
import fiona
from matplotlib import pyplot