Skip to content

Instantly share code, notes, and snippets.

View thepabloaguilar's full-sized avatar
🎯
Focusing

Pablo Aguilar thepabloaguilar

🎯
Focusing
View GitHub Profile
@thepabloaguilar
thepabloaguilar / article-monkey_patching_is_not_too_bad-result_container_example.py
Last active August 12, 2020 19:49
[Article] Monkey Patching is not too bad - Result container example
from returns.result import Failure, Result, Success
def is_even(number: int) -> Result[int, int]:
if arg % 2 == 0:
return Success(number)
return Failure(number)
assert is_even(2) == Success(2)
assert is_even(1) == Failure(1)
@thepabloaguilar
thepabloaguilar / article-monkey_patching_is_not_too_bad-example_class_with_env_variable.py
Last active August 12, 2020 19:50
[Article] Monkey Patching is not too bad - Example class with env variable
import os
class Example:
def __init__(self) -> None:
if os.getenv('RETURNS_TRACE'):
self._tracking = []
@thepabloaguilar
thepabloaguilar / article-monkey_patching_is_not_too_bad-collect_traces_example.py
Last active August 13, 2020 12:33
[Article] Monkey Patching is not too bad - `collect_traces` example
from returns.result import Failure, Result
from returns.primitives.tracing import collect_traces
@collect_traces
def get_failure(argument: str) -> Result[str, str]:
return Failure(argument)
failure = get_failure('example')
for trace_line in failure.trace:
@thepabloaguilar
thepabloaguilar / article-monkey_patching_is_not_too_bad-has_trace_example.py
Last active August 12, 2020 19:51
[Article] Monkey Patching is not too bad - pytest plugin example
from returns.result import Result, Success, Failure
def example_function(arg: str) -> Result[int, str]:
if arg.isnumeric():
return Success(int(arg))
return Failure('"{0}" is not a number'.format(arg))
def test_if_failure_is_created_at_example_function(returns):
with returns.has_trace(Failure, example_function):
Success('not a number').bind(example_function)
@thepabloaguilar
thepabloaguilar / map.go
Created April 27, 2021 02:23
A `map` implementation for go Slices
package main
import (
"fmt"
"strconv"
)
func Map[T1, T2 any](s []T1, f func(T1) T2) []T2 {
newSlice := make([]T2, 0)
for _, item := range s {
@thepabloaguilar
thepabloaguilar / union_protocol.py
Created July 24, 2021 04:03
Playing with union of protocols
from abc import ABCMeta
from dataclasses import dataclass
from typing import (
Any,
ClassVar,
Iterable,
Protocol,
Union,
get_args,
get_origin,