Skip to content

Instantly share code, notes, and snippets.

View arthuralvim's full-sized avatar

Arthur Alvim arthuralvim

View GitHub Profile
@arthuralvim
arthuralvim / queue_threading.py
Created February 21, 2019 17:18
Example of python queues and multithreading.
import queue
import threading
num_worker_threads = 1
def do_work(item):
print(item)
@arthuralvim
arthuralvim / group_tags.py
Created August 21, 2014 04:33
A template tag to check if an user belongs to a specific group.
from django import template
from django.contrib.auth.models import Group
register = template.Library()
@register.filter(name='has_group')
def has_group(user, group_name):
group = Group.objects.get(name=group_name)
return True if group in user.groups.all() else False
@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 / csv2djfixtures.py
Created August 19, 2015 21:05
Convert .csv to Django fixtures.
# -*- coding: utf-8 -*-
# requirements
# pip install unipath
# pip install python-decouple
from os.path import join
from unipath import Path
from decouple import config
import csv
@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 ...