Skip to content

Instantly share code, notes, and snippets.


Lucas Hiago luchiago

View GitHub Profile
luchiago /
Created Oct 8, 2020
Fetch json from an API and saves in a file
import json
import requests
def save_file(file_name, data):
with open(file_name, 'w') as file:
json_data = json.dumps(data)
def fetch_json(url, headers={}):
response = requests.get(url, headers=headers)

DRF Endpoints, how to build it

In this gist, I will show the difference between three ways to build endpoints with Django Rest Framework. The pros and cons of each one, when you should use it and so on.

Class-based Views

A REST framework provides an APIView class, which subclasses Django's View class.

What's the Django's View class

According to the docs, the View class is a callable which takes a request and returns a response. Is more than the view function because we can wrappe better the request-response flow, following REST principles

luchiago /
Created Jul 10, 2020


Web server gateway interface From Wikpedia: Is a simple calling convention for web servers to forward requests to web applications or frameworks written in Python. Originally specified as PEP-333

The file in Django

It is a file that we can barely write. Or need to.

What it does

Is a web server that communicates with out application and handle all requests and responses. The django project need it, to work as a web application.

luchiago /
Created Jul 9, 2020
Abstract Methods vs Concret Methods

Differences between abstract methods and concrete methods

What's abstract methods

One abstract method is a method that is declared, but contains no implementation. Abstract classes cannot be instantiated, and require subclasses to provide implementations for the abstract methods. It's kinda like Interfaces in Java. Even if the methods don't implements anything, the child classes should implemente those methods.

What's concret methods

Different thant an abstract method, a concret method does implement something even the class is an Abstract Class

luchiago /
Created Jul 9, 2020
[BACKEND][PYTHON][DJANGO] What are mixins?



Motivation to use Mixins

A way to achieve modularity and reusability in OO is through this concept. Multiple languages implement this in different ways. In Python, mixins are supported via multiple inheritance

Context: Python

Mixin is a parent class that provides functionality to subclasses but is not intended to be instantiated itself. It can have both and concrete and abstract methods that are basically abstract base classes. Mixins can be regarded as a specific strain of abstract base classes where they can house both concrete and abstract methods but don't keep any internal states. These can help you when

  • You want to provide a lot of optional features for a class.
  • You want to provide a lot of not-optional features for a class, but you want the features in separate classes so that each of them is about one feature (behavior).
  • You want to use one particular feature in many different
luchiago /
Created Jul 9, 2020


According to official website, tox aims to automate and standardize testing in Python. It is part of a larger vision of easing the packaging, testing and release process of Python software.

What is tox?

tox is a generic virtualenv management and test command line tool you can use for:

  • checking your package installs correctly with different Python versions and interpreters
  • running your tests in each of the environments, configuring your test tool of choice
  • acting as a frontend to Continuous Integration servers, greatly reducing boilerplate and merging CI and shell-based testing.


You can install tox from PyPi using the command pip install tox. After configuring a tox.ini fie you can run tox and see if everything is correct

The config file

View jewels_and_stones.rb
# @param {String} j
# @param {String} s
# @return {Integer}
def num_jewels_in_stones(j, s)
jewels = j.split("")
total = 0
s.split("").each do |stone|
total += 1 if jewels.include?(stone)
luchiago / create_meetings.rb
Last active Jun 20, 2020
Migration for multiple foreign keys with the same model in Rails 6
View create_meetings.rb
class CreateMeetings < ActiveRecord::Migration[6.0]
def change
create_table :meetings do |t|
t.datetime :starts_at, null: false
t.datetime :ends_at, null: false
t.references :available_user, null: false
t.references :requester_user, null: false
View install-hooks.bash
#!/usr/bin/env bash
GIT_DIR=$(git rev-parse --git-dir)
echo "Installing hooks..."
# this command creates symlink to our pre-commit script
ln -s ~/Scripts/ruby/pre-commit.bash $GIT_DIR/hooks/pre-commit
ln -s ~/Scripts/ruby/pre-push.bash $GIT_DIR/hooks/pre-push
echo "Done!"
luchiago / pre-push.bash
Created Apr 23, 2020
Hook for pre-push
View pre-push.bash
// pre-push
#!/usr/bin/env bash
echo "Running pre-push hook"
# $? stores exit value of the last command
if [ $? -ne 0 ]; then
echo "Brakeman and Tests must pass before pushing!"