Skip to content

Instantly share code, notes, and snippets.

@calippo
calippo / Mappable.scala
Created July 27, 2016 21:51
Convert case class to map in shapeless
object Mappable {
implicit class ToMapOps[A](val a: A) extends AnyVal {
import shapeless._
import ops.record._
def toMap[L <: HList](implicit
gen: LabelledGeneric.Aux[A, L],
tmr: ToMap[L]
): Map[String, Any] = {
val m: Map[tmr.Key, tmr.Value] = tmr(gen.to(a))
@calippo
calippo / Http4sEndpoints.scala
Created January 14, 2020 09:17
tapiro http4s
object ControllerHttp4sEndpoints {
def routes(controller: Controller[F])(
implicit ...
): HttpRoutes[F] = {
val endpoints = ControllerEndpoints.create()
val getContainers =
endpoints.getSomething.toRoutes((controller.getSomething _).tupled)
val getContainerStatus = endpoints.doSomething.toRoutes(controller.doSomething)
)
NonEmptyList(
@calippo
calippo / endpoints.scala
Last active January 14, 2020 09:15
tapiro endpoints
object ControllerEndpoints {
def create(statusCodes: String => Int = _ => 422)(
implicit stringJsonCodec: JsonCodec[String],
listJsonCodec: JsonCodec[Result],
) = new DockerMonitorControllerFEndpoints {
override val getSomething
: Endpoint[String, GenericError, Result, Nothing] =
endpoint.get
.in("getContainers")
@calippo
calippo / Controllers.scala
Last active January 14, 2020 09:12
tapiro models
import scala.annotation.StaticAnnotation
class query extends StaticAnnotation
class command extends StaticAnnotation
import io.circe.generic.JsonCodec
@JsonCodec case class GenericError(
message: String = "",
code: Int = 500
)
@calippo
calippo / build.sbt
Last active January 14, 2020 09:10
tapiro build.sbt
lazy val dockerMonitor = project
.settings(libraryDependencies ++= dependencies)
.settings(
tapiro / tapiroRoutesPath := "relative path to the routes",
tapiro / tapiroModelsPath := "relative path to the models",
tapiro / tapiroOutputPath := "relative path for the generated models",
tapiro / tapiroEndpointsPackage := cats.data.NonEmptyList("dockerMonitor", Nil),
)
.enablePlugins(SbtTapiro)
.dependsOn(authContracts)
@calippo
calippo / eblow.py
Last active November 11, 2019 13:21
[scikit-learn/sklearn, pandas] Plot percent of variance explained for KMeans (Elbow Method)
import pandas as pd
import matplotlib.pyplot as plt
import seaborn
from sklearn.cluster import KMeans
import numpy as np
from scipy.spatial.distance import cdist, pdist
def elbow(df, n):
kMeansVar = [KMeans(n_clusters=k).fit(df.values) for k in range(1, n)]
centroids = [X.cluster_centers_ for X in kMeansVar]
package intervals
import scala.language.implicitConversions
import cats.Monoid
import cats.syntax.foldable._
import cats.instances.list._
import scala.math.Ordering
object Main extends App {
import Interval._
@calippo
calippo / fillWithMean.py
Last active March 5, 2018 16:31
[pandas] Replace `NaN` values with the mean of the column and remove all the completely empty columns
import pandas as pd
def fillWithMean(df):
return df.fillna(df.mean()).dropna(axis=1, how='all')
@calippo
calippo / goemailgo.sh
Last active March 2, 2018 16:32
Send email from command line
echo “very spoofy” | mail -s “much boring” recipient@domain.tld
@calippo
calippo / midi.py
Last active November 7, 2017 22:04
python-midi example
import midi #pip install python-midi (https://github.com/vishnubob/python-midi)
pattern = midi.Pattern()
track = midi.Track()
pattern.append(track)
on = midi.NoteOnEvent(tick=0, velocity=100, pitch=60)
track.append(on)
off = midi.NoteOffEvent(tick=100, pitch=60)
track.append(off)
eot = midi.EndOfTrackEvent(tick=1)