Skip to content

Instantly share code, notes, and snippets.

View mahenzon's full-sized avatar

Suren Khorenyan mahenzon

View GitHub Profile
@mahenzon
mahenzon / pyproject.toml
Created November 16, 2024 17:24
Generic classes in Python
[tool.mypy]
strict = true
@mahenzon
mahenzon / pyproject.toml
Created October 27, 2024 13:08
Generic Python annotation examples
[tool.mypy]
strict = true
@mahenzon
mahenzon / example_01_intro.py
Created October 27, 2024 05:26
TypeVar examples
from typing import TypeVar, Sequence
T = TypeVar("T")
def repeat(val: T, times: int) -> list[T]:
return [val] * times
def get_first_element(items: Sequence[T]) -> T:
@mahenzon
mahenzon / service.py
Created October 21, 2024 10:04
Action Service example
"""
Based on https://github.com/tough-dev-school/education-backend/blob/master/src/core/services.py
"""
from abc import ABC, abstractmethod
from collections.abc import Callable, Sequence
from typing import Any, Generic, TypeVar
ReturnType = TypeVar('ReturnType')
@mahenzon
mahenzon / index.js
Last active October 15, 2024 17:24
LeetCode graphql query example: get question details
const fetchLeetCodeTaskDetails = async (taskId) => {
const query = `
query {
question(titleSlug: "${taskId}") {
questionId
title
difficulty
title
titleSlug
topicTags {
@mahenzon
mahenzon / common.py
Created October 1, 2024 20:44
How to annotate Python decorator
import logging
# DEFAULT_LOG_FORMAT = "[%(asctime)s.%(msecs)03d] %(funcName)20s %(module)s:%(lineno)d %(levelname)-8s - %(message)s"
DEFAULT_LOG_FORMAT = (
"%(funcName)10s %(module)s:%(lineno)d %(levelname)-8s - %(message)s"
)
def configure_logging(level: int = logging.INFO) -> None:
logging.basicConfig(
@mahenzon
mahenzon / hash-keys-mutable.ipynb
Created September 12, 2024 18:56
Python mutable dict keys example
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mahenzon
mahenzon / cycle-speed.ipynb
Created September 7, 2024 16:31
Python sum comprehension vs sum generator
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mahenzon
mahenzon / main.py
Created August 24, 2024 20:06
Python *args and **kwargs annotations examples
def true_div(
number: float,
divisor: float,
) -> float:
return number / divisor
def squares(*numbers: int) -> list[int]:
print("type of numbers:", type(numbers))
return [n * n for n in numbers]
@mahenzon
mahenzon / main.ipynb
Created August 18, 2024 16:36
Python function always returns one obj
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.