Skip to content

Instantly share code, notes, and snippets.

@ssteuteville
ssteuteville / sort.py
Created September 14, 2016 03:28
count sort
def counting_sort(l, upper_bound):
counts = [0] * (upper_bound + 1)
ret = []
# count how many times each number occurs
for i in l:
counts[i] += 1
# fill in ret
for i, count in enumerate(counts):
for _ in range(count):
ret.append(i)
@ssteuteville
ssteuteville / batch_qs.gist
Created April 1, 2016 22:55
batching a query set in django
def batch_query_set(query_set, single=False, batch_size=1000):
try:
last_id = query_set.order_by('-id')[0].id
first_id = query_set.order_by('id')[0].id
for start in range(first_id, last_id + 1, batch_size):
end = min(start + batch_size - 1, last_id)
records = query_set.filter(id__range=(start, end)).all()
if single:
for record in records:
yield record
@ssteuteville
ssteuteville / reddit_spider
Created June 26, 2015 03:44
basic scrapyz example
from scrapyz.spiders.core import GenericSpider, Target
class RedditSpider(GenericSpider):
name = "reddit"
start_urls = ["https://www.reddit.com/"]
class Meta:
elements = ".thing"
targets = [
Target("rank", ".rank::text"),
@ssteuteville
ssteuteville / example
Last active August 29, 2015 14:00
loopj + initialize database
if champion data base doesn't exist
{
client.get(url, null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject response)
{
button1.setVisibility(View.GONE);
button2.setVisibility(View.GONE);
button3.setVisibility(View.GONE);
progressBar.setVisibility(view.visible);
@ssteuteville
ssteuteville / gist:5596848
Last active December 17, 2015 10:39
A good example of pointer manipulation.
#include <stdio.h>
void main()
{
char* str = "Cool Way To Loop Through An Array Of Characters\n";
for(; *str; str++)
printf("%c", str[0]);
}