Skip to content

Instantly share code, notes, and snippets.

Avatar

Elmer de Looff edelooff

View GitHub Profile
@edelooff
edelooff / functional_trees.py
Created Nov 24, 2020
Functional recursive tree creation from pre-order sequence.
View functional_trees.py
# 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."""
@edelooff
edelooff / tree_reconstruction.py
Created Oct 20, 2020
Recreation of a binary tree from pre and post-order sequentialisations.
View tree_reconstruction.py
"""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)
@edelooff
edelooff / eager_defaults.py
Created Oct 13, 2020
SQLAlchemy actively applied column defaults
View eager_defaults.py
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:
@edelooff
edelooff / design.lark
Created Jul 11, 2020
Parsing a simple DSL with Lark
View design.lark
?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"
@edelooff
edelooff / expression.py
Created Jun 12, 2020
Evaluating SQLAlchemy expressions in Python (for ORM objects and such)
View expression.py
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 (
@edelooff
edelooff / deep_add.py
Last active Jun 4, 2020
Sum any nested level of iterables, with optional initial value
View deep_add.py
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
@edelooff
edelooff / column_flag.py
Last active May 12, 2020
SQLAlchemy columns with a hybrid property boolean flag to check for presence (non-nullness)
View column_flag.py
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
@edelooff
edelooff / sqla_secondary.py
Created Apr 24, 2020
SQLAlchemy secondary join
View sqla_secondary.py
from sqlalchemy import (
Column,
ForeignKey,
Integer,
Text,
create_engine)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import (
relationship,
sessionmaker)
@edelooff
edelooff / merge_benchmark.py
Created Apr 11, 2020
Benchmarking functions to merge two sorted lists
View merge_benchmark.py
"""Merging two sorted lists in Python in four ways.
1. The "stupid naive" way of adding them together and sorting the whole thing.
This should be pretty fast as timsort is designed to be fast on partially
sorted lists. However, it uses more memory and might not scale very well?
2. The "picking an algorithm designed for this" way of using heapq.merge. This
provides a generator that walks the two lists without additional use of
memory.
3. The "C-way" of indexing our way through two lists. This being Python though,
we will be yielding the values though (wrapping the resulting generator in
@edelooff
edelooff / duplicate_perf.py
Last active Oct 12, 2018
Collection length checking before determining duplicate items
View duplicate_perf.py
import timeit
NO_DUPLICATES = (
'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve')
WITH_DUPLICATES = (
'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'one', 'nine', 'two', 'eleven', 'three')
You can’t perform that action at this time.