Skip to content

Instantly share code, notes, and snippets.

View sebclaeys's full-sized avatar

Sebastien Claeys sebclaeys

View GitHub Profile
@sebclaeys
sebclaeys / autodict.py
Created September 19, 2011 20:49
Python autodict: implicitly create a dictionary instead of raising KeyError + support for object-like dot access + dictionary visitor
# Autodict class
# Create new autodict if accessing undefined key
# support dot access syntax (mydict.test == mydict['test'])
class autodict(dict):
def __getitem__(self, name):
if not name in self:
dict.__setitem__(self, name, autodict())
return dict.__getitem__(self, name)
def __getattr__(self, name):
@sebclaeys
sebclaeys / example.cpp
Created September 19, 2011 21:19
Light weight C++ XML writer
#include "xml_writer.hpp"
#include <iostream>
int main()
{
Writer writer(std::cout);
writer.openElt("Movies");
writer.openElt("Goldeneye").attr("date", "1998").content("This is a James Bond movie").endElt();
writer.openElt("Leon").attr("director", "Luc Besson");
writer.openElt("Actor").attr("role", "Leon").attr("name", "Jean Reno").endAll();
@sebclaeys
sebclaeys / non_blocking_read.py
Created September 21, 2011 13:56
Python non-blocking read with subprocess.Popen
import fcntl
import os
from subprocess import *
def non_block_read(output):
fd = output.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
try:
return output.read()
@sebclaeys
sebclaeys / hash.cpp
Created September 21, 2011 20:21
Majors hash function implementation in C
#include "hash.hh"
HashFunc::HashFunc ()
{
}
HashFunc::~HashFunc ()
{
}
@sebclaeys
sebclaeys / example.output
Created September 21, 2011 21:03
Read and merge multiple sorted file in C++
shell $ g++ mergetest.cpp
shell $ cat f1.txt
a:1
c:2
f:3
h:4
j:5
l:6
shell $ cat f2.txt
a:1