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 / 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 / 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 / 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 / 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 / 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):