Skip to content

Instantly share code, notes, and snippets.

@fredhsu
fredhsu / counting-change.clj
Created May 8, 2011 22:31
SICP 1.2 in Clojure
(defn first-denomination [kinds-of-coins]
(cond (= kinds-of-coins 1) 1
(= kinds-of-coins 2) 5
(= kinds-of-coins 3) 10
(= kinds-of-coins 4) 25
(= kinds-of-coins 5) 50))
(defn cc [amount kinds-of-coins]
(cond (= amount 0) 1
(or (< amount 0) (= kinds-of-coins 0)) 0
:else (+ (cc amount
@fredhsu
fredhsu / twitterdump.java
Created September 11, 2011 23:36
Twitter stream to MongoDB
import com.mongodb.*;
import com.mongodb.util.JSON;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.StatusListener;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.internal.org.json.JSONObject;
import twitter4j.json.DataObjectFactory;
@fredhsu
fredhsu / twitterdump.clj
Created September 11, 2011 23:40
Twitter streaming api to mongodb in clojure
(ns twitterstream.core
(:require
[http.async.client :as client]
[org.danlarkin.json :as json])
(:use somnium.congomongo))
(mongo! :db "twitterdb") ; Mongo database called twitterdb
(def url "https://stream.twitter.com/1/statuses/sample.json")
(def cred {:user "username" :password "password"})
@fredhsu
fredhsu / EulerTour.py
Created August 6, 2012 18:51
Eulerian Tour Recursive
# Given a graph that is a list of edges between nodes
# i.e. [(0,1), (1,5), (5, 7), (7,1)]
def find_eulerian_tour(graph):
tour = []
find_tour(graph[0][0], graph, tour)
return tour
def find_tour(u, graph, tour):
for edge in graph:
if u == edge[0]:
@fredhsu
fredhsu / odl-stats.py
Last active May 7, 2018 13:34
OpenDayLight REST API call using Python getting flow statistics
import httplib2
import json
h = httplib2.Http(".cache")
h.add_credentials('admin', 'admin')
#resp, content = h.request('http://10.55.17.20:8080/controller/nb/v2/statistics/default/flowstats', "GET")
# Updated 8 SEP 2013 to reflect new REST API
resp, content = h.request('http://10.55.17.20:8080/controller/nb/v2/statistics/default/flow', "GET")
allFlowStats = json.loads(content)
flowStats = allFlowStats['flowStatistics']
@fredhsu
fredhsu / mystats-pom.xml
Last active December 17, 2015 07:28
POM file for mystats example
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.opendaylight.controller</groupId>
<artifactId>commons.opendaylight</artifactId>
<version>1.4.0-SNAPSHOT</version>
<relativePath>../../../controller/opendaylight/commons/opendaylight</relativePath>
</parent>
@fredhsu
fredhsu / MyStats.java
Created May 14, 2013 17:57
Activator for mystats example with OpenDaylight
package com.example.mystats;
import org.opendaylight.controller.sal.core.Node;
import org.opendaylight.controller.sal.match.MatchType;
import org.opendaylight.controller.sal.reader.FlowOnNode;
import org.opendaylight.controller.sal.utils.ServiceHelper;
import org.opendaylight.controller.statisticsmanager.IStatisticsManager;
import org.opendaylight.controller.switchmanager.ISwitchManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@fredhsu
fredhsu / mystats-scala-activator.scala
Created May 21, 2013 16:35
Scala OSGi activator for OpenDaylight
package com.example.mystats
import org.osgi.framework.{ BundleActivator, BundleContext }
import org.opendaylight.controller.sal._
import org.opendaylight.controller.statisticsmanager.IStatisticsManager
import org.opendaylight.controller.switchmanager.ISwitchManager
import org.opendaylight.controller.sal.utils.ServiceHelper
import scala.collection.JavaConversions._
class Activator extends BundleActivator {
@fredhsu
fredhsu / topo.js
Created June 3, 2013 20:57
Graphing a simple topology from OpenDaylight with D3.js
var w = 400,
h = 400,
fill = d3.scale.category20();
var svg = d3.select("#chart")
.append("svg:svg")
.attr("width", w)
.attr("height", h);
d3.json("topo.json", function(json) {
@fredhsu
fredhsu / pom.xml
Created July 11, 2013 13:53
getpackets pom file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.opendaylight.controller</groupId>
<artifactId>commons.opendaylight</artifactId>
<version>1.4.0-SNAPSHOT</version>
<relativePath>../../../controller/opendaylight/commons/opendaylight</relativePath>
</parent>