Skip to content

Instantly share code, notes, and snippets.

View arkanister's full-sized avatar

Rafael Souza da Silva arkanister

View GitHub Profile
@arkanister
arkanister / coalesce.py
Created August 19, 2020 19:40
Pure Python coalesce alternative.
class Unset:
"""
This class represent an unset instance.
"""
unset = Unset()
def first_or_default(sequence, condition=unset, default=None):
@arkanister
arkanister / timestamp.py
Created August 12, 2020 13:25
TimeStamp Field For Django Rest Framework
import datetime
from django.conf import settings
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers, compat
class TimestampField(serializers.Field):
default_error_messages = {
@arkanister
arkanister / cnpj.py
Last active August 10, 2020 19:57
CNPJ Generator
import itertools
import random
def _cnpj_dv(digits):
"""
Returns the calculated verification digit.
"""
mul = list(itertools.chain(*[range(len(digits) - 7, 1, -1), range(9, 1, -1)]))
return ((sum([a * b for a, b in zip(digits, mul)]) * 10) % 11) % 10
@arkanister
arkanister / cpf.py
Last active August 10, 2020 19:58
CPF Generator
import random
def _cpf_dv(digits):
"""
Returns the calculated verification digit.
"""
mod = sum(a * b for a, b in zip(digits, range(len(digits) + 1, 1, -1))) % 11
return 0 if mod < 2 else 11 - mod
@arkanister
arkanister / correios.py
Created January 29, 2020 13:30
Consulta de CEP utilizando a api do correios.
"""
Cep query by brazilian correios web services.
requirements:
- zeep==3.4.0
"""
from zeep import Client, exceptions
@arkanister
arkanister / pearsonr_ci.R
Created December 16, 2019 19:04
pearsonr_ci
## Função para o calculo do coeficiente de Pearson
## e do intervalo de confiança de Pearson.
pearsonr_ci <- function(x, y, alpha = 0.05) {
r <- cor(x, y)
p <- cor.test(x, y)$p.value
r_z <- tanh(r)
se <- 1 / sqrt(length(x) - 3)
z <- qnorm(1 - alpha / 2)
lo_z <- r_z - z * se
hi_z <- r_z + z * se
@arkanister
arkanister / decorators.py
Created April 24, 2019 13:14
Cache function results on the instance.
"""
@author: arkanister;
"""
def cache_it(method=None, name=None):
"""
Decorator to cache a function result in the class.
Using this is expected that the function can only be called once by instance,
which means that the second time the function is called, the cached result
@arkanister
arkanister / filters.py
Created April 22, 2019 12:14
Django filter interface to use in any kind of view
# coding: utf-8
"""
This is an interface for simplify the django queryset handler
when we need to apply a filter on it.
Usage:
>>> from core import models
>>> from filters import FilterableQuerysetMixin, SimpleFilter, SimpleSearchFilter
>>>
@arkanister
arkanister / example.js
Last active April 29, 2019 20:28
NodeJS - Simple Pagination for Express and Sequelize
const express = require('express');
const { MyModel } = require('../models/index');
const { paginate } = require('./pagination');
const router = express.Router();
router.get('/', async (request, response) => {
@arkanister
arkanister / python_singleton_decorator.py
Last active December 6, 2018 14:29
A simple implementation from singleton pattern using python classes.
def singleton(kls):
"""
A decorator to grant that only a single class
instance will be created.
Usage:
>>> @singleton
>>> class MyCustomClass:
>>> ... do something ...
>>>