Skip to content

Instantly share code, notes, and snippets.

View wonderbeyond's full-sized avatar
🎯
Focusing

Wonder wonderbeyond

🎯
Focusing
View GitHub Profile
@wonderbeyond
wonderbeyond / chunking.py
Created June 8, 2024 15:23
[Python]Split text into chunks with overlap.
def make_chunks(text, chunk_size: int, overlap: int = 0):
"""Split text into chunks with overlap."""
chunks = []
len_ = len(text)
for i in range(0, len_, chunk_size - overlap):
chunks.append(text[i: i + chunk_size])
if i + chunk_size >= len_:
break
return chunks
@wonderbeyond
wonderbeyond / pgcall.py
Last active August 24, 2023 08:33
Execute SQL on remote Postgres server.
#!/usr/bin/env python
import argparse
import urllib.parse
import textwrap
import psycopg2
import psycopg2.extras
def main():
@wonderbeyond
wonderbeyond / jsonb.py
Last active July 21, 2023 08:08
Live easily with jsonb under SQLAlchemy
from typing import Any
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import array as pg_array, JSONB
def jsonb_set(target, path: list[str], val: Any):
"""
An easy wrapper over sa.func.jsonb_set().
@wonderbeyond
wonderbeyond / tempfile.py
Created July 17, 2023 04:30
An alternative to tempfile.NamedTemporaryFile working with Windows NT.
import os
from contextlib import contextmanager
from tempfile import mkstemp
@contextmanager
def MakeTemporaryFile(suffix=None, prefix=None, dir=None):
"""An alternative to tempfile.NamedTemporaryFile working with Windows NT."""
try:
fd, name = mkstemp(suffix=suffix, prefix=prefix, dir=dir)
@wonderbeyond
wonderbeyond / sa-task-step-dep.py
Last active June 21, 2023 02:48
[SQLAlchemy] A Task-Step model that can describe steps' upstream and downstream relationships.
"""
A Task-Step model that can describe steps' upstream and downstream relationships.
Also refer to https://docs.sqlalchemy.org/en/20/orm/join_conditions.html#self-referential-many-to-many-relationship
"""
from typing import Annotated, List
import sqlalchemy as sa
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship, backref
from sqlalchemy.orm import MappedAsDataclass, DeclarativeBase
@wonderbeyond
wonderbeyond / sa-1.4-adb.py
Last active June 13, 2023 06:03
SQLAlchemy db (engine + session) wrapper
class AsyncSQLAlchemy:
def __init__(
self,
url: str,
engine_options: Optional[Dict[str, Any]] = None,
session_options: Optional[Dict[str, Any]] = None,
):
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
@wonderbeyond
wonderbeyond / .eslintrc.json
Created May 26, 2023 07:23
eslintrc in practice
{
"env": {
"browser": true,
"es2021": true,
"commonjs": true,
"jest/globals": true
},
"plugins": ["jest"],
"extends": "eslint:recommended",
"overrides": [
@wonderbeyond
wonderbeyond / merge-deep.js
Last active May 26, 2023 05:41
[javascript] deep merge objects
/**
* Performs a deep merge of objects and returns new object. Does not modify
* objects (immutable) and merges arrays via concatenation.
*
* @param {...object} objects - Objects to merge
* @returns {object} New object with merged key/values
*/
export default function mergeDeep(...objects) {
const isObject = obj => obj && typeof obj === 'object';
@wonderbeyond
wonderbeyond / group-by.js
Created May 25, 2023 05:35
[javascript] group by
/**
*
* @param {Object[]} dataset
* @param {string} key
* @returns {Object.<string, Object[]>}
* @example groupBy([
* {scope: 'step:1', name: 'API'},
* {scope: 'step:1', name: 'amount'},
* {scope: 'step:2', name: 'Solvent'}
* ], 'scope') => {
@wonderbeyond
wonderbeyond / sqlalchemy-practice.md
Last active May 24, 2023 05:20
SQLAlchemy - Good practice to make life easy

Make a Custom JSON Encoder/Serializer

A example of custom json serializer:

# file: json.py
def custom_json_encoder(o):
    if isinstance(o, pydantic.BaseModel):
        res = o.dict()
 return res['__root__'] if '__root__' in res else res