Skip to content

Instantly share code, notes, and snippets.

View markwatson's full-sized avatar
💻
hacking the mainframe

Mark Watson markwatson

💻
hacking the mainframe
View GitHub Profile
@markwatson
markwatson / csv.py
Last active August 29, 2015 13:56
Reads CSV files while handling all types of edge cases. (Built as an exercise - probably not useful)
import re
import unittest
class CsvReader(object):
"""
Reads CSV files while handling all types of edge cases.
"""
def __init__(self, lines):
"""
A new CSV reader with the given lines.
function countCSSRules() {
var results = '',
log = '';
if (!document.styleSheets) {
return;
}
for (var i = 0; i < document.styleSheets.length; i++) {
countSheet(document.styleSheets[i]);
}
function countSheet(sheet) {
@markwatson
markwatson / createEl.js
Created May 29, 2014 22:01
Create elements in javascript. Useful to me, not that useful in general.
var createEl = function(type, props, children) {
var el = document.createElement(type);
if (props) {
for (var key in props) {
if (props.hasOwnProperty(key)) {
if (key == "css") {
if (props[key].length > 0) {
el.style.cssText = props[key].join(';') + ";";
}
} else {
@markwatson
markwatson / onChange.js
Created February 23, 2015 19:27
Helper to live check for input box changes in JavaScript.
$.fn.mwOnChange = function(callback, timeout) {
return $(this).each(function() {
if (timeout === undefined) {
timeout = 500;
}
var eventNames = 'keydown paste input';
var timeoutId = null;
$(this).on(eventNames, function(e) {
var self = this;
@markwatson
markwatson / Template.php
Created December 12, 2008 09:59
Codeignitor full page templates.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
ob_start();
/**
* Template Library
*
* @package Template
* @category Libraries
* @author Mark Watson
* @link http://markedup.org
*
<?php
// error logging function that saves arrays, objects - anything.
// easier to type then always typing "error_log(print_r($var,1));
function e($var, $mess=null)
{
error_log($mess.': '.print_r($var,1));
}
<?php
/**
* The global model! All hail potentate MY_Controller!
* It provides some great default queries...
*/
class MY_Model extends Model
{
var $default_table;
function get_all()
<?php
/**
* Intelligent News Parser Library
*
* @package Parse_news
* @category Libraries
* @author Mark Watson
* @link http://markwatson.us
*
* A simple library that parses the html from news articles to pull out metadata.
The first is to use the object oriented way:
$config = array(
           'indent'         => true,
           'output-xml'   => true,
           'wrap'           => 200);
// Tidy
$tidy = new tidy;
$tidy->parseString($xml, $config, 'utf8');
$tidy->cleanRepair();
@markwatson
markwatson / gist:572149
Created September 9, 2010 16:46
ceiling function
#include <stdio.h>
double my_ceil(double x) {
double y = (double) ((long long) x);
if (x - y == 0) {
return x;
} else {
if (x > 0) {
return y + 1;
} else {