Skip to content

Instantly share code, notes, and snippets.

View metatoaster's full-sized avatar

Tommy Yu metatoaster

  • Auckland Bioengineering Institute, University of Auckland
  • Auckland, New Zealand
View GitHub Profile
@metatoaster
metatoaster / mocking_chrono.rs
Last active July 30, 2023 03:15
Example on how chrono may be mocked.
/*
[dependencies]
chrono = "0.4.26"
*/
use crate::demo::in_the_future;
pub fn main() {
let dt = chrono::Utc::now();
println!("{dt} is in the future: {}", in_the_future(dt));
}
# This is the complete code for the StackOverflow answer:
# https://stackoverflow.com/a/24947583
from typing import List
from typing import Literal
from typing import get_args
from sqlalchemy import Enum
from sqlalchemy import ForeignKey
from sqlalchemy import String
from sqlalchemy import and_
from sqlalchemy import create_engine
# Complete code for the answer at https://stackoverflow.com/a/67578848
from sqlalchemy import ForeignKey, String
from sqlalchemy import create_engine, select
from sqlalchemy.orm import DeclarativeBase, Mapped, Session
from sqlalchemy.orm import mapped_column, relationship
from sqlalchemy.orm import contains_eager
class Base(DeclarativeBase):
pass
@metatoaster
metatoaster / infinite_ampersand.rs
Last active May 27, 2023 00:43
Thankfully the way errors get generated appeared to be fixed in 1.70.0-beta
// Compare between
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=74eaa1906a58b5daad47c210241143a6
// and
// https://play.rust-lang.org/?version=beta&mode=debug&edition=2021&gist=74eaa1906a58b5daad47c210241143a6
struct Thing(String);
struct View<'a>(&'a str);
impl<'a> From<&'a Thing> for View<'a>
{
use std::collections::HashMap;
struct ListOfUsers(Vec<(String, String)>); // (User, Email)
struct UserToEmail<'a>(HashMap<&'a str, &'a str>);
struct EmailToUser<'a>(HashMap<&'a str, &'a str>);
impl<'a> From<&'a ListOfUsers> for UserToEmail<'a> {
fn from(value: &'a ListOfUsers) -> Self {
Self(value.0.iter()
.map(|(u, e)| (u.as_ref(), e.as_ref()))
#[derive(Debug)]
struct EmailAddr(String); // e.g. user@example.com
#[derive(Debug)]
struct Domain<'a>(&'a str); // a slice onward from the @ symbol of the above
impl<'a> From<&'a EmailAddr> for Domain<'a> {
fn from(value: &'a EmailAddr) -> Self {
Self(&value.0[value.0.find('@').unwrap_or(0)..])
}
}
@metatoaster
metatoaster / so76160846.py
Last active May 3, 2023 08:09
StackOverflow 76160846
# pip install SQLAlchemy==2.0.12 psycopg2==2.9.6
import random
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy import select
from sqlalchemy.orm import declarative_base, sessionmaker, scoped_session
Base = declarative_base()
class Entity(Base):
@metatoaster
metatoaster / README.md
Last active May 30, 2023 04:47
Soft delete mixin class for SQLAlchemy with event hooks

SQLAlchemy mixin example with bulk event assignment

This is a full example that shows how mixin classes can be implemented for use with SQLALchemy in a way that also automatically apply event listeners to appropriate subclasses of a given mixin class. The use case started with this question on StackOverflow which asked about how the before_delete mapper event can be used, which lead down the path of exploration that lead to this more generic skeleton on how this might be achieved in a more generic manner.

The following is an example run of the associated Python code:

INFO:sqlalchemy.engine.Engine:SELECT entity.id AS entity_id, entity.field_a AS entity_field_a, entity.field_b AS entity_field_b, entity.created_at AS entity_created_at, entity.deleted_at AS entity_deleted_at 
FROM entity
INFO:sqlalchemy.engine.Engine:[generated in 0.00009s] ()
/*
[dependencies]
rand = "0.8"
*/
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::collections::HashMap;
fn main() {
let w: i16 = 8;
from random import shuffle
W = 8
H = 8
B = 10
A = W * H
S = A - B
line = [1] * B + [0] * S
shuffle(line)
checkidxs = [
[chk for chk in [