Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀
Learning

Lucas Hiago luchiago

🚀
Learning
View GitHub Profile
View drf_views.md

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 Jul 10, 2020
[BACKEND][PYTHON][DJANGO] WSGI
View wsgi.md

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 Jul 9, 2020
Abstract Methods vs Concret Methods
View abs_vs_con.md

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 Jul 9, 2020
[BACKEND][PYTHON][DJANGO] What are mixins?
View mixins.md

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 Jul 9, 2020
[BACKEND][PYTHON][DJANGO] Notes about Tox
View tox.md

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

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)
end
@luchiago
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
t.timestamps
end
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
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"
./scripts/run-brakeman.bash
./scripts/run-tests.bash
# $? stores exit value of the last command
if [ $? -ne 0 ]; then
echo "Brakeman and Tests must pass before pushing!"
@luchiago
luchiago / pre-commit.bash
Last active Apr 28, 2020
Hook for pre-commit to check rubocop and erb problems
View pre-commit.bash
#!/usr/bin/env bash
echo "Running pre-commit hook"
fileList_rb=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(rb)$')
fileList_erb=$(git diff --diff-filter=d --cached --name-only | grep -E '\.(erb)$')
if [ ${#fileList_rb} -lt 1 ] && [ ${#fileList_erb} -lt 1 ]; then
echo -e "You have no staged .rb or .erb files to test\n"
exit
You can’t perform that action at this time.