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 / gravatar.py
Created January 16, 2015 19:06
Django gravatar url by template filter
# coding: utf-8
import hashlib
import httplib
import urllib
from django import template
from django.conf import settings
register = template.Library()
GRAVATAR_DOMAIN = "gravatar.com"
/* ========================================================================
* Share
* ========================================================================
* Copyright 2017 Arkanister.
* ======================================================================== */
/**
* <a href="https://github.com/arkanister/" title="Arkanister Profile" data-share="facebook">Share on Facebook</a>
* <a href="https://github.com/arkanister/" title="Arkanister Profile" data-share="twitter">Twitter</a>
@arkanister
arkanister / running-dbviews-on-django.md
Created September 24, 2018 16:52
How to use DB Views with Django?

Sometimes is necessary to create database scripts and run it as a view in Django, but it could be hard, because you must manage the scripts outside the Django project structure.

To make it simple I've created a simple way to run those database view scripts integrated with django.

Setup

To make it possible, is necessary to create an engine to find and run these scripts inside the django apps. In your project create a file named db.py and put the bellow code inside:

# coding: utf-8
# coding: utf-8
"""
What if I wanna handle a python dict as a object?
This object provides the ability to a dict behave as a python object.
Usage:
>>> a = ObjectDict({'foo': 'bar', 'bar': {'foo': [{'bar': 'foo'}]}})
>>> a.foo
@arkanister
arkanister / week_range.py
Last active October 12, 2018 14:04
How to get the first and last day of a week based on a date.
# coding: utf-8
"""
Sometimes we really need to get the first and the last date
on a week, this function will help you to get this whatever
day your week starts.
"""
import datetime
MO, TU, WE, TH, FR, SA, SU = range(1, 8)
@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 ...
>>>
@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 / 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 / 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 / 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