Skip to content

Instantly share code, notes, and snippets.

@hltbra
hltbra / runlength.py
Created November 20, 2012 13:28
run length encoding using recursion
def encode(text):
if not text:
return ""
else:
last_char = text[0]
max_index = len(text)
i = 1
while i < max_index and last_char == text[i]:
i += 1
return last_char + str(i) + encode(text[i:])
@hltbra
hltbra / b.c
Created April 6, 2020 17:13
Demo of Lua integrated with C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main (int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "Missing arguments. Use it like ./program <a> <b>\n");
import ast
code = """
import foo
a = 1 + 2
b = "a" + "b" * 3
@hltbra
hltbra / cdredis.c
Created May 24, 2019 21:19
Experiments using ae.c/anet.c with Cython + dredis
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "ae.h"
#include "anet.h"
#include "cdredis.h"
def scrape_listing():
for link in get_links(LISTING_URL):
scrape_link(link)
def scrape_link(url):
resp = requests.get(url)
info = get_link_info(resp.content)
save('link', info)
schedule('scrape_link', 'http://example.com', seconds=0)
for _ in range(10):
schedule('scrape_link', link, seconds=0)
@starting_task
def scrape_listing():
for link in get_links(LISTING_URL):
schedule('scrape_link', link, seconds=0)
@subtask
def scrape_link(url):
resp = requests.get(url)
info = get_link_info(resp.content)
@hltbra
hltbra / ysh.py
Last active January 4, 2019 05:19
Basic implementation of a shell in Python. This is from a talk I gave at YipitData on August 29, 2018.
# REPL
# read, eval, print, loop program
import os
import subprocess
import shlex
import sys
last_returncode = 0
try:
@hltbra
hltbra / get-inbound-and-egress-sg-rules.py
Last active November 15, 2018 12:04
Get AWS ingress and egress rules for a given security group
import boto
ec2 = boto.connect_ec2()
groups = ec2.get_all_security_groups()
def get_groups_that_have_ingress_rules_to(groups, group_id):
result = []
for group in groups:
for rule in group.rules:
for grant in rule.grants: