Skip to content

Instantly share code, notes, and snippets.

View oxlade39's full-sized avatar

Dan Oxlade oxlade39

View GitHub Profile
import aiohttp
import asyncio
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
results = [wait_for_code(404, session, 'http://python.org') for i in range(2)]
@oxlade39
oxlade39 / colour_map.py
Last active October 25, 2018 22:09
draw a colour map with automatic gradient
import matplotlib.pyplot as plt
# generate some example data
matrix = [[1,2,3,4,5,6,7,8,9,10],[1,1,1,1,1,1,1,1,1,1],[5,5,5,5,5,5,5,5,5,5]]
# plot the matrix as an image with an appropriate colormap
plt.imshow(matrix, aspect='auto', cmap="bwr")
# add the values
for nrow in range(0,len(matrix)):
@oxlade39
oxlade39 / example.nomad
Created August 6, 2018 16:50
trying out nomad example
# There can only be a single job definition per file. This job is named
# "example" so it will create a job with the ID and Name "example".
# The "job" stanza is the top-most configuration option in the job
# specification. A job is a declarative specification of tasks that Nomad
# should run. Jobs have a globally unique name, one or many task groups, which
# are themselves collections of one or many tasks.
#
# For more information and examples on the "job" stanza, please see
# the online documentation at:
@oxlade39
oxlade39 / DoorChoice.scala
Last active August 29, 2015 14:00
Akka demonstration of the Monty Hall problem
package com.doorchoice
import scala.util.Random
import akka.actor._
import com.doorchoice.DoorChoice.{SwitchingStrategy, GameStrategyRunner, StickingStrategy}
import akka.util.Timeout
import akka.actor.Terminated
import java.util.concurrent.TimeUnit
object DoorChoice {
@oxlade39
oxlade39 / BValue.scala
Last active December 20, 2015 11:29
bencoding using scala parser
sealed trait BValue {
def encode: String
}
case class BInt(value: Int) extends BValue{
lazy val encode = "i" + value + "e"
}
case class BString(value: String) extends BValue{
lazy val encode = value.size + ":" + value
}
case class BList(values: BValue*) extends BValue{
@oxlade39
oxlade39 / BigDecimalMath.scala
Created June 10, 2013 20:34
An example of using the Newton Raphson Approximations method to calculating the square root of a number. Taken from https://github.com/oxlade39/scala-betfair/blob/master/src/main/scala/com/github/oxlade39/scalabetfair/math/BigDecimalMath.scala
object BigDecimalMath {
implicit def toBigDecimal(decimal: String): BigDecimal = BigDecimal(decimal)
def sqrt(x: BigDecimal): BigDecimal = {
val maxIterations = x.mc.getPrecision + 1
val guessSteam: Stream[BigDecimal] = newtonRaphsonApproximations(x).take(maxIterations)
val exactMatch: Option[Stream[BigDecimal]] = guessSteam.sliding(2).find(a => a(0) == a(1))
val root: Stream[BigDecimal] = exactMatch.getOrElse(Stream(guessSteam.last))
private Observable<EsperEvent> sendDisconnectOnUnsubscribe(final Observable<EsperEvent> responseForThisRequest,
final CreateEventStreamRequest createEventStreamRequest) {
return Observable.create(new Func1<Observer<EsperEvent>, Subscription>() {
@Override
public Subscription call(final Observer<EsperEvent> esperEventObserver) {
final Subscription wrapped = responseForThisRequest.subscribe(new Action1<EsperEvent>() {
@Override
public void call(EsperEvent esperEvent) {
esperEventObserver.onNext(esperEvent);
}
@oxlade39
oxlade39 / Service.java
Last active December 16, 2015 21:10
Example of Vaadin 7.1 server push with rxjava
package com.github.oxlade39.vaadin;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.subjects.Subject;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@oxlade39
oxlade39 / InputStreamLineEnumerator.scala
Created March 1, 2013 07:53
Reading lines from an InputStream using a play.api.libs.iteratee.Enumerator
lazy val bufferedReader = new BufferedReader(new InputStreamReader(inputstream))
val responseStream: Enumerator[String] = Enumerator.generateM[String] {
Future{
logger.trace("about to read line")
val line: String = bufferedReader.readLine()
logger.trace("read line")
Option(line)
}
}
@oxlade39
oxlade39 / rakefile.rb
Created April 20, 2012 19:45
automated jekyll deployment with plugins
desc 'Delete generated _site files'
task :clean do
system "rm -fR _site2"
system "git submodule update"
system "cd _site && git checkout master . && git clean -d -f"
end
desc 'Run the jekyll dev server'
task :server do
system "jekyll --server"