Skip to content

Instantly share code, notes, and snippets.

@rayansostenes
rayansostenes / register_decorator.py
Created March 19, 2024 01:08
Type hint for a generic decorator that can be applied multiple times and checks argument.
# pyright: strict
# Code taken from: https://github.com/microsoft/pyright/discussions/7404
from collections.abc import Callable
from typing import Protocol, overload
class RegisterResult[T](Protocol):
@overload
def __call__[U, R](self, handler: Callable[[T | U], R]) -> Callable[[T | U], R]: ...
@rayansostenes
rayansostenes / test.py
Created March 31, 2023 23:32
A Hacky way of getting a custom Enum to report the correct type for it value property
import enum
from typing import TYPE_CHECKING
class ChoiceEnum(enum.Enum):
def __init__(self, value: str, label: str):
self._value_ = value
self._label_: str = label
@property
@rayansostenes
rayansostenes / brainfuck.py
Last active March 19, 2023 06:39
Brainfuck Interpreter in Python
from __future__ import annotations
import curses
CODE = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
INCREMENT_PTR = ">"
DECREMENT_PTR = "<"
INCREMENT_VAL = "+"

Keybase proof

I hereby claim:

  • I am rayansostenes on github.
  • I am rayansostenes (https://keybase.io/rayansostenes) on keybase.
  • I have a public key whose fingerprint is 821B AF4F 5BE7 20E5 C708 174F 7B78 B50A F2E3 3BB5

To claim this, I am signing this object:

class lazy_loading_property:
attr_name: str
method_name: str
def __init__(self, method_name):
self.method_name = method_name
def __set_name__(self, owner, name):
if not hasattr(owner, self.method_name):
raise TypeError(f'method {self.method_name} does not exists')
from typing import TYPE_CHECKING, Iterable, TypeVar, Union, cast
if TYPE_CHECKING:
from typing import reveal_type # type: ignore
else:
reveal_type = lambda x: x # noqa
_T = TypeVar("_T")
while True:
try:
num = int(input() or 0)
except EOFError:
num = 0
if not num:
break
chars_saved = 0
@rayansostenes
rayansostenes / python_email.py
Created February 12, 2019 14:27
Sending e-mail with python
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
sender_email = "my@gmail.com"
receiver_email = "your@gmail.com"
password = "your_password"
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
FROM ruby:2.5.1
RUN groupadd --gid 1000 node \
&& useradd --uid 1000 --gid node --shell /bin/bash --create-home node
# gpg keys listed at https://github.com/nodejs/node#release-team
RUN set -ex \
&& for key in \
94AE36675C464D64BAFA68DD7434390BDBE9B9C5 \
FD3A5288F042B6850C66B31F09FE44734EB7990E \
71DCFD284A79C3B38668286BC97EC7A07EDE3FC1 \
@rayansostenes
rayansostenes / randomLocationInsideCircle.js
Created June 1, 2018 22:47
Random Location Inside Circle
const RADIUS_EARTH = 6378000;
const randomInsideCircle = (center, radius) => {
const { lat, lng } = center;
const [ a, b ] = [ Math.random(), Math.random() ].sort();
const dx = b * radius * Math.cos((2 * Math.PI * a) / b);
const dy = b * radius * Math.sin((2 * Math.PI * a) / b);