Skip to content

Instantly share code, notes, and snippets.

View luchiago's full-sized avatar
🚀
Learning

Lucas Hiago luchiago

🚀
Learning
View GitHub Profile
@luchiago
luchiago / mdc.py
Created January 24, 2024 02:55
MDC
# MDC Maximo Divisor Comum: Maior número que é divisor de dois ou mais números simultaneamente
# Input: Lista separada por virgula com os numeros a serem testados
# Output: Número divisor
from operator import mul
from functools import reduce
def get_prime_list(n: int) -> list:
# Sieve of Erastothenes
is_prime_list = {}
@luchiago
luchiago / fetch_json.py
Created October 8, 2020 13:03
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)
file.write(json_data)
def fetch_json(url, headers={}):
response = requests.get(url, headers=headers)
@luchiago
luchiago / drf_views.md
Created July 25, 2020 22:15
DRF Views

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
luchiago / wsgi.md
Created July 10, 2020 13:36
[BACKEND][PYTHON][DJANGO] WSGI

WSGI

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 wsgi.py 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
luchiago / abs_vs_con.md
Created July 9, 2020 23:20
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
luchiago / mixins.md
Created July 9, 2020 23:10
[BACKEND][PYTHON][DJANGO] What are mixins?

Mixins

Reference

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
luchiago / tox.md
Created July 9, 2020 19:38
[BACKEND][PYTHON][DJANGO] Notes about Tox

Tox

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.

Usage

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

# @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)
end
@luchiago
luchiago / create_meetings.rb
Last active June 20, 2020 22:55
Migration for multiple foreign keys with the same model in Rails 6
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
t.timestamps
end
#!/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!"