Skip to content

Instantly share code, notes, and snippets.

View viesti's full-sized avatar

Kimmo Koskinen viesti

View GitHub Profile
@viesti
viesti / ordered.clj
Created July 29, 2022 16:55
Keep the order of keys from a JSON object
0% cat bb.edn
{:deps {org.flatland/ordered {:mvn/version "1.15.10"}
org.clojure/data.json {:mvn/version "2.4.0"}}}
0% cat ordered.clj
(ns ordered
(:require [flatland.ordered.map :as ordered-map]
[clojure.data.json :as data-json])
(:import (java.io PushbackReader)))
;; Taken from https://github.com/clojure/data.json/blob/master/src/main/clojure/clojure/data/json.clj#L295-L317
@viesti
viesti / utils.py
Created May 2, 2019 15:54
Stream and decompress a gzip file from S3
import boto3
import zlib
def decompress_object(bucket, key, out_file):
s3 = boto3.client("s3")
body = s3.get_object(Bucket=bucket, Key=key)['Body']
with open(out_file, "ab") as out:
decompressor = zlib.decompressobj(zlib.MAX_WBITS|32)
for chunk in body.iter_chunks():
out.write(decompressor.decompress(chunk))
@viesti
viesti / bb.edn
Created October 28, 2021 19:13
bb task to run nrepl server and send initialization code to the server when it starts
;; Task to run nrepl server and send some init code into it, when the nrepl server starts
{:tasks {dev {:requires ([bencode.core :as b]
[babashka.wait :as wait]
[babashka.process :as p :refer [process]])
:task (do
(let [;; Put the command to start nrepl server here, along any other aliases
proc (process "clj -M:dev/nrepl:dev/hashp" {:inherit true})]
(wait/wait-for-path ".nrepl-port")
(try
(let [port (Integer/parseInt (slurp ".nrepl-port"))]
;; dependency: [software.amazon.awssdk/s3 "2.16.7"]
(ns some-ns
(:import (software.amazon.awssdk.services.s3.presigner S3Presigner)
(software.amazon.awssdk.services.s3.model GetObjectRequest)
(software.amazon.awssdk.services.s3.presigner.model GetObjectPresignRequest)
(software.amazon.awssdk.auth.credentials AwsCredentialsProvider)
(software.amazon.awssdk.auth.credentials AwsBasicCredentials)
(software.amazon.awssdk.regions Region)
(java.net URI)
locals {
monitoring_emails = [
"user1@example.com",
"user2@example.com"
]
}
resource "aws_sns_topic" "monitoring" {
name = "monitoring"
}
(re-find #"\d+"(.getName (java.lang.management.ManagementFactory/getRuntimeMXBean)))
@viesti
viesti / glow-sample.clj
Last active April 28, 2019 17:35
Clojure code highlight via Glow
(spit "talk-samples.html" (format "<html><head><style>%s</style></head><body>%s</body></html"
(g/generate-css (-> glow.colorschemes/solarized-dark
(dissoc :background)
(assoc :symbol "#0b2a34")
(assoc :string "#1f7a73")))
(g/highlight-html (slurp "talk-samples.clj"))))
@viesti
viesti / gist:bba5c88abbb2f6ecb914
Last active May 31, 2018 14:52
Custom reporter for cljs.test
(ns myapp.test
(:require [cljs.test :as test :refer-macros [deftest is] :refer [report]]))
(enable-console-print!)
(defmethod report [:karma :begin-test-ns] [m]
(println "\nTesting with Karma" (name (:ns m))))
(defmethod report [:karma :summary] [m]
(println "All done"))
;; Solves math excercise:
;; 9 = 5 [ ] 3 [ ] 4 [ ] 5,
;; , where [ ] should be one of operations: +, -
;; Uses just random brute force :)
user> (def ops #{+ -})
user> (loop [cnt 0
op1 (first ops)
op2 (first ops)
op3 (first ops)]
@viesti
viesti / filters.py
Created September 17, 2015 13:10
Ansible filter plugin to create rules fo ec2_group
def make_rules(hosts, ports, proto):
return [{"proto": proto,
"from_port": port,
"to_port": port,
"cidr_ip": host} for host in hosts for port in map(int, ports.split(","))]
class FilterModule(object):
def filters(self):
return {'make_rules': make_rules}