Skip to content

Instantly share code, notes, and snippets.

View jourdanrodrigues's full-sized avatar
🏡
Working remotely 😎

Jourdan Rodrigues jourdanrodrigues

🏡
Working remotely 😎
  • Software Engineer
  • Brasil
View GitHub Profile
@jourdanrodrigues
jourdanrodrigues / date_range.py
Last active March 14, 2017 18:06
A simple Python function to create an iterable date range.
import datetime
def date_range(start_date, end_date):
"""
Yields each date in a range of dates
Source for future reference: http://stackoverflow.com/a/1060330/4694834
:param start_date: date to start the iteration
:type start_date: datetime.datetime
:param end_date: date to end the iteration
from django.db.models import Aggregate
class Round(Aggregate):
"""
Solution source for future reference: http://stackoverflow.com/a/34622417/4694834
"""
function = 'ROUND'
template = '%(function)s(CAST(%(expressions)s AS NUMERIC), {decimals})'
name = 'Round'
@jourdanrodrigues
jourdanrodrigues / dot_env_reader.py
Last active July 1, 2019 16:37
Light dot env file reader for Python (no need for a module)
import os
import re
from typing import List
class DotEnvReader:
"""
Usage: `DotEnvReader('path').read()`
"""
@jourdanrodrigues
jourdanrodrigues / base_test_case.py
Last active May 22, 2017 16:41
Classes for better testing w/ Django 1.11 & DRF 3.6.
from copy import deepcopy
from django.contrib.admin import AdminSite
from django.test import TestCase
from django.utils.translation import ugettext_lazy as _
from rest_framework import status
from rest_framework.response import Response
from rest_framework.test import APITestCase
@jourdanrodrigues
jourdanrodrigues / locale_message_compiling.py
Last active April 15, 2018 22:59
A custom "compilemessages" Django 1.11 command to compile only the "LOCALE_PATHS" settings (meant for who uses the environment inside the project).
from django.conf import settings
from django.core.management.commands import compilemessages
class Command(compilemessages.Command):
def compile_messages(self, locations):
# If a received location match a locale set, send to compile
if not any(location[0].startswith(locale) for location in locations for locale in settings.LOCALE_PATHS):
return
super(Command, self).compile_messages(locations)
@jourdanrodrigues
jourdanrodrigues / locale_message_making.py
Last active April 18, 2017 18:32
A custom "makemessages" Django 1.11 command to exclude environment code (meant for who uses the environment inside the project).
from django.core.management.commands import makemessages
class Command(makemessages.Command):
def handle(self, *args, **options):
options['ignore_patterns'] += ['env', 'venv']
super(Command, self).handle(*args, **options)
#!/usr/bin/env bash
NC='\033[0m'
ERROR='\033[0;31m'
SUCCESS='\033[0;32m'
SECTION='\033[1;33m'
function space () {
echo ""
printf "${1}${NC}\n"
echo ""
@jourdanrodrigues
jourdanrodrigues / perform_backup.py
Created May 22, 2017 02:37
Backup commands for Django.
from datetime import datetime
import os
from dj_database_url import parse as db_parse
from django.conf import settings
from django.core.management import BaseCommand
from assets.utils import console_log
BACKUPS_PATH = os.path.join(settings.BASE_DIR, 'backups')
@jourdanrodrigues
jourdanrodrigues / date_parser.py
Created June 8, 2017 22:28
Python parser: date string -> date object.
import datetime
from re import match
date_separator = r'(/|-|.)'
time_separator = r'(:|-|.)'
date_formats = '"D/M/YYYY" | "D-M-YYYY" | "D.M.YYYY"'
datetime_formats = '"D/M/YYYY H:M:S" | "D-M-YYYY H-M-S" | "D.M.YYYY H.M.S"'
@jourdanrodrigues
jourdanrodrigues / flattenerFunction.js
Last active May 9, 2019 18:26
Function to flatten an array object.
/**
* Function to flatten an array object
* Example: [[1, 2, [3]], 4] -> [1, 2, 3, 4]
* @param: {Array} array: Array object to be flattened
*/
function flatten (array) {
return array.reduce((flat, next) => flat.concat(next instanceof Array ? flatten(next) : next), []);
}