Skip to content

Instantly share code, notes, and snippets.

View arthuralvim's full-sized avatar

Arthur Alvim arthuralvim

View GitHub Profile
@arthuralvim
arthuralvim / example.py
Created December 19, 2022 23:05 — forked from bblanchon/example.py
Django Subquery Aggregate (Count, Sum...)
from django.db.models import OuterRef
weapons = Weapon.objects.filter(unit__player_id=OuterRef('id'))
units = Unit.objects.filter(player_id=OuterRef('id'))
qs = Player.objects.annotate(weapon_count=SubqueryCount(weapons),
rarity_sum=SubquerySum(units, 'rarity'))
@arthuralvim
arthuralvim / simple-bash-loop.sh
Created August 21, 2022 13:34
Simple loop in bash.
cd /path/to/files &&
for file in *.mp3 *.mp4; do
if [[ -f "$file" ]]; then
if [[ -f "${file}.md5" ]]; then
echo "already exists: ${file}.md5"
else
md5sum -- "$file" > "${file}.md5";
cat "${file}.md5";
fi
fi
@arthuralvim
arthuralvim / gpg-summary.md
Created August 21, 2022 13:30
GPG Summary

PGP Summary

Installation with Brew

$ brew install gnupg

List Keys

@arthuralvim
arthuralvim / future_wars.py
Created March 11, 2022 19:59
Somebody said that WWI was at 28/07/1914 (and that 28 + 07 + 19 +14 = 68) and WWII was at 01/09/1939 (and that 01 + 09 + 19 + 39 = 68)... and that the invasion of Ukraine was at 24/02/2022 (and that 24 + 02 + 20 + 22 = 68)...
# So... I took that as a challenge and displayed all dates that sums 68 from 1900 to 2088 (when I'm going to make 100 years).
# Be safe and avoid those dates. =P
from datetime import date
from datetime import timedelta
def sum_date(date):
date_str = date.strftime('%d %m %Y')
date_str = date_str[:8] + ' ' + date_str[8:]
return sum(map(int, date_str.split()))
@arthuralvim
arthuralvim / luigi_first_steps.md
Created August 18, 2021 14:18 — forked from tomsing1/luigi_first_steps.md
First steps with the Luigi workflow manager

First steps with the Luigi workflow manager

As an introduction into Luigi, I am following this tutorial with some modifications, e.g. installation using conda.

The problems and solutions described in the examples below have led to the development of sciluigi,

@arthuralvim
arthuralvim / sqlalchemy_conftest.py
Created February 25, 2021 03:50 — forked from kissgyorgy/sqlalchemy_conftest.py
Python: py.test fixture for SQLAlchemy test in a transaction, create tables only once!
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from myapp.models import BaseModel
import pytest
@pytest.fixture(scope="session")
def engine():
return create_engine("postgresql://localhost/test_database")
@arthuralvim
arthuralvim / kafka-cheat-sheet.md
Created February 2, 2021 23:13 — forked from ursuad/kafka-cheat-sheet.md
Quick command reference for Apache Kafka

Kafka Topics

List existing topics

bin/kafka-topics.sh --zookeeper localhost:2181 --list

Describe a topic

bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic mytopic

Purge a topic

bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic mytopic --config retention.ms=1000

... wait a minute ...

@arthuralvim
arthuralvim / postgres_queries_and_commands.sql
Created September 8, 2020 12:46 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@arthuralvim
arthuralvim / email-to-spreadsheet.gs
Created August 12, 2020 14:09
Google Apps Script para enviar dados de email do Gmail para o Google Spreadsheets.
function count_emails() {
var max = 500;
var offset = 0;
var emails = [];
while (true) {
var threads = GmailApp.search("in:anywhere", offset, max);
emails = searchThreads.concat(threads);
if (threads.length < max) {
@arthuralvim
arthuralvim / GetGmailEmails.gs
Created August 12, 2020 13:22 — forked from moayadhani/GetGmailEmails.gs
Get Gmail Emails By Assigned Label
var ui = SpreadsheetApp.getUi();
function onOpen(e){
ui.createMenu("Gmail Manager").addItem("Get Emails by Label", "getGmailEmails").addToUi();
}
function getGmailEmails(){
var input = ui.prompt('Label Name', 'Enter the label name that is assigned to your emails:', Browser.Buttons.OK_CANCEL);