Skip to content

Instantly share code, notes, and snippets.

@arthuralvim
arthuralvim / sqlalchemy_conftest.py
Created Feb 25, 2021 — forked from kissgyorgy/sqlalchemy_conftest.py
Python: py.test fixture for SQLAlchemy test in a transaction, create tables only once!
View sqlalchemy_conftest.py
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 Feb 2, 2021 — forked from ursuad/kafka-cheat-sheet.md
Quick command reference for Apache Kafka
View kafka-cheat-sheet.md

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 ...

View postgres_queries_and_commands.sql
-- 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 Aug 12, 2020
Google Apps Script para enviar dados de email do Gmail para o Google Spreadsheets.
View email-to-spreadsheet.gs
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 Aug 12, 2020 — forked from MoayadAbuRmilah/GetGmailEmails.gs
Get Gmail Emails By Assigned Label
View GetGmailEmails.gs
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);
@arthuralvim
arthuralvim / pizza.py
Created Jul 12, 2020
Máquina escolha minha pizza, por favor.
View pizza.py
import random
from collections import Counter
random.seed(3)
pizzas = ['caipira', 'portuguesa', 'pepperoni', '4 queijos']
print(Counter([random.sample(pizzas, 1)[0] for r in range(1000)]))
View ipdb-jupyter.py
IPython.core.debugger.Pdb.set_trace()
# DeprecationWarning: `Tracer` is deprecated since version 5.1, directly use `IPython.core.debugger.Pdb.set_trace()`
from IPython.core.debugger import Tracer; Tracer()()
View logout.js
// paste it in your console and change the url to your needs.
function logout(to_url) {
var out = window.location.href.replace(/:\/\//, '://log:out@');
jQuery.get(out).error(function() {
window.location = to_url;
});
}
View basic_math_utils.py
from math import gcd
from functools import reduce
# or
# def gcd(a, b):
# """Return greatest common divisor using Euclid's Algorithm."""
# while b:
# a, b = b, a % b
# return a
@arthuralvim
arthuralvim / postgres_table_row_count.sql
Created Apr 9, 2020
SQL to count rows in all PostgreSQL tables.
View postgres_table_row_count.sql
# credits for -> https://www.sisense.com/blog/exact-row-counts-for-every-database-table/
CREATE OR REPLACE FUNCTION
count_rows(schema text, tablename text) RETURNS integer
AS
$BODY$
DECLARE
result integer;
query varchar;
BEGIN