Skip to content

Instantly share code, notes, and snippets.

@eribeiro
eribeiro / quotes.txt
Created December 10, 2013 19:18
Programming Quotes
1. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”
- C.A.R. Hoare (British computer scientist, winner of the 1980 Turing Award)
2. “If debugging is the process of removing software bugs, then programming must be the process of putting them in.”
- Edsger Dijkstra (Dutch computer scientist, winner of the 1972 Turing Award)
3. “Measuring programming progress by lines of code is like measuring aircraft building progress by weight.”
- Bill Gates (co-founder of Microsoft)
4. “Nine people can’t make a baby in a month.” (regarding the addition of more programmers to get a project completed faster)
@eribeiro
eribeiro / wma2mp3.py
Created August 9, 2012 17:04
Convert a WMA file into MP3
#!/usr/bin/python
# This script converts a WMA file into a MP3 file.
# Requisites: mplayer e lame
# Disclaimer: it worked last time I checked, but it was a long time ago,
# so I put it here just to reference. Use it at your own risk.
import os
list = os.listdir(".")
@eribeiro
eribeiro / search_relevance_evaluation_metric.py
Last active August 21, 2022 13:20
Search relevance evaluation metrics
##
## Python implementations of the search relevance evaluation metrics described at
## https://opensourceconnections.com/blog/2020/02/28/choosing-your-search-relevance-metric/
##
##
def precision(docs):
return sum(docs) / len(docs) if docs else 0
def avg_precision(docs):
@eribeiro
eribeiro / gource-ffmpeg.sh
Last active February 11, 2022 16:23
Run Gource on a git repo, outputs as a movie (movie.mp4) and compresses it (output.mp4)
gource -s .06 -1280x720 --auto-skip-seconds .1 --multi-sampling --stop-at-end --key --highlight-users --hide mouse,progress,files,filenames,dirnames --file-idle-time 0 --max-files 0 --background-colour 000000 --font-size 22 --title "Lucene/Solr" --output-ppm-stream - --output-framerate 30 | avconv -y -r 30 -f image2pipe -vcodec ppm -i - -b 65536K movie.mp4
&& ffmpeg -i movie.mp4 -b:v 3048780 -vcodec libx264 -crf 24 output.mp4
@eribeiro
eribeiro / MapBasic.scala
Last active July 25, 2021 05:41
How to do a foldLeft with a Map in Scala, plus basic stuff.
val list = (1 to 10).toList
// imutable map
val map1 = list.foldLeft(Map.empty[Int,String])( (map, value) => map + (value -> value.toString) )
// this is equivalent to
val map1 = list.foldLeft(Map.empty[Int,String])( (map, value) => map + ((value, value.toString)) )
// imutable map with more complex value (note the extra parenthesis)
@eribeiro
eribeiro / solr_password.py
Created November 27, 2020 17:07
Generates sha256 password with salt for use in Solr's Basic Auth setup
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import secrets
import sys
from hashlib import sha256
from base64 import b64encode, b64decode
##
## Based on the logic here: https://github.com/apache/lucene-solr/blob/master/solr/core/src/java/org/apache/solr/security/Sha256AuthenticationProvider.java
@eribeiro
eribeiro / fetcher.sh
Last active January 29, 2020 22:51
Retrieves all the documents from a Solr core/collection
#!/bin/bash
## Usage:
## $ chmod +x fetcher.sh
## $fetcher.sh <output file>
SOLR_URL="http://localhost:8983/solr"
COLLECTION="teste"
ROWS=10
FL=*,score # No space around commas
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.core.WhitespaceTokenizer;
import org.apache.lucene.analysis.miscellaneous.DelimitedTermFrequencyTokenFilter;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.TopDocs;
package com.company;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@eribeiro
eribeiro / weakref-snippet.java
Last active January 5, 2017 14:34
If you are using a *WeakHashMap* and your keys are boxed primitive types like, for example, an Integer with value 10, then you should *NOT* use "Integer.valueOf(10)" or even rely on autoboxing -- e.g., map.put(10, "blabla") -- to populate the keys of this map. REASON: both ".valueOf()" and autoboxing do a *caching* of primitive values (up to a t…
WeakHashMap<Integer, String> map = new WeakHashMap<Integer, String>();
map.put(new Integer(10), "aaa"); // remove entry
// map.put(10, "aaa"); // don't remove entry
// map.put(Integer.valueOf(10), "aaa"); // don't remove entry
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if (map.size() != 0) {
System.out.println("At iteration " + i + " the map still holds the reference to object");
} else {