Skip to content

Instantly share code, notes, and snippets.

View AntiKnot's full-sized avatar
🎯
Focusing

Yan.xxx AntiKnot

🎯
Focusing
View GitHub Profile
@AntiKnot
AntiKnot / sqlalchemy_one_to_many.py
Created December 27, 2021 12:05
sqlalchemy one to many
import sqlalchemy as sa
import clickhouse_sqlalchemy as cs
from sqlalchemy.orm import relationship
DB_URL = "sqlite:///foo.db"
e = engine = sa.create_engine(DB_URL)
s = session = cs.make_session(engine)
m = metadata = sa.MetaData(bind=engine)
b = Base = cs.get_declarative_base(metadata=metadata)
@AntiKnot
AntiKnot / zeller_congruence.py
Last active December 13, 2021 03:22
Zeller's congruence
"""
https://en.wikipedia.org/wiki/Zeller%27s_congruence
https://zh.wikipedia.org/wiki/%E8%94%A1%E5%8B%92%E5%85%AC%E5%BC%8F
"""
from functools import wraps
from icecream import ic
# h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday)
WEEK = {0: 'Sun.',
1: 'Mon.',
@AntiKnot
AntiKnot / foo.py
Last active October 25, 2021 09:31
draft - excel to SqlAlchemy model schema
import typing as ty
from jinja2 import Template
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.worksheet.worksheet import Worksheet
tmpl = """
class {{ model_name }}(db.Model):
{% for field in fields -%}
{{ field.field_name }}=db.Column(db.{{ field.field_type }})
@AntiKnot
AntiKnot / map_early_return.ml
Created October 18, 2021 03:11
Ocaml map a iterable array if (fun iterm)->bool early return
(* build a schema map find solution
When a matching target is found and returned, no subsequent calculations are performed. .
*)
let rec do_until f array predicate=
match array with
| [] -> []
| hd::tl -> let hd_solution = f hd in
match predicate hd_solution with
| false -> do_until f tl predicate
| true -> hd_solution
@AntiKnot
AntiKnot / knight_tour.py
Created October 14, 2021 03:30
Knight Tour
"""
There is a disagreement regarding the original reference handling the
lambda key.
And there is no need to find a closed patrol path here. Modify the extend
function
Refs.
original https://linuxgazette.net/110/kapil.html#denouement
wiki https://en.wikipedia.org/wiki/Knight%27s_tour
WolframMathWord https://mathworld.wolfram.com/KnightGraph.html
@AntiKnot
AntiKnot / foo.py
Created October 12, 2021 02:45
flask-sqlalchemy bulk update after limit
import os
import flask_sqlalchemy
from flask import Flask
from sqlalchemy import func
flask_app = Flask(__name__)
SQLALCHEMY_DATABASE_URI = os.environ.get(
'SQLALCHEMY_DATABASE_URI',
'mysql+mysqlconnector://username:pw@addr:port/dbname')
flask_app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
@AntiKnot
AntiKnot / queens.py
Created September 28, 2021 08:41
N-Queens
from collections import namedtuple
from copy import deepcopy
from typing import List
from functools import partial
Point = namedtuple('Point', ['x', 'y'])
def check(p: Point, p_array: List[Point]):
return all(map(partial(is_safe, p), p_array))
@AntiKnot
AntiKnot / LRUCache.py
Created July 25, 2021 11:08
This piece of LRUCache code is generated by github-copilot.
class LRUCache:
def __init__(self, capacity):
# initialize class variables
self.capacity = capacity
self.cache = {}
self.lru = []
self.lru_count = 0
def get(self, key):
# get value from cache
@AntiKnot
AntiKnot / foo.py
Created June 20, 2021 05:36
python init 2vector matrix
# right
def init_matrix(n,m):
return [[0 for _ in range(n)] for _ in range(m)]
# wrong
# In this way, the rest of the positions will also be modified when the value is modified.
def init_matrix_wrong(n,m):
return [[0] * n] * m
@AntiKnot
AntiKnot / mergesort.go
Created February 3, 2021 13:43
golang MergeSort return
package main
// MergeSort performs the merge sort algorithm.
// Please supplement this function to accomplish the home work.
func Merge(a []int64, b []int64) []int64 {
size, i, j := len(a)+len(b), 0, 0
result := make([]int64, size)
for k := 0; k < size; k++ {
switch true {