Skip to content

Instantly share code, notes, and snippets.

@bootandy
bootandy / lambdas.py
Created November 4, 2011 11:16
Python - lambdas
#-------------- Concept: Lambdas & string splits: --------------
TurnSpacesIntoColonsFunction = lambda x: '::'.join(x.split(" "))
NumberOfWordsFunction = lambda x: len(x.split(" "))
#-------------- Concept: lambdas inside a regex --------------
re.sub('[aeiou]', lambda match: match.group().upper()*3, 'abcdefghijklmnopqrstuvwxyz')
#-------------- Concept: crazy function stacking with lambdas --------------
@bootandy
bootandy / DataLoaderNextPrevious.py
Created November 15, 2011 11:48
GAE Python Data Loader that handles Next & Previous correctly
class LoadHelper:
PAGE_SIZE = 10
""" load objects, ordered by a parameter
supports next & previous"""
def loadByString(self, request, order, filter):
next = request.get('next')
prev = request.get('prev')
query = MyObject.all()
@bootandy
bootandy / ShortestPath.py
Created January 20, 2012 15:13
Python Shortest Path & Tuples
neighbours = {
"A":{"B":3,"C":4},
"B":{"D":2},
"C":{"D":1,"E":1,"F":6},
"D":{"H":1},
"E":{"H":7},
"F":{"G":1},
"G":{},
"H":{"G":3}
}
@bootandy
bootandy / tornado_mongo.py
Last active December 15, 2016 17:49
Very simple sample code of tornado and mongodb
from datetime import datetime
from pymongo.connection import Connection
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
@bootandy
bootandy / dice.py
Created February 12, 2012 11:43
Python. Random dice rolling sim.
import random
def role():
return int(random.random() * 6) + 1
def main():
hits5plus = 0
hits6 = 0
roles = 10000
@bootandy
bootandy / looping.py
Created February 12, 2012 12:53
Python. Backwards Loops: reversed > xrange > range
from random import random
import time
def main():
list = [] #[5,6,3,6,8,3,10,0,4]
for i in range(0, 1000000):
list.append(int( random() * 1000000000 ) )
with_reversed(list)
backwards_xrange(list)
@bootandy
bootandy / basics.rb
Created February 28, 2012 14:13
Ruby Basics
How 2 ruby debug:
type debugger in file
on cmd line:
l - print where i am
c - continue
n - step over
e @var - dump @var as tostring()
disp - dump current vars
irb - Interactive Ruby Shell
@bootandy
bootandy / mongodb
Last active October 1, 2015 05:58
monogo notes
cheat sheet:
http://cheat.errtheblog.com/s/mongo
for more on update see:
http://www.mongodb.org/display/DOCS/Updating#Updating-update%28%29
======================== DEV ==============================
---search by date:
db.houses.find({"dateadded": { "$gt" : ISODate("2013-01-01")} } )
-- Get by subdocument key field:
@bootandy
bootandy / ruby_basics.rb
Created March 15, 2012 16:55
basics of ruby
#Hashes:
def test_combining_hashes
hash = { "jim" => 53, "amy" => 20, "dan" => 23 }
new_hash = hash.merge({ "jim" => 54, "jenny" => 26 })
def default_value_hash
hash2 = Hash.new("default")
hash2[:one] = 1
assert_equal 1, hash2[:one]
@bootandy
bootandy / mod_wsgi_notes.txt
Created March 27, 2012 23:39
Notes on mod_wsgi
Had trouble with mod_wsgi and virtual env.
You need to build mod_wsgi yourself or ubuntu will use your default python.
You also need to install the python dev tools (specify the python version).
sudo apt-get install python2.6-dev
source: http://helloit.es/2011/05/mod_wsgi-en-apache/