Skip to content

Instantly share code, notes, and snippets.

View kdrakon's full-sized avatar

Sean Policarpio kdrakon

View GitHub Profile
@kdrakon
kdrakon / download-pdf.py
Created May 15, 2023 04:42
Using Selenium and Chrome driver (with Dev tools) to download a page as a PDF
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
import base64
options = webdriver.ChromeOptions()
options.add_argument('--headless')
url = "https://google.com"
@kdrakon
kdrakon / fastapi_cookies.py
Created December 20, 2022 23:23
FastAPI wrapping cookies in quotes (`"..."`)?
# doing this...
cookie_name = "Some-Cookie"
cookie = "yum"
response.set_cookie(cookie_name, cookie)
# and getting a cookie wrapped in quotes that your browser or downstream server doesn't like?
# do this instead...
response.headers['set-cookie'] = f"{cookie_name}={cookie}; Path=/; Secure; HttpOnly"
# this will write the cookie as a raw header that the client/browser should treat better
@kdrakon
kdrakon / mariadb-connector-c.md
Last active November 30, 2022 02:59
Using nix and building mysql/maria db dependencies and getting complaints about `mysql_config` missing?

try nix-shell -p mariadb-connector-c to have mysql_config in scope

@kdrakon
kdrakon / GenerateSwaggerJson.scala
Created November 7, 2019 23:26
A helper to write the JSON OpenAPI spec from tapir (https://github.com/softwaremill/tapir)
package io.policarp
import java.io.{BufferedWriter, FileWriter}
import java.nio.file.Paths
import cats.implicits._
import cats.kernel.Semigroup
import io.circe.Printer
import io.circe.syntax._
import tapir.docs.openapi._
@kdrakon
kdrakon / IOShortCircuit.scala
Created May 9, 2019 00:26
Short-circuit a list of Cats Effect (IO) using a foldLeft
object Main extends App {
val l = List(IO(println(1).asRight), IO(println(2).asRight), IO("foo".asLeft), IO(println(4).asRight))
val io = l.foldLeft[IO[Either[String, Unit]]](IO(Right(())))((prev, next) => {
prev.flatMap({
case Left(err) => IO(Left(err))
case Right(_) => next
})
})
@kdrakon
kdrakon / Rusts_Bindgen_+_Fuse_in_2019.md
Last active July 29, 2021 21:38
Demonstrates how I got bindgen to generate the bindings to libfuse

Rust's Bindgen + Fuse in 2019

I will quickly show how I got bindgen (https://rust-lang.github.io/rust-bindgen) to generate the bindings to Fuse (libfuse) with the current stable release of Rust. By doing so, this should demonstrate how to bootstrap writing your own Fuse file system in Rust.

I do realise that there are some crates that already exist that aid in making Fuse drivers in Rust, but this was more or less an excuse to also try out bindgen, which I don't believe those existing libraries utilise.

I will be using:

@kdrakon
kdrakon / Avro4s.scala
Last active December 16, 2022 15:29
Using Avro4s with Confluent Kafka Avro Serializer + Schema Registry
import java.util
import com.sksamuel.avro4s.RecordFormat
import org.apache.avro.generic.GenericRecord
import org.apache.kafka.common.serialization.{Deserializer, Serde, Serializer}
object Avro4s {
implicit class CaseClassSerde(inner: Serde[GenericRecord]) {
def forCaseClass[T](implicit recordFormat: RecordFormat[T]): Serde[T] = {
val caseClassSerializer: Serializer[T] = new Serializer[T] {
@kdrakon
kdrakon / how-to-sequence-in-rust.md
Created June 15, 2018 13:29
How to sequence in Rust

How to sequence in Rust

Well, I wouldn't say it's exactly the same sequencing in terms of FP languages like Scala et al., but it does the job for the given collection types in Rust; invert something of type F<G<A>> into G<F<A>>.

Usually, we'd expect F and G as types that are iterable and applicative. In the case of Rust, what I seem to understand from the standard library is that it is expected that there exists a trait implementation—FromIterator—that should allow G to be treated like an applicative. Since collect() is already iterating over a collection of G<A>, it's straightforward for an implementation of FromIterator for F to turn the results (i.e. a collection of them) of the first FromIterator into a G<F<A>>—which is what it does.

let opt = vec![Some(1), Some(2), Some(3)];
let sequenced = opt.into_iter().collect::<Option<Vec<i32>>>();
print!("{:?}", sequenced); // Some([1, 2, 3])
@kdrakon
kdrakon / paged_vec.rs
Created June 5, 2018 06:45
A paginated Vec<A>: PagedVec
pub struct PagedVec<'a, A: 'a> {
indexes: usize,
page_length: usize,
pages: Vec<Vec<&'a A>>,
}
impl<'a, A> PagedVec<'a, A> {
pub fn from(vec: &'a Vec<A>, page_length: usize) -> PagedVec<'a, A> {
PagedVec {
indexes: vec.len(),
@kdrakon
kdrakon / RustProblem-ConflictingTraitUse.md
Last active May 17, 2018 23:24
Rust: How to use more than one Trait implementation while avoiding conflicts

Problem

Say you initially have code like this:

struct DeserializeError(String);
trait Deserialize<T> {
    fn into_type(self) -> Result<T, DeserializeError>
}

struct Foo {}
impl Deserialize<Foo> for Vec<u8> {