Skip to content

Instantly share code, notes, and snippets.

@ferminhg
ferminhg / difference_between_init_and_call.py
Created May 13, 2019 15:10
Difference between __init__ and __call__
#The first is used to initialise newly created object, and receives arguments used to do that:
class Foo:
def __init__(self, a, b, c):
# ...
x = Foo(1, 2, 3) # __init__
#The second implements function call operator.
class Foo:
@ferminhg
ferminhg / timeit_measure_execution.py
Created May 15, 2019 08:48
Timeit to measure the execution
# The "timeit" module lets you measure the execution
# time of small bits of Python code
>>> import timeit
>>> timeit.timeit('"-".join(str(n) for n in range(100))',
number=10000)
0.3412662749997253
>>> timeit.timeit('"-".join([str(n) for n in range(100)])',
@ferminhg
ferminhg / decorator_template.py
Created May 15, 2019 14:19
Decorator template
def decorator(func):
@functools.wraps(func)
def wrapper_decorator(*args, **kwargs):
# Do something before
value = func(**args, **kwargs)
# Do something after
return value
return wrapper_decorator
@ferminhg
ferminhg / in-place_value_swapping.py
Last active May 20, 2019 07:46
Python's shorthand for in-place value swapping
# Why Python Is Great:
# In-place value swapping
# Let's say we want to swap
# the values of a and b...
a = 23
b = 42
# The "classic" way to do it
# with a temporary variable:
# "is" vs "=="
>>> a = [1, 2, 3]
>>> b = a
>>> a is b
True
>>> a == b
True
@ferminhg
ferminhg / temperatura.js
Created March 28, 2020 18:56
Ejemplo de funciones
console.clear();
console.log("Welcome to the function jungle 🦌🌳🌲");
// calcular la temperatura actual del salon
// ✅ Obtener un numero aleatorio entre 24-32
// - Obtener random entre el 0-8 y sumarle 24
// ✅ Imprimir "Temperatura: XXºC" donde XX es la temperatura anterior
// ✅ Hacer una funcion que calcule la temperatura_interior y devuela el valor
function obtener_temperatura_interior() {
puts "Enter your first name"
first_name = gets.chomp
puts "Enter your last name"
last_name = gets.chomp
full_name = "#{first_name} #{last_name}"
puts "Your full name si #{full_name}"
puts "Your full name si #{full_name.reverse}"
full_name_len = full_name.gsub(" ", "").length
puts "Your name has #{full_name_len} character in it"
console.log("\t\tPair programming Lucia Fermin")
var cities = {
alicante: 96,
valencia: 93,
castellon: 91,
murcia: 68,
teruel: 70,
granada: 88,
gijon: 60,
barcelona: 12,
#clean architecture layers
# https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
# ^^you can read read here the beneficts of use this patterns to write better code
# The dependency rule (->): dependencies can only point inwards
# Infrastructure -> Application -> Domain
# Summary: my business logic (domain) don't know infra or apliccation layers
# Use case summary
# Given a slug
@ferminhg
ferminhg / cart_example_v1.py
Last active June 10, 2020 16:59
Builder and Factory example
class Cart:
def __init__(self, user=None):
self.user = user
self.elements = {}
self.discounts = {}
# this is a factory
@staticmethod
def create(user):