Skip to content

Instantly share code, notes, and snippets.

View frgomes's full-sized avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / proxmox_openwrt.txt
Last active March 16, 2024 22:08
proxmox: install openwrt
proxmox / local / CT Templates
------------------------------
Download from URL: https://images.linuxcontainers.org/images/openwrt/23.05/amd64/default/20240316_11%3A57/rootfs.tar.xz
SHA-256: d6df8e1abeb24965146e22186b7aa767e3d42fac7fd71be0805cbc0c82377917
proxmox console
---------------
pct create 201 /var/lib/vz/template/cache/openwrt-20240316.tar.xz --arch amd64 --hostname openwrt --rootfs local-lvm:201 --memory 1024 --cores 2 --ostype unmanaged --unprivileged 1 --net0 name=eth0 --net1 name=eth1 --storage local-lvm
@frgomes
frgomes / scala_base64_encode_decode.scala
Created July 28, 2017 12:39
Scala - Base64 encode/decode
// Base64 encode
val text = "This is plaintext."
val bytesEncoded = java.util.Base64.getEncoder.encode(text.getBytes())
// Base64 decode
val textDecoded = new String(java.util.Base64.getDecoder.decode(bytesEncoded))
println(textDecoded)
System Information
-------------------
Manufacturer: HP
Product Name: HP ENVY x360 Convertible 15-ee0xxx
@frgomes
frgomes / pattern_matching_on_runtimeClass.scala
Last active May 16, 2023 18:12
Scala - Pattern matching on a runtime class employing ClassTag and stable identifiers
import com.typesafe.config.Config
implicit class ImplicitConfigHelper(val c: Option[Config]) extends AnyVal {
import scala.reflect.ClassTag
import scala.util.control.NonFatal
import ImplicitConfigHelper._
implicit def valueOf[T : ClassTag](prop: String)(implicit ev: ClassTag[T]) : T =
ev.runtimeClass match { // you need stable identifiers here
case String_ => c.get.getString(prop).asInstanceOf[T]
case Int_ => c.get.getInt(prop).asInstanceOf[T]
@frgomes
frgomes / RuntimeTypeTag.scala
Last active February 25, 2023 01:42
Scala / Spark: allows obtaining schema from runtime TypeTag obtained from class loaded over the wire
import scala.reflect.runtime.universe._
import scala.reflect.api
def runtimeTypeTagOf(name: String, parent: ClassLoader, url: java.net.URL): TypeTag[_] =
runtimeTypeTagOf(name, parent, Seq(url))
def runtimeTypeTagOf(name: String, parent: ClassLoader, urls: Seq[java.net.URL]): TypeTag[_] =
runtimeTypeTagOf(name, new java.net.URLClassLoader(urls.toArray, parent))
def runtimeTypeTagOf(name: String, cl: ClassLoader): TypeTag[_] = {
val c = Class.forName(name, true, cl)
val mirror: Mirror = runtimeMirror(cl)
@frgomes
frgomes / TypeTag2ClassTag.scala
Last active January 31, 2023 12:52
Scala: obtain ClassTag from TypeTag
import reflect.runtime.universe._
import reflect.ClassTag
def typeTag2ClassTag[T: TypeTag]: ClassTag[T] = {
ClassTag[T]( typeTag[T].mirror.runtimeClass( typeTag[T].tpe ) )
}
@frgomes
frgomes / InputStream_from_String.scala
Created January 17, 2018 16:19
Scala - Create java.io.InputStream from String
val lines =
Seq(
"Item,Qty,Price",
"Banana,1,0.50")
val text = lines.mkString("\n")
val stream: java.io.InputStream = new java.io.ByteArrayInputStream(text.getBytes(java.nio.charset.StandardCharsets.UTF_8.name))
@frgomes
frgomes / typeclass-tutorial.scala
Last active January 5, 2023 12:03
Scala - typeclasses - simple tutorial
// QUICK EXAMPLE FOR THE IMPATIENT
/** This trait provides ability to instantiate a generic class */
trait Makeable[T] {
def make: T
}
/** This typeclass which allows making an instance of type `T` */
class Make[T: Makeable] {
def make(): T = implicitly[Makeable[T]].make
@frgomes
frgomes / SimilarSchema.scala
Last active December 18, 2022 03:58
Spark - Compare schemas, ignoring ``nullable`` settting
package object spark {
import org.apache.spark.sql.types.StructType
implicit class StructTypeExtension(schema: StructType) {
import org.apache.spark.sql.types.StructField
implicit def similar(other: StructType): Boolean = _similar(schema, other)
implicit val fieldOrdering: Ordering[StructField] = Ordering.by(field => field.name)
private final def _similar(_this: StructType, _other: StructType): Boolean =
@frgomes
frgomes / CaseClassFactory.scala
Last active November 27, 2022 02:33
Scala - Obtain structure of a case class and build an instance of it from data
// NOTE: This trickery below allows us to obtain the type of
// fields of a case class, so that we can play with generic
// pattern matching on field types.
// The post below is somewhat related.
// See also: http://www.alessandrolacava.com/blog/scala-case-classes-in-depth/
import scala.reflect.runtime.universe._