Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
hacking the mainframe

Mark Watson markwatson

💻
hacking the mainframe
View GitHub Profile
@markwatson
markwatson / csv.py
Last active Aug 29, 2015
Reads CSV files while handling all types of edge cases. (Built as an exercise - probably not useful)
View csv.py
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.
View countCSSRules.js
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
Create elements in javascript. Useful to me, not that useful in general.
View createEl.js
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 Feb 23, 2015
Helper to live check for input box changes in JavaScript.
View onChange.js
$.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 Dec 12, 2008
Codeignitor full page templates.
View Template.php
<?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
*
View error logging function
<?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));
}
View gist:47168
<?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()
View News Parser
<?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.
View gist:159782
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();
View gist:572149
#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 {