Skip to content

Instantly share code, notes, and snippets.

var myObject = {
property1: 'test',
myFunc: function(arg) {
// the 'this' object refers to the instance of the myObject variable
this.property1 = arg;
alert("Does this === myObject ? " + (this === myObject ? "yes" : "no") );
var a = function() {
// inside a closure the 'this' object is now === window
alert("Does this === window ? " + (this === window ? "yes" : "no"));
@smoak
smoak / TwistedStuff.py
Created May 18, 2011 00:38
Twisted Stuff
from twisted.internet import reactor, defer
class Api:
def __init__(self):
self.domainObjects = None
self.subscribers = []
def test(self, url):
# code for doing the async stuff here
@smoak
smoak / eventhooks
Created June 24, 2011 23:21
Event Hooks for python
class EventHook(object):
def __init__(self):
self.__handlers = []
def addHandler(self, handler):
self.__handlers.append(handler)
def removeHandler(self, handler):
self.__handlers.remove(handler)
@smoak
smoak / json
Created July 22, 2011 23:20
ASP.NET MVC 3 JSON
/*
Assuming you have JSON POSTed to your action in the form:
{ "id": 1, "name": "John Smith", "description": "Some description here" }
*/
// class for the POSTed data
[Serializable]
public class SomeJsonModelFormData
@smoak
smoak / gist:1189418
Created September 2, 2011 18:33 — forked from voodootikigod/gist:1155790
PyCodeConf Ticket Give-away
Day job:
Software Developer by day...and night
Favorite Python project:
Twisted
Favorite Conference:
Haven't been to any *sad trombone*
Python Experience Level:
@smoak
smoak / gist:1204901
Created September 8, 2011 22:04
HtmlHelper to get Controller name
public static class HtmlHelperExtensions
{
public static string ControllerName<TController>(this HtmlHelper helper) where TController : Controller
{
return typeof(TController).Name.Replace("Controller", string.Empty);
}
}
var orders = new [] { 1, 5, 7, 42 };
var customers = new [] { 1, 2, 3, 4, 5, 6, 7, 42 };
var customersWithOrdersCount = 0;
foreach (var c in customers) {
foreach (var o in orders) {
if (c == o) {
customersWithOrdersCount++;
var request = (HttpWebRequest)WebRequest.Create(this.Url);
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = "POST";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(this.PostBody);
}
var response = request.GetResponse();
string responseString = null;
[Test]
public void should_POST_to_RESTful_service()
{
// Arrange
var myAwesomeRestfulConsumer = MockRepository.GenerateMock<AwesomeRestfulConsumer>();
// Act
myAwesomeRestfulConsumer.ConsumeRestfulService();
// Assert
$ ls Foo\ -\ S1\ -\ *
Foo - S1 - 02 Foo - S1 - 06 Foo - S1 - 10
Foo - S1 - 03 Foo - S1 - 07 Foo - S1 - 11
Foo - S1 - 04 Foo - S1 - 08
Foo - S1 - 05 Foo - S1 - 09
$ for f in Foo\ -\ S1\ -\ *
do
newFile=`echo $f | sed 's/Foo - S1 - /Foo_S01_/g'`