Skip to content

Instantly share code, notes, and snippets.

@bricef
bricef / pinc.py
Created August 17, 2011 21:07
Arbitrary file parser allowing file include and include script output
import sys
import re
import shlex
import subprocess as sp
exe_pat = re.compile(r'(\s*)\(!>(.*)<\)\s*')
inc_pat = re.compile(r'(\s*)\(>(.*)<\)\s*')
if __name__ == "__main__":
for line in sys.stdin:
@bricef
bricef / test.py
Created August 10, 2011 19:10
Python script to be used from Java
#!/usr/bin/env python
class PythonClass:
def __init__(self):
"""A dummy constructor"""
def mySimpleMethod(self):
return "Hello World!"
anInstance = PythonClass()
@bricef
bricef / Simple.java
Created August 10, 2011 19:08
Using Python from Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class Simple {
public static void main(String[] args) {
@bricef
bricef / timing_0.3.c
Created June 17, 2011 21:39
Timing C Code with Glib
#include <glib.h>
// ...
{
GTimer* timeit = g_timer_new();
int i;
g_timer_start(timeit);
for ( i = 0 ; i < 10000 ; i++ ){
my_time_critical_function();
@bricef
bricef / timing_0.2.c
Created June 17, 2011 21:01
Using timing macros
#include <timing.h>
#define DEBUG 1
// ...
{
TIMEIT_INIT(timer_name);
int i;
TIMEIT_START(timer_name);
for ( i = 0 ; i < 10000 ; i++ ){
@bricef
bricef / timing.h
Created June 17, 2011 20:50
Timing Sections of C code using macros
#ifndef _TIMING_UTIL_H
#define _TIMING_UTIL_H
#ifdef DEBUG
#include <sys/time.h>
#define TIMEIT_INIT(name) \
struct timeval timeit_t1_##name, timeit_t2_##name, timeit_diff_##name; \
double timeit_interval_##name \
@bricef
bricef / timing_0.1.c
Created June 17, 2011 20:15
Naive Timing of C Code.
#include <sys/time.h>
// ...
{
struct timeval t1, t2, diff;
double interval;
int i;
gettimeofday(t1, NULL);
for ( i = 0 ; i < 10000 ; i++ ){
@bricef
bricef / lookup-batteries.py
Created June 9, 2011 19:04
Script to do a hostname lookup on an IP address. Batteries included.
#!/usr/bin/env python
import sys
from socket import gethostbyaddr, herror
def usage():
print("[usage]: %s [ip]"%(sys.argv[0]))
print("")
print("performs a reverse lookup on a host ip.")
@bricef
bricef / lookup-nobatteries.py
Created June 9, 2011 19:01
Python script to carry out a reverse hostname lookup. Version 1: The dummy way.
#!/usr/bin/env python
import sys, re, os, popen2
hosts_locs = [
"/etc/hosts",
"C:/Windows/system32/drivers/etc/hosts"
]
hosts = {}
// Will work as expected
"foo|bar|spam|eggs".split("\\|");
//will get a compilation error
"foo|bar|spam|eggs".split("\|");
//will split on each character
"foo|bar|spam|eggs".split("|");