Skip to content

Instantly share code, notes, and snippets.

View berlotto's full-sized avatar

Sérgio Berlotto Jr berlotto

View GitHub Profile
@berlotto
berlotto / quebra_periodo.py
Last active October 6, 2021 18:14
Método para quebrar um range de datas em frequências definidas (mensal, quinzenal e semanal)
def quebra_periodo(begin:str, end:str, freq: str = 'mensal') -> List[Tuple]:
dt_start = datetime.datetime.strptime(begin, '%Y-%m-%d')
dt_end = datetime.datetime.strptime(end, '%Y-%m-%d')
one_day = datetime.timedelta(1)
start_dates = [dt_start]
end_dates = []
today = dt_start
while today <= dt_end:
# Configuration for Alacritty, the GPU enhanced terminal emulator.
# Import additional configuration files
#
# Imports are loaded in order, skipping all missing files, with the importing
# file being loaded last. If a field is already present in a previous import, it
# will be replaced.
#
# All imports must either be absolute paths starting with `/`, or paths relative
# to the user's home directory starting with `~/`.
@berlotto
berlotto / pretty_print_django_query.py
Created April 6, 2021 21:21
How to print pretty Django Querysets query
import sqlparse
def pquery(qs):
# Pretty Print Queryset querys
statement = str(qs.query)
print sqlparse.format(statement, reindent=True, keyword_case='upper')
@berlotto
berlotto / profile_function.py
Created April 6, 2021 21:04
How to profile functions with decorators
import cProfile
import pstats
import io
def profile(fnc):
def inner(*arg, **kwargs):
pr = cProfile.Profile()
pr.enable()
retval = fnc(*arg, **kwargs)
# Configurar a exibição de dados no pandas (vale para o pdb/ipdb/web_pdb)
import pandas as pd
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
# o mesmo para numpy
import numpy
numpy.set_printoptions(linewidth=160)
@berlotto
berlotto / monty_hall.py
Last active July 20, 2020 00:43
Exibe a porcentagem de sucessos quando troca-se a porta no problema de Monty Hall
# Testando o problema de Monty Hall
# https://pt.wikipedia.org/wiki/Problema_de_Monty_Hall
#
# Nesta simulação, o jogador sempre troca a porta depois que o Monty Hall abre uma
# O resultado sempre fica em torno de 65~67% Execute várias vezes para ver...
#
import random
A='A'
B='B'
@berlotto
berlotto / python_3_email_with_attachment.py
Created June 24, 2020 12:01 — forked from rdempsey/python_3_email_with_attachment.py
Use Python 3 to send an email with an attachment using Gmail
#!/usr/bin/env python
# encoding: utf-8
"""
python_3_email_with_attachment.py
Created by Robert Dempsey on 12/6/14.
Copyright (c) 2014 Robert Dempsey. Use at your own peril.
This script works with Python 3.x
NOTE: replace values in ALL CAPS with your own values
@berlotto
berlotto / postgresql_date_function_index_howto.md
Created July 26, 2019 16:30 — forked from cobusc/postgresql_date_function_index_howto.md
Short explanation of the issues faced when trying to create a PostgreSQL index using the date() function and how to resolve it.

Given a table...

CREATE TABLE foo (
  id SERIAL PRIMARY KEY,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  ...
);
@berlotto
berlotto / all_aws_lambda_modules_python.md
Created May 19, 2019 01:50 — forked from gene1wood/all_aws_lambda_modules_python.md
AWS Lambda function to list all available Python modules for Python 2.7 3.6 and 3.7

This gist contains lists of modules available in

in AWS Lambda.

It also contains the code to run in Lambda to generate these lists. In addition there is a less_versbose module in the code that you can call to get a list of the top level modules installed and the version of those modules (if they contain a version

@berlotto
berlotto / CustomArrayField.py
Created April 10, 2019 18:45
Campo ArrayField compatível com SQLite
"""
Esta classe compatibiliza o campo ArrayField do Postgres para rodar na migration do SQLite.
Django 1.9
O correto é você testar em um banco de dados Postgres mesmo, mas para ambientes onde você execute os dois
testes, ajuda.
"""
from django.contrib.postgres.fields import ArrayField
class CustomArrayField(ArrayField):