Skip to content

Instantly share code, notes, and snippets.

View binki's full-sized avatar

Nathan Phillip Brink binki

View GitHub Profile
@binki
binki / Program.cs
Created July 23, 2016 15:10
Using struct as a parameter list guard
using System;
namespace ParameterDefaultsGuard
{
// The goal is to make it hard to accidentally call the wrong
// overload when wanting to provide multiple overloads and still
// allow overriding defaults through named parameters. To
// accomplish this, we create an empty struct type. This guards
// against passing in null to various positional parameters and
// that being mistakenly passed as the guard. The only way to
@binki
binki / ip.py
Last active July 4, 2016 04:44
Example of middleware for heroku forwarding
class ReverseProxy(object):
def process_request(self, request):
unsafe = True
try:
forwarded_for_split = [x.strip() for x in request.META['HTTP_X_FORWARDED_FOR'].split(',')]
# Client connecting to heroku’s stuff is the last listed one.
# http://stackoverflow.com/q/18264304/429091
request.META['REMOTE_ADDR'] = forwarded_for_split.pop()
unsafe = False
except KeyError:
@binki
binki / AsyncDisposal.cs
Last active April 10, 2019 20:04 — forked from thomaslevesque/AsyncDisposal.cs
Async disposal
using System;
using System.Threading.Tasks;
class Program
{
static void Main() => new Program().Run().Wait();
async Task Run()
{
Console.WriteLine("Before Using");
await Async.Using(new Test(), t =>
ohnobinki@gibby ~ $ python3
Python 3.3.5 (default, May 19 2015, 14:05:03)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> logger_connection = sqlite3.connect('/home/ohnobinki/log.sql')
>>> logger_connection.exec('CREATE TABLE IF NOT EXISTS submission_log (text TEXT)');
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'sqlite3.Connection' object has no attribute 'exec'
using System;
using System.Threading.Tasks;
namespace ArgumentListAwait
{
class Program
{
// Too bad to call a function with multiple task results you have to
// store the intermediate tasks in locals because of the order of
// evaluation of argument lists.
@binki
binki / ragespectate-rockpaperscissors.py
Created May 1, 2016 02:17
My “improved” version of ragespectate’s janken
#!/usr/bin/env python
import random
def intro():
print('Welcome to Rock, Paper, Scissors!')
def user():
user_move = ''
while user_move != 'r' and user_move != 'p' and user_move != 's':
user_move = input('What is your move? (r,p,s) ')
@binki
binki / better.patch
Created April 29, 2016 16:39
SQL WHERE 1=1 as a diff-cleaning technique
ohnob@DESKTOP-A4G2C0J ~/OneDrive/repos/Main
$ diff -u .better.sql.orig .better.sql.new
--- .better.sql.orig 2016-04-29 12:37:32.586963400 -0400
+++ .better.sql.new 2016-04-29 12:37:43.936262800 -0400
@@ -1,3 +1,2 @@
WHERE 1=1
-AND Squirrels=1
AND Humans=1
ohnob@DESKTOP-A4G2C0J ~/repos/MainContainer4
$ 7z a test.7z testdir
7-Zip [64] 15.14 : Copyright (c) 1999-2015 Igor Pavlov : 2015-12-31
Scanning the drive:
1 folder, 1 file, 0 bytes
Creating archive: test.7z
@binki
binki / -
Created March 8, 2016 05:03
Python is less like JavaScript in handling the dot operator than I thought
>>> def callCallback(cb):
... cb()
...
>>> class MyClass(object):
... def __init__(self, x):
... self.x = x
... def f(self):
... print('x = %s' % (self.x, ))
...
>>> myObject = MyClass(3)
@binki
binki / ahk_sendinput.ahk
Created March 2, 2016 22:47
SendInput() compatible with mstsc, using AutoHotKey
; Run with START /WAIT to wait for the keys to be sent.
; I think Process has options for that in .net
; Be compatible with mstsc and just work better™?
SendMode Input
; Rudimentary support for sending a chunk of chars, sending four chars at once instead of 1 probs will feel like a major speedup
; I’m using ordinals because just sending numerals as arguments avoids all that
; potential escaping nightmare. Is simple to code against.
a1 = %1%