Skip to content

Instantly share code, notes, and snippets.

@stantonk
stantonk / simple_http_get.scala
Last active December 30, 2015 16:19
making a simple http GET with scala to facebook's api
import scala.io._
val response = Source.fromURL("http://graph.facebook.com/SproutSocialInc")
val jsonString = response.mkString
println(jsonString)
@stantonk
stantonk / initproj
Created December 8, 2013 05:35
bootstrap scala projects with sbt..learning some other bash crap, etc.
#!/usr/bin/env bash
# build.sbt template
SBT_TMPL="/tmp/build.sbt.template"
cat << 'EOF' > $SBT_TMPL
name := "%name%"
version := "%version%"
scalaVersion := "%scalaversion%"
@stantonk
stantonk / mvnbootstrap.sh
Created November 14, 2013 22:32
Simple way to create a new maven project with the standard directory structure.
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
@stantonk
stantonk / passwordGen.py
Last active December 27, 2015 04:39
generate cryptographically secure passwords in Python, and report the bits of entropy for a given length password based on the CHOICES you allow for random selection. random.SystemRandom() uses /dev/urandom on a *nix operating system, which should be cryptographically secure. There are some potential caveats / things I haven't explored fully, bu…
#!/usr/bin/env python
# http://stackoverflow.com/questions/5480131/will-python-systemrandom-os-urandom-always-have-enough-entropy-for-good-crypto
import math
import random
import sys
CHOICES = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$^&*()+/?,.'
if __name__ == '__main__':
if len(sys.argv) != 2:
@stantonk
stantonk / tunnelVnc.sh
Created November 1, 2013 16:55
tunnel for vnc
#!/bin/sh
# your router config
ROUTER_PUBLIC_IP=home
ROUTER_OPEN_PORT=1234
# your remote computer, behind the router
REMOTE_PORT=5900
REMOTE_LAN_IP=10.0.1.1
@stantonk
stantonk / print_sql.py
Last active December 24, 2015 23:19
Print all SQL queries performed during a unittest in django
from django.conf import settings
from django.db import connections
#TODO: patch run method instead?
#TODO: print formatting
class LogSqlQueriesTestCase(TestCase):
@classmethod
@stantonk
stantonk / scalaforpy
Last active August 28, 2021 10:13
Scala for Python Programmers, examples
/*
Notes:
Despite Scala's appearances, it is, in fact, a Statically Typed language.
It has just eliminated a great deal of the "type vomit" people are used
to seeing in Statically Typed languages (e.g. C, C++, Java). It often
can infer the type on its own. It also combines functional and
object-oriented programming paradigms in a fashion that feels similar
to Python.
*/
@stantonk
stantonk / diff-repos.py
Last active June 30, 2020 18:21
Diff two repositories, file by file, to verify they're the same. Output all diffs if there are any. Written out of our mutual distrust of destructive, history-modifying mercurial extensions.
#!/usr/bin/env python
# authors: @stantonk, @salubriousdave
import os
import sys
import subprocess
def is_hg_repo(path):
hgchkpath = os.path.join(path, '.hg')
return os.path.exists(hgchkpath)
@stantonk
stantonk / slowtest.py
Last active December 23, 2015 11:49
Monkeypatch django.test.TestCase to call out slow running tests. Add this in your project's root __init__.py
import django.test
class TimedTestCase(django.test.TestCase):
_TOO_LONG = 0.100
def __init__(self, *args, **kwargs):
self._start = None
super(TimedTestCase, self).__init__(*args, **kwargs)
def setUp(self):
@stantonk
stantonk / safe_sql_where_in_clause_interpolation.py
Last active December 19, 2015 15:49
Helper method to encapsulate SQL injection safe "WHERE __ in (...)" clauses with Python's MySQLdb driver. An amalgamation from this SO post: http://stackoverflow.com/questions/589284/imploding-a-list-for-use-in-a-python-mysqldb-in-clause
def safe_sql(sql, *values):
assert sql.count('%s') == len(values), (sql, values)
placeholders, new_values = [], []
for value in values:
if isinstance(value, (list, tuple, set)):
placeholders.append(', '.join(['%s'] * len(value)))
new_values.extend(value)
else:
placeholders.append('%s')
new_values.append(value)