Skip to content

Instantly share code, notes, and snippets.

View asieira's full-sized avatar

Alexandre Sieira asieira

View GitHub Profile
@asieira
asieira / protect_attributes.py
Last active October 19, 2022 17:59
Python dataclass decorator to make a subset of fields read-only
from typing import Optional, Iterable, Callable, Union
from dataclasses import fields
def protect_attributes(cls: type):
"""
Decorator to protect attributes of <dataclasses.dataclass> classes.
To make this work, assign one of the following metadata values on fields that need protection:
* `noset` if True will prevent the field from being assigned to in `__setattr__`
* `nodel` if True will prevent the field from being deleted in `__delattr__`
@asieira
asieira / icpbr_import.sh
Created June 14, 2019 17:41
Script to Import Brazilian root CAs (ICP Brasil) into Mac OS X system keychain
#!/usr/bin/env bash
mkdir tmp
cd tmp
curl http://acraiz.icpbrasil.gov.br/credenciadas/CertificadosAC-ICP-Brasil/ACcompactado.zip --output ACcompactado.zip
unzip ACcompactado.zip
rm *.zip
for file in *.crt
@asieira
asieira / Makefile
Last active September 15, 2017 17:41
A *nix and OS X Makefile to help package and test Splunk Apps with Python code in them
APPDIR = TA-Niddel_Magnet_Alerts_Add-on
DOCKER_TMP = /tmp/splunk
DOCKER_TAG = 6.6.2
DOCKER_NAME = splunk
SPLUNK_CREDENTIALS = 'admin:changeme'
VERSION = $(shell grep '^version' $(APPDIR)/default/app.conf | egrep -o '[^ ]+$$')
BUILD = $(shell grep '^build' $(APPDIR)/default/app.conf | egrep -o '[^ ]+$$')
FILE = $(APPDIR)-$(VERSION)-$(BUILD).spl
PYTHON_PACKAGES := $(shell find $(APPDIR)/bin/ -maxdepth 1 -type d | tr '\n' ',')
@asieira
asieira / Internable.scala
Last active December 29, 2016 20:11
Generic implementation of instance interning (de-duplication) in Scala
// This trait defines the mechanism by which the global repository (pool) of objects
// is obtained for each type.
trait Interning[T] {
def repository: java.util.Map[T, T]
def intern(orig: T): T = {
val retval = repository.putIfAbsent(orig, orig)
if (retval == null) orig
else retval
}
@asieira
asieira / splitRange.md
Last active April 5, 2017 00:41
Scala - split Range (x to y, x until y) into similarly-sized sub-Ranges

This will only work with ranges with a step (by) of 1 so far, but is a simple solution to splitting a range into a given number of sub-ranges:

  def splitRange(r: Range, chunks: Int): Seq[Range] = {
    if (r.step != 1) 
      throw new IllegalArgumentException("Range must have step size equal to 1")
      
    val nchunks = scala.math.max(chunks, 1)
    val chunkSize = scala.math.max(r.length / nchunks, 1)
    val starts = r.by(chunkSize).take(nchunks)
    val ends = starts.map(_ - 1).drop(1) :+ r.end
@asieira
asieira / gist:7772953
Last active December 30, 2015 03:59
A wrapper around function rbindlist() in R package data.table that tries to handle some of its limitations. Basically it preprocess the input parameter by filling in any missing columns with NAs, ensuring all column types match and are in the same order before passing it along to rbindlist.
# A wrapper around rbindlist that ensures that all columns are matched in type and
# in order before calling it.
#
smartrbindlist <- function(l) {
if (missing(l) && !is.list(l)) {
stop("smartrbindlist: bad or missing argument 'l")
}
mergedColClasses = list()
i=1