Skip to content

Instantly share code, notes, and snippets.

@bigjason
bigjason / scerariotest.py
Created March 5, 2011 23:20
Python Scenario Testing Prototype
"""
(MIT License)
Copyright (c) 2011 Jason Webb
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWA
@bigjason
bigjason / dict_get_bench.py
Created June 25, 2011 15:27
Benchmark of get methods with python
from __future__ import print_function
a_dict = {x: str(x) for x in range(1000)}
def with_get():
result1 = a_dict.get(-1, None)
result2 = a_dict.get(900, None)
def with_exception():
try:
@bigjason
bigjason / gist:1198936
Created September 6, 2011 20:56
paginator compatible with pymongo and mongoengine
# pagination.py
class Paginator(object):
def __init__(self, query, per_page):
"""Initialize a paginator.
Arguments:
- query -- queryset from pymongo or mongoengine
- per_page -- number of items per page
@bigjason
bigjason / syntax_highlighting.py
Created September 22, 2011 22:38 — forked from mstarkman/syntax_highlighting.py
Ruby on Rails syntax highlight switcher for Sublime Text 2
import os
from fnmatch import fnmatch
import sublime, sublime_plugin
class DetectFileTypeCommand(sublime_plugin.EventListener):
"""
Detects current file type if the file's extension isn't
Modified for Ruby on Rails and Sublime Text 2 Original pastie
here: http://pastie.org/private/kz8gtts0cjcvkec0d4quqa
@bigjason
bigjason / gist:1989047
Created March 6, 2012 21:22
Rails Javascript/CSS Path outside of a controller
# Small snippet to get the paths of linked files for an asset. For use during development only.
# AssetPaths has built in support to resolve paths
ap = ActionView::Helpers::AssetTagHelper::AssetPaths.new(Rails.configuration.action_controller)
# Find the asset directly on the sprockets environment
a = Rails.application.assets.find_asset("crm")
# Compute the public facing paths
a.to_a.map{|path| ap.compute_public_path(path.logical_path, 'javascripts') }
@bigjason
bigjason / vim7.3_mac_install.rb
Created March 28, 2012 18:03 — forked from pengwynn/vim7.3_mac_install.rb
Script to install Vim 7.3 with ruby support for Mac OS X Lion
# requires root permissions in /usr/bin/
star = String.new
8.times { star += "*" }
Star = "\n#{star * 3}\n"
def newblock string
puts "\n#{Star}#{string}#{Star}\n"
end
import play.api.libs.json._
import play.api.libs.functional.syntax._
import play.api.data.validation.ValidationError
implicit class JsPathPimps(path: JsPath) extends JsPath {
def readOrError[T](error: => String)(implicit r: Reads[T]): Reads[T] = new Reads[T] {
def reads(json: JsValue): JsResult[T] = path.readNullable(r).reads(json) match {
case JsSuccess(Some(value), _) => JsSuccess(value, path)
case JsSuccess(None, _) => JsError((path, ValidationError(error)))
case err@JsError(_) => err
implicit class Function1Pimps[-T1](fn: Function1[T1, Boolean]) {
def complement: Function1[T1, Boolean] = (param: T1) => !fn(param)
}
scala> (1 to 100).map(n => if(n > 10) None else Some(n))
res1: scala.collection.immutable.IndexedSeq[Option[Int]] = Vector(Some(1), Some(2), Some(3), Some(4), Some(5), Some(6), Some(7), Some(8), Some(9), Some(10), None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None)
scala> (1 to 100).flatMap(n => if(n > 10) None else Some(n))
res2: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Here is some text and then some code in fenced style:
```
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
```