This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# inspired by: | |
# https://github.com/neogeny/late/blob/master/late/__init__.py | |
# and | |
# href="https://peps.python.org/pep-0671/ | |
from dataclasses import dataclass | |
from typing import Callable, Any | |
import functools | |
import inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Callable | |
import inspect | |
from typing import Protocol | |
from abc import abstractmethod | |
def _create_method(to_method_name: str, to_attr_name: str): | |
# returns a closure that traps the name of the method to invoke and the attribute_name of the object that will act as receiver-self | |
def new_method(self, *args, **kargs): | |
inner_self = getattr(self, to_attr_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def safe_access(fn): | |
try: | |
return fn() | |
except (TypeError, KeyError, IndexError, AttributeError): | |
return None | |
# usage: | |
class Person: | |
def __init__(self, name, age, profession): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from dataclasses import dataclass | |
import random | |
from typing import Any | |
import json | |
@dataclass | |
class AsyncAction: | |
future: asyncio.Future | |
awaitable_fn: Any # function that returns an awaitable (coroutine, a task...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AsyncAction{ | |
constructor(fn, args, resolveFn, rejectFn){ | |
this.fn = fn; | |
this.args = args; | |
this.resolveFn = resolveFn; | |
this.rejectFn = rejectFn; | |
} | |
} | |
export class AsyncExecutor{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
import queue | |
import threading | |
from time import sleep | |
from typing import Any, Dict, TextIO, List, Callable, Tuple, Optional, AnyStr, Match, cast | |
class TimeoutException(Exception): | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In Groovy we can add interception to an instance (not to a whole class) by means of adding it to the instance metaclass | |
# we can do something like that in Python by creating a new class that inherits the original class of the instance, and changing the class of the instance | |
# in that new class we implement __getattribute__ | |
class Person: | |
def __init__(self, name): | |
self.name = name | |
def say_hi(self, person): | |
#print("inside say_hi") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lazy(cls, *args, **kwargs): | |
class _Lazy(cls): | |
def __init__(self, *args, **kwargs): | |
_Lazy.original_args = args | |
_Lazy.original_kwargs = kwargs | |
def _lazy_init(self): | |
print(f"_lazy_init") | |
# remove the traps so that they do not interfere in the next accesses | |
del _Lazy.__setattr__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tee(iterable, num){ | |
let internalIterator = iterable[Symbol.iterator](); | |
let buffer = []; | |
//it's very interesting that a generator function can capture the outer variables in a closure, though | |
//in the end what is being generated by the compiler is an object with a next() method, not a function having access to the activation object | |
function* generatorFn(){ | |
let pos = 0; | |
let finished = false; | |
while (!finished){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function recursiveFactorial(n, accumulator) { | |
if (n <= 0) return accumulator; | |
return recursiveFactorial(n-1, n*accumulator); | |
} | |
console.log("recursiveFactorial: " + recursiveFactorial(50,1)); | |
console.log("--------------------------"); |
NewerOlder