Skip to content

Instantly share code, notes, and snippets.

View mjul's full-sized avatar

Martin Jul mjul

View GitHub Profile
@mjul
mjul / flaskloginevents.py
Created October 15, 2014 16:29
Subscribe to Flask-Login log in and log out events.
@flask.ext.login.user_logged_in.connect_via(app)
def when_user_logged_in(sender_app, user):
print "user_logged_in %s" % (user)
@flask.ext.login.user_logged_out.connect_via(app)
def when_user_logged_out(sender_app, user):
print "user_logged_out %s" % (user)
@mjul
mjul / NDepend.cql
Created April 30, 2015 11:26
NDepend - find ASP.NET web pages not using an authorization service
// <Name>Web pages that do not use the AuthorizationService</Name>
from t in JustMyCode.Types
where t.DeriveFrom("System.Web.UI.Page")
from pl in t.Methods
where pl.SimpleName == "Page_Load"
&& !pl.IsUsingType("Cyberdyne.AuthorizationService")
select new { t, t.BaseClass, pl.SimpleName }
@mjul
mjul / Library.fs
Last active September 4, 2015 14:16
Polyglot F# / C# mixture using F# to easily define Value Types (in the DDD sense - they are realized as .NET classes). Shows using types and discriminated unions from C#.
namespace Functionglot
type Customer = { First : string; Last: string; SSN: uint32; AccountNumber : uint32; }
type Shape =
| Rectangle of width : float * length : float
| Circle of radius : float
| EquilateralTriangle of side : float
| Square of side : float
(wc -l (grep "foo" (cat "foo.txt")))
(->> (cat "foo.txt") (grep "foo") "wc -l")
@mjul
mjul / core.clj
Created March 28, 2011 14:21
Publish a HTML5 Server-Sent Event to a local Kaazing WebSocket Gateway server via UDP
(ns kaazing-eval.core
(import [java.io File]
[java.net DatagramPacket DatagramSocket InetSocketAddress URI]))
(defn publish [msg]
(let [remote-uri (URI/create "udp://localhost:50505")
remote-addr (InetSocketAddress. (.getHost remote-uri)
(.getPort remote-uri))
socket (DatagramSocket.)]
(let [bytes (.getBytes msg)
@mjul
mjul / KaazingPublishTest.cs
Created March 28, 2011 14:57
Publish a HTML5 Server-Sent Event to a local Kaazing WebSocket Gateway server via UDP from C#
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Test.Playground
{
[TestClass]
public class KaazingPublishTest
@mjul
mjul / with-timeout.clj
Created May 15, 2011 18:51
with-timeout macro
(defmacro with-timeout [ms & body]
`(let [f# (future ~@body)]
(.get #^java.util.concurrent.Future f# ~ms
java.util.concurrent.TimeUnit/MILLISECONDS)))
@mjul
mjul / incanter.clj
Created October 14, 2011 14:08
Incanter statistical regression test, plot measurements of latency and overlay a linear model for the scalability (latency as function of number of clients) (Clojure Incanter)
(defn regression-nclients-ms [ds]
(with-data ds
(let [x ($ :nclients)
y ($ :ms)
lm (linear-model y x)]
(doto (scatter-plot x y :x-label "Number of clients." :y-label "Latency (ms)")
(add-lines x (:fitted lm))
(set-title "Regression: linear model of number of clients to latency")
view))))
@mjul
mjul / incanter-histogram-with-normal-distribution.clj
Created October 14, 2011 14:45
Plot the distribution of a set of measurements in a histogram and the corresponding normal distribution (Clojure Incanter)
(defn histogram-with-normal-distribution [ds]
"Produce a histgoram of the single-dimensional dataset ds and the corresponding normal distribution."
(with-data ds
(let [x-mean (mean ds)
x-sd (sd ds)
x-min (apply min ds)
x-max (apply max ds)
h (doto (histogram ds :nbins 100 :density true)
(set-stroke-color java.awt.Color/blue)
(set-title "Distribution of latencies")
@mjul
mjul / CustomSerializer.clj
Created October 16, 2011 17:42
Custom Serialization from Clojure for Backtype Storm (Joda DateTime example)
(ns storm-tutorial.CustomSerializer
(:import [backtype.storm StormSubmitter LocalCluster]
[backtype.storm.serialization ISerialization]
[java.io DataInputStream DataOutputStream]
[org.joda.time DateTime])
(:use [backtype.storm clojure config]
[clj-time.core :only (date-time now)]
[clj-time.format :only (formatters parse unparse)])
(:gen-class
:implements [backtype.storm.serialization.ISerialization]))