Skip to content

Instantly share code, notes, and snippets.

View datamafia's full-sized avatar
☠️
not here

" and 1=1 " datamafia

☠️
not here
  • At Desk
View GitHub Profile
@datamafia
datamafia / switch_and_eval_in_php.php
Created February 8, 2015 20:13
Using switch statement and additional evaluation in php super short demo.
<?
// Switch statment + additional eval
ini_set('display_errors',1);
$arr = array(
'match',
'other'
);
x($arr);
var scimoz = ko.views.manager.currentView.scimoz,
savedPos = scimoz.currentPos,
savedLinePos = scimoz.firstVisibleLine;
try {
scimoz.text = scimoz.text.replace(/\t/gm, " ");
scimoz.gotoPos(savedPos);
scimoz.lineScroll(0, savedLinePos-scimoz.firstVisibleLine);
} catch(e) {
return true;
<p>
My programming language of preference is python for the simple reason that I feel I write better code faster with it then I do with other languages. However also has a lot of nice tricks and idioms to do things well. And partly as a reminder to myself to use them, and partly because I thought this might be of general interest I have put together this collection of some of my favourite idioms. I am also putting this on <a href="https://gist.github.com/codefisher/9d7993ddbf404c505128">gist.github.com</a> so that anyone that wants to contribute there own things can, and I will try and keep this post up to date.
</p>
<h2>enumerate</h2>
<p>
A fairly common thing to do is loop over a list while also keeping track of what index we are up to. Now we could use a <code>count</code> variable, but python gives us a nicer syntax for this with the <code>enumerate()</code> function.
<script src="https://gist.github.com/codefisher/9d7993ddbf404c505128.js?file=enumerate.py"></script>
@datamafia
datamafia / notification.py
Created January 27, 2015 20:47
Global Notification Object -work in progress-
#!/usr/bin/python
# http://datamafia.com
from time import gmtime, strftime
class notification:
notif = {} # container for all messages
summary_order = ( # list of notif types + order. Override as needed.
'log',
'user_good',
@datamafia
datamafia / gist:baba53f5de148fd90c0f
Created January 10, 2015 00:17
Group match and replace to use liquid and similar template systems to document syntax.
#!/usr/bin/python
# Problem : Need to present {{, }}, {%, and %} in html document without
# triggering parser
# Solution : Simple python script to open a file and replace {{,}},{& and %} with
# HTML escaped characters inside pre tags. Key to documenting liquid and other
# template systems using that very template system for rendering
# Handles newline as a character / able to operate w/line breaks
# datamafia.com likes Python
import re
@datamafia
datamafia / playing_with_kwargs.py
Created December 11, 2014 19:40
A few ways to handle kwargs.
# A few ways to handle kwargs, notes form the field.
class simple(object):
def __init__(self, **kwargs):
print 'simple.__init__()'
print 'Kwargs :'
print kwargs
t = simple()
@datamafia
datamafia / gist:f74ea05dd687aefd5da8
Created December 7, 2014 05:00
Dynamically call method using a variable
class CallMe: # Class
def __init__(self):
return
def App(self): # Method one
print 'App'
def Foo(self): # Method two
print 'Foo'
@datamafia
datamafia / ternary_order.php
Created August 10, 2014 18:21
PHP order of Operations Check with Ternary
// Supports a post as an example
$a = true;
// double aka nested ternary
echo isset($a)?'Outter':isset($a)?'Inner':'Else';
// result "Inner"
@datamafia
datamafia / mysql_mavericks.sh
Created June 15, 2014 22:03
MySQL on OSX Mavericks via a really nics bash script.
#!/bin/bash
#############################################
# AUTHOR: JONATHAN SCHWENN @JONSCHWENN #
# MAC MINI VAULT - MAC MINI COLOCATION #
# MACMINIVAULT.COM - @MACMINIVAULT #
# VERSION 1.07 RELEASE DATE MAY 20 2014 #
# DESC: THIS SCRIPT INSTALLS MySQL on OSX #
#############################################
#REQUIREMENTS:
# OS X 10.7 or newer
@datamafia
datamafia / array_intersect_keys.php
Created June 3, 2014 22:05
Blog post support for simple validation using PHP's array_intersect_keys
<?php
/* Check for presence of multiple keys in PHP using array_intersect_key
* @param $a : array of data to be checked
* @param $required : array of keys required
* @datamafia // -- Marc
*/
// array to validate
$a = array(
'key1' => 'value1',