Skip to content

Instantly share code, notes, and snippets.

@methodin
methodin / Mongo
Created February 1, 2011 15:20
Useful commands/examples for Mongo
// Adding a user auth to a DB (read/write, add true as a third param for read-only)
use somedb
db.addUser("username","pasword")
// Update an entry (don't forget $set or it will be replace only with what is provided)
db.gadgets.update({key:"Some criteria"},{$set:{weight:17.6}})
// Dump a database/collection
mongodump -d databasename -c collection -o .
@methodin
methodin / common.php
Created February 1, 2011 15:24
Common functions
<?php
// Log to a file
function dump($data,$file='/var/log/mylog.log')
{
file_put_contents($file, $data."\n", FILE_APPEND);
}
// Use some locking for a cron job
function lockit($name)
{
@methodin
methodin / rememberme.php
Created March 3, 2011 04:05
Remember me for PHP (& Mongo)
define('REMEMBER_TIMEOUT',time()+60*60*24*2);
define('REMEMBER_SALT','secretword');
define('REMEMBER_COOKIE','some_cookie_name');
function remember_me($set=false, $username='')
{
$db = mongo();
// Set the cookie and add the db record
if($set)
@methodin
methodin / androidTest.java
Created April 30, 2011 17:42
Toast handler for Android (avoids contextual errors)
// Need a handler for toast
public void makeToast(String str) {
Message status = toaster.obtainMessage();
Bundle datax = new Bundle();
datax.putString("msg", str);
status.setData(datax);
toaster.sendMessage(status);
}
public Handler toaster = new Handler(){
@methodin
methodin / cron.coffee
Created November 30, 2011 03:31
Run function for a Node cron job
require './schema.js'
mongoose = require "mongoose"
mongodb = require "mongodb"
sys = require 'sys'
lock = require './lock.js'
run = require './run.js'
# Sample cron
# * * * * * /usr/local/bin/node /var/some/dir/cron.js > /var/log/cronjs.log
run.attempt('every 1 minute', () ->
@methodin
methodin / dataMap.js
Created December 13, 2011 00:33
The simplest jQuery plugin I ever wrote - map a JSON object to a container of DOM elements
/*
HTML:
<div id="someParent">
<div id="title"></div>
<img id="img"/>
</div>
Data:
var r = {title:'This is a title', img:'http://someimage.jpg'}
@methodin
methodin / binarySearch.js
Created December 28, 2011 02:52
Binary Search in JS
function binarySearch(searchFor, arr, index1, index2) {
if(index1 === undefined) index1 = 0;
if(index2 === undefined) index2 = arr.length-1;
var search = index1+Math.floor((index2-index1)/2);
if(arr[search] == searchFor) return true;
else if(index2-index1 <= 1) return false;
else return binarySearch(searchFor, arr, arr[search] > searchFor ? index1 : search, arr[search] > searchFor ? search : index2);
}
var found = binarySearch(4, [1,3,6,9,12,17,25,44]);
@methodin
methodin / fizzBuzz.js
Created December 28, 2011 03:08
Fizz Buzz
var str = '';
for(var i=1;i<=100;i++) {
var rep = '';
if(i % 3 == 0) rep = 'Fizz';
if(i % 5 == 0) rep += 'Buzz';
str += (rep||i)+', ';
}
@methodin
methodin / bst.js
Created December 28, 2011 03:17
Binary Search Tree
// Tree node
var Node = function(data) {
this.left = null;
this.right = null;
this.data = data;
this.key = data.age;
this.parent = null;
// Replaces the current node with the data and key of another
this.replaceWith = function(replacement) {
@methodin
methodin / hash.js
Created December 28, 2011 15:16
Hash Table
var Hash = function() {
this.tableSize = 16;
this.table = new Array(this.tableSize);
this.convert = function(key) {
var hash = 0;
for (var i=0;i<key.length;i++) hash += key[i].charCodeAt() * (i+1);
return Math.abs(hash) % this.tableSize;
};