Skip to content

Instantly share code, notes, and snippets.

View luzfcb's full-sized avatar

Fábio C. Barrionuevo da Luz luzfcb

View GitHub Profile
from django.db import connection
def idseq(model_class):
return '{}_id_seq'.format(model_class._meta.db_table)
def get_next_id(model_class):
cursor = connection.cursor()
sequence = idseq(model_class)
cursor.execute("select nextval('%s')" % sequence)
row = cursor.fetchone()
@eltonplima
eltonplima / bobp-python.md
Last active July 18, 2017 19:51 — forked from sloria/bobp-python.md
A "Best of the Best Practices" (BOBP) guide to developing in Python.

Guia das melhores das melhores práticas para Python

A versão original(em inglês) pode ser encontrada aqui

Um guia com as "melhores das melhores práticas" (BOBP) para o desenvolvimento em Python.

Geral

Valores

@bjinwright
bjinwright / cognito.py
Last active January 18, 2022 00:25
Example of how to make an authorized call to API Gateway using Boto3, Requests, and AWS4Auth. http://stackoverflow.com/questions/37336286/how-do-i-call-an-api-gateway-with-cognito-credentials-in-python
import boto3
import datetime
import json
from requests_aws4auth import AWS4Auth
import requests
boto3.setup_default_session(region_name='us-east-1')
identity = boto3.client('cognito-identity', region_name='us-east-1')
account_id='XXXXXXXXXXXXXXX'
@twidi
twidi / drf_utils.py
Created December 21, 2016 14:34
Make Django Rest Framework correctly handle Django ValidationError raised in the save method of a model
"""
Sometimes in your Django model you want to raise a ``ValidationError`` in the ``save`` method, for
some reason.
This exception is not managed by Django Rest Framework because it occurs after its validation
process. So at the end, you'll have a 500.
Correcting this is as simple as overriding the exception handler, by converting the Django
``ValidationError`` to a DRF one.
"""
from django.core.exceptions import ValidationError as DjangoValidationError
@eltondev
eltondev / API CPF Python
Created December 19, 2016 16:01
Consulta CPF Detran - Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests,sys,re
dados = {'nome':'','renach':'','cpf':'','dataNascimento':'','local':'','cfc':'','resultado':'', 'data':'','hora':''}
def get_dados(contents): # Regex tags XML
nome = re.search('<nome>(.*)</nome>',contents).group(0).replace('<nome>','').replace('</nome>','')
renach = re.search('<renach>(.*)</renach>',contents).group(0).replace('<renach>','').replace('</renach>','')
#-*- coding: utf-8 -*-
"""RoundRobin is a Python library that allow to use Redis for a message queue"""
from functools import wraps
import multiprocessing
from redis import Redis
try:
import cPickle as pickle
@goldsborough
goldsborough / enable_timeit.py
Last active May 10, 2017 00:39
A benchmarking decorator for easy use of timeit
def enable_timeit(number=1000, repeat=1):
"""
Adds a `timeit` member function to the decorated function.
This function allows for configuration of how `timeit` will be executed for
the decorated function. Note that ultimately, `timeit.repeat()` will be
called, not `timeit.timeit()`.
Args:
number (int): How often `timeit` should execute the function.
@travisjeffery
travisjeffery / influxdb-setup.md
Last active March 17, 2024 15:38
Guide to setting up InfluxData's TICK stack

Guide to setting up InfluxData's TICK stack

InfluxData's T.I.C.K. stack is made up from the following components:

Component Role
Telegraf Data collector
InfluxDB Stores data
Chronograf Visualizer
"""
Logical deletion for Django models. Uses is_void flag
to hide discarded items from queries. Overrides delete
methods to set flag and soft-delete instead of removing
rows from the database.
"""
from django.apps import apps
from django.contrib.admin.utils import NestedObjects
from django.db import models
from django.db.models import signals
@berinhard
berinhard / pop_up_admin_file_widget.py
Created June 14, 2016 16:40
This gist show a solution that I've figured out to open all documents links on model object detail page of Django's admin on a new tab.
class NewTabAdminFileWidget(AdminFileWidget):
"""
This widget is an extensions from AdminFileWidget which is used with all
ImageFields and FileFields at admin forms. This widget follows the api defined
by django.forms.widgets.ClearableFileInput widget.
"""
def __init__(self, *args, **kwargs):
super(NewTabAdminFileWidget, self).__init__(*args, **kwargs)
template_with_initial_new_tab = (