Skip to content

Instantly share code, notes, and snippets.

View theY4Kman's full-sized avatar

Zach Kanzler theY4Kman

View GitHub Profile
@theY4Kman
theY4Kman / deepcopy-immutable-direct-copy.py
Created August 17, 2017 19:03
A demonstration of `copy.deepcopy` directly copying certain immutable types, instead of creating copies. This is in contrast to a literal copy-paste of the list, which will instantiate new PyObjects for ints outside the 0-256 range.
import copy
x = [257, 258, 259]
y = copy.deepcopy(x)
z = [257, 258, 259]
header = [('', 'x', 'deepcopy(x)', 'z')]
rows = [(i, id(i), id(j), id(k)) for i, j, k, in zip(x, y, z)]
for cols in header + rows:
@theY4Kman
theY4Kman / fix-authorizenet-unicode-errors.py
Created February 13, 2017 15:50
Authorize.net uses xml.dom.minidom to parse their responses. minidom doesn't handle unicode. We must pass it an encoded (as in bytes, not codepoints) string to it. Luckily, authorize.net has a hook to run code after execution.
from authorizenet import apicontrollersbase
def afterexecute(self):
self._httpResponse = self._httpResponse.encode('utf8')
apicontrollersbase.APIOperationBase.afterexecute = afterexecute
var util = require('util');
var xmlrpc = require('xmlrpc');
var Serializer = require('xmlrpc/lib/serializer');
function DoubleType(raw) {
xmlrpc.CustomType.call(this, raw);
}
util.inherits(DoubleType, xmlrpc.CustomType);
DoubleType.prototype.tagName = 'double';
@theY4Kman
theY4Kman / dict-subset-sugar.py
Created October 28, 2016 14:38
Some sugar to ease dict subset checking.
def dict_contains_subset(superset, subset):
for key, value in subset.iteritems():
if key not in superset or superset[key] != value:
return False
else:
return True
class SupersetDict(dict):
def __contains__(self, other):
@theY4Kman
theY4Kman / hybrid-attr.py
Last active October 25, 2016 15:28
Demonstrates hybrid properties, which offer the ability to provide equivalent functionality at the instance and query levels with the same attribute name, reducing complexity therefore cognitive load
class Assignment(Model):
completed_at = Column(DateTime)
@hybrid_property
def completed(self):
return self.completed_at is not None
# Note this is a class method
@completed.expression
def completed(cls):
copy(Array.prototype.slice.call($$('.essays2015-essay')).map(function(e) { var title = $('.essays2015-essay-title', e), content = $('.essays2015-essay-content', e); return '## ' + title.innerText + '\n\n' + content.innerText.replace(/(.)(\n.)/mg, '$1 $2') }).join('\n\n\n'))
@theY4Kman
theY4Kman / autodiary.standalone.apps.js
Last active August 10, 2016 02:46
A Google Apps standalone script which automatically creates a new diary entry with a prefilled header, and clears out old, unfilled entries.
// To install, copy script over to new project at scripts.google.com.
// Create two folders, Diary and Archive (one may be inside the other).
// Configure the constants below (you can find the folder IDs in your address bar at drive.google.com)
// Create a new time-driven trigger set to Day timer (Resources > Current project's triggers > Add a new trigger)
// For diary entries
var DIARY_FOLDER_ID = '<configure me>';
// For empty diary entries cleaned out.
// Note: they're not deleted, due to concern of runaway deletion with no backup
var DIARY_ARCHIVE_FOLDER_ID = '<configure me>';
ITERATIONS = 10000
measure = (title, o) ->
table =
'lodash keys': do ->
# lodash keys()
begin = performance.now()
for i in [1..ITERATIONS]
_.keys(o)
performance.now() - begin
@theY4Kman
theY4Kman / ahhhitsonfire.py
Last active October 22, 2015 23:45
With a little bit o' CPython internals knowledge, you can fuck everything up.
import ast
import pyximport; pyximport.install()
import fuckitallup
def print_eval(s):
print s + ':', eval(s)
def extract_evals():
with open(__file__) as fp:
nodejs:
pkg.installed:
version: 'whatevers'