Skip to content

Instantly share code, notes, and snippets.

View tomthorogood's full-sized avatar
🎸

Tom Thorogood tomthorogood

🎸
View GitHub Profile
@tomthorogood
tomthorogood / index_in_tuple.py
Created February 13, 2012 13:43
Return position of element in Python tuple
def index_in_tuple(value, iterable):
return [num for num, val in enumerate(iterable) if val is value][0]
# Works for lists too, but those already have this functionality built in.
# How it works:
# value = "three"
# iterable = ("one", "two", "three", "four")
# enumerate(iterable) => ( (0, "one"), (1, "two") ... )
# num for num, val => 0, "one" [...] # num refers to the '0' in this case
# ..."if val is value" => but only if "one" is "three" (which it's not)
@tomthorogood
tomthorogood / PyC++Redis.cpp
Created March 21, 2012 17:36
Instantiating and calling Python classes from using Boost.Python within C++
// I had the worst time in the world trying to determine how to do this.
// I did not find the answer in one concise resource. I figured I'd make this available
// to anyone else having the same trouble.
#include <usr/include/boost/python.hpp> // use your path here
#include <iostream>
#include <string>
namespace py = boost::python;
@tomthorogood
tomthorogood / PointerTest.cpp
Created March 26, 2012 02:37
An exploration of pointers in C++
// I've been trying to better understand pointers, so I made this as a reference for myself.
// Figured it wouldn't hurt to make it available if anyone else is having any problems.
#include <iostream>
using namespace std;
int main()
{
@tomthorogood
tomthorogood / ptrarray.h
Created April 21, 2012 16:24
Managed Array of Pointers in C++
/*!
A class to manage arrays of pointers, all of which can be accessed individually.
Copyright (C) 2012 Tom Thorogood
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
void submitBroadcast(BroadcastMessage* broadcast)
{
namespace TypeBits = Enums::SignalTypes;
unsigned char signalType = broadcast->getSignalType();
// Verify the integrity of this broadcast.
if (signalType < SIGNALRANGE[0] || signalType > SIGNALRANGE[1])
{
//@TODO: Throw Error!
}
@tomthorogood
tomthorogood / fptr.cpp
Created September 12, 2012 13:52
Dynamic function members in C++
#include <string>
#include <iostream>
using namespace std;
typedef string (*callback)(string);
string cb1 (string str)
{
return str + "...in bed!";
}
@tomthorogood
tomthorogood / slideshow.mogu
Created September 22, 2012 15:09
Mogu Slideshow Recipe:
# This is a complete example of how to build a slideshow in mogu. For more information,
# check out the mogu wiki at http://www.github.com/tomthorogood/mogu/wiki
widgets["slidestack"] = \
{
type : container,
styles : "width_400 height_300"
name : "slidestack" #for event bindings
}
@tomthorogood
tomthorogood / MoguTestExample.cpp
Created October 9, 2012 20:45
Sample Mogu Test
/* The following is a demonstration of creating a test suite in the Mogu framework. There are some utility functions that allow you to hand-tool Mogu widgets for testing.
The following test will demonstrate checking the parsed string "|WidgetName| $value$", valid Mogu syntax in the Borel distribution. Having this string in your database should be parsed to the current text value of the widget registered as "WidgetName".
For instance, if you had the following:
```python
widgets["testwidget"] = \
{
type : text,
@tomthorogood
tomthorogood / opencompare.py
Created October 14, 2012 21:34
Easy VimDiff for Stanford compilers VM
#!/usr/bin/env python
#
# Note! The VM has Python 2.6.6, which does not have argparse installed by default.
#
# You can fix this by running the following two commands:
# sudo apt-get install python-pip
# sudo pip install argparse
#
# Make sure to chmod +x opencompare.py
# Then you can do ./opencompare.py test_name
@tomthorogood
tomthorogood / newUser.mogu
Created November 29, 2012 16:16
Add a Mogu User
# This will show a complete example of adding a user into a Mogu application.
# First, you will need to provide a username andpassword. Both of these widgets should be named widgets. In this example, we use an email address as a username to also exemplify adding a contact email for password resets.
# The username is going to be the user's email address.
# We are going to create a validator for this field, which will
# be updated in real time as a user enters their email.
# We also add a tooltip here.
widgets["registration:form:username"] = \