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 __future__ import annotations | |
from dataclasses import dataclass | |
from typing import Generic, Optional, Protocol, TypeVar | |
CT = TypeVar("CT", bound="Comparable") | |
class Comparable(Protocol): | |
def __lt__(self: CT, other: CT) -> bool: |
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
# Haskell functional trees: https://gist.github.com/jerbaroo/bd48ff01a9988650ec698cbce4b0f2da | |
from collections import namedtuple | |
from typing import Any, List, Optional, Tuple | |
Node = namedtuple("Node", ["value", "left", "right"], defaults=[None, None]) | |
def dfs_pre(node: Node) -> Any: | |
"""Depth-first search, pre-order sequence.""" |
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
"""Recreation of a binary tree from pre and post-order sequentialisations. | |
There are no constraints or expectations of explicit ordering. The algorithm | |
runs in linear time and storage space, with a small stack that is dependent | |
on the height of the tree. | |
""" | |
def recreate_tree(pre_order, in_order): | |
print(f"\nStack recreating tree from PO {pre_order} and IO {in_order}") | |
pre_order_iter = iter(pre_order) |
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 datetime import datetime | |
from sqlalchemy import Column, DateTime, Integer, Text, func, inspect | |
from sqlalchemy.ext.declarative import as_declarative, declared_attr | |
from sqlalchemy.sql.functions import Function | |
def defaults_included_constructor(instance, **kwds): | |
mapper = inspect(instance).mapper | |
for column in mapper.columns: |
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
?start: design | |
design: UCASE SIZE flower* INT+ | |
flower: INT+ LCASE | |
SIZE: ("S"|"L") | |
%import common.LCASE_LETTER -> LCASE | |
%import common.UCASE_LETTER -> UCASE | |
%import common.INT | |
%ignore "\n" |
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 __future__ import annotations | |
import operator | |
from collections import deque | |
from enum import Enum, auto | |
from typing import Any, Dict, Iterator, NamedTuple, Optional, Tuple | |
from sqlalchemy import Column | |
from sqlalchemy.sql import operators | |
from sqlalchemy.sql.elements import ( |
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 collections.abc import Iterable | |
def deep_add(*values, start=None): | |
def _traverser(parts): | |
for part in parts: | |
if isinstance(part, Iterable): | |
yield from _traverser(part) | |
else: | |
yield part |
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 sqlalchemy import ( | |
Column, | |
Integer, | |
DateTime, | |
Text, | |
create_engine, | |
func) | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.ext.hybrid import hybrid_property | |
from sqlalchemy.inspection 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 sqlalchemy import ( | |
Column, | |
ForeignKey, | |
Integer, | |
Text, | |
create_engine) | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.orm import ( | |
relationship, | |
sessionmaker) |
NewerOlder