Skip to content

Instantly share code, notes, and snippets.

View mjul's full-sized avatar

Martin Jul mjul

View GitHub Profile
@mjul
mjul / form_to_opencv.py
Created July 2, 2014 19:27
Decode Python (Flask or Werkzeug) photo file uploaded via HTTP POST request in-memory to an OpenCV matrix.
#
# Example from code built on the Flask web framework (and Werkzeug)
# Accepts uploading a photo file in the 'photo' form member, then
# copies it into a memory byte array and converts it to a numpy array
# which in turn can be decoded by OpenCV.
#
# Beware that this increases the memory pressure and you should
# configure a max request size before doing so.
#
# It saves a round-trip to a temporary file, though.
@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 / application.py
Created December 10, 2014 12:36
Configure a Python Flask application to use the associated Amazon RDS Postgres (or MySQL) database when running on Elastic Beanstalk (inside Docker).
driver = 'mysql+pymysql://'
driver = 'postgresql+psycopg2://'
app.config['SQLALCHEMY_DATABASE_URI'] = driver \
+ os.environ['RDS_USERNAME'] + ':' + os.environ['RDS_PASSWORD'] \
+'@' + os.environ['RDS_HOSTNAME'] + ':' + os.environ['RDS_PORT'] \
+ '/' + os.environ['RDS_DB_NAME']
@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
@mjul
mjul / VS-command-prompt.ps1
Created December 16, 2015 09:35
Add Visual Studio 2015 developer tools to the PowerShell path
#
# Set environment variables for Visual Studio Command Prompt
# The technique is to start a Developer Command Prompt and capture its environment into PowerShell
#
# (VS2013) http://stackoverflow.com/questions/2124753/how-i-can-use-powershell-with-the-visual-studio-command-prompt
# (VS2015) https://github.com/jbake/Powershell_scripts/blob/master/Invoke-BatchFile.ps1
#
# Add this to your $PROFILE file in PowerShell
pushd 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\'
@mjul
mjul / NordicMarkets.fsx
Created March 1, 2016 10:23
World Bank TypeProvider example to get data on the major Nordic countries
// World Bank data example
// Look up some indicators for the Nordic markets
#r @"..\packages\FSharp.Data.2.2.5\lib\net40\FSharp.Data.dll"
open FSharp.Data
let wb= WorldBankData.GetDataContext()
let countries = [
wb.Countries.Denmark
@mjul
mjul / ReactComponents.md
Last active July 29, 2017 09:01
React components
@mjul
mjul / chironlab.fsx
Created April 5, 2016 15:13
F# Chiron JSON serialization experiments
#I "./packages"
#r "FParsec/lib/net40-client/FParsec.dll"
#r "FParsec/lib/net40-client/FParsecCS.dll"
#r "Aether/lib/net35/Aether.dll"
#r "Chiron/lib/net40/Chiron.dll"
#r "System.Runtime.Serialization"
open Chiron
@mjul
mjul / fizzbuzz.fsx
Created April 6, 2016 09:11
FizzBuzz F#
// F# FizzBuzz using Active Patterns
let (|DivisibleBy|_|) by n =
match n%by with
| 0 -> Some DivisibleBy
| _ -> None
let fizzbuzz = function
| DivisibleBy 3 & DivisibleBy 5 -> "FizzBuzz"
| DivisibleBy 3 -> "Fizz"