Skip to content

Instantly share code, notes, and snippets.

@stephenmm
stephenmm / runc
Last active August 29, 2015 14:01
Simple script to allow me to run c-code from the command line
#!/usr/bin/perl -w
use warnings;
use strict;
sub main {
#sub say { print "@args\n" };
my $dir = "/tmp/$ENV{USER}/runc";
my $file = "oneliner.c";
my $out = $file;
$out =~ s/..$//;
@stephenmm
stephenmm / runc.py
Last active August 29, 2015 14:01
Simple script to allow me to run c-code from the command line (now in python)
#!/usr/bin/env python
import argparse, os, sys, re, inspect, fileinput
from textwrap import dedent
# [NOTE001] -- Hack to get multiple argparse.formatter_class See stackoverflow.com #18462610 for more details
__author__ = "Stephen Meckley"
__created__ = "05/11/2014"
def main(argv=None):
"""main function for module"""
@stephenmm
stephenmm / define_vs_bitfield.c
Created May 18, 2014 03:47
Trying to decide which is better to use define or bitfield
// Run with:
// ~/bin/runc -f ~/examples/c/define_vs_bitfield.c
// Good resource that talks about the tradeoffs between bitfields and macros:
// http://docs.embarcadero.com/prodt3ts/rad_studio/delphiAnt2pp2009/HelpUpdate2/EN/html/devwin32/bitfields_xml.html
#include <stdio.h>
#ifndef UINT8
#define UINT8 uint8
typedef unsigned char uint8_t;
#endif
#include <stdio.h>
enum {
plain = 0,
bold = 1,
italic = 2
};
void PrintString(const char* message, int size, int style)
{
printf("%s[%s] %s: %d %d\n" ,__FILE__,__FUNCTION__, message, size, style);
from PIL import Image
backgroundColor = (0,)*3
pixelSize = 9
image = Image.open('input.png')
image = image.resize((image.size[0]/pixelSize, image.size[1]/pixelSize), Image.NEAREST)
image = image.resize((image.size[0]*pixelSize, image.size[1]*pixelSize), Image.NEAREST)
pixel = image.load()
@stephenmm
stephenmm / gist:a3e150a64f1fb64fc23a
Created July 28, 2014 00:50
Find files between certain dates
find -type f -newermt 20111222 \! -newermt 20111225
@stephenmm
stephenmm / do_leaf_nodes.py
Created September 13, 2014 19:30
Apply function to all string based leaf nodes of an arbitrary structure
r = ['crossABC', ['A', 'B', 'C'], ['r n w ', 'r n r ', 'r n n ', 'n w w ', 'n w r ', 'n w n', 'n r w ', 'n r r ', 'n r n ', 'n n w ', 'n n r ', 'n n n ']]
rm_space = lambda x: re.sub('\s+', ' ', x)
def do_leaf_nodes(it, str_func):
""" Apply function to all string based leaf nodes of an arbitrary structure """
if isinstance(it, list):
for i, item in enumerate(it):
it[i] = do_leaf_nodes(item, str_func)
return it
@stephenmm
stephenmm / parse_strct_txt_file.py
Last active September 4, 2023 08:40
Simple example of how to parse any structured text file with a little bit of python
#!/bin/env python
# Ex: python parse_strct_txt_file.py grpinfo.txt
# Tried to use "textFSM" but it was too restricting and writting the FSM was not that hard.
import sys, re, inspect, pprint
from collections import defaultdict
pp = pprint.PrettyPrinter(indent=4)
def parse_cov_strct_txt_file( cov_strct_txt_file ):
C = '' # The current register we are working on
s = 'Samples_crossed' # The current state of the parser
@stephenmm
stephenmm / mergedicts.py
Last active August 29, 2015 14:06
Recursively merge an arbitrary number of nested dictionaries and lists
def mergedicts(d1, d2):
"""Recursively merge an arbitrary number of dictionaries
For common keys d2 will overwrite d1 values.
import pprint
d1 = 1: {'a': 'A'},
2: {'b': 'B'},
3: {'c': {'c': 'C'}},
4: ['i', 'j']}
d2 = {2: {'b': 'BB',
'd': {'d': 'D'}},
@stephenmm
stephenmm / do_leaf_nodes.py
Last active August 29, 2015 14:06
Recursively apply function to an arbitrary number of nested dictionaries and lists
def do_leaf_nodes(it, str_func):
""" Apply function to all string based leaf nodes of an arbitrary structure. Ex:
do_leaf_nodes( self.test_opts, self.check_for_required_args )
"""
if isinstance(it, list):
for i, item in enumerate(it):
it[i] = do_leaf_nodes(item, str_func)
return it
elif isinstance(it, dict):
for key in it: