Skip to content

Instantly share code, notes, and snippets.

View hernantz's full-sized avatar
🏠
Working from home

hernantz

🏠
Working from home
View GitHub Profile
@hernantz
hernantz / if.py
Created November 24, 2019 22:33
Real world ifs and elses
def extract_timestamp(self):
record = self.get_parsed_record()
timestamp = record.get('timestamp')
if timestamp is None:
return None
if isinstance(timestamp, datetime):
return timestamp
return dateutil.parser.parse(record['timestamp'])
@hernantz
hernantz / logging_extra_formatter.py
Created March 26, 2019 12:55
Logs all extra args
import logging
class ExtraFormatter(logging.Formatter):
defs = set(logging.LogRecord(None, None, None, None, None, None, None).__dict__.keys())
def format(self, record):
extra = set(record.__dict__.keys()) - self.defs
if extra:
# Override message with extra info
@hernantz
hernantz / batalla naval
Created January 3, 2019 04:23
Estrategia muy simple de la batalla naval
Armar listados
--------------
1) Tener un listado de puntos para cada barco donde hay que disparar
2) Para obtener esos puntos dividir la grilla en grillas menores de n x n, donde n es el tamanio del barco
3) Para cada grilla, agregar los puntos de la diagonal (superior izquierdo a inferior derecho) a los listados de puntos a disparar para cada barco de ese tamanio
4) Crear un listado vacio para almacenar los puntos a donde no disparar (lleno de aciertos y fallos)
5) Ir a modo busqueda
Modo busqueda
@hernantz
hernantz / convo.md
Last active June 9, 2017 13:07
Unit tests vs Meaningful tests

That dev told me:

This too is to abstract the test from things i don't want to really test, that sould be tested in the calendar's model test. Abstracting from others components and models behavior is especially useful when you don't know ALL the behavior of everything in the app, and because that behavior shouldn't be tested in this model, so you save time by not having to know how that component/model exactly works and by not setting a lot of enviroment variables.

And I answered:

You are right, if writing unit tests is what you want.

I'm asking, please, do not, go further, and write tests that assume less.

from collections import deque
def sol(inp):
strd = sorted(inp)
enum = enumerate(strd)
for idx, item in enum:
# ASEGURARSE QUE ESTO SEA UN TRIO SIEMPRE
next_3 = idx + 3
@hernantz
hernantz / httpie_post_json.sh
Last active April 16, 2016 01:57
HTTPie POST JSON
# Example of how to query elasticsearch with httpie
echo '{"query": {"match": {"name": "beer"}}}' | http POST :9200/my_index/my_doctype/_search/
@hernantz
hernantz / play.sh
Created April 8, 2016 23:04
play any song from youtube
# play <some song name>
function play {
youtube-dl --default-search=ytsearch: \
--youtube-skip-dash-manifest \
--output="${TMPDIR:-/tmp/}%(title)-s%(id)s.%(ext)s" \
--restrict-filenames \
--format="bestaudio[ext!=webm]" \
--exec=mplayer -vvv "$*"
}
@hernantz
hernantz / a.py
Created July 29, 2015 01:20
The golden rule for where to mock.patch()
class SomeClass:
def some_method(self):
some_function()
def some_function():
pass
@hernantz
hernantz / recursive.hs
Created April 28, 2015 01:25
Some homework on learning haskell
-- repeat
rep :: Int -> x -> [x]
rep 0 x = []
rep n x = [x] ++ rep (n-1) x
-- get value at
at :: [x] -> Int -> x
at (x:xs) 0 = x
at (x:xs) n = at (xs) (n-1)
@hernantz
hernantz / post-checkout-hook
Created April 22, 2015 16:32
Delete pyc files git hook
$ cat ~/path/to/.git/hooks/post-checkout
#!/bin/bash
echo "Cleaning pyc"
find . -iname '*.pyc' -delete