Skip to content

Instantly share code, notes, and snippets.

View mahenzon's full-sized avatar

Suren Khorenyan mahenzon

View GitHub Profile
@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.
@mahenzon
mahenzon / example_00_please_no_named_tuples.py
Created April 20, 2024 18:54
Please don't use these Python instruments
from collections import namedtuple
from dataclasses import dataclass
# from typing import NamedTuple
# Point = namedtuple("Point", "x, y")
AgeAndWeight = namedtuple(
"AgeAndWeight",
"age, weight",
)
@mahenzon
mahenzon / five.py
Last active April 13, 2024 20:42
Python Final and @ final examples
from typing import Final, Sequence
ADMIN_IDS: Final[Sequence[int]] = [1, 5]
# ADMIN_IDS: Final[Sequence[int]] = (1, 5)
# ADMIN_IDS: Final[tuple[int, ...]] = (1, 5, 8)
def main() -> None:
print(ADMIN_IDS)
ADMIN_IDS.append(7)
@mahenzon
mahenzon / main.ipynb
Created March 31, 2024 19:17
Pydantic union multi models validation
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
Last active March 8, 2024 15:42
SQLAlchemy m2m to self example
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, select
from sqlalchemy.orm import sessionmaker, relationship, DeclarativeBase, selectinload
# DB_URL = "sqlite:///tags.db"
DB_URL = "sqlite:///:memory:"
DB_ECHO = False
class Base(DeclarativeBase):
id = Column(Integer, primary_key=True)
@mahenzon
mahenzon / step_0.py
Created February 17, 2024 19:33
Never and NoReturn Annotations Python examples
def get_num() -> int:
return 42
def get_word() -> str:
return "spam"
def main() -> None:
num = get_num()
@mahenzon
mahenzon / DictionaryExample.java
Created January 21, 2024 09:09
Java example dictionary with "default" HashSet of integers
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 2, 4, 5, 3, 6, 7, 4};
HashMap<Integer, HashSet<Integer>> dictionary = new HashMap<>();
for (int i = 0; i < numbers.length; i++) {
int number = numbers[i];
if (!dictionary.containsKey(number)) {