Skip to content

Instantly share code, notes, and snippets.

@Cilyan
Cilyan / monitor_state.c
Created October 14, 2014 23:54
Monitor Systemd's SystemState to light control led
/*
* simple_dbus.c
*
* Copyright 2014 Cilyan Olowen <gaknar@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
@Cilyan
Cilyan / format.py
Created March 15, 2015 03:30
A formatter that cleans a text's whitespacing around punctuation, quotes, parenthesis, brackets or curly brackets.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re # Oh yeah :)
class WhiteFormater:
"""
A formatter that cleans a text's whitespacing around punctuation,
quotes, parenthesis, brackets or curly brackets. There should be no
spaces before a punctuation or a closing delimiter, but at least a space
@Cilyan
Cilyan / treemodel_unevenrows1.py
Created March 15, 2015 20:15
This snippet illustrates the creation of a custom GtkTreeModel in PyGTK / Python 2 where column number is different between children and parents.
import pygtk
pygtk.require('2.0')
import gtk
data = [
[('val_a1', 'val_b1', 'val_c1', 'val_d1', 'val_e1'), ('val_x1', 'val_y1', 'val_z1'), ('val_x2', 'val_y2', 'val_z2')],
[('val_a2', 'val_b2', 'val_c2', 'val_d2', 'val_e2'), ('val_x3', 'val_y3', 'val_z3')],
[('val_a3', 'val_b3', 'val_c3', 'val_d3', 'val_e3')],
[('val_a4', 'val_b4', 'val_c4', 'val_d4', 'val_e4'), ('val_x4', 'val_y4', 'val_z4'), ('val_x5', 'val_y5', 'val_z5')],
]
@Cilyan
Cilyan / treemodel_unevenrows_headers.py
Created March 15, 2015 20:30
This snippet illustrates the creation of a custom GtkTreeModel in PyGTK / Python 2 where column number is different between children and parents, and model generates fake headers in each cell
import pygtk
pygtk.require('2.0')
import gtk
data = [
[('val_a1', 'val_b1', 'val_c1', 'val_d1', 'val_e1'), ('val_x1', 'val_y1', 'val_z1'), ('val_x2', 'val_y2', 'val_z2')],
[('val_a2', 'val_b2', 'val_c2', 'val_d2', 'val_e2'), ('val_x3', 'val_y3', 'val_z3')],
[('val_a3', 'val_b3', 'val_c3', 'val_d3', 'val_e3')],
[('val_a4', 'val_b4', 'val_c4', 'val_d4', 'val_e4'), ('val_x4', 'val_y4', 'val_z4'), ('val_x5', 'val_y5', 'val_z5')],
]
@Cilyan
Cilyan / celldatafunc_headers.py
Created March 15, 2015 21:04
This snippet illustrates how to use a CellDataFunc to add heading to each cells.
import pygtk
pygtk.require('2.0')
import gtk
data = [
[('val_a1', 'val_b1', 'val_c1', 'val_d1', 'val_e1'), ('', 'val_x1', 'val_y1', 'val_z1', ''), ('', 'val_x2', 'val_y2', 'val_z2', '')],
[('val_a2', 'val_b2', 'val_c2', 'val_d2', 'val_e2'), ('', 'val_x3', 'val_y3', 'val_z3', '')],
[('val_a3', 'val_b3', 'val_c3', 'val_d3', 'val_e3')],
[('val_a4', 'val_b4', 'val_c4', 'val_d4', 'val_e4'), ('', 'val_x4', 'val_y4', 'val_z4', ''), ('', 'val_x5', 'val_y5', 'val_z5', '')],
]
import timeit
import itertools
import operator
import random
import sys
import functools
import pprint
DEBUG = False
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import timeit
# http://www.gutenberg.org/cache/epub/1597/pg1597.txt
# Public Domain
text = """
Many years ago, there was an Emperor, who was so excessively fond of
@Cilyan
Cilyan / robot.c
Created March 31, 2015 16:28
Strange nested construct of structures
#include <stdio.h>
#include <string.h>
/* gcc -Wall -Werror -std=c11 -pedantic -o robotc robot.c */
struct Robot_st {
int pos_x;
int pos_y;
struct BatteryStatus_st {
int capacity;
@Cilyan
Cilyan / recursivedescent.py
Last active August 29, 2015 14:22
Example parser using a stateful lexer and a recursive descent parser. Goal is to parse a C file containing structures, unions and variables (no functions) and some doxygen comments, building an AST. (Originally part of a bigger project.)
# Nodes for the Abstract Syntax Tree
class Node(object):
def __init__(self):
self.children = []
self.parent = None
for attr in self.__attributes__:
setattr(self, attr, None)
def __repr__(self):
main_attr = self.__attributes__[0]
return "<{class_} {attr_name}={attr_value}".format(
@Cilyan
Cilyan / depgraph.py
Created August 6, 2015 16:31
Simple utility to resolve dependencies between arbitrary (but hashable) objects.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class DependencyGraph(object):
"""
Handle a dependency graph. Using :py:meth:`insert`, you add objects
to the graph and connect them with their dependencies, and the
:py:meth:`resolve` function will yield them back so that any object
comes after all its dependencies.
"""